mirror of
https://github.com/servo/servo.git
synced 2025-07-30 10:40:27 +01:00
Use Result instead of panicking when the resource dir can't be found
This commit is contained in:
parent
20b1764d71
commit
ceb85795b1
11 changed files with 81 additions and 64 deletions
|
@ -21,24 +21,24 @@ pub fn set_resources_path(path: Option<String>) {
|
|||
}
|
||||
|
||||
#[cfg(target_os = "android")]
|
||||
pub fn resources_dir_path() -> PathBuf {
|
||||
PathBuf::from("/sdcard/servo/")
|
||||
pub fn resources_dir_path() -> io::Result<PathBuf> {
|
||||
Ok(PathBuf::from("/sdcard/servo/"))
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "android"))]
|
||||
pub fn resources_dir_path() -> PathBuf {
|
||||
pub fn resources_dir_path() -> io::Result<PathBuf> {
|
||||
let mut dir = CMD_RESOURCE_DIR.lock().unwrap();
|
||||
|
||||
if let Some(ref path) = *dir {
|
||||
return PathBuf::from(path);
|
||||
return Ok(PathBuf::from(path));
|
||||
}
|
||||
|
||||
// FIXME: Find a way to not rely on the executable being
|
||||
// under `<servo source>[/$target_triple]/target/debug`
|
||||
// or `<servo source>[/$target_triple]/target/release`.
|
||||
let mut path = env::current_exe().expect("can't get exe path");
|
||||
let mut path = try!(env::current_exe());
|
||||
// Follow symlink
|
||||
path = path.canonicalize().expect("path does not exist");
|
||||
path = try!(path.canonicalize());
|
||||
|
||||
while path.pop() {
|
||||
path.push("resources");
|
||||
|
@ -54,11 +54,11 @@ pub fn resources_dir_path() -> PathBuf {
|
|||
path.pop();
|
||||
}
|
||||
*dir = Some(path.to_str().unwrap().to_owned());
|
||||
path
|
||||
Ok(path)
|
||||
}
|
||||
|
||||
pub fn read_resource_file<P: AsRef<Path>>(relative_path: P) -> io::Result<Vec<u8>> {
|
||||
let mut path = resources_dir_path();
|
||||
let mut path = try!(resources_dir_path());
|
||||
path.push(relative_path);
|
||||
let mut file = try!(File::open(&path));
|
||||
let mut data = Vec::new();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue