Skip to content

Commit

Permalink
Use a larger buffer size when checking previously downloaded file has…
Browse files Browse the repository at this point in the history
…hes, and make consistent with partial download code.
  • Loading branch information
jelford committed Mar 29, 2017
1 parent 1286686 commit d9f34d8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/download/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub fn download_to_path_with_backend(
if let Some(cb) = callback {
try!(cb(Event::ResumingPartialDownload));

let mut buf = vec![0; 1024*1024*10];
let mut buf = vec![0; 32768];
let mut downloaded_so_far = 0;
loop {
let n = try!(partial.read(&mut buf));
Expand All @@ -102,7 +102,7 @@ pub fn download_to_path_with_backend(
.create(true)
.open(&path)
.chain_err(|| "error opening file for download"));

try!(possible_partial.seek(SeekFrom::End(0)));

(possible_partial, downloaded_so_far)
Expand Down
33 changes: 19 additions & 14 deletions src/rustup-dist/src/dist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,23 @@ impl ops::Deref for File {
}
}

fn file_hash(path: &Path) -> Result<String> {
let mut hasher = Sha256::new();
use std::io::Read;
let mut downloaded = try!(fs::File::open(&path).chain_err(|| "opening already downloaded file"));
let mut buf = vec![0; 32768];
loop {
if let Ok(n) = downloaded.read(&mut buf) {
if n == 0 { break; }
hasher.input(&buf[..n]);
} else {
break;
}
}

Ok(hasher.result_str())
}

impl<'a> DownloadCfg<'a> {

pub fn download(&self, url: &Url, hash: &str, notify_handler: &'a Fn(Notification)) -> Result<File> {
Expand All @@ -510,19 +527,7 @@ impl<'a> DownloadCfg<'a> {
let target_file = self.download_dir.join(Path::new(hash));

if target_file.exists() {
let mut hasher = Sha256::new();
use std::io::Read;
let mut downloaded = try!(fs::File::open(&target_file).chain_err(|| "opening already downloaded file"));
let mut buf = [0; 1024];
loop {
if let Ok(n) = downloaded.read(&mut buf) {
if n == 0 { break; }
hasher.input(&buf[..n]);
} else {
break;
}
}
let cached_result = hasher.result_str();
let cached_result = try!(file_hash(&target_file));
if hash == cached_result {
notify_handler(Notification::FileAlreadyDownloaded);
notify_handler(Notification::ChecksumValid(&url.to_string()));
Expand All @@ -543,7 +548,7 @@ impl<'a> DownloadCfg<'a> {
+ ".partial");

let mut hasher = Sha256::new();

try!(utils::download_file_with_resume(&url,
&partial_file_path,
Some(&mut hasher),
Expand Down

0 comments on commit d9f34d8

Please sign in to comment.