mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 19:47:45 +00:00
fix(mktemp) - Make mktemp --tempdir foo.XXXXXX works (#1716)
Used by apt-key
This commit is contained in:
parent
6c2bca110d
commit
b8987f3d5f
2 changed files with 68 additions and 14 deletions
|
@ -100,6 +100,33 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
.get_matches_from(args);
|
.get_matches_from(args);
|
||||||
|
|
||||||
let template = matches.value_of(ARG_TEMPLATE).unwrap();
|
let template = matches.value_of(ARG_TEMPLATE).unwrap();
|
||||||
|
let tmpdir = matches.value_of(OPT_TMPDIR).unwrap_or_default();
|
||||||
|
|
||||||
|
let (template, mut tmpdir) = if matches.is_present(OPT_TMPDIR)
|
||||||
|
&& !PathBuf::from(tmpdir).is_dir() // if a temp dir is provided, it must be an actual path
|
||||||
|
&& tmpdir.contains("XXX")
|
||||||
|
// If this is a template, it has to contain at least 3 X
|
||||||
|
&& template == DEFAULT_TEMPLATE
|
||||||
|
// That means that clap does not think we provided a template
|
||||||
|
{
|
||||||
|
// Special case to workaround a limitation of clap when doing
|
||||||
|
// mktemp --tmpdir apt-key-gpghome.XXX
|
||||||
|
// The behavior should be
|
||||||
|
// mktemp --tmpdir $TMPDIR apt-key-gpghome.XX
|
||||||
|
// As --tmpdir is empty
|
||||||
|
//
|
||||||
|
// Fixed in clap 3
|
||||||
|
// See https://github.com/clap-rs/clap/pull/1587
|
||||||
|
let tmp = env::temp_dir();
|
||||||
|
(tmpdir, tmp)
|
||||||
|
} else {
|
||||||
|
if !matches.is_present(OPT_TMPDIR) {
|
||||||
|
let tmp = env::temp_dir();
|
||||||
|
(template, tmp)
|
||||||
|
} else {
|
||||||
|
(template, PathBuf::from(tmpdir))
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
let make_dir = matches.is_present(OPT_DIRECTORY);
|
let make_dir = matches.is_present(OPT_DIRECTORY);
|
||||||
let dry_run = matches.is_present(OPT_DRY_RUN);
|
let dry_run = matches.is_present(OPT_DRY_RUN);
|
||||||
|
@ -130,18 +157,14 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
crash!(1, "suffix cannot contain any path separators");
|
crash!(1, "suffix cannot contain any path separators");
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut tmpdir = match matches.value_of(OPT_TMPDIR) {
|
if matches.is_present(OPT_TMPDIR) {
|
||||||
Some(s) => {
|
if PathBuf::from(prefix).is_absolute() {
|
||||||
if PathBuf::from(prefix).is_absolute() {
|
show_info!(
|
||||||
show_info!(
|
"invalid template, ‘{}’; with --tmpdir, it may not be absolute",
|
||||||
"invalid template, ‘{}’; with --tmpdir, it may not be absolute",
|
template
|
||||||
template
|
);
|
||||||
);
|
return 1;
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
PathBuf::from(s)
|
|
||||||
}
|
}
|
||||||
None => env::temp_dir(),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if matches.is_present(OPT_T) {
|
if matches.is_present(OPT_T) {
|
||||||
|
@ -209,18 +232,17 @@ fn exec(
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
if !quiet {
|
if !quiet {
|
||||||
show_info!("{}", e);
|
show_info!("{}: {}", e, tmpdir.display());
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let tmpfile = Builder::new()
|
let tmpfile = Builder::new()
|
||||||
|
.prefix(prefix)
|
||||||
.rand_bytes(rand)
|
.rand_bytes(rand)
|
||||||
.suffix(suffix)
|
.suffix(suffix)
|
||||||
.tempfile_in(tmpdir);
|
.tempfile_in(tmpdir);
|
||||||
|
|
||||||
let tmpfile = match tmpfile {
|
let tmpfile = match tmpfile {
|
||||||
Ok(f) => f,
|
Ok(f) => f,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
use crate::common::util::*;
|
use crate::common::util::*;
|
||||||
|
|
||||||
|
use std::path::PathBuf;
|
||||||
use tempfile::tempdir;
|
use tempfile::tempdir;
|
||||||
|
|
||||||
static TEST_TEMPLATE1: &'static str = "tempXXXXXX";
|
static TEST_TEMPLATE1: &'static str = "tempXXXXXX";
|
||||||
|
@ -380,3 +381,34 @@ fn test_mktemp_tmpdir() {
|
||||||
.arg(TEST_TEMPLATE8)
|
.arg(TEST_TEMPLATE8)
|
||||||
.fails();
|
.fails();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_mktemp_tmpdir_one_arg() {
|
||||||
|
let scene = TestScenario::new(util_name!());
|
||||||
|
|
||||||
|
let result = scene
|
||||||
|
.ucmd()
|
||||||
|
.arg("--tmpdir")
|
||||||
|
.arg("apt-key-gpghome.XXXXXXXXXX")
|
||||||
|
.succeeds();
|
||||||
|
println!("stdout {}", result.stdout);
|
||||||
|
println!("stderr {}", result.stderr);
|
||||||
|
assert!(result.stdout.contains("apt-key-gpghome."));
|
||||||
|
assert!(PathBuf::from(result.stdout.trim()).is_file());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_mktemp_directory_tmpdir() {
|
||||||
|
let scene = TestScenario::new(util_name!());
|
||||||
|
|
||||||
|
let result = scene
|
||||||
|
.ucmd()
|
||||||
|
.arg("--directory")
|
||||||
|
.arg("--tmpdir")
|
||||||
|
.arg("apt-key-gpghome.XXXXXXXXXX")
|
||||||
|
.succeeds();
|
||||||
|
println!("stdout {}", result.stdout);
|
||||||
|
println!("stderr {}", result.stderr);
|
||||||
|
assert!(result.stdout.contains("apt-key-gpghome."));
|
||||||
|
assert!(PathBuf::from(result.stdout.trim()).is_dir());
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue