From ee9147e824a1ce01cf23daf3736d11a92b233777 Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Thu, 11 Jul 2024 15:01:23 -0400 Subject: [PATCH] kargs: Introduce usage of fs_utf8 This avoids annoyances with constantly checking for utf-8. Signed-off-by: Colin Walters --- Cargo.lock | 18 ++++++++++-------- lib/Cargo.toml | 2 +- lib/src/kargs.rs | 30 +++++++++++++++++------------- 3 files changed, 28 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 82ddac146..196dd5722 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -237,9 +237,9 @@ dependencies = [ [[package]] name = "cap-primitives" -version = "3.0.0" +version = "3.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90a0b44fc796b1a84535a63753d50ba3972c4db55c7255c186f79140e63d56d0" +checksum = "6d00bd8d26c4270d950eaaa837387964a2089a1c3c349a690a1fa03221d29531" dependencies = [ "ambient-authority", "fs-set-times", @@ -254,10 +254,11 @@ dependencies = [ [[package]] name = "cap-std" -version = "3.0.0" +version = "3.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "266626ce180cf9709f317d0bf9754e3a5006359d87f4bf792f06c9c5f1b63c0f" +checksum = "19eb8e3d71996828751c1ed3908a439639752ac6bdc874e41469ef7fc15fbd7f" dependencies = [ + "camino", "cap-primitives", "io-extras", "io-lifetimes", @@ -266,9 +267,9 @@ dependencies = [ [[package]] name = "cap-std-ext" -version = "4.0.0" +version = "4.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c1285af420ee8cbf7ec30c02c08f9f25125fc0f7efd6d34eacad5b18e62d4e58" +checksum = "e7f98c66b91a87715bb68b426cc36f9a5a2e0970a4c0adc631aaf06422d4185b" dependencies = [ "cap-primitives", "cap-tempfile", @@ -277,10 +278,11 @@ dependencies = [ [[package]] name = "cap-tempfile" -version = "3.0.0" +version = "3.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8748c8ae8186b7362da62c7cb8edf7aabae095c86175e988d76387a842574e16" +checksum = "53880047c3f37cd64947775f0526795498d614182603a718c792616b762ce777" dependencies = [ + "camino", "cap-std", "rand", "rustix", diff --git a/lib/Cargo.toml b/lib/Cargo.toml index 6afd0b2d0..b79adbdf7 100644 --- a/lib/Cargo.toml +++ b/lib/Cargo.toml @@ -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" diff --git a/lib/src/kargs.rs b/lib/src/kargs.rs index 5dac9158e..bc8222e90 100644 --- a/lib/src/kargs.rs +++ b/lib/src/kargs.rs @@ -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; @@ -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> { + // 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::>>()?; - // 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) }