diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-04 12:47:55 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-04 12:47:55 +0000 |
commit | 2aadc03ef15cb5ca5cc2af8a7c08e070742f0ac4 (patch) | |
tree | 033cc839730fda84ff08db877037977be94e5e3a /vendor/tempfile/src/util.rs | |
parent | Initial commit. (diff) | |
download | cargo-upstream.tar.xz cargo-upstream.zip |
Adding upstream version 0.70.1+ds1.upstream/0.70.1+ds1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/tempfile/src/util.rs')
-rw-r--r-- | vendor/tempfile/src/util.rs | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/vendor/tempfile/src/util.rs b/vendor/tempfile/src/util.rs new file mode 100644 index 0000000..d426ba3 --- /dev/null +++ b/vendor/tempfile/src/util.rs @@ -0,0 +1,47 @@ +use std::ffi::{OsStr, OsString}; +use std::path::{Path, PathBuf}; +use std::{io, iter::repeat_with}; + +use crate::error::IoResultExt; + +fn tmpname(prefix: &OsStr, suffix: &OsStr, rand_len: usize) -> OsString { + let mut buf = OsString::with_capacity(prefix.len() + suffix.len() + rand_len); + buf.push(prefix); + let mut char_buf = [0u8; 4]; + for c in repeat_with(fastrand::alphanumeric).take(rand_len) { + buf.push(c.encode_utf8(&mut char_buf)); + } + buf.push(suffix); + buf +} + +pub fn create_helper<R>( + base: &Path, + prefix: &OsStr, + suffix: &OsStr, + random_len: usize, + mut f: impl FnMut(PathBuf) -> io::Result<R>, +) -> io::Result<R> { + let num_retries = if random_len != 0 { + crate::NUM_RETRIES + } else { + 1 + }; + + for _ in 0..num_retries { + let path = base.join(tmpname(prefix, suffix, random_len)); + return match f(path) { + Err(ref e) if e.kind() == io::ErrorKind::AlreadyExists && num_retries > 1 => continue, + // AddrInUse can happen if we're creating a UNIX domain socket and + // the path already exists. + Err(ref e) if e.kind() == io::ErrorKind::AddrInUse && num_retries > 1 => continue, + res => res, + }; + } + + Err(io::Error::new( + io::ErrorKind::AlreadyExists, + "too many temporary files exist", + )) + .with_err_path(|| base) +} |