diff --git a/libimagstore/src/error.rs b/libimagstore/src/error.rs index e05fcdb6..41aec3b8 100644 --- a/libimagstore/src/error.rs +++ b/libimagstore/src/error.rs @@ -15,17 +15,19 @@ pub enum StoreErrorKind { OutOfMemory, FileNotFound, FileNotCreated, + StorePathExists, // maybe more } fn store_error_type_as_str(e: &StoreErrorKind) -> &'static str { match e { - &StoreErrorKind::FileError => "File Error", - &StoreErrorKind::IdLocked => "ID locked", - &StoreErrorKind::IdNotFound => "ID not found", - &StoreErrorKind::OutOfMemory => "Out of Memory", - &StoreErrorKind::FileNotFound => "File corresponding to ID not found", - &StoreErrorKind::FileNotCreated => "File corresponding to ID could not be created", + &StoreErrorKind::FileError => "File Error", + &StoreErrorKind::IdLocked => "ID locked", + &StoreErrorKind::IdNotFound => "ID not found", + &StoreErrorKind::OutOfMemory => "Out of Memory", + &StoreErrorKind::FileNotFound => "File corresponding to ID not found", + &StoreErrorKind::FileNotCreated => "File corresponding to ID could not be created", + &StoreErrorKind::StorePathExists => "Store path exists", } } diff --git a/libimagstore/src/store.rs b/libimagstore/src/store.rs index 7667680b..2ca35995 100644 --- a/libimagstore/src/store.rs +++ b/libimagstore/src/store.rs @@ -54,19 +54,21 @@ pub struct Store { impl Store { /// Create a new Store object - pub fn new(location: PathBuf) -> Store { + pub fn new(location: PathBuf) -> Result { use std::fs::create_dir_all; if !location.exists() { create_dir_all(location.clone()).ok(); // TODO: Error handling? + } else { + if location.is_file() { + return Err(StoreError::new(StoreErrorKind::StorePathExists, None)); + } } - // TODO: Path exists, but is a file? What now? - - Store { + Ok(Store { location: location, entries: Arc::new(RwLock::new(HashMap::new())), - } + }) } /// Creates the Entry at the given location (inside the entry)