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

TODO:
- cleanup if statement sphagetti
  • Loading branch information
joebonrichie committed Jul 17, 2024
1 parent aa18514 commit 237ee8f
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 1 deletion.
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
81 changes: 80 additions & 1 deletion boulder/src/package/analysis/handler.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{path::PathBuf, process::Command};
use std::{fs, os::unix::fs::symlink, path::PathBuf, process::Command};

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

Expand Down Expand Up @@ -134,3 +134,82 @@ 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> {
let file_path = info.path.clone().into_os_string().into_string().unwrap_or_default();
let mut generated_path = PathBuf::new();

if !bucket.recipe.parsed.options.compressman {
return Ok(Decision::NextHandler.into());
}

if (file_path.contains("man") && file_path.ends_with(|c| ('1'..'9').contains(&c)))
|| (file_path.contains("info") && file_path.ends_with(".info"))
{
/* If we have a man/info symlink update the link to the compressed file */
if info.path.is_symlink() {
let parent_path = info.path.parent().unwrap().display();
let new_original = format!("{}/{}.zst", &parent_path, fs::read_link(&file_path)?.display());
let new_link = format!("{}.zst", &file_path);

/*
* 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(PathBuf::from(format!(
"{}/{}",
&parent_path,
fs::read_link(&file_path)?.display()
)))
.unwrap_or_default();
let _ = bucket.paths.install().guest.join(compressed_file);
}

symlink(format!("{}.zst", fs::read_link(&file_path)?.display()), &new_link).unwrap_or_else(|why| {
println!("! {:?}", why.kind());
});
let path = bucket.paths.install().guest.join(new_link);
generated_path.push(path);
return Ok(Decision::ReplaceFile {
newpath: generated_path,
}
.into());
} else {
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 !std::path::Path::new(&compressed_file).exists() {
compressed_file = compress_file_zstd(info.path.clone()).unwrap_or_default();
}
let path = bucket.paths.install().guest.join(compressed_file);
generated_path.push(path);
}
} else {
return Ok(Decision::NextHandler.into());
}

pub fn compress_file_zstd(path: PathBuf) -> Result<PathBuf, BoxError> {
extern crate zstd;
use std::fs::File;
use std::io::{BufReader, BufWriter, Write};
use zstd::stream::copy_encode;

let output_path = PathBuf::from(format!("{}.zst", path.display()));
let input = File::create(&output_path).unwrap();

let mut reader = BufReader::new(File::open(path).unwrap());
let mut writer = BufWriter::new(input);

copy_encode(&mut reader, &mut writer, 16)?;

writer.flush()?;

return Ok(output_path);

Check warning on line 208 in boulder/src/package/analysis/handler.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] boulder/src/package/analysis/handler.rs#L208

warning: unneeded `return` statement --> boulder/src/package/analysis/handler.rs:208:9 | 208 | return Ok(output_path); | ^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_return = note: `#[warn(clippy::needless_return)]` on by default help: remove `return` | 208 - return Ok(output_path); 208 + Ok(output_path) |
Raw output
boulder/src/package/analysis/handler.rs:208:9:w:warning: unneeded `return` statement
   --> boulder/src/package/analysis/handler.rs:208:9
    |
208 |         return Ok(output_path);
    |         ^^^^^^^^^^^^^^^^^^^^^^
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_return
    = note: `#[warn(clippy::needless_return)]` on by default
help: remove `return`
    |
208 -         return Ok(output_path);
208 +         Ok(output_path)
    |


__END__
}

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 237ee8f

Please sign in to comment.