diff --git a/src/uu/install/src/install.rs b/src/uu/install/src/install.rs index f50b7e81b..330124467 100644 --- a/src/uu/install/src/install.rs +++ b/src/uu/install/src/install.rs @@ -20,6 +20,7 @@ use uucore::display::Quotable; use uucore::entries::{grp2gid, usr2uid}; use uucore::error::{FromIo, UError, UIoError, UResult, UUsageError}; use uucore::format_usage; +use uucore::fs::dir_strip_dot_for_creation; use uucore::mode::get_umask; use uucore::perms::{wrap_chown, Verbosity, VerbosityLevel}; @@ -399,12 +400,7 @@ fn directory(paths: &[String], b: &Behavior) -> UResult<()> { // install -d foo/. should work and just create foo/ // std::fs::create_dir("foo/."); fails in pure Rust // See also mkdir.rs for another occurrence of this - let path_to_create = if path.to_string_lossy().ends_with("/.") { - // Do a simple dance to strip the "/." - Path::new(path).components().collect::() - } else { - path.to_path_buf() - }; + let path_to_create = dir_strip_dot_for_creation(path.to_path_buf()); // Differently than the primary functionality // (MainFunction::Standard), the directory functionality should // create all ancestors (or components) of a directory diff --git a/src/uucore/src/lib/features/fs.rs b/src/uucore/src/lib/features/fs.rs index f5295f17f..1b7fb24b3 100644 --- a/src/uucore/src/lib/features/fs.rs +++ b/src/uucore/src/lib/features/fs.rs @@ -470,6 +470,20 @@ pub fn display_permissions_unix(mode: mode_t, display_file_type: bool) -> String result } +// For some programs like install or mkdir, dir/. can be provided +// Special case to match GNU's behavior: +// install -d foo/. should work and just create foo/ +// std::fs::create_dir("foo/."); fails in pure Rust +// See also mkdir.rs for another occurrence of this +pub fn dir_strip_dot_for_creation(path: PathBuf) -> PathBuf { + if path.to_string_lossy().ends_with("/.") { + // Do a simple dance to strip the "/." + Path::new(&path).components().collect::() + } else { + path.to_path_buf() + } +} + #[cfg(test)] mod tests { // Note this useful idiom: importing names from outer (for mod tests) scope.