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 Jul 21, 2024
1 parent 1b1d0bf commit ebcb9ef
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 2 deletions.
1 change: 1 addition & 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 boulder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,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
78 changes: 76 additions & 2 deletions boulder/src/package/analysis/handler.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
use std::{path::PathBuf, process::Command};

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 @@ -134,3 +142,69 @@ 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();

/* 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)?;
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())?;
}

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 ebcb9ef

Please sign in to comment.