From 6a1ffe059b418ed8838c193e01d83ebaafcacb63 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 5 Sep 2016 14:13:16 +0200 Subject: [PATCH 1/5] This patch puts the hasher name in the header This was a bug, as the previous implementation simply sets the hash, but we need to set the hash in a place where we can re-find it with a hasher. --- libimagref/src/reference.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/libimagref/src/reference.rs b/libimagref/src/reference.rs index fabb26a5..ba01f341 100644 --- a/libimagref/src/reference.rs +++ b/libimagref/src/reference.rs @@ -168,13 +168,17 @@ impl<'a> Ref<'a> { }; for tpl in [ - Some(("ref", Value::Table(BTreeMap::new()))), - Some(("ref.permissions", Value::Table(BTreeMap::new()))), + Some((String::from("ref"), Value::Table(BTreeMap::new()))), + Some((String::from("ref.permissions"), Value::Table(BTreeMap::new()))), + Some((String::from("ref.path"), Value::String(canonical_path))), + Some((String::from("ref.content_hash"), Value::Table(BTreeMap::new()))), - Some(("ref.path", Value::String(canonical_path))), - - content_hash.map(|h| ("ref.content_hash", Value::String(h))), - permissions.map(|p| ("ref.permissions.ro", Value::Boolean(p.readonly()))), + content_hash.map(|hash| { + (format!("ref.content_hash.{}", h.hash_name()), Value::String(hash)) + }), + permissions.map(|p| { + (String::from("ref.permissions.ro"), Value::Boolean(p.readonly())) + }), ].into_iter() { match tpl { From 8a3d4fc0b204607c73785a0b5a6d036054125ddd Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 5 Sep 2016 14:17:55 +0200 Subject: [PATCH 2/5] Fix Ref::update_ref() to set the appropriate header field --- libimagref/src/reference.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/libimagref/src/reference.rs b/libimagref/src/reference.rs index ba01f341..3fed6680 100644 --- a/libimagref/src/reference.rs +++ b/libimagref/src/reference.rs @@ -352,7 +352,7 @@ impl<'a> Ref<'a> { /// Update the Ref by re-checking the file from FS /// This errors if the file is not present or cannot be read() pub fn update_ref(&mut self) -> Result<()> { - let current_hash = try!(self.get_current_hash()); + let current_hash = try!(self.get_current_hash()); // uses the default hasher let current_perm = try!(self.get_current_permissions()); try!(self.0 @@ -362,9 +362,11 @@ impl<'a> Ref<'a> { .map_err(|e| REK::StoreWriteError.into_error_with_cause(e)) ); + let hasher_name = DefaultHasher::new().hash_name(); + try!(self.0 .get_header_mut() - .set("ref.content_hash", Value::String(current_hash)) + .set(&format!("ref.content_hash.{}", hasher_name)[..], Value::String(current_hash)) .map_err(Box::new) .map_err(|e| REK::StoreWriteError.into_error_with_cause(e)) ); From a6ca041903aef5b3c91dab00797a2f25a28eab68 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 5 Sep 2016 14:19:37 +0200 Subject: [PATCH 3/5] Fix Ref::get_stored_hash() for reading the right header field --- libimagref/src/reference.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libimagref/src/reference.rs b/libimagref/src/reference.rs index 3fed6680..571312bf 100644 --- a/libimagref/src/reference.rs +++ b/libimagref/src/reference.rs @@ -239,7 +239,8 @@ impl<'a> Ref<'a> { /// Get the hash of the link target which is stored in the ref object pub fn get_stored_hash(&self) -> Result { - match self.0.get_header().read("ref.content_hash") { + let hasher_name = DefaultHasher::new().hash_name(); + match self.0.get_header().read(&format!("ref.content_hash.{}", hasher_name)[..]) { // content hash stored... Ok(Some(Value::String(s))) => Ok(s), From 5399b8ca7bf6f478c56c815b23b2be9833a73505 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 5 Sep 2016 14:21:56 +0200 Subject: [PATCH 4/5] Add variant of Ref::update_ref() to update with custom Hasher instance --- libimagref/src/reference.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/libimagref/src/reference.rs b/libimagref/src/reference.rs index 571312bf..e1d0f02a 100644 --- a/libimagref/src/reference.rs +++ b/libimagref/src/reference.rs @@ -353,6 +353,12 @@ impl<'a> Ref<'a> { /// Update the Ref by re-checking the file from FS /// This errors if the file is not present or cannot be read() pub fn update_ref(&mut self) -> Result<()> { + self.update_ref_with_hasher(&DefaultHasher::new()) + } + + /// Update the Ref by re-checking the file from FS using the passed Hasher instance + /// This errors if the file is not present or cannot be read() + pub fn update_ref_with_hasher(&mut self, h: &H) -> Result<()> { let current_hash = try!(self.get_current_hash()); // uses the default hasher let current_perm = try!(self.get_current_permissions()); @@ -363,11 +369,9 @@ impl<'a> Ref<'a> { .map_err(|e| REK::StoreWriteError.into_error_with_cause(e)) ); - let hasher_name = DefaultHasher::new().hash_name(); - try!(self.0 .get_header_mut() - .set(&format!("ref.content_hash.{}", hasher_name)[..], Value::String(current_hash)) + .set(&format!("ref.content_hash.{}", h.hash_name())[..], Value::String(current_hash)) .map_err(Box::new) .map_err(|e| REK::StoreWriteError.into_error_with_cause(e)) ); From 55846168dce4a296d87ef8fa582e6e72e304717e Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 5 Sep 2016 14:23:47 +0200 Subject: [PATCH 5/5] Add variant of Ref::get_stored_hash() to get stored hash with custom hasher --- libimagref/src/reference.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/libimagref/src/reference.rs b/libimagref/src/reference.rs index e1d0f02a..8df36f93 100644 --- a/libimagref/src/reference.rs +++ b/libimagref/src/reference.rs @@ -239,8 +239,13 @@ impl<'a> Ref<'a> { /// Get the hash of the link target which is stored in the ref object pub fn get_stored_hash(&self) -> Result { - let hasher_name = DefaultHasher::new().hash_name(); - match self.0.get_header().read(&format!("ref.content_hash.{}", hasher_name)[..]) { + self.get_stored_hash_with_hasher(&DefaultHasher::new()) + } + + /// Get the hahs of the link target which is stored in the ref object, which is hashed with a + /// custom Hasher instance. + pub fn get_stored_hash_with_hasher(&self, h: &H) -> Result { + match self.0.get_header().read(&format!("ref.content_hash.{}", h.hash_name())[..]) { // content hash stored... Ok(Some(Value::String(s))) => Ok(s),