diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2018-02-13 22:37:22 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2018-02-19 12:14:32 +0100 |
commit | cbb47cffcbb3d6cd2d238c587a8b3745ac5ed384 (patch) | |
tree | 04b8860e4b6e9ab0491abea462affc5eca59af48 /lib/entry/libimagentryref | |
parent | ed69fd4b3599a5a939904d64b4dc29ba27eadb06 (diff) |
Implement Ref functions
Diffstat (limited to 'lib/entry/libimagentryref')
-rw-r--r-- | lib/entry/libimagentryref/src/reference.rs | 37 |
1 files changed, 21 insertions, 16 deletions
diff --git a/lib/entry/libimagentryref/src/reference.rs b/lib/entry/libimagentryref/src/reference.rs index 43222a86..b2828e28 100644 --- a/lib/entry/libimagentryref/src/reference.rs +++ b/lib/entry/libimagentryref/src/reference.rs @@ -42,7 +42,7 @@ pub trait Ref { /// Get the stored hash. /// /// Does not need a `UniqueRefPathGenerator` as it reads the hash stored in the header - fn get_hash(&self) -> Result<String>; + fn get_hash(&self) -> Result<&str>; /// Get the referenced path. /// @@ -50,12 +50,7 @@ pub trait Ref { fn get_path(&self) -> Result<PathBuf>; /// Check whether the referenced file still matches its hash - fn hash_valid<RPG: UniqueRefPathGenerator>(&self) -> Result<bool>; - - /// Update the stored hash - /// - /// This updates the hash in the header and moves the entry to the appropriate location - fn update_hash<RPG: UniqueRefPathGenerator>(&mut self, store: &Store) -> Result<bool>; + fn hash_valid<RPG: UniqueRefPathGenerator>(&self) -> RResult<bool, RPG::Error>; /// Alias for `r.fs_link_exists() && r.deref().is_file()` fn is_ref_to_file(&self) -> Result<bool> { @@ -83,20 +78,30 @@ impl Ref for Entry { self.is::<IsRef>().map_err(From::from) } - fn get_hash(&self) -> Result<String> { - unimplemented!() + fn get_hash(&self) -> Result<&str> { + self.get_header() + .read("ref.hash") + .map_err(RE::from)? + .ok_or_else(|| REK::HeaderFieldMissingError("ref.hash").into()) + .and_then(|v| v.as_str().ok_or_else(|| REK::HeaderTypeError("ref.hash", "string").into())) } fn get_path(&self) -> Result<PathBuf> { - unimplemented!() - } - - fn hash_valid<RPG: UniqueRefPathGenerator>(&self) -> Result<bool> { - unimplemented!() + self.get_header() + .read("ref.path") + .map_err(RE::from)? + .ok_or_else(|| REK::HeaderFieldMissingError("ref.path").into()) + .and_then(|v| v.as_str().ok_or_else(|| REK::HeaderTypeError("ref.path", "string").into())) + .map(PathBuf::from) } - fn update_hash<RPG: UniqueRefPathGenerator>(&mut self, store: &Store) -> Result<bool> { - unimplemented!() + fn hash_valid<RPG: UniqueRefPathGenerator>(&self) -> RResult<bool, RPG::Error> { + self.get_path() + .map(PathBuf::from) + .map_err(RE::from) + .map_err(RPG::Error::from) + .and_then(|pb| RPG::unique_hash(pb)) + .and_then(|h| Ok(h == self.get_hash()?)) } } |