From 079675c4f07f6435d03d93e6061d3cb6258cabf3 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 23 Jun 2016 15:26:28 +0200 Subject: [PATCH] Impl Ref::fs_link_valid_hash() --- libimagref/src/error.rs | 1 + libimagref/src/reference.rs | 27 +++++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/libimagref/src/error.rs b/libimagref/src/error.rs index 50c0723a..4fee1429 100644 --- a/libimagref/src/error.rs +++ b/libimagref/src/error.rs @@ -2,6 +2,7 @@ generate_error_module!( generate_error_types!(RefError, RefErrorKind, StoreReadError => "Store read error", StoreWriteError => "Store write error", + IOError => "IO Error", HeaderTypeError => "Header type error", HeaderFieldMissingError => "Header field missing error", HeaderFieldWriteError => "Header field cannot be written", diff --git a/libimagref/src/reference.rs b/libimagref/src/reference.rs index 9d15dfd6..fb0d34eb 100644 --- a/libimagref/src/reference.rs +++ b/libimagref/src/reference.rs @@ -223,8 +223,31 @@ impl<'a> Ref<'a> { } /// Check whether the Hashsum of the referenced file is equal to the stored hashsum - pub fn fs_link_valid_hash(&self) -> bool { - unimplemented!() + pub fn fs_link_valid_hash(&self) -> Result { + let stored_hash = try!(match self.0.get_header().read("ref.content_hash") { + // content hash stored... + Ok(Some(Value::String(s))) => Ok(s), + + // content hash header field has wrong type + Ok(Some(_)) => Err(REK::HeaderTypeError.into_error()), + + // content hash not stored + Ok(None) => Err(REK::HeaderFieldMissingError.into_error()), + + // Error + Err(e) => Err(REK::StoreReadError.into_error_with_cause(Box::new(e))), + }); + + let current_hash = try!(self.fs_file() + .and_then(|pb| { + File::open(pb) + .map_err(Box::new) + .map_err(|e| REK::IOError.into_error_with_cause(e)) + }) + .map(|mut file| hash_file_contents(&mut file)) + ); + + Ok(stored_hash == current_hash) } /// Update the Ref by re-checking the file from FS