mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 12:07:46 +00:00
Merge pull request #2291 from tertsdiepraam/remove-tempdir
Replace `tempdir` with `tempfile`
This commit is contained in:
commit
222bd81190
8 changed files with 46 additions and 132 deletions
35
Cargo.lock
generated
35
Cargo.lock
generated
|
@ -254,7 +254,6 @@ dependencies = [
|
||||||
"rand 0.7.3",
|
"rand 0.7.3",
|
||||||
"regex",
|
"regex",
|
||||||
"sha1",
|
"sha1",
|
||||||
"tempdir",
|
|
||||||
"tempfile",
|
"tempfile",
|
||||||
"textwrap",
|
"textwrap",
|
||||||
"time",
|
"time",
|
||||||
|
@ -1232,19 +1231,6 @@ dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "rand"
|
|
||||||
version = "0.4.6"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293"
|
|
||||||
dependencies = [
|
|
||||||
"fuchsia-cprng",
|
|
||||||
"libc",
|
|
||||||
"rand_core 0.3.1",
|
|
||||||
"rdrand",
|
|
||||||
"winapi 0.3.9",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rand"
|
name = "rand"
|
||||||
version = "0.5.6"
|
version = "0.5.6"
|
||||||
|
@ -1389,15 +1375,6 @@ dependencies = [
|
||||||
"num_cpus",
|
"num_cpus",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "rdrand"
|
|
||||||
version = "0.4.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2"
|
|
||||||
dependencies = [
|
|
||||||
"rand_core 0.3.1",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "redox_syscall"
|
name = "redox_syscall"
|
||||||
version = "0.1.57"
|
version = "0.1.57"
|
||||||
|
@ -1662,16 +1639,6 @@ dependencies = [
|
||||||
"unicode-xid 0.2.2",
|
"unicode-xid 0.2.2",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "tempdir"
|
|
||||||
version = "0.3.7"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "15f2b5fb00ccdf689e0149d1b1b3c03fead81c2b37735d812fa8bddbbf41b6d8"
|
|
||||||
dependencies = [
|
|
||||||
"rand 0.4.6",
|
|
||||||
"remove_dir_all",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tempfile"
|
name = "tempfile"
|
||||||
version = "3.2.0"
|
version = "3.2.0"
|
||||||
|
@ -2587,7 +2554,7 @@ dependencies = [
|
||||||
"rand 0.7.3",
|
"rand 0.7.3",
|
||||||
"rayon",
|
"rayon",
|
||||||
"semver 0.9.0",
|
"semver 0.9.0",
|
||||||
"tempdir",
|
"tempfile",
|
||||||
"unicode-width",
|
"unicode-width",
|
||||||
"uucore",
|
"uucore",
|
||||||
"uucore_procs",
|
"uucore_procs",
|
||||||
|
|
|
@ -347,7 +347,6 @@ time = "0.1"
|
||||||
unindent = "0.1"
|
unindent = "0.1"
|
||||||
uucore = { version=">=0.0.8", package="uucore", path="src/uucore", features=["entries"] }
|
uucore = { version=">=0.0.8", package="uucore", path="src/uucore", features=["entries"] }
|
||||||
walkdir = "2.2"
|
walkdir = "2.2"
|
||||||
tempdir = "0.3"
|
|
||||||
|
|
||||||
[target.'cfg(unix)'.dev-dependencies]
|
[target.'cfg(unix)'.dev-dependencies]
|
||||||
rust-users = { version="0.10", package="users" }
|
rust-users = { version="0.10", package="users" }
|
||||||
|
|
|
@ -15,14 +15,11 @@ use clap::{App, Arg};
|
||||||
|
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::iter;
|
use std::iter;
|
||||||
use std::mem::forget;
|
|
||||||
use std::path::{is_separator, PathBuf};
|
use std::path::{is_separator, PathBuf};
|
||||||
|
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use tempfile::Builder;
|
use tempfile::Builder;
|
||||||
|
|
||||||
mod tempdir;
|
|
||||||
|
|
||||||
static ABOUT: &str = "create a temporary file or directory.";
|
static ABOUT: &str = "create a temporary file or directory.";
|
||||||
static VERSION: &str = env!("CARGO_PKG_VERSION");
|
static VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||||
|
|
||||||
|
@ -214,49 +211,54 @@ pub fn dry_exec(mut tmpdir: PathBuf, prefix: &str, rand: usize, suffix: &str) ->
|
||||||
}
|
}
|
||||||
|
|
||||||
fn exec(
|
fn exec(
|
||||||
tmpdir: PathBuf,
|
dir: PathBuf,
|
||||||
prefix: &str,
|
prefix: &str,
|
||||||
rand: usize,
|
rand: usize,
|
||||||
suffix: &str,
|
suffix: &str,
|
||||||
make_dir: bool,
|
make_dir: bool,
|
||||||
quiet: bool,
|
quiet: bool,
|
||||||
) -> i32 {
|
) -> i32 {
|
||||||
if make_dir {
|
let res = if make_dir {
|
||||||
match tempdir::new_in(&tmpdir, prefix, rand, suffix) {
|
let tmpdir = Builder::new()
|
||||||
Ok(ref f) => {
|
.prefix(prefix)
|
||||||
println!("{}", f);
|
.rand_bytes(rand)
|
||||||
return 0;
|
.suffix(suffix)
|
||||||
}
|
.tempdir_in(&dir);
|
||||||
Err(e) => {
|
|
||||||
if !quiet {
|
// `into_path` consumes the TempDir without removing it
|
||||||
show_error!("{}: {}", e, tmpdir.display());
|
tmpdir.map(|d| d.into_path().to_string_lossy().to_string())
|
||||||
|
} else {
|
||||||
|
let tmpfile = Builder::new()
|
||||||
|
.prefix(prefix)
|
||||||
|
.rand_bytes(rand)
|
||||||
|
.suffix(suffix)
|
||||||
|
.tempfile_in(&dir);
|
||||||
|
|
||||||
|
match tmpfile {
|
||||||
|
Ok(f) => {
|
||||||
|
// `keep` ensures that the file is not deleted
|
||||||
|
match f.keep() {
|
||||||
|
Ok((_, p)) => Ok(p.to_string_lossy().to_string()),
|
||||||
|
Err(e) => {
|
||||||
|
show_error!("'{}': {}", dir.display(), e);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
}
|
Err(x) => Err(x)
|
||||||
}
|
|
||||||
let tmpfile = Builder::new()
|
|
||||||
.prefix(prefix)
|
|
||||||
.rand_bytes(rand)
|
|
||||||
.suffix(suffix)
|
|
||||||
.tempfile_in(tmpdir);
|
|
||||||
let tmpfile = match tmpfile {
|
|
||||||
Ok(f) => f,
|
|
||||||
Err(e) => {
|
|
||||||
if !quiet {
|
|
||||||
show_error!("failed to create tempfile: {}", e);
|
|
||||||
}
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let tmpname = tmpfile.path().to_string_lossy().to_string();
|
match res {
|
||||||
|
Ok(ref f) => {
|
||||||
println!("{}", tmpname);
|
println!("{}", f);
|
||||||
|
0
|
||||||
// CAUTION: Not to call `drop` of tmpfile, which removes the tempfile,
|
}
|
||||||
// I call a dangerous function `forget`.
|
Err(e) => {
|
||||||
forget(tmpfile);
|
if !quiet {
|
||||||
|
show_error!("{}: {}", e, dir.display());
|
||||||
0
|
}
|
||||||
|
1
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,51 +0,0 @@
|
||||||
// spell-checker:ignore (ToDO) tempdir tmpdir
|
|
||||||
|
|
||||||
// Mainly taken from crate `tempdir`
|
|
||||||
|
|
||||||
use rand::distributions::Alphanumeric;
|
|
||||||
use rand::{thread_rng, Rng};
|
|
||||||
|
|
||||||
use std::io::Result as IOResult;
|
|
||||||
use std::io::{Error, ErrorKind};
|
|
||||||
use std::path::Path;
|
|
||||||
|
|
||||||
// How many times should we (re)try finding an unused random name? It should be
|
|
||||||
// enough that an attacker will run out of luck before we run out of patience.
|
|
||||||
const NUM_RETRIES: u32 = 1 << 31;
|
|
||||||
|
|
||||||
#[cfg(any(unix, target_os = "redox"))]
|
|
||||||
fn create_dir<P: AsRef<Path>>(path: P) -> IOResult<()> {
|
|
||||||
use std::fs::DirBuilder;
|
|
||||||
use std::os::unix::fs::DirBuilderExt;
|
|
||||||
|
|
||||||
DirBuilder::new().mode(0o700).create(path)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(windows)]
|
|
||||||
fn create_dir<P: AsRef<Path>>(path: P) -> IOResult<()> {
|
|
||||||
::std::fs::create_dir(path)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn new_in<P: AsRef<Path>>(
|
|
||||||
tmpdir: P,
|
|
||||||
prefix: &str,
|
|
||||||
rand: usize,
|
|
||||||
suffix: &str,
|
|
||||||
) -> IOResult<String> {
|
|
||||||
let mut rng = thread_rng();
|
|
||||||
for _ in 0..NUM_RETRIES {
|
|
||||||
let rand_chars: String = rng.sample_iter(&Alphanumeric).take(rand).collect();
|
|
||||||
let leaf = format!("{}{}{}", prefix, rand_chars, suffix);
|
|
||||||
let path = tmpdir.as_ref().join(&leaf);
|
|
||||||
match create_dir(&path) {
|
|
||||||
Ok(_) => return Ok(path.to_string_lossy().into_owned()),
|
|
||||||
Err(ref e) if e.kind() == ErrorKind::AlreadyExists => {}
|
|
||||||
Err(e) => return Err(e),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Err(Error::new(
|
|
||||||
ErrorKind::AlreadyExists,
|
|
||||||
"too many temporary directories already exist",
|
|
||||||
))
|
|
||||||
}
|
|
|
@ -25,7 +25,7 @@ ouroboros = "0.9.3"
|
||||||
rand = "0.7"
|
rand = "0.7"
|
||||||
rayon = "1.5"
|
rayon = "1.5"
|
||||||
semver = "0.9.0"
|
semver = "0.9.0"
|
||||||
tempdir = "0.3.7"
|
tempfile = "3"
|
||||||
unicode-width = "0.1.8"
|
unicode-width = "0.1.8"
|
||||||
uucore = { version=">=0.0.8", package="uucore", path="../../uucore", features=["fs"] }
|
uucore = { version=">=0.0.8", package="uucore", path="../../uucore", features=["fs"] }
|
||||||
uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" }
|
uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" }
|
||||||
|
|
|
@ -23,7 +23,7 @@ use std::{
|
||||||
|
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
|
|
||||||
use tempdir::TempDir;
|
use tempfile::TempDir;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
chunks::{self, Chunk},
|
chunks::{self, Chunk},
|
||||||
|
@ -34,7 +34,7 @@ const MIN_BUFFER_SIZE: usize = 8_000;
|
||||||
|
|
||||||
/// Sort files by using auxiliary files for storing intermediate chunks (if needed), and output the result.
|
/// Sort files by using auxiliary files for storing intermediate chunks (if needed), and output the result.
|
||||||
pub fn ext_sort(files: &mut impl Iterator<Item = Box<dyn Read + Send>>, settings: &GlobalSettings) {
|
pub fn ext_sort(files: &mut impl Iterator<Item = Box<dyn Read + Send>>, settings: &GlobalSettings) {
|
||||||
let tmp_dir = crash_if_err!(1, TempDir::new_in(&settings.tmp_dir, "uutils_sort"));
|
let tmp_dir = crash_if_err!(1, tempfile::Builder::new().prefix("uutils_sort").tempdir_in(&settings.tmp_dir));
|
||||||
let (sorted_sender, sorted_receiver) = std::sync::mpsc::sync_channel(1);
|
let (sorted_sender, sorted_receiver) = std::sync::mpsc::sync_channel(1);
|
||||||
let (recycled_sender, recycled_receiver) = std::sync::mpsc::sync_channel(1);
|
let (recycled_sender, recycled_receiver) = std::sync::mpsc::sync_channel(1);
|
||||||
thread::spawn({
|
thread::spawn({
|
||||||
|
|
|
@ -406,10 +406,9 @@ fn test_domain_socket() {
|
||||||
use std::io::prelude::*;
|
use std::io::prelude::*;
|
||||||
use std::sync::{Arc, Barrier};
|
use std::sync::{Arc, Barrier};
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use tempdir::TempDir;
|
|
||||||
use unix_socket::UnixListener;
|
use unix_socket::UnixListener;
|
||||||
|
|
||||||
let dir = TempDir::new("unix_socket").expect("failed to create dir");
|
let dir = tempfile::Builder::new().prefix("unix_socket").tempdir().expect("failed to create dir");
|
||||||
let socket_path = dir.path().join("sock");
|
let socket_path = dir.path().join("sock");
|
||||||
let listener = UnixListener::bind(&socket_path).expect("failed to create socket");
|
let listener = UnixListener::bind(&socket_path).expect("failed to create socket");
|
||||||
|
|
||||||
|
|
|
@ -19,9 +19,7 @@ use std::path::PathBuf;
|
||||||
#[cfg(not(windows))]
|
#[cfg(not(windows))]
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
#[cfg(not(windows))]
|
#[cfg(not(windows))]
|
||||||
extern crate tempdir;
|
extern crate tempfile;
|
||||||
#[cfg(not(windows))]
|
|
||||||
use self::tempdir::TempDir;
|
|
||||||
|
|
||||||
#[cfg(not(windows))]
|
#[cfg(not(windows))]
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
|
@ -1087,7 +1085,7 @@ fn test_ls_indicator_style() {
|
||||||
{
|
{
|
||||||
use self::unix_socket::UnixListener;
|
use self::unix_socket::UnixListener;
|
||||||
|
|
||||||
let dir = TempDir::new("unix_socket").expect("failed to create dir");
|
let dir = tempfile::Builder::new().prefix("unix_socket").tempdir().expect("failed to create dir");
|
||||||
let socket_path = dir.path().join("sock");
|
let socket_path = dir.path().join("sock");
|
||||||
let _listener = UnixListener::bind(&socket_path).expect("failed to create socket");
|
let _listener = UnixListener::bind(&socket_path).expect("failed to create socket");
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue