Skip to content

Commit 4edb90e

Browse files
committed
boulder/analysis: Add compress man handler
compresses man/info pages
1 parent 1b1d0bf commit 4edb90e

File tree

5 files changed

+81
-2
lines changed

5 files changed

+81
-2
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

boulder/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,4 @@ strum.workspace = true
4343
thiserror.workspace = true
4444
tokio.workspace = true
4545
url.workspace = true
46+
zstd.workspace = true

boulder/src/package/analysis.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ impl<'a> Chain<'a> {
3838
Box::new(handler::elf),
3939
Box::new(handler::pkg_config),
4040
Box::new(handler::cmake),
41+
Box::new(handler::compressman),
4142
// Catch-all if not excluded
4243
Box::new(handler::include_any),
4344
],

boulder/src/package/analysis/handler.rs

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1-
use std::{path::PathBuf, process::Command};
2-
1+
use std::fs::File;
2+
use std::io::{BufReader, BufWriter, Write};
3+
use std::{
4+
fs,
5+
os::unix::fs::symlink,
6+
path::{Component, PathBuf},
7+
process::Command,
8+
};
9+
10+
use itertools::Itertools;
311
use moss::{dependency, Dependency, Provider};
412

513
use crate::package::collect::PathInfo;
@@ -134,3 +142,69 @@ pub fn cmake(bucket: &mut BucketMut, info: &mut PathInfo) -> Result<Response, Bo
134142

135143
Ok(Decision::NextHandler.into())
136144
}
145+
146+
pub fn compressman(bucket: &mut BucketMut, info: &mut PathInfo) -> Result<Response, BoxError> {
147+
if !bucket.recipe.parsed.options.compressman {
148+
return Ok(Decision::NextHandler.into());
149+
}
150+
151+
let is_man_file = info.path.components().contains(&Component::Normal("man".as_ref()))
152+
&& info.file_name().ends_with(|c| ('1'..'9').contains(&c));
153+
let is_info_file =
154+
info.path.components().contains(&Component::Normal("info".as_ref())) && info.file_name().ends_with(".info");
155+
156+
if !(is_man_file || is_info_file) {
157+
return Ok(Decision::NextHandler.into());
158+
}
159+
160+
let mut generated_path = PathBuf::new();
161+
162+
/* If we have a man/info symlink update the link to the compressed file */
163+
if info.path.is_symlink() {
164+
let new_original = format!("{}.zst", fs::canonicalize(&info.path)?.display());
165+
let new_link = format!("{}.zst", &info.path.display());
166+
167+
/*
168+
* Depending on the order the files get analysed the new compressed file may not yet exist,
169+
* compress it _now_ so the correct metadata src info is returned to the binary writer.
170+
*/
171+
if !std::path::Path::new(&new_original).exists() {
172+
let compressed_file = compress_file_zstd(PathBuf::from(fs::canonicalize(&info.path)?))?;
173+
let _ = bucket.paths.install().guest.join(compressed_file);
174+
}
175+
176+
symlink(format!("{}.zst", fs::read_link(&info.path)?.display()), &new_link)?;
177+
generated_path.push(bucket.paths.install().guest.join(new_link));
178+
return Ok(Decision::ReplaceFile {
179+
newpath: generated_path,
180+
}
181+
.into());
182+
}
183+
184+
let mut compressed_file = PathBuf::from(format!("{}.zst", info.path.display()));
185+
186+
/* We may have already compressed the file if we encountered a symlink to this file first */
187+
if !&compressed_file.exists() {
188+
compressed_file = compress_file_zstd(info.path.clone())?;
189+
}
190+
191+
generated_path.push(bucket.paths.install().guest.join(compressed_file));
192+
193+
pub fn compress_file_zstd(path: PathBuf) -> Result<PathBuf, BoxError> {
194+
let output_path = PathBuf::from(format!("{}.zst", path.display()));
195+
let input = File::create(&output_path)?;
196+
let mut reader = BufReader::new(File::open(&path)?);
197+
let mut writer = BufWriter::new(input);
198+
199+
zstd::stream::copy_encode(&mut reader, &mut writer, 16)?;
200+
201+
writer.flush()?;
202+
203+
Ok(output_path)
204+
}
205+
206+
Ok(Decision::ReplaceFile {
207+
newpath: generated_path,
208+
}
209+
.into())
210+
}

crates/stone_recipe/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ pub struct Options {
9393
pub strip: bool,
9494
#[serde(default, deserialize_with = "stringy_bool")]
9595
pub networking: bool,
96+
#[serde(default, deserialize_with = "stringy_bool")]
97+
pub compressman: bool,
9698
}
9799

98100
#[derive(Debug, Clone, Deserialize)]

0 commit comments

Comments
 (0)