Skip to content

Commit

Permalink
boulder/analysis: Add compress man handler
Browse files Browse the repository at this point in the history
compresses man/info pages
  • Loading branch information
joebonrichie committed Aug 19, 2024
1 parent af03405 commit f29c2d7
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 2 deletions.
24 changes: 24 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ dirs = "5.0.1"
elf = "0.7.4"
indicatif = "0.17.8"
itertools = "0.13.0"
filetime = "0.2.24"
futures = "0.3.30"
glob = "0.3.1"
hex = "0.4.3"
Expand Down
2 changes: 2 additions & 0 deletions boulder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ clap.workspace = true
derive_more.workspace = true
dirs.workspace = true
elf.workspace = true
filetime.workspace = true
glob.workspace = true
futures.workspace = true
hex.workspace = true
Expand All @@ -43,3 +44,4 @@ strum.workspace = true
thiserror.workspace = true
tokio.workspace = true
url.workspace = true
zstd.workspace = true
1 change: 1 addition & 0 deletions boulder/src/package/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ impl<'a> Chain<'a> {
Box::new(handler::elf),
Box::new(handler::pkg_config),
Box::new(handler::cmake),
Box::new(handler::compressman),
// Catch-all if not excluded
Box::new(handler::include_any),
],
Expand Down
98 changes: 96 additions & 2 deletions boulder/src/package/analysis/handler.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
use std::{path::PathBuf, process::Command};

use filetime::FileTime;
use std::fs::File;
use std::io::{BufReader, BufWriter, Write};
use std::{
fs,
os::unix::fs::symlink,
path::{Component, PathBuf},
process::Command,
};

use itertools::Itertools;
use moss::{dependency, Dependency, Provider};

use crate::package::collect::PathInfo;
Expand Down Expand Up @@ -136,3 +145,88 @@ pub fn cmake(bucket: &mut BucketMut, info: &mut PathInfo) -> Result<Response, Bo

Ok(Decision::NextHandler.into())
}

pub fn compressman(bucket: &mut BucketMut, info: &mut PathInfo) -> Result<Response, BoxError> {
if !bucket.recipe.parsed.options.compressman {
return Ok(Decision::NextHandler.into());
}

let is_man_file = info.path.components().contains(&Component::Normal("man".as_ref()))
&& info.file_name().ends_with(|c| ('1'..'9').contains(&c));
let is_info_file =
info.path.components().contains(&Component::Normal("info".as_ref())) && info.file_name().ends_with(".info");

if !(is_man_file || is_info_file) {
return Ok(Decision::NextHandler.into());
}

let mut generated_path = PathBuf::new();

let metadata = fs::metadata(&info.path)?;
let atime = metadata.modified()?;
let mtime = metadata.accessed()?;

/* If we have a man/info symlink update the link to the compressed file */
if info.path.is_symlink() {
let new_original = format!("{}.zst", fs::canonicalize(&info.path)?.display());
let new_link = format!("{}.zst", &info.path.display());

/*
* Depending on the order the files get analysed the new compressed file may not yet exist,
* compress it _now_ so the correct metadata src info is returned to the binary writer.
*/
if !std::path::Path::new(&new_original).exists() {
let compressed_file = compress_file_zstd(fs::canonicalize(&info.path)?)?;
let _ = bucket.paths.install().guest.join(compressed_file);
}

symlink(format!("{}.zst", fs::read_link(&info.path)?.display()), &new_link)?;

/* Restore the original {a,m}times for reproducibility */
filetime::set_symlink_file_times(
&new_link,
FileTime::from_system_time(atime),
FileTime::from_system_time(mtime),
)?;

generated_path.push(bucket.paths.install().guest.join(new_link));
return Ok(Decision::ReplaceFile {
newpath: generated_path,
}
.into());
}

let mut compressed_file = PathBuf::from(format!("{}.zst", info.path.display()));

/* We may have already compressed the file if we encountered a symlink to this file first */
if !&compressed_file.exists() {
compressed_file = compress_file_zstd(info.path.clone())?;
}

/* Restore the original {a,m}times for reproducibility */
filetime::set_file_handle_times(
&File::open(&compressed_file)?,
Some(FileTime::from_system_time(atime)),
Some(FileTime::from_system_time(mtime)),
)?;

generated_path.push(bucket.paths.install().guest.join(compressed_file));

pub fn compress_file_zstd(path: PathBuf) -> Result<PathBuf, BoxError> {
let output_path = PathBuf::from(format!("{}.zst", path.display()));
let input = File::create(&output_path)?;
let mut reader = BufReader::new(File::open(&path)?);
let mut writer = BufWriter::new(input);

zstd::stream::copy_encode(&mut reader, &mut writer, 16)?;

writer.flush()?;

Ok(output_path)
}

Ok(Decision::ReplaceFile {
newpath: generated_path,
}
.into())
}
2 changes: 2 additions & 0 deletions crates/stone_recipe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ pub struct Options {
pub strip: bool,
#[serde(default, deserialize_with = "stringy_bool")]
pub networking: bool,
#[serde(default, deserialize_with = "stringy_bool")]
pub compressman: bool,
}

#[derive(Debug, Clone, Deserialize)]
Expand Down

0 comments on commit f29c2d7

Please sign in to comment.