Merge pull request #800 from matthiasbeyer/libimagstore/reduce-unwraps
libimagstore: reduce unwraps
This commit is contained in:
commit
38f0951df7
3 changed files with 39 additions and 40 deletions
|
@ -29,11 +29,12 @@ mod fs {
|
|||
use error::StoreErrorKind as SEK;
|
||||
use std::io::Cursor;
|
||||
use std::path::PathBuf;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Mutex;
|
||||
|
||||
use libimagerror::into::IntoError;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Mutex;
|
||||
use error::MapErrInto;
|
||||
|
||||
lazy_static! {
|
||||
static ref MAP: Mutex<HashMap<PathBuf, Cursor<Vec<u8>>>> = {
|
||||
|
@ -58,7 +59,7 @@ mod fs {
|
|||
debug!("Getting lazy file: {:?}", self);
|
||||
match *self {
|
||||
FileAbstraction::Absent(ref f) => {
|
||||
let map = MAP.lock().unwrap();
|
||||
let map = try!(MAP.lock().map_err_into(SEK::LockPoisoned));
|
||||
return map.get(f).cloned().ok_or(SEK::FileNotFound.into_error());
|
||||
},
|
||||
};
|
||||
|
@ -67,7 +68,7 @@ mod fs {
|
|||
pub fn write_file_content(&mut self, buf: &[u8]) -> Result<(), SE> {
|
||||
match *self {
|
||||
FileAbstraction::Absent(ref f) => {
|
||||
let mut map = MAP.lock().unwrap();
|
||||
let mut map = try!(MAP.lock().map_err_into(SEK::LockPoisoned));
|
||||
if let Some(ref mut cur) = map.get_mut(f) {
|
||||
let mut vec = cur.get_mut();
|
||||
vec.clear();
|
||||
|
@ -82,19 +83,21 @@ mod fs {
|
|||
}
|
||||
|
||||
pub fn remove_file(path: &PathBuf) -> Result<(), SE> {
|
||||
MAP.lock().unwrap().remove(path);
|
||||
Ok(())
|
||||
try!(MAP.lock().map_err_into(SEK::LockPoisoned))
|
||||
.remove(path)
|
||||
.map(|_| ())
|
||||
.ok_or(SEK::FileNotFound.into_error())
|
||||
}
|
||||
|
||||
pub fn copy(from: &PathBuf, to: &PathBuf) -> Result<(), SE> {
|
||||
let mut map = MAP.lock().unwrap();
|
||||
let mut map = try!(MAP.lock().map_err_into(SEK::LockPoisoned));
|
||||
let a = try!(map.get(from).cloned().ok_or(SEK::FileNotFound.into_error()));
|
||||
map.insert(to.clone(), a);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn rename(from: &PathBuf, to: &PathBuf) -> Result<(), SE> {
|
||||
let mut map = MAP.lock().unwrap();
|
||||
let mut map = try!(MAP.lock().map_err_into(SEK::LockPoisoned));
|
||||
let a = try!(map.get(from).cloned().ok_or(SEK::FileNotFound.into_error()));
|
||||
map.insert(to.clone(), a);
|
||||
Ok(())
|
||||
|
|
|
@ -57,6 +57,7 @@ use hook::Hook;
|
|||
use libimagerror::into::IntoError;
|
||||
use libimagerror::trace::trace_error;
|
||||
use libimagutil::iter::FoldResult;
|
||||
use libimagutil::debug_result::*;
|
||||
|
||||
use self::glob_store_iter::*;
|
||||
|
||||
|
@ -159,19 +160,16 @@ impl StoreEntry {
|
|||
}
|
||||
|
||||
fn get_entry(&mut self) -> Result<Entry> {
|
||||
let id = &self.id.clone();
|
||||
if !self.is_borrowed() {
|
||||
let file = self.file.get_file_content();
|
||||
if let Err(err) = file {
|
||||
if err.err_type() == SEK::FileNotFound {
|
||||
Ok(Entry::new(self.id.clone()))
|
||||
self.file
|
||||
.get_file_content()
|
||||
.and_then(|mut file| Entry::from_reader(id.clone(), &mut file))
|
||||
.or_else(|err| if err.err_type() == SEK::FileNotFound {
|
||||
Ok(Entry::new(id.clone()))
|
||||
} else {
|
||||
Err(err)
|
||||
}
|
||||
} else {
|
||||
// TODO:
|
||||
let entry = Entry::from_reader(self.id.clone(), &mut file.unwrap());
|
||||
entry
|
||||
}
|
||||
})
|
||||
} else {
|
||||
Err(SE::new(SEK::EntryAlreadyBorrowed, None))
|
||||
}
|
||||
|
@ -245,12 +243,9 @@ impl Store {
|
|||
.map_err_into(SEK::IoError);
|
||||
}
|
||||
|
||||
debug!("Creating store path");
|
||||
let c = FileAbstraction::create_dir_all(&location);
|
||||
if c.is_err() {
|
||||
debug!("Failed");
|
||||
return Err(SEK::StorePathCreate.into_error_with_cause(Box::new(c.unwrap_err())));
|
||||
}
|
||||
try!(FileAbstraction::create_dir_all(&location)
|
||||
.map_err_into(SEK::StorePathCreate)
|
||||
.map_dbg_err_str("Failed"));
|
||||
} else if location.is_file() {
|
||||
debug!("Store path exists as file");
|
||||
return Err(SEK::StorePathExists.into_error());
|
||||
|
@ -638,12 +633,15 @@ impl Store {
|
|||
-> Result<()>
|
||||
{
|
||||
let new_id = new_id.with_base(self.path().clone());
|
||||
let hsmap = self.entries.write();
|
||||
if hsmap.is_err() {
|
||||
return Err(SE::new(SEK::LockPoisoned, None)).map_err_into(SEK::MoveCallError)
|
||||
}
|
||||
if hsmap.unwrap().contains_key(&new_id) {
|
||||
return Err(SE::new(SEK::EntryAlreadyExists, None)).map_err_into(SEK::MoveCallError)
|
||||
let hsmap = try!(
|
||||
self.entries
|
||||
.write()
|
||||
.map_err(|_| SEK::LockPoisoned.into_error())
|
||||
.map_err_into(SEK::MoveCallError)
|
||||
);
|
||||
|
||||
if hsmap.contains_key(&new_id) {
|
||||
return Err(SEK::EntryAlreadyExists.into_error()).map_err_into(SEK::MoveCallError)
|
||||
}
|
||||
|
||||
let old_id = entry.get_location().clone();
|
||||
|
|
|
@ -95,16 +95,14 @@ impl StoreId {
|
|||
}
|
||||
|
||||
pub fn to_str(&self) -> Result<String> {
|
||||
if self.base.is_some() {
|
||||
let mut base = self.base.as_ref().cloned().unwrap();
|
||||
base.push(self.id.clone());
|
||||
base
|
||||
} else {
|
||||
self.id.clone()
|
||||
}
|
||||
.to_str()
|
||||
.map(String::from)
|
||||
.ok_or(SEK::StoreIdHandlingError.into_error())
|
||||
self.base
|
||||
.as_ref()
|
||||
.cloned()
|
||||
.map(|mut base| { base.push(self.id.clone()); base })
|
||||
.unwrap_or_else(|| self.id.clone())
|
||||
.to_str()
|
||||
.map(String::from)
|
||||
.ok_or(SEK::StoreIdHandlingError.into_error())
|
||||
}
|
||||
|
||||
/// Returns the components of the `id` part of the StoreId object.
|
||||
|
|
Loading…
Reference in a new issue