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

Avoid zero-mtime files #250

Merged
merged 1 commit into from May 27, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/entry.rs
Expand Up @@ -610,6 +610,13 @@ impl<'a> EntryFields<'a> {

if self.preserve_mtime {
if let Ok(mtime) = self.header.mtime() {
// For some more information on this see the comments in
// `Header::fill_platform_from`, but the general idea is that
// we're trying to avoid 0-mtime files coming out of archives
// since some tools don't ingest them well. Perhaps one day
// when Cargo stops working with 0-mtime archives we can remove
// this.
let mtime = if mtime == 0 { 1 } else { mtime };
let mtime = FileTime::from_unix_time(mtime as i64, 0);
filetime::set_file_handle_times(&f, Some(mtime), Some(mtime)).map_err(|e| {
TarError::new(&format!("failed to set mtime for `{}`", dst.display()), e)
Expand Down
11 changes: 10 additions & 1 deletion src/header.rs
Expand Up @@ -734,7 +734,16 @@ impl Header {
self.set_mode(meta.mode() as u32);
}
HeaderMode::Deterministic => {
self.set_mtime(0);
// We could in theory set the mtime to zero here, but not all
// tools seem to behave well when ingesting files with a 0
// timestamp. For example rust-lang/cargo#9512 shows that lldb
// doesn't ingest files with a zero timestamp correctly.
//
// We just need things to be deterministic here so just pick
// something that isn't zero. This time, chosen after careful
// deliberation, corresponds to Nov 29, 1973.
self.set_mtime(123456789);

self.set_uid(0);
self.set_gid(0);

Expand Down
23 changes: 22 additions & 1 deletion tests/all.rs
Expand Up @@ -11,7 +11,7 @@ use std::iter::repeat;
use std::path::{Path, PathBuf};

use filetime::FileTime;
use tar::{Archive, Builder, EntryType, Header};
use tar::{Archive, Builder, EntryType, Header, HeaderMode};
use tempfile::{Builder as TempBuilder, TempDir};

macro_rules! t {
Expand Down Expand Up @@ -645,6 +645,27 @@ fn file_times() {
assert_eq!(atime.nanoseconds(), 0);
}

#[test]
fn zero_file_times() {
let td = t!(TempBuilder::new().prefix("tar-rs").tempdir());

let mut ar = Builder::new(Vec::new());
ar.mode(HeaderMode::Deterministic);
let path = td.path().join("tmpfile");
t!(File::create(&path));
t!(ar.append_path_with_name(&path, "a"));

let data = t!(ar.into_inner());
let mut ar = Archive::new(&data[..]);
assert!(ar.unpack(td.path()).is_ok());

let meta = fs::metadata(td.path().join("a")).unwrap();
let mtime = FileTime::from_last_modification_time(&meta);
let atime = FileTime::from_last_access_time(&meta);
assert!(mtime.unix_seconds() != 0);
assert!(atime.unix_seconds() != 0);
}

#[test]
fn backslash_treated_well() {
// Insert a file into an archive with a backslash
Expand Down