mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-27 11:07:44 +00:00
Merge pull request #3521 from jfinkels/mktemp-tmpdir-absolute-path
mktemp: correct error message on absolute path
This commit is contained in:
commit
d92107362b
2 changed files with 21 additions and 3 deletions
|
@ -152,12 +152,14 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||||
let dry_run = matches.is_present(OPT_DRY_RUN);
|
let dry_run = matches.is_present(OPT_DRY_RUN);
|
||||||
let suppress_file_err = matches.is_present(OPT_QUIET);
|
let suppress_file_err = matches.is_present(OPT_QUIET);
|
||||||
|
|
||||||
let (prefix, rand, suffix) = parse_template(template, matches.value_of(OPT_SUFFIX))?;
|
// If `--tmpdir` is given, the template cannot be an absolute
|
||||||
|
// path. For example, `mktemp --tmpdir=a /XXX` is not allowed.
|
||||||
if matches.is_present(OPT_TMPDIR) && PathBuf::from(prefix).is_absolute() {
|
if matches.is_present(OPT_TMPDIR) && PathBuf::from(template).is_absolute() {
|
||||||
return Err(MkTempError::InvalidTemplate(template.into()).into());
|
return Err(MkTempError::InvalidTemplate(template.into()).into());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let (prefix, rand, suffix) = parse_template(template, matches.value_of(OPT_SUFFIX))?;
|
||||||
|
|
||||||
let res = if dry_run {
|
let res = if dry_run {
|
||||||
dry_exec(tmpdir, prefix, rand, suffix)
|
dry_exec(tmpdir, prefix, rand, suffix)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -414,6 +414,22 @@ fn test_mktemp_directory_tmpdir() {
|
||||||
assert!(PathBuf::from(result.stdout_str().trim()).is_dir());
|
assert!(PathBuf::from(result.stdout_str().trim()).is_dir());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Test that an absolute path is disallowed when --tmpdir is provided.
|
||||||
|
#[test]
|
||||||
|
fn test_tmpdir_absolute_path() {
|
||||||
|
#[cfg(windows)]
|
||||||
|
let path = r"C:\XXX";
|
||||||
|
#[cfg(not(windows))]
|
||||||
|
let path = "/XXX";
|
||||||
|
new_ucmd!()
|
||||||
|
.args(&["--tmpdir=a", path])
|
||||||
|
.fails()
|
||||||
|
.stderr_only(format!(
|
||||||
|
"mktemp: invalid template, '{}'; with --tmpdir, it may not be absolute\n",
|
||||||
|
path
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
/// Decide whether a string matches a given template.
|
/// Decide whether a string matches a given template.
|
||||||
///
|
///
|
||||||
/// In the template, the character `'X'` is treated as a wildcard,
|
/// In the template, the character `'X'` is treated as a wildcard,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue