Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/uu/mktemp/src/mktemp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,16 @@ impl Params {
let prefix_str = prefix_path.to_string_lossy();
if prefix_str.ends_with(MAIN_SEPARATOR) {
(prefix_path, String::new())
} else if prefix_from_template.ends_with("/.") || prefix_from_template == "." {
// Path normalizes trailing '.' away, making both parent() and file_name()
// return wrong results for hidden files like /tmp/.XXXXXXXX.
// Use prefix_from_template directly instead.
let directory = Path::new(&prefix_from_option)
.join(&prefix_from_template[..prefix_from_template.len() - 1]); // strip trailing '.'

let prefix = ".".to_string();

(directory, prefix)
} else {
let directory = match prefix_path.parent() {
None => PathBuf::new(),
Expand Down
29 changes: 29 additions & 0 deletions tests/by-util/test_mktemp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1172,3 +1172,32 @@ fn test_non_utf8_tmpdir_directory_creation() {
// but we can verify the command succeeds
ucmd.arg("-d").arg("-p").arg(at.plus(dir_name)).succeeds();
}

#[test]
#[cfg(unix)]
fn test_mktemp_hidden_file_single_dot() {
Comment thread
cakebaker marked this conversation as resolved.
let scene = TestScenario::new(util_name!());
let dir = tempdir().unwrap();
let template_name = ".XXXXXX";
let template = dir.path().join(template_name);

let result = scene.ucmd().arg(template.to_str().unwrap()).succeeds();

let path = result.stdout_str().trim();
let filename = std::path::Path::new(path)
.file_name()
.unwrap()
.to_str()
.unwrap();

assert!(
filename.starts_with('.'),
"expected hidden file, got {path}"
);
assert_eq!(
filename.len(),
template_name.len(),
"expected filename of length {}, got {filename}",
template_name.len()
);
}
Loading