Rename remove_dir_if_exists to ensure_empty_dir and create the dir in this function

This avoids removing the directory, which may conflict with sandbox
systems like Landlock.
This commit is contained in:
bjorn3 2024-09-14 17:34:24 +00:00
parent b7272c236a
commit 6fbe4d90b8
4 changed files with 34 additions and 15 deletions

View File

@ -6,7 +6,7 @@
use crate::prepare::apply_patches;
use crate::rustc_info::{get_default_sysroot, get_file_name};
use crate::utils::{
remove_dir_if_exists, spawn_and_wait, try_hard_link, CargoProject, Compiler, LogGroup,
ensure_empty_dir, spawn_and_wait, try_hard_link, CargoProject, Compiler, LogGroup,
};
use crate::{config, CodegenBackend, SysrootKind};
@ -24,8 +24,7 @@ pub(crate) fn build_sysroot(
let dist_dir = RelPath::DIST.to_path(dirs);
remove_dir_if_exists(&dist_dir);
fs::create_dir_all(&dist_dir).unwrap();
ensure_empty_dir(&dist_dir);
fs::create_dir_all(dist_dir.join("bin")).unwrap();
fs::create_dir_all(dist_dir.join("lib")).unwrap();
@ -223,7 +222,7 @@ fn build_clif_sysroot_for_triple(
if !config::get_bool("keep_sysroot") {
// Cleanup the deps dir, but keep build scripts and the incremental cache for faster
// recompilation as they are not affected by changes in cg_clif.
remove_dir_if_exists(&build_dir.join("deps"));
ensure_empty_dir(&build_dir.join("deps"));
}
// Build sysroot

View File

@ -1,7 +1,7 @@
use std::fs;
use std::path::PathBuf;
use crate::utils::remove_dir_if_exists;
use crate::utils::ensure_empty_dir;
#[derive(Debug, Clone)]
pub(crate) struct Dirs {
@ -64,7 +64,6 @@ pub(crate) fn ensure_exists(&self, dirs: &Dirs) {
pub(crate) fn ensure_fresh(&self, dirs: &Dirs) {
let path = self.to_path(dirs);
remove_dir_if_exists(&path);
fs::create_dir_all(path).unwrap();
ensure_empty_dir(&path);
}
}

View File

@ -5,7 +5,7 @@
use std::process::Command;
use crate::path::{Dirs, RelPath};
use crate::utils::{copy_dir_recursively, remove_dir_if_exists, spawn_and_wait};
use crate::utils::{copy_dir_recursively, ensure_empty_dir, spawn_and_wait};
pub(crate) fn prepare(dirs: &Dirs) {
RelPath::DOWNLOAD.ensure_exists(dirs);
@ -202,8 +202,7 @@ pub(crate) fn apply_patches(dirs: &Dirs, crate_name: &str, source_dir: &Path, ta
eprintln!("[COPY] {crate_name} source");
remove_dir_if_exists(target_dir);
fs::create_dir_all(target_dir).unwrap();
ensure_empty_dir(target_dir);
copy_dir_recursively(source_dir, target_dir);
init_git_repo(target_dir);

View File

@ -172,11 +172,33 @@ pub(crate) fn spawn_and_wait(mut cmd: Command) {
}
}
pub(crate) fn remove_dir_if_exists(path: &Path) {
match fs::remove_dir_all(&path) {
Ok(()) => {}
Err(err) if err.kind() == io::ErrorKind::NotFound => {}
Err(err) => panic!("Failed to remove {path}: {err}", path = path.display()),
/// Create the specified directory if it doesn't exist yet and delete all contents.
pub(crate) fn ensure_empty_dir(path: &Path) {
fs::create_dir_all(path).unwrap();
let read_dir = match fs::read_dir(&path) {
Ok(read_dir) => read_dir,
Err(err) if err.kind() == io::ErrorKind::NotFound => {
return;
}
Err(err) => {
panic!("Failed to read contents of {path}: {err}", path = path.display())
}
};
for entry in read_dir {
let entry = entry.unwrap();
if entry.file_type().unwrap().is_dir() {
match fs::remove_dir_all(entry.path()) {
Ok(()) => {}
Err(err) if err.kind() == io::ErrorKind::NotFound => {}
Err(err) => panic!("Failed to remove {path}: {err}", path = entry.path().display()),
}
} else {
match fs::remove_file(entry.path()) {
Ok(()) => {}
Err(err) if err.kind() == io::ErrorKind::NotFound => {}
Err(err) => panic!("Failed to remove {path}: {err}", path = entry.path().display()),
}
}
}
}