Skip to content

Commit

Permalink
kargs: Introduce usage of fs_utf8
Browse files Browse the repository at this point in the history
This avoids annoyances with constantly checking for utf-8.

Signed-off-by: Colin Walters <[email protected]>
  • Loading branch information
cgwalters committed Jul 11, 2024
1 parent eb75a2b commit ee9147e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
18 changes: 10 additions & 8 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ ostree-ext = { version = "0.14.0" }
chrono = { version = "0.4.38", features = ["serde"] }
clap = { version= "4.5.4", features = ["derive","cargo"] }
clap_mangen = { version = "0.2.20", optional = true }
cap-std-ext = "4"
cap-std-ext = { version = "4.0.1", features = ["fs_utf8"] }
hex = "^0.4.3"
fn-error-context = "0.2.1"
gvariant = "0.5.0"
Expand Down
30 changes: 17 additions & 13 deletions lib/src/kargs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use anyhow::{Context, Result};
use camino::Utf8Path;
use cap_std_ext::cap_std;
use cap_std_ext::cap_std::fs::Dir;
use cap_std_ext::dirext::CapStdExtDirExt;
use cap_std_ext::cap_std::fs_utf8::Dir as DirUtf8;
use cap_std_ext::dirext::CapStdExtDirExtUtf8;
use ostree::gio;
use ostree_ext::ostree;
use ostree_ext::ostree::Deployment;
Expand Down Expand Up @@ -34,24 +35,27 @@ impl Config {
/// Load and parse all bootc kargs.d files in the specified root, returning
/// a combined list.
pub(crate) fn get_kargs_in_root(d: &Dir, sys_arch: &str) -> Result<Vec<String>> {
// TODO optimize https://github.com/bytecodealliance/cap-std/pull/361
let d = &DirUtf8::from_cap_std(d.try_clone()?);
// If the directory doesn't exist, that's OK.
let Some(d) = d.open_dir_optional("usr/lib/bootc/kargs.d")? else {
return Ok(Default::default());
};
let mut ret = Vec::new();
// Read all the entries
let mut entries = d.entries()?.collect::<std::io::Result<Vec<_>>>()?;
// cc https://github.com/rust-lang/rust/issues/85573 re the allocation-per-comparison here
entries.sort_by_key(|a| a.file_name());
for ent in entries {
let name = ent.file_name();
let name = name
.to_str()
.ok_or_else(|| anyhow::anyhow!("Invalid non-UTF8 filename: {name:?}"))?;
if !Config::filename_matches(name) {
continue;
}
let buf = d.read_to_string(name)?;
let mut entries = d
.entries()?
.try_fold(Vec::new(), |mut acc, ent| -> Result<_> {
let ent = ent?;
let name = ent.file_name()?;
if Config::filename_matches(&name) {
acc.push(name);
}
Ok(acc)
})?;
entries.sort();
for name in entries {
let buf = d.read_to_string(&name)?;
let kargs = parse_kargs_toml(&buf, sys_arch).with_context(|| format!("Parsing {name}"))?;
ret.extend(kargs)
}
Expand Down

0 comments on commit ee9147e

Please sign in to comment.