mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
mktemp: fix error msg when suffix has path sep.
Correct the error message when the template argument contains a path separator in its suffix. Before this commit: $ mktemp aXXX/b mktemp: too few X's in template 'b' After this commit: $ mktemp aXXX/b mktemp: invalid suffix '/b', contains directory separator This error message is more appropriate and matches the behavior of GNU mktemp.
This commit is contained in:
parent
0638e285f1
commit
6260333415
2 changed files with 29 additions and 1 deletions
|
@ -17,7 +17,7 @@ use std::env;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
use std::iter;
|
use std::iter;
|
||||||
use std::path::{is_separator, Path, PathBuf};
|
use std::path::{is_separator, Path, PathBuf, MAIN_SEPARATOR};
|
||||||
|
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use tempfile::Builder;
|
use tempfile::Builder;
|
||||||
|
@ -129,6 +129,19 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||||
};
|
};
|
||||||
let filename = path.file_name();
|
let filename = path.file_name();
|
||||||
let template = filename.unwrap().to_str().unwrap();
|
let template = filename.unwrap().to_str().unwrap();
|
||||||
|
// If the command line was `mktemp aXXX/b`, then we will
|
||||||
|
// find that `tmp`, which is the result of getting the
|
||||||
|
// parent when treating the argument as a path, contains
|
||||||
|
// at least three consecutive Xs. This means that there
|
||||||
|
// was a path separator in the suffix, which is not
|
||||||
|
// allowed.
|
||||||
|
if tmp.display().to_string().contains("XXX") {
|
||||||
|
return Err(MkTempError::SuffixContainsDirSeparator(format!(
|
||||||
|
"{}{}",
|
||||||
|
MAIN_SEPARATOR, template
|
||||||
|
))
|
||||||
|
.into());
|
||||||
|
}
|
||||||
(template, tmp)
|
(template, tmp)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -496,3 +496,18 @@ fn test_template_path_separator() {
|
||||||
"a/bXXX".quote()
|
"a/bXXX".quote()
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Test that a suffix with a path separator is invalid.
|
||||||
|
#[test]
|
||||||
|
fn test_suffix_path_separator() {
|
||||||
|
#[cfg(not(windows))]
|
||||||
|
new_ucmd!()
|
||||||
|
.arg("aXXX/b")
|
||||||
|
.fails()
|
||||||
|
.stderr_only("mktemp: invalid suffix '/b', contains directory separator\n");
|
||||||
|
#[cfg(windows)]
|
||||||
|
new_ucmd!()
|
||||||
|
.arg(r"aXXX\b")
|
||||||
|
.fails()
|
||||||
|
.stderr_only("mktemp: invalid suffix '\\b', contains directory separator\n");
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue