]>
git.proxmox.com Git - rustc.git/blob - vendor/tempfile/src/util.rs
1 use rand
::distributions
::Alphanumeric
;
3 use std
::ffi
::{OsStr, OsString}
;
4 use std
::path
::{Path, PathBuf}
;
7 use crate::error
::IoResultExt
;
9 fn tmpname(prefix
: &OsStr
, suffix
: &OsStr
, rand_len
: usize) -> OsString
{
10 let mut buf
= OsString
::with_capacity(prefix
.len() + suffix
.len() + rand_len
);
13 // Push each character in one-by-one. Unfortunately, this is the only
14 // safe(ish) simple way to do this without allocating a temporary
18 .sample_iter(&Alphanumeric
)
20 .for_each(|b
| buf
.push(str::from_utf8_unchecked(&[b
as u8])))
26 pub fn create_helper
<F
, R
>(
34 F
: Fn(PathBuf
) -> io
::Result
<R
>,
36 let num_retries
= if random_len
!= 0 {
42 for _
in 0..num_retries
{
43 let path
= base
.join(tmpname(prefix
, suffix
, random_len
));
44 return match f(path
) {
45 Err(ref e
) if e
.kind() == io
::ErrorKind
::AlreadyExists
=> continue,
51 io
::ErrorKind
::AlreadyExists
,
52 "too many temporary files exist",
54 .with_err_path(|| base
)