From f885e6c80b1ab25d20d3e472ed9230e9d86e5cbe Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 17 Mar 2016 13:39:32 +0100 Subject: [PATCH] Implement move_by_id() --- libimagstore/src/error.rs | 2 +- libimagstore/src/store.rs | 22 +++++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/libimagstore/src/error.rs b/libimagstore/src/error.rs index 6432e0f3..3b3fa39b 100644 --- a/libimagstore/src/error.rs +++ b/libimagstore/src/error.rs @@ -33,6 +33,7 @@ generate_custom_error_types!(StoreError, StoreErrorKind, CustomErrorData, GlobError => "glob() error", EncodingError => "Encoding error", StorePathError => "Store Path error", + EntryRenameError => "Entry rename error", CreateCallError => "Error when calling create()", RetrieveCallError => "Error when calling retrieve()", @@ -42,7 +43,6 @@ generate_custom_error_types!(StoreError, StoreErrorKind, CustomErrorData, UpdateCallError => "Error when calling update()", RetrieveCopyCallError => "Error when calling retrieve_copy()", DeleteCallError => "Error when calling delete()" - ); generate_custom_error_types!(ParserError, ParserErrorKind, CustomErrorData, diff --git a/libimagstore/src/store.rs b/libimagstore/src/store.rs index ba254ac7..1c4b9aa7 100644 --- a/libimagstore/src/store.rs +++ b/libimagstore/src/store.rs @@ -598,7 +598,27 @@ impl Store { /// Move an entry without loading pub fn move_by_id(&self, old_id: StoreId, new_id: StoreId) -> Result<()> { - unimplemented!() + use std::fs::rename; + + let new_id = self.storify_id(new_id); + let old_id = self.storify_id(old_id); + let hsmap = self.entries.write(); + if hsmap.is_err() { + return Err(SE::new(SEK::LockPoisoned, None)) + } + if hsmap.unwrap().contains_key(&old_id) { + return Err(SE::new(SEK::EntryAlreadyBorrowed, None)); + } else { + match rename(old_id, new_id.clone()) { + Err(e) => { + let kind = SEK::EntryRenameError; + return Err(SE::new(kind, Some(Box::new(e)))); + }, + _ => { + debug!("Rename worked"); + }, + } + } } /// Gets the path where this store is on the disk