From 3c40df1ae70600562348f94da49f73a194d77731 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 14 Apr 2016 17:15:19 +0200 Subject: [PATCH] Add Store::save_to() to create a copy of an entry --- libimagstore/src/store.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/libimagstore/src/store.rs b/libimagstore/src/store.rs index 2225073b..33115715 100644 --- a/libimagstore/src/store.rs +++ b/libimagstore/src/store.rs @@ -607,6 +607,27 @@ impl Store { .map_err(|e| SEK::DeleteCallError.into_error_with_cause(e)) } + /// Save a copy of the Entry in another place + /// Executes the post_move_aspects for the new id + pub fn save_to(&self, entry: &FileLockEntry, new_id: StoreId) -> Result<()> { + use std::fs::copy; + + 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(); + + copy(old_id.clone(), new_id.clone()) + .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)) + } + /// Save an Entry in another place /// Removes the original entry /// Executes the post_move_aspects for the new id