From 80b0501d03ae4f073dc49a487934d7b82de22f3c Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 17 Jan 2016 23:12:38 +0100 Subject: [PATCH] Introduce error if the store path exists but is a file --- libimagstore/src/error.rs | 14 ++++++++------ libimagstore/src/store.rs | 12 +++++++----- 2 files changed, 15 insertions(+), 11 deletions(-) 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)