Make tarball generation more deterministic

This commit is contained in:
Jakub Beránek 2024-03-30 21:27:58 +01:00
parent 8caef4e6c3
commit 18d9d44bd6
No known key found for this signature in database
GPG Key ID: 909CD0D26483516B

View File

@ -2,7 +2,7 @@
use std::fs::{read_link, symlink_metadata};
use std::io::{BufWriter, Write};
use std::path::Path;
use tar::{Builder, Header};
use tar::{Builder, Header, HeaderMode};
use walkdir::WalkDir;
use crate::{
@ -61,6 +61,8 @@ pub fn run(self) -> Result<()> {
// first, so files may be directly created. (See rust-lang/rustup.rs#1092.)
let buf = BufWriter::with_capacity(1024 * 1024, encoder);
let mut builder = Builder::new(buf);
// Make uid, gid and mtime deterministic to improve reproducibility
builder.mode(HeaderMode::Deterministic);
let pool = rayon::ThreadPoolBuilder::new().num_threads(2).build().unwrap();
pool.install(move || {
@ -91,7 +93,8 @@ pub fn run(self) -> Result<()> {
fn append_path<W: Write>(builder: &mut Builder<W>, src: &Path, path: &String) -> Result<()> {
let stat = symlink_metadata(src)?;
let mut header = Header::new_gnu();
header.set_metadata(&stat);
header.set_metadata_in_mode(&stat, HeaderMode::Deterministic);
if stat.file_type().is_symlink() {
let link = read_link(src)?;
builder.append_link(&mut header, path, &link)?;