From 7b57f2b9d04c14e0ffe0d444e7b918b9fd7a95a4 Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Fri, 30 Apr 2021 09:56:05 -0400 Subject: [PATCH] Add and use the `camino` library for UTF-8 paths ostree hard requires UTF-8 paths (and really we should never have any non-UTF-8 paths in the OS in general). The camino library has types that are both `Path` and `&str` and has a convenient `try_into()` too to avoid us duplicating the error handling. --- Cargo.lock | 1 + Cargo.toml | 1 + rust/src/composepost.rs | 11 ++++------- rust/src/initramfs.rs | 9 ++++----- 4 files changed, 10 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6f74fe18..9c1515a5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1394,6 +1394,7 @@ version = "0.1.0" dependencies = [ "anyhow", "c_utf8", + "camino", "chrono", "clap", "curl", diff --git a/Cargo.toml b/Cargo.toml index 2634df35..4cf752e3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,6 +27,7 @@ rpm = "4" [dependencies] anyhow = "1.0.40" c_utf8 = "0.1.0" +camino = "1.0.4" chrono = { version = "0.4.19", features = ["serde"] } clap = "2.33.3" curl = "0.4.36" diff --git a/rust/src/composepost.rs b/rust/src/composepost.rs index 90ba43fd..04c16bee 100644 --- a/rust/src/composepost.rs +++ b/rust/src/composepost.rs @@ -10,12 +10,14 @@ use crate::cxxrsutil::{CxxResult, FFIGObjectWrapper}; use crate::passwd::PasswdDB; use crate::treefile::Treefile; use anyhow::{anyhow, bail, Context, Result}; +use camino::Utf8Path; use fn_error_context::context; use gio::CancellableExt; use nix::sys::stat::Mode; use openat_ext::OpenatDirExt; use rayon::prelude::*; use std::borrow::Cow; +use std::convert::TryInto; use std::fmt::Write as FmtWrite; use std::fs::File; use std::io::{BufRead, BufReader, BufWriter, Seek, Write}; @@ -648,13 +650,8 @@ fn convert_path_to_tmpfiles_d_recurse( }; let subpath = subpath?; - let full_path = { - let fname = subpath.file_name(); - let path_name = fname - .to_str() - .ok_or_else(|| anyhow!("invalid non-UTF-8 path: {:?}", fname))?; - format!("{}/{}", ¤t_prefix, &path_name) - }; + let fname: &Utf8Path = Path::new(subpath.file_name()).try_into()?; + let full_path = format!("{}/{}", ¤t_prefix, fname); let path_type = subpath.simple_type().unwrap_or(SimpleType::Other); // Workaround for nfs-utils in RHEL7: diff --git a/rust/src/initramfs.rs b/rust/src/initramfs.rs index 55a3aaf4..19de7595 100644 --- a/rust/src/initramfs.rs +++ b/rust/src/initramfs.rs @@ -3,15 +3,18 @@ use crate::cxxrsutil::*; use anyhow::{Context, Result}; +use camino::Utf8Path; use gio::prelude::*; use openat::SimpleType; use std::collections::BTreeSet; use std::collections::HashSet; +use std::convert::TryInto; use std::fs; use std::io; use std::io::prelude::*; use std::os::unix::io::AsRawFd; use std::os::unix::io::IntoRawFd; +use std::path::Path; use std::pin::Pin; use std::rc; use subprocess::Exec; @@ -33,11 +36,7 @@ fn list_files_recurse>( SimpleType::Dir => { for e in d.list_dir(path).context("readdir")? { let e = e.context("readdir")?; - let name = e.file_name(); - let name = match name.to_str() { - Some(n) => n, - None => anyhow::bail!("Invalid UTF-8 name {}", name.to_string_lossy()), - }; + let name: &Utf8Path = Path::new(e.file_name()).try_into()?; let subpath = format!("{}/{}", path, name); list_files_recurse(&d, &subpath, filelist, cancellable)?; }