1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-21 21:50:00 -05:00

fix(ext/fs): retry if file already exists in makeTempFile (#17787)

Closes #17781.
This commit is contained in:
ud2 2023-03-14 03:54:34 +08:00 committed by GitHub
parent b9d2ac32d5
commit 3db03abf88
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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)]