From 12cac663d0942deca1254bbea2233dd786ba37cf Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 17 Mar 2016 13:40:06 +0100 Subject: [PATCH] Implement save_as() --- libimagstore/src/store.rs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/libimagstore/src/store.rs b/libimagstore/src/store.rs index 1c4b9aa7..5cdd7b0b 100644 --- a/libimagstore/src/store.rs +++ b/libimagstore/src/store.rs @@ -591,9 +591,29 @@ impl Store { .map_err(|e| SEK::DeleteCallError.into_error_with_cause(e)) } - /// Move an entry + /// Save an Entry in another place + /// Removes the original entry + /// Executes the post_move_aspects for the new id pub fn save_as(&self, entry: FileLockEntry, new_id: StoreId) -> Result<()> { - unimplemented!() + use std::fs::copy; + use std::fs::remove_file; + + let new_id = self.storify_id(new_id); + let hsmap = self.entries.write(); + if hsmap.is_err() { + return Err(StoreError::new(StoreErrorKind::LockPoisoned, None)) + } + if hsmap.unwrap().contains_key(&new_id) { + return Err(StoreError::new(StoreErrorKind::EntryAlreadyExists, None)) + } + + let old_id = entry.get_location().clone(); + drop(entry); // so no funny things happen in the next step + + copy(old_id.clone(), new_id.clone()) + .and_then(|_| remove_file(old_id)) + .map_err(|e| StoreError::new(StoreErrorKind::FileError, Some(Box::new(e)))) + .and_then(|_| self.execute_hooks_for_id(self.post_move_aspects.clone(), &new_id)) } /// Move an entry without loading