Skip to content

Commit 443e276

Browse files
committed
Auto merge of #7946 - hbina:add_stream_hashu64, r=alexcrichton
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
2 parents e618d47 + 0ac7aee commit 443e276

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/cargo/ops/cargo_package.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -746,8 +746,8 @@ fn hash_all(path: &Path) -> CargoResult<HashMap<PathBuf, u64>> {
746746
let entry = entry?;
747747
let file_type = entry.file_type();
748748
if file_type.is_file() {
749-
let contents = fs::read(entry.path())?;
750-
let hash = util::hex::hash_u64(&contents);
749+
let file = File::open(entry.path())?;
750+
let hash = util::hex::hash_u64_file(&file)?;
751751
result.insert(entry.path().to_path_buf(), hash);
752752
} else if file_type.is_symlink() {
753753
let hash = util::hex::hash_u64(&fs::read_link(entry.path())?);

src/cargo/util/hex.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#![allow(deprecated)]
22

3+
use std::fs::File;
34
use std::hash::{Hash, Hasher, SipHasher};
5+
use std::io::Read;
46

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

26+
pub fn hash_u64_file(mut file: &File) -> std::io::Result<u64> {
27+
let mut hasher = SipHasher::new_with_keys(0, 0);
28+
let mut buf = [0; 64 * 1024];
29+
loop {
30+
let n = file.read(&mut buf)?;
31+
if n == 0 {
32+
break;
33+
}
34+
hasher.write(&buf[..n]);
35+
}
36+
Ok(hasher.finish())
37+
}
38+
2439
pub fn short_hash<H: Hash>(hashable: &H) -> String {
2540
to_hex(hash_u64(hashable))
2641
}

0 commit comments

Comments
 (0)