diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2016-06-23 15:26:18 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2016-07-04 19:29:02 +0200 |
commit | 6757c673c4307299c7fa7ddd249dbd8564ad15c8 (patch) | |
tree | f7a14243697c4d0609788d694eed2ca3bb1ed2ac /libimagref | |
parent | 7613526aff7a23a15986620d60d49891de739bf6 (diff) |
Extract file-content-hashing functionality to new private function
Diffstat (limited to 'libimagref')
-rw-r--r-- | libimagref/src/reference.rs | 24 |
1 files changed, 14 insertions, 10 deletions
diff --git a/libimagref/src/reference.rs b/libimagref/src/reference.rs index ca8ca190..9d15dfd6 100644 --- a/libimagref/src/reference.rs +++ b/libimagref/src/reference.rs @@ -5,6 +5,8 @@ use std::path::PathBuf; use std::ops::Deref; use std::ops::DerefMut; use std::collections::BTreeMap; +use std::fs::File; +use std::io::Read; use libimagstore::store::FileLockEntry; use libimagstore::storeid::StoreId; @@ -13,6 +15,8 @@ use libimagstore::store::Store; use libimagerror::into::IntoError; use toml::Value; +use crypto::sha1::Sha1; +use crypto::digest::Digest; use error::RefErrorKind as REK; use flags::RefFlags; @@ -43,11 +47,6 @@ impl<'a> Ref<'a> { /// Create a Ref object which refers to `pb` pub fn create(store: &'a Store, pb: PathBuf, flags: RefFlags) -> Result<Ref<'a>> { - use std::fs::File; - use std::io::Read; - use crypto::sha1::Sha1; - use crypto::digest::Digest; - if !pb.exists() { return Err(REK::RefTargetDoesNotExist.into_error()); } @@ -64,11 +63,7 @@ impl<'a> Ref<'a> { // we hash the contents of the file and return (file, hash) .and_then(|mut file| { let opt_contenthash = if flags.get_content_hashing() { - let mut hasher = Sha1::new(); - let mut s = String::new(); - file.read_to_string(&mut s); - hasher.input_str(&s[..]); - Some(hasher.result_str()) + Some(hash_file_contents(&mut file)) } else { None }; @@ -285,3 +280,12 @@ impl<'a> DerefMut for Ref<'a> { } } + +fn hash_file_contents(f: &mut File) -> String { + let mut hasher = Sha1::new(); + let mut s = String::new(); + f.read_to_string(&mut s); + hasher.input_str(&s[..]); + hasher.result_str() +} + |