1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 03:57:44 +00:00

create a function dir_strip_dot_for_creation to manage the /. issue

This commit is contained in:
Sylvestre Ledru 2022-04-02 13:10:28 +02:00
parent 2628f3ed60
commit 845b2294e1
2 changed files with 16 additions and 6 deletions

View file

@ -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::<PathBuf>()
} 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

View file

@ -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::<PathBuf>()
} else {
path.to_path_buf()
}
}
#[cfg(test)]
mod tests {
// Note this useful idiom: importing names from outer (for mod tests) scope.