Skip to content

Commit

Permalink
Fixes issue rust-lang#7543
Browse files Browse the repository at this point in the history
Added a helper function for util::hex::hash_64 that uses streams
the content instead of reading through the entire content in one
go.
  • Loading branch information
hbina committed Feb 27, 2020
1 parent e618d47 commit bdec8c2
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) -> u64 {
let mut hasher = SipHasher::new_with_keys(0, 0);
let mut buf = [0; 64 * 1024];
loop {
let n = file.read(&mut buf).unwrap();
if n == 0 {
break;
}
hasher.write(&buf);
}
hasher.finish()
}

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

0 comments on commit bdec8c2

Please sign in to comment.