Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes cargo package buffering entire contents into memory #7946

Merged
merged 3 commits into from
Feb 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
}