From 4f71563eb446884dafeaa42b1566a36dd06d481b Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 17 Jan 2016 17:42:40 +0100 Subject: [PATCH 1/6] Store::new(): Create path if nonexistent --- libimagstore/src/store.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/libimagstore/src/store.rs b/libimagstore/src/store.rs index 89e8ea89..7667680b 100644 --- a/libimagstore/src/store.rs +++ b/libimagstore/src/store.rs @@ -55,6 +55,14 @@ impl Store { /// Create a new Store object pub fn new(location: PathBuf) -> Store { + use std::fs::create_dir_all; + + if !location.exists() { + create_dir_all(location.clone()).ok(); // TODO: Error handling? + } + + // TODO: Path exists, but is a file? What now? + Store { location: location, entries: Arc::new(RwLock::new(HashMap::new())), From 80b0501d03ae4f073dc49a487934d7b82de22f3c Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 17 Jan 2016 23:12:38 +0100 Subject: [PATCH 2/6] 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) From 0c3bcc3f15503cd866b6cff63ed321d68f93f812 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 18 Jan 2016 23:00:39 +0100 Subject: [PATCH 3/6] Add error kind: Create errors --- libimagstore/src/error.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libimagstore/src/error.rs b/libimagstore/src/error.rs index 41aec3b8..9dc48bb5 100644 --- a/libimagstore/src/error.rs +++ b/libimagstore/src/error.rs @@ -16,6 +16,7 @@ pub enum StoreErrorKind { FileNotFound, FileNotCreated, StorePathExists, + StorePathCreate, // maybe more } @@ -28,6 +29,7 @@ fn store_error_type_as_str(e: &StoreErrorKind) -> &'static str { &StoreErrorKind::FileNotFound => "File corresponding to ID not found", &StoreErrorKind::FileNotCreated => "File corresponding to ID could not be created", &StoreErrorKind::StorePathExists => "Store path exists", + &StoreErrorKind::StorePathCreate => "Store path create", } } From c48f3afcf470f19ea9a5b831ed243fcb45fb043c Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 18 Jan 2016 23:00:51 +0100 Subject: [PATCH 4/6] Return error if create() fails --- libimagstore/src/store.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/libimagstore/src/store.rs b/libimagstore/src/store.rs index 2ca35995..74ffcc58 100644 --- a/libimagstore/src/store.rs +++ b/libimagstore/src/store.rs @@ -58,7 +58,11 @@ impl Store { use std::fs::create_dir_all; if !location.exists() { - create_dir_all(location.clone()).ok(); // TODO: Error handling? + let c = create_dir_all(location.clone()); + if c.is_err() { + return Err(StoreError::new(StoreErrorKind::StorePathCreate, + Some(Box::new(c.err().unwrap())))); + } } else { if location.is_file() { return Err(StoreError::new(StoreErrorKind::StorePathExists, None)); From 6b2502c4d705bc6c43de0a02ca1af69811f92999 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 21 Jan 2016 18:42:43 +0100 Subject: [PATCH 5/6] rt: Add error for instantiation --- libimagrt/src/error.rs | 57 ++++++++++++++++++++++++++++++++++++++++++ libimagrt/src/lib.rs | 1 + 2 files changed, 58 insertions(+) create mode 100644 libimagrt/src/error.rs diff --git a/libimagrt/src/error.rs b/libimagrt/src/error.rs new file mode 100644 index 00000000..6d72cbe0 --- /dev/null +++ b/libimagrt/src/error.rs @@ -0,0 +1,57 @@ +use std::error::Error; +use std::fmt::Display; +use std::fmt::Formatter; +use std::fmt::Error as FmtError; +use std::clone::Clone; + +#[derive(Debug, PartialEq, Clone, Copy)] +pub enum RuntimeErrorKind { + Instantiate, + + // more? +} + +#[derive(Debug)] +pub struct RuntimeError { + kind: RuntimeErrorKind, + cause: Option>, +} + +impl RuntimeError { + + pub fn new(kind: RuntimeErrorKind, cause: Option>) -> RuntimeError { + RuntimeError { + kind: kind, + cause: cause, + } + } + +} + +fn runtime_error_kind_as_str(e: &RuntimeErrorKind) -> &'static str { + match e { + &RuntimeErrorKind::Instantiate => "Could not instantiate", + } +} + +impl Display for RuntimeError { + + fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> { + try!(write!(fmt, "{}", runtime_error_kind_as_str(&self.kind))); + Ok(()) + } + +} + +impl Error for RuntimeError { + + fn description(&self) -> &str { + runtime_error_kind_as_str(&self.kind) + } + + fn cause(&self) -> Option<&Error> { + self.cause.as_ref().map(|e| &**e) + } + +} + diff --git a/libimagrt/src/lib.rs b/libimagrt/src/lib.rs index 5d6c194c..4ef07144 100644 --- a/libimagrt/src/lib.rs +++ b/libimagrt/src/lib.rs @@ -9,6 +9,7 @@ extern crate libimagstore; extern crate libimagutil; mod configuration; +mod error; mod logger; mod runtime; From aec16491669a8f111ad51cf7f54c3152ff61e9d6 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 21 Jan 2016 18:43:03 +0100 Subject: [PATCH 6/6] rt: Runtime::new() should return Result<> --- libimagrt/src/runtime.rs | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/libimagrt/src/runtime.rs b/libimagrt/src/runtime.rs index e5a81a57..fe6e87d2 100644 --- a/libimagrt/src/runtime.rs +++ b/libimagrt/src/runtime.rs @@ -1,4 +1,5 @@ use std::path::PathBuf; +use std::result::Result as RResult; pub use clap::App; @@ -7,6 +8,8 @@ use log; use log::LogLevelFilter; use configuration::Configuration; +use error::RuntimeError; +use error::RuntimeErrorKind; use logger::ImagLogger; use libimagstore::store::Store; @@ -28,7 +31,7 @@ impl<'a> Runtime<'a> { * The cli_spec object should be initially build with the ::get_default_cli_builder() function. * */ - pub fn new(cli_spec: App<'a, 'a, 'a, 'a, 'a, 'a>) -> Runtime<'a> { + pub fn new(cli_spec: App<'a, 'a, 'a, 'a, 'a, 'a>) -> Result, RuntimeError> { let matches = cli_spec.get_matches(); let rtp : PathBuf = matches.value_of("runtimepath").unwrap_or("~/.imag/").into(); let storepath = matches.value_of("storepath") @@ -38,12 +41,17 @@ impl<'a> Runtime<'a> { spath.push("/store"); spath }); - Runtime { - cli_matches: matches, - configuration: Configuration::new(&rtp).unwrap_or(Configuration::default()), - rtp: rtp, - store: Store::new(storepath), - } + Store::new(storepath).map(|store| { + Runtime { + cli_matches: matches, + configuration: Configuration::new(&rtp).unwrap_or(Configuration::default()), + rtp: rtp, + store: store, + } + }) + .map_err(|e| { + RuntimeError::new(RuntimeErrorKind::Instantiate, Some(Box::new(e))) + }) } /**