From a9ca4b95295ce84ec1ba89a657647e2de03bb132 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Sat, 28 May 2022 22:05:43 -0500 Subject: Add checksum verification for rustfmt downloads --- src/bootstrap/builder.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'src/bootstrap/builder.rs') diff --git a/src/bootstrap/builder.rs b/src/bootstrap/builder.rs index 47551c5082e..38d4f15d3c8 100644 --- a/src/bootstrap/builder.rs +++ b/src/bootstrap/builder.rs @@ -879,7 +879,6 @@ impl<'a> Builder<'a> { ) { // Use a temporary file in case we crash while downloading, to avoid a corrupt download in cache/. let tempfile = self.tempdir().join(dest_path.file_name().unwrap()); - // FIXME: support `do_verify` (only really needed for nightly rustfmt) self.download_with_retries(&tempfile, &format!("{}/{}", base, url), help_on_error); t!(std::fs::rename(&tempfile, dest_path)); } @@ -971,6 +970,28 @@ impl<'a> Builder<'a> { t!(fs::remove_dir_all(dst.join(directory_prefix))); } + /// Returns whether the SHA256 checksum of `path` matches `expected`. + pub(crate) fn verify(&self, path: &Path, expected: &str) -> bool { + use sha2::Digest; + + self.verbose(&format!("verifying {}", path.display())); + let mut hasher = sha2::Sha256::new(); + // FIXME: this is ok for rustfmt (4.1 MB large at time of writing), but it seems memory-intensive for rustc and larger components. + // Consider using streaming IO instead? + let contents = if self.config.dry_run { vec![] } else { t!(fs::read(path)) }; + hasher.update(&contents); + let found = hex::encode(hasher.finalize().as_slice()); + let verified = found == expected; + if !verified && !self.config.dry_run { + println!( + "invalid checksum: \n\ + found: {found}\n\ + expected: {expected}", + ); + } + return verified; + } + /// Obtain a compiler at a given stage and for a given host. Explicitly does /// not take `Compiler` since all `Compiler` instances are meant to be /// obtained through this function, since it ensures that they are valid -- cgit 1.4.1-3-g733a5