Skip to content

Commit

Permalink
Auto merge of #7523 - bruceg:fix-zero-timestamps, r=alexcrichton
Browse files Browse the repository at this point in the history
Set timestamp on generated files in archive to now

When generating files (Cargo.lock, Cargo.toml, and
.cargo_vcs_info.json), cargo neglected to set any timestamp on the file
in the archive. This results in them being created on disk with a
timestamp of 0 (Jan 1 1970 GMT) which is confusing another tool I use.

This patch alters the behavior to set the mtime to now.

Signed-off-by: Bruce Guenter <bruce@untroubled.org>
  • Loading branch information
bors committed Oct 21, 2019
2 parents 02e0c39 + b33c9e2 commit 04efd9c
Showing 1 changed file with 29 additions and 25 deletions.
54 changes: 29 additions & 25 deletions src/cargo/ops/cargo_package.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use std::collections::{BTreeSet, HashMap};
use std::fmt::Display;
use std::fs::{self, File};
use std::io::prelude::*;
use std::io::SeekFrom;
use std::path::{self, Path, PathBuf};
use std::rc::Rc;
use std::sync::Arc;
use std::time::SystemTime;

use flate2::read::GzDecoder;
use flate2::write::GzEncoder;
use flate2::{Compression, GzBuilder};
use log::debug;
use serde_json::{self, json};
Expand Down Expand Up @@ -438,16 +441,8 @@ fn tar(
internal(format!("could not archive source file `{}`", relative_str))
})?;

let mut header = Header::new_ustar();
let toml = pkg.to_registry_toml(ws.config())?;
header.set_path(&path)?;
header.set_entry_type(EntryType::file());
header.set_mode(0o644);
header.set_size(toml.len() as u64);
header.set_cksum();
ar.append(&header, toml.as_bytes()).chain_err(|| {
internal(format!("could not archive source file `{}`", relative_str))
})?;
add_generated_file(&mut ar, &path, &toml, relative_str)?;
} else {
header.set_cksum();
ar.append(&header, &mut file).chain_err(|| {
Expand Down Expand Up @@ -475,14 +470,7 @@ fn tar(
.set_path(&path)
.chain_err(|| format!("failed to add to archive: `{}`", fnd))?;
let json = format!("{}\n", serde_json::to_string_pretty(json)?);
let mut header = Header::new_ustar();
header.set_path(&path)?;
header.set_entry_type(EntryType::file());
header.set_mode(0o644);
header.set_size(json.len() as u64);
header.set_cksum();
ar.append(&header, json.as_bytes())
.chain_err(|| internal(format!("could not archive source file `{}`", fnd)))?;
add_generated_file(&mut ar, &path, &json, fnd)?;
}

if pkg.include_lockfile() {
Expand All @@ -497,14 +485,7 @@ fn tar(
pkg.version(),
path::MAIN_SEPARATOR
);
let mut header = Header::new_ustar();
header.set_path(&path)?;
header.set_entry_type(EntryType::file());
header.set_mode(0o644);
header.set_size(new_lock.len() as u64);
header.set_cksum();
ar.append(&header, new_lock.as_bytes())
.chain_err(|| internal("could not archive source file `Cargo.lock`"))?;
add_generated_file(&mut ar, &path, &new_lock, "Cargo.lock")?;
}

let encoder = ar.into_inner()?;
Expand Down Expand Up @@ -786,3 +767,26 @@ fn check_filename(file: &Path) -> CargoResult<()> {
}
Ok(())
}

fn add_generated_file<D: Display>(
ar: &mut Builder<GzEncoder<&File>>,
path: &str,
data: &str,
display: D,
) -> CargoResult<()> {
let mut header = Header::new_ustar();
header.set_path(path)?;
header.set_entry_type(EntryType::file());
header.set_mode(0o644);
header.set_mtime(
SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_secs(),
);
header.set_size(data.len() as u64);
header.set_cksum();
ar.append(&header, data.as_bytes())
.chain_err(|| internal(format!("could not archive source file `{}`", display)))?;
Ok(())
}

0 comments on commit 04efd9c

Please sign in to comment.