Skip to content

Commit

Permalink
Refactor the common code for generating an archive member
Browse files Browse the repository at this point in the history
Signed-off-by: Bruce Guenter <bruce@untroubled.org>
  • Loading branch information
bruceg committed Oct 18, 2019
1 parent 27d54a8 commit b33c9e2
Showing 1 changed file with 28 additions and 43 deletions.
71 changes: 28 additions & 43 deletions src/cargo/ops/cargo_package.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::{BTreeSet, HashMap};
use std::fmt::Display;
use std::fs::{self, File};
use std::io::prelude::*;
use std::io::SeekFrom;
Expand All @@ -8,6 +9,7 @@ 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 @@ -439,22 +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_mtime(
SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_secs(),
);
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 @@ -482,20 +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_mtime(
SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_secs(),
);
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 @@ -510,20 +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_mtime(
SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_secs(),
);
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 @@ -805,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 b33c9e2

Please sign in to comment.