Skip to content

Commit

Permalink
fix: Use rfind to slice string up to last /
Browse files Browse the repository at this point in the history
  • Loading branch information
bryantbiggs authored and mxpv committed Sep 21, 2023
1 parent 3489707 commit d3088c4
Showing 1 changed file with 3 additions and 13 deletions.
16 changes: 3 additions & 13 deletions crates/shim/src/mount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<char>>()
.iter()
.rev()
.collect::<String>()
}
}
s[0..s.rfind('/').unwrap_or(0) + 1].to_owned()
}

#[cfg(target_os = "linux")]
Expand Down Expand Up @@ -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()));
Expand Down

0 comments on commit d3088c4

Please sign in to comment.