From ed862db76ffe3311eb534d69a6eb99ff63fdeb5a Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 19 Feb 2019 21:49:52 +0100 Subject: [PATCH] Fix: Strip the prefix of the path The previous implementation did not strip the prefix of the "relpath" header value, which resulted in the whole path being in the header, which is obviously wrong. Signed-off-by: Matthias Beyer --- lib/entry/libimagentryref/src/reference.rs | 30 ++++++++++++++++------ 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/lib/entry/libimagentryref/src/reference.rs b/lib/entry/libimagentryref/src/reference.rs index bcb7ba66..9815f4eb 100644 --- a/lib/entry/libimagentryref/src/reference.rs +++ b/lib/entry/libimagentryref/src/reference.rs @@ -305,7 +305,14 @@ impl<'a, H> MutRef for MutRefWithHasher<'a, H> debug!("Entry hashing..."); let _ = H::hash(&file_path) - .and_then(|hash| make_header_section(hash, H::NAME, path, collection_name)) + .and_then(|hash| { + // stripping the prefix of "path" + let relpath = path.as_ref() + .strip_prefix(get_basepath(collection_name.as_ref(), config)?)?; + trace!("Using relpath = {} to make header section", relpath.display()); + + make_header_section(hash, H::NAME, relpath, collection_name) + }) .and_then(|h| self.0.get_header_mut().insert("ref", h).map_err(Error::from)) .and_then(|_| self.0.set_isflag::()) .context("Making ref out of entry")?; @@ -356,6 +363,13 @@ pub(crate) fn make_header_section(hash: String, hashname: H, relpath: P Ok(header_section) } +fn get_basepath<'a, Coll: AsRef>(collection_name: Coll, config: &'a Config) -> Result<&'a PathBuf> { + config.get(collection_name.as_ref()) + .ok_or_else(|| format_err!("Collection {} seems not to exist in config", + collection_name.as_ref())) + .map_err(Error::from) +} + fn get_file_path

(config: &Config, collection_name: &str, path: P) -> Result where P: AsRef { @@ -420,11 +434,11 @@ mod test { setup_logging(); let store = get_store(); let mut entry = store.retrieve(PathBuf::from("test_makeref")).unwrap(); - let file = PathBuf::from("/"); // has to exist + let file = PathBuf::from("/tmp"); // has to exist let collection_name = "some_collection"; let config = Config({ let mut c = BTreeMap::new(); - c.insert(String::from("some_collection"), PathBuf::from("/tmp")); + c.insert(String::from("some_collection"), PathBuf::from("/")); c }); @@ -437,11 +451,11 @@ mod test { setup_logging(); let store = get_store(); let mut entry = store.retrieve(PathBuf::from("test_makeref_isref")).unwrap(); - let file = PathBuf::from("/"); // has to exists + let file = PathBuf::from("/tmp"); // has to exists let collection_name = "some_collection"; let config = Config({ let mut c = BTreeMap::new(); - c.insert(String::from("some_collection"), PathBuf::from("/tmp")); + c.insert(String::from("some_collection"), PathBuf::from("/")); c }); @@ -456,7 +470,7 @@ mod test { setup_logging(); let store = get_store(); let mut entry = store.retrieve(PathBuf::from("test_makeref_is_ref_with_testhash")).unwrap(); - let file = PathBuf::from("/"); // has to exist + let file = PathBuf::from("/tmp"); // has to exist let collection_name = "some_collection"; let config = Config({ let mut c = BTreeMap::new(); @@ -479,8 +493,8 @@ mod test { assert_eq!(var.unwrap(), shouldbe, "{} is not == {}", location, shouldbe); }; - check_isstr(&entry, "ref.relpath", "/"); - check_isstr(&entry, "ref.hash.Testhasher", "/"); // TestHasher hashes by returning the path itself + check_isstr(&entry, "ref.relpath", "tmp"); + check_isstr(&entry, "ref.hash.Testhasher", "/tmp"); // TestHasher hashes by returning the path itself check_isstr(&entry, "ref.collection", "some_collection"); }