diff --git a/libimagstore/Cargo.toml b/libimagstore/Cargo.toml index 4089feda..acee580b 100644 --- a/libimagstore/Cargo.toml +++ b/libimagstore/Cargo.toml @@ -66,3 +66,8 @@ verify = [] # early-panic=[] +# File system locking +# +# Enable this feature to enable file-system locking in the store. +fs-locking = [] + diff --git a/libimagstore/src/store.rs b/libimagstore/src/store.rs index 4d7e811d..333622dc 100644 --- a/libimagstore/src/store.rs +++ b/libimagstore/src/store.rs @@ -130,11 +130,18 @@ impl Iterator for Walk { } } - impl StoreEntry { fn new(id: StoreId) -> Result { let pb = try!(id.clone().into_pathbuf()); + + #[cfg(feature = "fs-lock")] + { + try!(open_file(pb.clone()) + .and_then(|f| f.lock_exclusive().map_err_into(SEK::FileError)) + .map_err_into(SEK::IoError)); + } + Ok(StoreEntry { id: id, file: FileAbstraction::Absent(pb), @@ -176,6 +183,19 @@ impl StoreEntry { } } +#[cfg(feature = "fs-lock")] +impl Drop for StoreEntry { + + fn drop(self) { + self.get_entry() + .and_then(|entry| open_file(entry.get_location().clone()).map_err_into(SEK::IoError)) + .and_then(|f| f.unlock().map_err_into(SEK::FileError)) + .map_err_into(SEK::IoError) + } + +} + + /// The Store itself, through this object one can interact with IMAG's entries pub struct Store { location: PathBuf,