/rust/registry/src/index.crates.io-1949cf8c6b5b557f/tempfile-3.19.1/src/util.rs
Line | Count | Source |
1 | | use std::ffi::{OsStr, OsString}; |
2 | | use std::path::{Path, PathBuf}; |
3 | | use std::{io, iter::repeat_with}; |
4 | | |
5 | | use crate::error::IoResultExt; |
6 | | |
7 | 0 | fn tmpname(rng: &mut fastrand::Rng, prefix: &OsStr, suffix: &OsStr, rand_len: usize) -> OsString { |
8 | 0 | let capacity = prefix |
9 | 0 | .len() |
10 | 0 | .saturating_add(suffix.len()) |
11 | 0 | .saturating_add(rand_len); |
12 | 0 | let mut buf = OsString::with_capacity(capacity); |
13 | 0 | buf.push(prefix); |
14 | 0 | let mut char_buf = [0u8; 4]; |
15 | 0 | for c in repeat_with(|| rng.alphanumeric()).take(rand_len) { |
16 | 0 | buf.push(c.encode_utf8(&mut char_buf)); |
17 | 0 | } |
18 | 0 | buf.push(suffix); |
19 | 0 | buf |
20 | 0 | } |
21 | | |
22 | 0 | pub fn create_helper<R>( |
23 | 0 | base: &Path, |
24 | 0 | prefix: &OsStr, |
25 | 0 | suffix: &OsStr, |
26 | 0 | random_len: usize, |
27 | 0 | mut f: impl FnMut(PathBuf) -> io::Result<R>, |
28 | 0 | ) -> io::Result<R> { |
29 | | // Make the path absolute. Otherwise, changing the current directory can invalidate a stored |
30 | | // path (causing issues when cleaning up temporary files. |
31 | 0 | let mut base = base; // re-borrow to shrink lifetime |
32 | | let base_path_storage; // slot to store the absolute path, if necessary. |
33 | 0 | if !base.is_absolute() { |
34 | 0 | let cur_dir = std::env::current_dir()?; |
35 | 0 | base_path_storage = cur_dir.join(base); |
36 | 0 | base = &base_path_storage; |
37 | 0 | } |
38 | | |
39 | 0 | let num_retries = if random_len != 0 { |
40 | 0 | crate::NUM_RETRIES |
41 | | } else { |
42 | 0 | 1 |
43 | | }; |
44 | | |
45 | | // We fork the fastrand rng. |
46 | 0 | let mut rng = fastrand::Rng::new(); |
47 | 0 | for i in 0..num_retries { |
48 | | // If we fail to create the file the first three times, re-seed from system randomness in |
49 | | // case an attacker is predicting our randomness (fastrand is predictable). If re-seeding |
50 | | // doesn't help, either: |
51 | | // |
52 | | // 1. We have lots of temporary files, possibly created by an attacker but not necessarily. |
53 | | // Re-seeding the randomness won't help here. |
54 | | // 2. We're failing to create random files for some other reason. This shouldn't be the case |
55 | | // given that we're checking error kinds, but it could happen. |
56 | | #[cfg(all( |
57 | | feature = "getrandom", |
58 | | any(windows, unix, target_os = "redox", target_os = "wasi") |
59 | | ))] |
60 | 0 | if i == 3 { |
61 | 0 | if let Ok(seed) = getrandom::u64() { |
62 | 0 | rng.seed(seed); |
63 | 0 | } |
64 | 0 | } |
65 | 0 | let path = base.join(tmpname(&mut rng, prefix, suffix, random_len)); |
66 | 0 | return match f(path) { |
67 | 0 | Err(ref e) if e.kind() == io::ErrorKind::AlreadyExists && num_retries > 1 => continue, |
68 | | // AddrInUse can happen if we're creating a UNIX domain socket and |
69 | | // the path already exists. |
70 | 0 | Err(ref e) if e.kind() == io::ErrorKind::AddrInUse && num_retries > 1 => continue, |
71 | 0 | res => res, |
72 | | }; |
73 | | } |
74 | | |
75 | 0 | Err(io::Error::new( |
76 | 0 | io::ErrorKind::AlreadyExists, |
77 | 0 | "too many temporary files exist", |
78 | 0 | )) |
79 | 0 | .with_err_path(|| base) |
80 | 0 | } Unexecuted instantiation: tempfile::util::create_helper::<tempfile::dir::TempDir, <tempfile::Builder>::tempdir_in<&std::path::Path>::{closure#0}>Unexecuted instantiation: tempfile::util::create_helper::<tempfile::dir::TempDir, <tempfile::Builder>::tempdir_in<std::path::PathBuf>::{closure#0}>Unexecuted instantiation: tempfile::util::create_helper::<tempfile::file::NamedTempFile, <tempfile::Builder>::tempfile_in<std::path::PathBuf>::{closure#0}>Unexecuted instantiation: tempfile::util::create_helper::<std::fs::File, tempfile::file::imp::platform::create_unix::{closure#0}> |