Skip to content

Commit 9d3bafc

Browse files
gabrielhnfcakebaker
authored andcommitted
mktemp: fix hidden file creation with dot prefix
1 parent 83a5cd9 commit 9d3bafc

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

src/uu/mktemp/src/mktemp.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,16 @@ impl Params {
281281
let prefix_str = prefix_path.to_string_lossy();
282282
if prefix_str.ends_with(MAIN_SEPARATOR) {
283283
(prefix_path, String::new())
284+
} else if prefix_from_template.ends_with("/.") || prefix_from_template == "." {
285+
// Path normalizes trailing '.' away, making both parent() and file_name()
286+
// return wrong results for hidden files like /tmp/.XXXXXXXX.
287+
// Use prefix_from_template directly instead.
288+
let directory = Path::new(&prefix_from_option)
289+
.join(&prefix_from_template[..prefix_from_template.len() - 1]); // strip trailing '.'
290+
291+
let prefix = ".".to_string();
292+
293+
(directory, prefix)
284294
} else {
285295
let directory = match prefix_path.parent() {
286296
None => PathBuf::new(),

tests/by-util/test_mktemp.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,3 +1172,32 @@ fn test_non_utf8_tmpdir_directory_creation() {
11721172
// but we can verify the command succeeds
11731173
ucmd.arg("-d").arg("-p").arg(at.plus(dir_name)).succeeds();
11741174
}
1175+
1176+
#[test]
1177+
#[cfg(unix)]
1178+
fn test_mktemp_hidden_file_single_dot() {
1179+
let scene = TestScenario::new(util_name!());
1180+
let dir = tempdir().unwrap();
1181+
let template_name = ".XXXXXX";
1182+
let template = dir.path().join(template_name);
1183+
1184+
let result = scene.ucmd().arg(template.to_str().unwrap()).succeeds();
1185+
1186+
let path = result.stdout_str().trim();
1187+
let filename = std::path::Path::new(path)
1188+
.file_name()
1189+
.unwrap()
1190+
.to_str()
1191+
.unwrap();
1192+
1193+
assert!(
1194+
filename.starts_with('.'),
1195+
"expected hidden file, got {path}"
1196+
);
1197+
assert_eq!(
1198+
filename.len(),
1199+
template_name.len(),
1200+
"expected filename of length {}, got {filename}",
1201+
template_name.len()
1202+
);
1203+
}

0 commit comments

Comments
 (0)