Skip to content

Commit

Permalink
kargs: Introduce usage of fs_utf8, use new sorting API
Browse files Browse the repository at this point in the history
This avoids annoyances with constantly checking for utf-8.
Also the new `filenames_filtered_sorted` API we added
to cap-std-ext is more concise and efficient.

Signed-off-by: Colin Walters <[email protected]>
  • Loading branch information
cgwalters committed Jul 16, 2024
1 parent 63f593d commit 80b6673
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
2 changes: 2 additions & 0 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
23 changes: 9 additions & 14 deletions lib/src/kargs.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use anyhow::{Context, Result};
use camino::Utf8Path;
use cap_std_ext::cap_std::fs::Dir;
use cap_std_ext::cap_std::fs_utf8::Dir as DirUtf8;
use cap_std_ext::dirext::CapStdExtDirExt;
use cap_std_ext::dirext::CapStdExtDirExtUtf8;
use ostree::gio;
use ostree_ext::ostree;
use ostree_ext::ostree::Deployment;
Expand Down Expand Up @@ -35,23 +37,16 @@ impl Config {
/// a combined list.
pub(crate) fn get_kargs_in_root(d: &Dir, sys_arch: &str) -> Result<Vec<String>> {
// If the directory doesn't exist, that's OK.
let Some(d) = d.open_dir_optional("usr/lib/bootc/kargs.d")? else {
let Some(d) = d
.open_dir_optional("usr/lib/bootc/kargs.d")?
.map(DirUtf8::from_cap_std)
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 entries = d.filenames_filtered_sorted(|_, name| Config::filename_matches(name))?;
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 80b6673

Please sign in to comment.