Use Result instead of panicking when the resource dir can't be found

This commit is contained in:
Manish Goregaokar 2016-07-21 12:04:18 +05:30
parent 20b1764d71
commit ceb85795b1
No known key found for this signature in database
GPG key ID: 3BBF4D3E2EF79F98
11 changed files with 81 additions and 64 deletions

View file

@ -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();