Skip to content

Commit

Permalink
make builds deterministic (#392)
Browse files Browse the repository at this point in the history
This was just for testing. 
I think we might want some more control.

The `append_file` function wasn't actually using the prepared header so
the `mtime` was still set from the file system. Changing that to the
`tar::HeaderMode::Deterministic` works. I think we might still want more
control. The Deterministic header from the tar library does something to
the `chmod` bits and also sets the mtime to the same constant that I
copied out because that is the birthdate of Rust and some programs don't
handle a mtime of `0` not well apparently.
  • Loading branch information
wolfv authored Nov 7, 2023
1 parent 4249d7a commit b66d753
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 18 deletions.
5 changes: 4 additions & 1 deletion crates/rattler_conda_types/src/package/archive_type.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use std::path::Path;

use serde::{Deserialize, Serialize};

/// Describes the type of package archive. This can be derived from the file extension of a package.
#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash)]
#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ArchiveType {
/// A file with the `.tar.bz2` extension.
TarBz2,
Expand Down
19 changes: 6 additions & 13 deletions crates/rattler_package_streaming/src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,22 +229,15 @@ fn prepare_header(
header.as_gnu_mut().unwrap().name[..name.len()].clone_from_slice(&name[..]);

let stat = fs::symlink_metadata(path)?;
header.set_metadata(&stat);

// erase some fields
header.set_uid(0);
header.set_gid(0);
header.set_device_minor(0)?;
header.set_device_major(0)?;
header.set_metadata_in_mode(&stat, tar::HeaderMode::Deterministic);

if let Some(timestamp) = timestamp {
header.set_mtime(timestamp.timestamp().unsigned_abs());
} else {
// 1-1-2023 00:00:00 (Fixed date in the past for reproducible builds)
header.set_mtime(1672531200);
}

// let file_size = stat.len();
// TODO do we need this
// + 1 to be compliant with GNU tar
// header.set_size(file_size + 1);
Ok(header)
}

Expand All @@ -264,10 +257,10 @@ fn append_path_to_archive(
.map_err(|err| trace_file_error(&base_path.join(path), err))?;

if header.entry_type().is_file() {
let mut file = fs::File::open(base_path.join(path))
let file = fs::File::open(base_path.join(path))
.map_err(|err| trace_file_error(&base_path.join(path), err))?;

archive.append_file(path, &mut file)?;
archive.append_data(&mut header, path, &file)?;
} else if header.entry_type().is_symlink() || header.entry_type().is_hard_link() {
let target = fs::read_link(base_path.join(path))
.map_err(|err| trace_file_error(&base_path.join(path), err))?;
Expand Down
4 changes: 0 additions & 4 deletions crates/rattler_virtual_packages/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ fn try_detect_virtual_packages() -> Result<Vec<VirtualPackage>, DetectVirtualPac
#[derive(Clone, Eq, PartialEq, Hash, Debug, Deserialize)]
pub struct Linux {
/// The version of linux
/// #[serde(deserialize_with = "from_str")]
pub version: Version,
}

Expand Down Expand Up @@ -194,7 +193,6 @@ pub struct LibC {
pub family: String,

/// The version of the libc distribution.
/// #[serde(deserialize_with = "from_str")]
pub version: Version,
}

Expand Down Expand Up @@ -230,7 +228,6 @@ impl From<LibC> for VirtualPackage {
#[derive(Clone, Eq, PartialEq, Hash, Debug, Deserialize)]
pub struct Cuda {
/// The maximum supported Cuda version.
/// #[serde(deserialize_with = "from_str")]
pub version: Version,
}

Expand Down Expand Up @@ -316,7 +313,6 @@ impl From<Archspec> for VirtualPackage {
#[derive(Clone, Eq, PartialEq, Hash, Debug, Deserialize)]
pub struct Osx {
/// The OSX version
/// #[serde(deserialize_with = "from_str")]
pub version: Version,
}

Expand Down

0 comments on commit b66d753

Please sign in to comment.