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
2 changes: 2 additions & 0 deletions src/uu/mktemp/locales/en-US.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ 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
mktemp-error-failed-to-create-file-via-template = failed to create file via template { $path }: { $error }

# Template types
mktemp-template-type-directory = directory
Expand Down
2 changes: 2 additions & 0 deletions src/uu/mktemp/locales/fr-FR.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ 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
mktemp-error-failed-to-create-file-via-template = fichier à créer à partir d'un modèle { $path }: { $error }

# Types de modèle
mktemp-template-type-directory = répertoire
Expand Down
15 changes: 13 additions & 2 deletions src/uu/mktemp/src/mktemp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ enum MkTempError {

#[error("{}", translate!("mktemp-error-not-found", "template_type" => .0, "template" => .1.quote()))]
NotFound(String, PathBuf),

#[error("{}", translate!("mktemp-error-failed-to-create-file-via-template", "path" => .0.quote(), "error" => .1 ))]
FailedToCreate(PathBuf, String),
}

impl UError for MkTempError {
Expand Down Expand Up @@ -560,7 +563,11 @@ fn make_temp_dir(dir: &Path, prefix: &str, rand: usize, suffix: &str) -> UResult
let path = Path::new(dir).join(filename);
Err(MkTempError::NotFound(translate!("mktemp-template-type-directory"), path).into())
}
Err(e) => Err(e.into()),
Err(e) => {
let filename = format!("{prefix}{}{suffix}", "X".repeat(rand));
let path = Path::new(dir).join(filename);
Err(MkTempError::FailedToCreate(path, uucore::error::strip_errno(&e)).into())
}
}
}

Expand Down Expand Up @@ -589,7 +596,11 @@ fn make_temp_file(dir: &Path, prefix: &str, rand: usize, suffix: &str) -> UResul
let path = Path::new(dir).join(filename);
Err(MkTempError::NotFound(translate!("mktemp-template-type-file"), path).into())
}
Err(e) => Err(e.into()),
Err(e) => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about make_temp_dir? mktemp -d -p /ro will still show the os error

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'll even add this to the test

let filename = format!("{prefix}{}{suffix}", "X".repeat(rand));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same two lines as the NotFound arm just above, could be dedup, no?

let path = Path::new(dir).join(filename);
Err(MkTempError::FailedToCreate(path, uucore::error::strip_errno(&e)).into())
}
}
}

Expand Down
34 changes: 34 additions & 0 deletions tests/by-util/test_mktemp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1269,3 +1269,37 @@ fn test_write_error_removes_the_temporary_directory() {
.collect();
assert!(leftovers.is_empty(), "left behind: {leftovers:?}");
}

#[test]
#[cfg(unix)]
fn test_permission_denied_no_os_error() {
use rustix::process::geteuid;
use std::fs;
use std::os::unix::fs::PermissionsExt;

let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;

if geteuid().is_root() {
return;
}

at.mkdir("dir");
fs::set_permissions(at.plus("dir"), fs::Permissions::from_mode(0o444)).unwrap();

scene
.ucmd()
.args(&["-p", "dir"])
.fails_with_code(1)
.stderr_is(
"mktemp: failed to create file via template 'dir/tmp.XXXXXXXXXX': Permission denied\n",
);

scene
.ucmd()
.args(&["-d", "-p", "dir"])
.fails_with_code(1)
.stderr_is(
"mktemp: failed to create file via template 'dir/tmp.XXXXXXXXXX': Permission denied\n",
);
}
Loading