Skip to content

Commit

Permalink
Auto merge of #7946 - hbina:add_stream_hashu64, r=alexcrichton
Browse files Browse the repository at this point in the history
Fixes issue #7543

Added a helper function for `util::hex::hash_64` that uses streams
the content instead of reading through the entire content in one
go.

Edit: Should I add test cases for this?

Edit2: Per #7543
  • Loading branch information
bors committed Feb 28, 2020
2 parents e618d47 + 0ac7aee commit 443e276
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/cargo/ops/cargo_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -746,8 +746,8 @@ fn hash_all(path: &Path) -> CargoResult<HashMap<PathBuf, u64>> {
let entry = entry?;
let file_type = entry.file_type();
if file_type.is_file() {
let contents = fs::read(entry.path())?;
let hash = util::hex::hash_u64(&contents);
let file = File::open(entry.path())?;
let hash = util::hex::hash_u64_file(&file)?;
result.insert(entry.path().to_path_buf(), hash);
} else if file_type.is_symlink() {
let hash = util::hex::hash_u64(&fs::read_link(entry.path())?);
Expand Down
15 changes: 15 additions & 0 deletions src/cargo/util/hex.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#![allow(deprecated)]

use std::fs::File;
use std::hash::{Hash, Hasher, SipHasher};
use std::io::Read;

pub fn to_hex(num: u64) -> String {
hex::encode(&[
Expand All @@ -21,6 +23,19 @@ pub fn hash_u64<H: Hash>(hashable: H) -> u64 {
hasher.finish()
}

pub fn hash_u64_file(mut file: &File) -> std::io::Result<u64> {
let mut hasher = SipHasher::new_with_keys(0, 0);
let mut buf = [0; 64 * 1024];
loop {
let n = file.read(&mut buf)?;
if n == 0 {
break;
}
hasher.write(&buf[..n]);
}
Ok(hasher.finish())
}

pub fn short_hash<H: Hash>(hashable: &H) -> String {
to_hex(hash_u64(hashable))
}

0 comments on commit 443e276

Please sign in to comment.