diff --git a/libimagstore/src/error.rs b/libimagstore/src/error.rs index 9d612475..e4c86619 100644 --- a/libimagstore/src/error.rs +++ b/libimagstore/src/error.rs @@ -6,6 +6,8 @@ use std::clone::Clone; #[derive(Clone, Copy, Debug)] pub enum StoreErrorKind { + FileError, + IdLocked, IdNotFound, OutOfMemory, // maybe more @@ -13,6 +15,8 @@ pub enum StoreErrorKind { 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", } diff --git a/libimagstore/src/store.rs b/libimagstore/src/store.rs index 5d9e3fbe..dddfd302 100644 --- a/libimagstore/src/store.rs +++ b/libimagstore/src/store.rs @@ -1,5 +1,5 @@ use std::collections::HashMap; -use std::fs::File; +use std::fs::{File, remove_file}; use std::ops::Drop; use std::path::PathBuf; use std::result::Result as RResult; @@ -9,7 +9,7 @@ use std::sync::RwLock; use fs2::FileExt; use entry::Entry; -use error::StoreError; +use error::{StoreError, StoreErrorKind}; /// The Result Type returned by any interaction with the store that could fail pub type Result = RResult; @@ -156,7 +156,17 @@ impl Store { /// Delete an entry pub fn delete(&self, id: StoreId) -> Result<()> { - unimplemented!(); + let mut entries_lock = self.entries.write(); + let mut entries = entries_lock.unwrap(); + + // if the entry is currently modified by the user, we cannot drop it + if entries.get(&id).map(|e| e.is_borrowed()).unwrap_or(false) { + return Err(StoreError::new(StoreErrorKind::IdLocked, None)); + } + + // remove the entry first, then the file + entries.remove(&id); + remove_file(&id).map_err(|e| StoreError::new(StoreErrorKind::FileError, Some(Box::new(e)))) } }