diff --git a/imagrc.toml b/imagrc.toml index f11659e1..82a2dae3 100644 --- a/imagrc.toml +++ b/imagrc.toml @@ -3,6 +3,10 @@ [store] +# Set to false if you do not want imag to create the directory where the store +# lives implicitely +implicit-create = false + # Hooks which get executed right before the Store is closed. # They get the store path as StoreId passed, so they can alter the complete # store, so these hooks should be chosen carefully. diff --git a/libimagstore/src/configuration.rs b/libimagstore/src/configuration.rs index 33e2dd6f..ff94a1a3 100644 --- a/libimagstore/src/configuration.rs +++ b/libimagstore/src/configuration.rs @@ -148,6 +148,32 @@ pub fn config_is_valid(config: &Option) -> bool { } } +/// Checks whether the store configuration has a key "implicit-create" which maps to a boolean +/// value. If that key is present, the boolean is returned, otherwise false is returned. +pub fn config_implicit_store_create_allowed(config: Option<&Value>) -> bool { + config.map(|t| { + match *t { + Value::Table(ref t) => { + match t.get("implicit-create") { + Some(&Value::Boolean(b)) => b, + Some(_) => { + warn!("Key 'implicit-create' does not contain a Boolean value"); + false + } + None => { + warn!("Key 'implicit-create' in store configuration missing"); + false + }, + } + } + _ => { + warn!("Store configuration seems to be no Table"); + false + }, + } + }).unwrap_or(false) +} + pub fn get_store_unload_aspect_names(value: &Option) -> Vec { get_aspect_names_for_aspect_position("store-unload-hook-aspects", value) } diff --git a/libimagstore/src/error.rs b/libimagstore/src/error.rs index d9a7fd99..30a00ab4 100644 --- a/libimagstore/src/error.rs +++ b/libimagstore/src/error.rs @@ -6,6 +6,7 @@ pub struct CustomErrorData {} generate_custom_error_types!(StoreError, StoreErrorKind, CustomErrorData, ConfigurationError => "Store Configuration Error", + CreateStoreDirDenied => "Creating store directory implicitely denied", FileError => "File Error", IoError => "IO Error", IdLocked => "ID locked", diff --git a/libimagstore/src/store.rs b/libimagstore/src/store.rs index be05710e..8e63329f 100644 --- a/libimagstore/src/store.rs +++ b/libimagstore/src/store.rs @@ -212,6 +212,15 @@ impl Store { debug!("Building new Store object"); if !location.exists() { + if !config_implicit_store_create_allowed(store_config.as_ref()) { + warn!("Implicitely creating store directory is denied"); + warn!(" -> Either because configuration does not allow it"); + warn!(" -> or because there is no configuration"); + return Err(SEK::CreateStoreDirDenied.into_error()) + .map_err_into(SEK::FileError) + .map_err_into(SEK::IoError); + } + debug!("Creating store path"); let c = create_dir_all(location.clone()); if c.is_err() { diff --git a/tests/utils.sh b/tests/utils.sh index 6fc23ad8..e0153bdd 100644 --- a/tests/utils.sh +++ b/tests/utils.sh @@ -37,7 +37,7 @@ imag-call-binary() { local binary=$1; shift [[ -d $searchdir ]] || { err "FATAL: No directory $searchdir"; exit 1; } local bin=$(find $searchdir -iname $binary -type f -executable) - local flags="--debug --rtp $RUNTIME" + local flags="--config ./imagrc.toml --override-config store.implicit-create=true --debug --rtp $RUNTIME" out "Calling '$bin $flags $*'" $bin $flags $* }