/rust/registry/src/index.crates.io-1949cf8c6b5b557f/tempfile-3.25.0/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 | 7.75k | fn tmpname(rng: &mut fastrand::Rng, prefix: &OsStr, suffix: &OsStr, rand_len: usize) -> OsString { |
8 | 7.75k | let capacity = prefix |
9 | 7.75k | .len() |
10 | 7.75k | .saturating_add(suffix.len()) |
11 | 7.75k | .saturating_add(rand_len); |
12 | 7.75k | let mut buf = OsString::with_capacity(capacity); |
13 | 7.75k | buf.push(prefix); |
14 | 7.75k | let mut char_buf = [0u8; 4]; |
15 | 46.5k | for c in repeat_with(|| rng.alphanumeric()).take(rand_len) { |
16 | 46.5k | buf.push(c.encode_utf8(&mut char_buf)); |
17 | 46.5k | } |
18 | 7.75k | buf.push(suffix); |
19 | 7.75k | buf |
20 | 7.75k | } |
21 | | |
22 | 7.75k | pub fn create_helper<R>( |
23 | 7.75k | base: &Path, |
24 | 7.75k | prefix: &OsStr, |
25 | 7.75k | suffix: &OsStr, |
26 | 7.75k | random_len: usize, |
27 | 7.75k | mut f: impl FnMut(PathBuf) -> io::Result<R>, |
28 | 7.75k | ) -> 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 | 7.75k | let mut base = base; // re-borrow to shrink lifetime |
32 | | let base_path_storage; // slot to store the absolute path, if necessary. |
33 | 7.75k | 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 | 7.75k | } |
38 | | |
39 | 7.75k | let num_retries = if random_len != 0 { |
40 | 7.75k | crate::NUM_RETRIES |
41 | | } else { |
42 | 0 | 1 |
43 | | }; |
44 | | |
45 | | // We fork the fastrand rng. |
46 | 7.75k | let mut rng = fastrand::Rng::new(); |
47 | 7.75k | 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 | 7.75k | if i == 3 { |
61 | 0 | if let Ok(seed) = getrandom::u64() { |
62 | 0 | rng.seed(seed); |
63 | 0 | } |
64 | 7.75k | } |
65 | 7.75k | let _ = i; // avoid unused variable warning for the above. |
66 | | |
67 | 7.75k | let path = base.join(tmpname(&mut rng, prefix, suffix, random_len)); |
68 | 7.75k | return match f(path) { |
69 | 0 | Err(ref e) if e.kind() == io::ErrorKind::AlreadyExists && num_retries > 1 => continue, |
70 | | // AddrInUse can happen if we're creating a UNIX domain socket and |
71 | | // the path already exists. |
72 | 0 | Err(ref e) if e.kind() == io::ErrorKind::AddrInUse && num_retries > 1 => continue, |
73 | 7.75k | res => res, |
74 | | }; |
75 | | } |
76 | | |
77 | 0 | Err(io::Error::new( |
78 | 0 | io::ErrorKind::AlreadyExists, |
79 | 0 | "too many temporary files exist", |
80 | 0 | )) |
81 | 0 | .with_err_path(|| base) |
82 | 7.75k | } tempfile::util::create_helper::<tempfile::dir::TempDir, <tempfile::Builder>::tempdir_in<std::path::PathBuf>::{closure#0}>Line | Count | Source | 22 | 4.98k | pub fn create_helper<R>( | 23 | 4.98k | base: &Path, | 24 | 4.98k | prefix: &OsStr, | 25 | 4.98k | suffix: &OsStr, | 26 | 4.98k | random_len: usize, | 27 | 4.98k | mut f: impl FnMut(PathBuf) -> io::Result<R>, | 28 | 4.98k | ) -> 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 | 4.98k | let mut base = base; // re-borrow to shrink lifetime | 32 | | let base_path_storage; // slot to store the absolute path, if necessary. | 33 | 4.98k | 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 | 4.98k | } | 38 | | | 39 | 4.98k | let num_retries = if random_len != 0 { | 40 | 4.98k | crate::NUM_RETRIES | 41 | | } else { | 42 | 0 | 1 | 43 | | }; | 44 | | | 45 | | // We fork the fastrand rng. | 46 | 4.98k | let mut rng = fastrand::Rng::new(); | 47 | 4.98k | 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 | 4.98k | if i == 3 { | 61 | 0 | if let Ok(seed) = getrandom::u64() { | 62 | 0 | rng.seed(seed); | 63 | 0 | } | 64 | 4.98k | } | 65 | 4.98k | let _ = i; // avoid unused variable warning for the above. | 66 | | | 67 | 4.98k | let path = base.join(tmpname(&mut rng, prefix, suffix, random_len)); | 68 | 4.98k | return match f(path) { | 69 | 0 | Err(ref e) if e.kind() == io::ErrorKind::AlreadyExists && num_retries > 1 => continue, | 70 | | // AddrInUse can happen if we're creating a UNIX domain socket and | 71 | | // the path already exists. | 72 | 0 | Err(ref e) if e.kind() == io::ErrorKind::AddrInUse && num_retries > 1 => continue, | 73 | 4.98k | res => res, | 74 | | }; | 75 | | } | 76 | | | 77 | 0 | Err(io::Error::new( | 78 | 0 | io::ErrorKind::AlreadyExists, | 79 | 0 | "too many temporary files exist", | 80 | 0 | )) | 81 | 0 | .with_err_path(|| base) | 82 | 4.98k | } |
tempfile::util::create_helper::<tempfile::file::NamedTempFile, <tempfile::Builder>::tempfile_in<std::path::PathBuf>::{closure#0}>Line | Count | Source | 22 | 2.76k | pub fn create_helper<R>( | 23 | 2.76k | base: &Path, | 24 | 2.76k | prefix: &OsStr, | 25 | 2.76k | suffix: &OsStr, | 26 | 2.76k | random_len: usize, | 27 | 2.76k | mut f: impl FnMut(PathBuf) -> io::Result<R>, | 28 | 2.76k | ) -> 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 | 2.76k | let mut base = base; // re-borrow to shrink lifetime | 32 | | let base_path_storage; // slot to store the absolute path, if necessary. | 33 | 2.76k | 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 | 2.76k | } | 38 | | | 39 | 2.76k | let num_retries = if random_len != 0 { | 40 | 2.76k | crate::NUM_RETRIES | 41 | | } else { | 42 | 0 | 1 | 43 | | }; | 44 | | | 45 | | // We fork the fastrand rng. | 46 | 2.76k | let mut rng = fastrand::Rng::new(); | 47 | 2.76k | 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 | 2.76k | if i == 3 { | 61 | 0 | if let Ok(seed) = getrandom::u64() { | 62 | 0 | rng.seed(seed); | 63 | 0 | } | 64 | 2.76k | } | 65 | 2.76k | let _ = i; // avoid unused variable warning for the above. | 66 | | | 67 | 2.76k | let path = base.join(tmpname(&mut rng, prefix, suffix, random_len)); | 68 | 2.76k | return match f(path) { | 69 | 0 | Err(ref e) if e.kind() == io::ErrorKind::AlreadyExists && num_retries > 1 => continue, | 70 | | // AddrInUse can happen if we're creating a UNIX domain socket and | 71 | | // the path already exists. | 72 | 0 | Err(ref e) if e.kind() == io::ErrorKind::AddrInUse && num_retries > 1 => continue, | 73 | 2.76k | res => res, | 74 | | }; | 75 | | } | 76 | | | 77 | 0 | Err(io::Error::new( | 78 | 0 | io::ErrorKind::AlreadyExists, | 79 | 0 | "too many temporary files exist", | 80 | 0 | )) | 81 | 0 | .with_err_path(|| base) | 82 | 2.76k | } |
Unexecuted instantiation: tempfile::util::create_helper::<std::fs::File, tempfile::file::imp::platform::create_unix::{closure#0}> |