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

Set timestamp on generated files in archive to now #7523

Merged
merged 2 commits into from
Oct 21, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(())
}