Skip to content

Commit

Permalink
Avoid zero-mtime files (#250)
Browse files Browse the repository at this point in the history
With rust-lang/cargo#9512 discovered this is, uh, odd fallout of the
"deterministic" header mode in this crate. This is an unintended
consequence of the deterministic header mode which should ideally be
fixable. This commit changes the `tar` crate to use a different constant
than 0 when creating archives with the deterministic header mode
(specifically 123456789: Nov 29, 1973). It also will now refuse to
create files with a 0 mtime, instead resetting the mtime to 1 which
should help the mtime be nonzero so tools like lldb don't accidentally
think it's zero.
  • Loading branch information
alexcrichton committed May 27, 2021
1 parent 7f2a355 commit e81f172
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
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

0 comments on commit e81f172

Please sign in to comment.