Skip to content

Commit

Permalink
feat: zip before signing if is directory (#29)
Browse files Browse the repository at this point in the history
* feat: zip before signing if is directory

* lint

* fix extension name

* swap logic
  • Loading branch information
amr-crabnebula authored Sep 22, 2023
1 parent ffad113 commit 182ac2e
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 34 deletions.
4 changes: 2 additions & 2 deletions crates/packager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ relative-path = "1.9"
walkdir = "2"
os_pipe = "1"
minisign = "0.7"
tar = "0.4"
libflate = "2.0"

[target."cfg(target_os = \"windows\")".dependencies]
winreg = "0.51"
Expand All @@ -64,8 +66,6 @@ image = "0.24"
md5 = "0.7"
heck = "0.4"
ar = "0.9"
tar = "0.4"
libflate = "2.0"

[target."cfg(target_os = \"macos\")".dependencies]
icns = { package = "tauri-icns", version = "0.1" }
Expand Down
4 changes: 2 additions & 2 deletions crates/packager/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,11 @@ fn run(cli: Cli) -> Result<()> {
}

// create the packages
let packages = package(&config)?;
let mut packages = package(&config)?;

// sign the packages
if let Some(signing_config) = &signing_config {
let s = sign_outputs(signing_config, &packages)?;
let s = sign_outputs(signing_config, &mut packages)?;
signatures.extend(s);
}

Expand Down
27 changes: 20 additions & 7 deletions crates/packager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,27 @@ pub use package::{package, PackageOuput};
/// Sign the specified packages and return the signatures paths.
pub fn sign_outputs(
config: &SigningConfig,
packages: &[PackageOuput],
packages: &mut Vec<PackageOuput>,
) -> crate::Result<Vec<PathBuf>> {
let mut signatures = Vec::new();
for package in packages {
if !matches!(package.format, PackageFormat::App) {
for path in &package.paths {
signatures.push(sign::sign_file(config, path)?);
}
for path in &package.paths.clone() {
let path = if path.is_dir() {
let extension = path.extension().unwrap_or_default().to_string_lossy();
let zip = path.with_extension(format!(
"{}{}tar.gz",
extension,
if extension.is_empty() { "" } else { "." }
));
let dest_file = util::create_file(&zip)?;
let gzip_encoder = libflate::gzip::Encoder::new(dest_file)?;
util::create_tar_from_dir(path, gzip_encoder)?;
package.paths.push(zip);
package.paths.last().unwrap()
} else {
path
};
signatures.push(sign::sign_file(config, path)?);
}
}

Expand All @@ -60,7 +73,7 @@ pub fn package_and_sign(
config: &Config,
signing_config: &SigningConfig,
) -> crate::Result<(Vec<PackageOuput>, Vec<PathBuf>)> {
let packages = package(config)?;
let signatures = sign_outputs(signing_config, &packages)?;
let mut packages = package(config)?;
let signatures = sign_outputs(signing_config, &mut packages)?;
Ok((packages, signatures))
}
24 changes: 1 addition & 23 deletions crates/packager/src/package/deb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,28 +300,6 @@ fn generate_md5sums(control_dir: &Path, data_dir: &Path) -> crate::Result<()> {
Ok(())
}

/// Writes a tar file to the given writer containing the given directory.
fn create_tar_from_dir<P: AsRef<Path>, W: Write>(src_dir: P, dest_file: W) -> crate::Result<W> {
let src_dir = src_dir.as_ref();
let mut tar_builder = tar::Builder::new(dest_file);
for entry in WalkDir::new(src_dir) {
let entry = entry?;
let src_path = entry.path();
if src_path == src_dir {
continue;
}
let dest_path = src_path.strip_prefix(src_dir)?;
if entry.file_type().is_dir() {
tar_builder.append_dir(dest_path, src_path)?;
} else {
let mut src_file = std::fs::File::open(src_path)?;
tar_builder.append_file(dest_path, &mut src_file)?;
}
}
let dest_file = tar_builder.into_inner()?;
Ok(dest_file)
}

/// Creates a `.tar.gz` file from the given directory (placing the new file
/// within the given directory's parent directory), then deletes the original
/// directory and returns the path to the new file.
Expand All @@ -330,7 +308,7 @@ fn tar_and_gzip_dir<P: AsRef<Path>>(src_dir: P) -> crate::Result<PathBuf> {
let dest_path = src_dir.with_extension("tar.gz");
let dest_file = util::create_file(&dest_path)?;
let gzip_encoder = libflate::gzip::Encoder::new(dest_file)?;
let gzip_encoder = create_tar_from_dir(src_dir, gzip_encoder)?;
let gzip_encoder = util::create_tar_from_dir(src_dir, gzip_encoder)?;
let mut dest_file = gzip_encoder.finish().into_result()?;
dest_file.flush()?;
Ok(dest_path)
Expand Down
22 changes: 22 additions & 0 deletions crates/packager/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,3 +370,25 @@ fn make_icns_image(img: image::DynamicImage) -> std::io::Result<icns::Image> {
};
icns::Image::from_data(pixel_format, img.width(), img.height(), img.into_bytes())
}

/// Writes a tar file to the given writer containing the given directory.
pub fn create_tar_from_dir<P: AsRef<Path>, W: Write>(src_dir: P, dest_file: W) -> crate::Result<W> {
let src_dir = src_dir.as_ref();
let mut tar_builder = tar::Builder::new(dest_file);
for entry in walkdir::WalkDir::new(src_dir) {
let entry = entry?;
let src_path = entry.path();
if src_path == src_dir {
continue;
}
let dest_path = src_path.strip_prefix(src_dir)?;
if entry.file_type().is_dir() {
tar_builder.append_dir(dest_path, src_path)?;
} else {
let mut src_file = std::fs::File::open(src_path)?;
tar_builder.append_file(dest_path, &mut src_file)?;
}
}
let dest_file = tar_builder.into_inner()?;
Ok(dest_file)
}

0 comments on commit 182ac2e

Please sign in to comment.