From e4e5b5217115e7942a783e3a9f1cc2acf6de2a49 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 5 Sep 2016 18:12:29 +0200 Subject: [PATCH 1/4] Fix Iterator impl for GlobStoreIdIterator This patch fixes the `impl Iterator for GlobStoreIdIterator` which used the glob() result to fetch the files from the FS, but glob() returns the absolute pathes (to filesystem root). We have to strip the `store_path` prefix and use the local part for building the StoreId object. --- libimagstore/src/store.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/libimagstore/src/store.rs b/libimagstore/src/store.rs index 0a510ab9..b1709574 100644 --- a/libimagstore/src/store.rs +++ b/libimagstore/src/store.rs @@ -1503,6 +1503,8 @@ mod glob_store_iter { impl GlobStoreIdIterator { pub fn new(paths: Paths, store_path: PathBuf) -> GlobStoreIdIterator { + debug!("Create a GlobStoreIdIterator(store_path = {:?}, /* ... */)", store_path); + GlobStoreIdIterator { store_path: store_path, paths: paths, @@ -1518,8 +1520,13 @@ mod glob_store_iter { self.paths .next() .and_then(|o| { + debug!("GlobStoreIdIterator::next() => {:?}", o); o.map_err_into(SEK::StoreIdHandlingError) - .and_then(|p| StoreId::new(Some(self.store_path.clone()), p)) + .and_then(|p| { + let p = try!(p.strip_prefix(&self.store_path) + .map_err_into(SEK::StoreIdHandlingError)); + StoreId::new(Some(self.store_path.clone()), PathBuf::from(p)) + }) .map_err(|e| { debug!("GlobStoreIdIterator error: {:?}", e); trace_error(&e); From f60afa581c66fa8da5f5ba39397587cf270605c3 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 5 Sep 2016 18:21:18 +0200 Subject: [PATCH 2/4] Add error type for failed build of StoreId object from full path --- libimagstore/src/error.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/libimagstore/src/error.rs b/libimagstore/src/error.rs index 2eef6d53..795f1a14 100644 --- a/libimagstore/src/error.rs +++ b/libimagstore/src/error.rs @@ -43,6 +43,7 @@ generate_custom_error_types!(StoreError, StoreErrorKind, CustomErrorData, EntryRenameError => "Entry rename error", StoreIdHandlingError => "StoreId handling error", StoreIdLocalPartAbsoluteError => "StoreId 'id' part is absolute (starts with '/') which is not allowed", + StoreIdBuildFromFullPathError => "Building StoreId from full file path failed", CreateCallError => "Error when calling create()", RetrieveCallError => "Error when calling retrieve()", From 8362b077e2b4269a1a32d76bc4d9e65985f71de8 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 5 Sep 2016 18:21:34 +0200 Subject: [PATCH 3/4] Add StoreId::from_full_path() --- libimagstore/src/storeid.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/libimagstore/src/storeid.rs b/libimagstore/src/storeid.rs index 4bb26bf6..5cf2037a 100644 --- a/libimagstore/src/storeid.rs +++ b/libimagstore/src/storeid.rs @@ -1,3 +1,5 @@ +use std::ops::Deref; +use std::path::Path; use std::path::PathBuf; use std::fmt::{Display, Debug, Formatter}; @@ -8,6 +10,7 @@ use std::path::Components; use libimagerror::into::IntoError; use error::StoreErrorKind as SEK; +use error::MapErrInto; use store::Result; use store::Store; @@ -24,6 +27,21 @@ impl StoreId { StoreId::new_baseless(id).map(|mut sid| { sid.base = base; sid }) } + /// Try to create a StoreId object from a filesystem-absolute path. + /// + /// Automatically creates a StoreId object which has a `base` set to `store_part` if stripping + /// the `store_part` from the `full_path` succeeded. + /// + /// Returns a `StoreErrorKind::StoreIdBuildFromFullPathError` if stripping failes. + pub fn from_full_path(store_part: &PathBuf, full_path: D) -> Result + where D: Deref + { + let p = try!( + full_path.strip_prefix(store_part).map_err_into(SEK::StoreIdBuildFromFullPathError) + ); + StoreId::new(Some(store_part.clone()), PathBuf::from(p)) + } + pub fn new_baseless(id: PathBuf) -> Result { if id.is_absolute() { Err(SEK::StoreIdLocalPartAbsoluteError.into_error()) From 364ad01179dfe233708403ec3b0d674862309163 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 5 Sep 2016 18:22:55 +0200 Subject: [PATCH 4/4] Use StoreId::from_full_path() in GlobStoreIdIterator::next() --- libimagstore/src/store.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/libimagstore/src/store.rs b/libimagstore/src/store.rs index b1709574..5ebd9953 100644 --- a/libimagstore/src/store.rs +++ b/libimagstore/src/store.rs @@ -1522,11 +1522,7 @@ mod glob_store_iter { .and_then(|o| { debug!("GlobStoreIdIterator::next() => {:?}", o); o.map_err_into(SEK::StoreIdHandlingError) - .and_then(|p| { - let p = try!(p.strip_prefix(&self.store_path) - .map_err_into(SEK::StoreIdHandlingError)); - StoreId::new(Some(self.store_path.clone()), PathBuf::from(p)) - }) + .and_then(|p| StoreId::from_full_path(&self.store_path, p)) .map_err(|e| { debug!("GlobStoreIdIterator error: {:?}", e); trace_error(&e);