Skip to content
Open
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
1 change: 0 additions & 1 deletion src/uu/mktemp/locales/en-US.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ mktemp-error-suffix-contains-separator = invalid suffix { $suffix }, contains di
mktemp-error-invalid-template = invalid template, { $template }; with --tmpdir, it may not be absolute
mktemp-error-too-many-templates = too many templates
mktemp-error-not-found = failed to create { $template_type } via template { $template }: No such file or directory
mktemp-error-failed-print = failed to print directory name

# Template types
mktemp-template-type-directory = directory
Expand Down
1 change: 0 additions & 1 deletion src/uu/mktemp/locales/fr-FR.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ mktemp-error-suffix-contains-separator = suffixe invalide { $suffix }, contient
mktemp-error-invalid-template = modèle invalide, { $template } ; avec --tmpdir, il ne peut pas être absolu
mktemp-error-too-many-templates = trop de modèles
mktemp-error-not-found = échec de la création de { $template_type } via le modèle { $template } : Aucun fichier ou répertoire de ce type
mktemp-error-failed-print = échec de l'affichage du nom de répertoire

# Types de modèle
mktemp-template-type-directory = répertoire
Expand Down
18 changes: 15 additions & 3 deletions src/uu/mktemp/src/mktemp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ use uucore::translate;

use std::env;
use std::ffi::{OsStr, OsString};
use std::fs;
use std::io::ErrorKind;
use std::iter;
use std::path::{MAIN_SEPARATOR, Path, PathBuf};

#[cfg(unix)]
use std::fs;
#[cfg(unix)]
use std::os::unix::prelude::PermissionsExt;

Expand Down Expand Up @@ -435,7 +434,20 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
} else {
res
};
println_verbatim(res?).map_err_context(|| translate!("mktemp-error-failed-print"))
let path = res?;
if let Err(e) = println_verbatim(&path) {
// The caller never learns the name, so leaving the file behind would
// litter the temporary directory with something nothing can clean up.
if !dry_run {
let _ = if make_dir {
fs::remove_dir(&path)
} else {
fs::remove_file(&path)
};
}
return Err(e).map_err_context(|| translate!("common-write-error"));
}
Ok(())
}

pub fn uu_app() -> Command {
Expand Down
38 changes: 38 additions & 0 deletions tests/by-util/test_mktemp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1209,3 +1209,41 @@ fn test_mktemp_hidden_file_single_dot() {
template_name.len()
);
}

// When the name cannot be printed the caller never learns it, so the file must
// not be left behind for nothing to clean up.
#[test]
#[cfg(target_os = "linux")]
fn test_write_error_removes_the_temporary_file() {
let (at, mut ucmd) = at_and_ucmd!();
ucmd.arg("keep-none-XXXX")
.set_stdout(std::fs::File::create("/dev/full").unwrap())
.fails()
.stderr_is("mktemp: write error: No space left on device\n");

let leftovers: Vec<_> = std::fs::read_dir(at.as_string())
.unwrap()
.filter_map(Result::ok)
.map(|entry| entry.file_name().to_string_lossy().into_owned())
.filter(|name| name.starts_with("keep-none-"))
.collect();
assert!(leftovers.is_empty(), "left behind: {leftovers:?}");
}

#[test]
#[cfg(target_os = "linux")]
fn test_write_error_removes_the_temporary_directory() {
let (at, mut ucmd) = at_and_ucmd!();
ucmd.args(&["-d", "keep-none-XXXX"])
.set_stdout(std::fs::File::create("/dev/full").unwrap())
.fails()
.stderr_is("mktemp: write error: No space left on device\n");

let leftovers: Vec<_> = std::fs::read_dir(at.as_string())
.unwrap()
.filter_map(Result::ok)
.map(|entry| entry.file_name().to_string_lossy().into_owned())
.filter(|name| name.starts_with("keep-none-"))
.collect();
assert!(leftovers.is_empty(), "left behind: {leftovers:?}");
}
Loading