diff options
author | ud2 <sjx233@qq.com> | 2023-03-14 03:54:34 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-13 20:54:34 +0100 |
commit | 3db03abf880ae1217d49d8a530a9dfa3f3760e51 (patch) | |
tree | e070667308d7f45a38a81aa5ab1e4c564acc947f /ext/fs/lib.rs | |
parent | b9d2ac32d54110636a3abd7863ed00af030fec84 (diff) |
fix(ext/fs): retry if file already exists in makeTempFile (#17787)
Closes #17781.
Diffstat (limited to 'ext/fs/lib.rs')
-rw-r--r-- | ext/fs/lib.rs | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/ext/fs/lib.rs b/ext/fs/lib.rs index 26904e7fe..387241ee7 100644 --- a/ext/fs/lib.rs +++ b/ext/fs/lib.rs @@ -1875,7 +1875,8 @@ fn make_temp( } .join("_"); let mut rng = thread_rng(); - loop { + const MAX_TRIES: u32 = 10; + for _ in 0..MAX_TRIES { let unique = rng.gen::<u32>(); buf.set_file_name(format!("{prefix_}{unique:08x}{suffix_}")); let r = if is_dir { @@ -1895,8 +1896,7 @@ fn make_temp( use std::os::unix::fs::OpenOptionsExt; open_options.mode(0o600); } - open_options.open(buf.as_path())?; - Ok(()) + open_options.open(buf.as_path()).map(drop) }; match r { Err(ref e) if e.kind() == std::io::ErrorKind::AlreadyExists => continue, @@ -1904,6 +1904,10 @@ fn make_temp( Err(e) => return Err(e), } } + Err(io::Error::new( + io::ErrorKind::AlreadyExists, + "too many temp files exist", + )) } #[derive(Deserialize)] |