From d3088c42ee7b24a3b29b8e7f5f39b9233652c744 Mon Sep 17 00:00:00 2001 From: Bryant Biggs Date: Wed, 20 Sep 2023 13:59:24 -0400 Subject: [PATCH] fix: Use `rfind` to slice string up to last `/` --- crates/shim/src/mount.rs | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/crates/shim/src/mount.rs b/crates/shim/src/mount.rs index a2658960..9c93c8c6 100644 --- a/crates/shim/src/mount.rs +++ b/crates/shim/src/mount.rs @@ -267,19 +267,7 @@ fn longest_common_prefix(dirs: &[String]) -> &str { // however, there is assumption that the common dir is ${root}/io.containerd.v1.overlayfs/snapshots. #[cfg(target_os = "linux")] fn trim_flawed_dir(s: &str) -> String { - match s.ends_with('/') { - true => s.to_owned(), - false => { - // Iterate in reverse order to find the last '/', then return string in correct order - s.chars() - .rev() - .skip_while(|x| *x != '/') - .collect::>() - .iter() - .rev() - .collect::() - } - } + s[0..s.rfind('/').unwrap_or(0) + 1].to_owned() } #[cfg(target_os = "linux")] @@ -619,6 +607,8 @@ mod tests { #[test] fn test_trim_flawed_dir() { let mut tcases: Vec<(&str, String)> = Vec::new(); + tcases.push(("/", "/".to_string())); + tcases.push(("/foo", "/".to_string())); tcases.push(("/.foo-_bar/foo", "/.foo-_bar/".to_string())); tcases.push(("/.foo-_bar/foo/", "/.foo-_bar/foo/".to_string())); tcases.push(("/.foo-_bar/foo/bar", "/.foo-_bar/foo/".to_string()));