Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ dependencies = [
name = "build_helper"
version = "0.1.0"
dependencies = [
"fastrand",
"serde",
"serde_derive",
]
Expand Down
7 changes: 7 additions & 0 deletions src/bootstrap/Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ dependencies = [
name = "build_helper"
version = "0.1.0"
dependencies = [
"fastrand",
"serde",
"serde_derive",
]
Expand Down Expand Up @@ -233,6 +234,12 @@ dependencies = [
"windows-sys 0.52.0",
]

[[package]]
name = "fastrand"
version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be"

[[package]]
name = "fd-lock"
version = "4.0.2"
Expand Down
1 change: 1 addition & 0 deletions src/build_helper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
fastrand = "2.3.0"
serde = "1"
serde_derive = "1"
27 changes: 26 additions & 1 deletion src/build_helper/src/fs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! Misc filesystem related helpers for use by bootstrap and tools.
use std::ffi::{OsStr, OsString};
use std::fs::Metadata;
use std::path::Path;
use std::{fs, io};
Expand Down Expand Up @@ -100,6 +101,30 @@ where

pub fn remove_and_create_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
let path = path.as_ref();
recursive_remove(path)?;

// Attempt to rename the directory in case removing fails.
// We allow either the rename to fail or the remove to fail but not both.
let rm_path = rand_name(path.as_os_str());
if fs::rename(path, &rm_path).is_err() {
// Rename failed, try to remove the original path
recursive_remove(&path)?;
} else {
// Rename succeeded, try to remove the renamed path
let _ = recursive_remove(&rm_path);
}
fs::create_dir_all(path)
}

fn rand_name(prefix: &OsStr) -> OsString {
let mut name: OsString = prefix.into();
name.push("-");
let mut rand_suffix = [0; 8];
for n in rand_suffix.iter_mut() {
*n = fastrand::alphanumeric() as u8;
}
// SAFETY: `fastrand::alphanumeric` only returns valid ascii.
// Since an `OsStr` is a superset of UTF-8, ascii must be a valid `OsStr`.
let ascii = unsafe { OsStr::from_encoded_bytes_unchecked(&rand_suffix) };
name.push(ascii);
name
}
Loading