From 78701c73325b37446d8c2086126eba3f820fee86 Mon Sep 17 00:00:00 2001 From: Julian Ganz Date: Sun, 17 Jan 2016 16:20:58 +0100 Subject: [PATCH 1/4] Add error type for communicating that an entry is locked We need this error type to let the user know when she is trying to remove an item which is currently edited. --- libimagstore/src/error.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libimagstore/src/error.rs b/libimagstore/src/error.rs index 8a2d56d8..4b36d3dc 100644 --- a/libimagstore/src/error.rs +++ b/libimagstore/src/error.rs @@ -10,6 +10,7 @@ use std::io::Error as IOError; #[derive(Clone, Copy, Debug)] pub enum StoreErrorKind { + IdLocked, IdNotFound, OutOfMemory, // maybe more @@ -17,6 +18,7 @@ pub enum StoreErrorKind { fn store_error_type_as_str(e: &StoreErrorKind) -> &'static str { match e { + &StoreErrorKind::IdLocked => "ID locked", &StoreErrorKind::IdNotFound => "ID not found", &StoreErrorKind::OutOfMemory => "Out of Memory", } From bcebe865466de0d2401ec9cdc1460742f639f482 Mon Sep 17 00:00:00 2001 From: Julian Ganz Date: Sun, 17 Jan 2016 18:01:52 +0100 Subject: [PATCH 2/4] Add new error type for low-level file system errors --- libimagstore/src/error.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libimagstore/src/error.rs b/libimagstore/src/error.rs index 4b36d3dc..b9057a2c 100644 --- a/libimagstore/src/error.rs +++ b/libimagstore/src/error.rs @@ -10,6 +10,7 @@ use std::io::Error as IOError; #[derive(Clone, Copy, Debug)] pub enum StoreErrorKind { + FileError, IdLocked, IdNotFound, OutOfMemory, @@ -18,6 +19,7 @@ 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", From e6c05e4f5b39d07a04e722756d86c213573db397 Mon Sep 17 00:00:00 2001 From: Julian Ganz Date: Sun, 17 Jan 2016 16:22:50 +0100 Subject: [PATCH 3/4] Implement Store::delete() --- libimagstore/src/store.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/libimagstore/src/store.rs b/libimagstore/src/store.rs index 1385584b..2857661b 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, Mutex}; 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; @@ -147,7 +147,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))) } ) } } From 82ed978e90e95c17cc813e3d32b376968a0ed3d4 Mon Sep 17 00:00:00 2001 From: Julian Ganz Date: Sun, 17 Jan 2016 19:08:15 +0100 Subject: [PATCH 4/4] Styling: remove some whitespace and a scope --- libimagstore/src/store.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libimagstore/src/store.rs b/libimagstore/src/store.rs index 2857661b..2d754af3 100644 --- a/libimagstore/src/store.rs +++ b/libimagstore/src/store.rs @@ -151,13 +151,13 @@ impl Store { 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) { + 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))) } ) + remove_file(&id).map_err(|e| StoreError::new(StoreErrorKind::FileError, Some(Box::new(e)))) } }