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:
parent
b7272c236a
commit
6fbe4d90b8
4 changed files with 34 additions and 15 deletions
|
@ -6,7 +6,7 @@ use crate::path::{Dirs, RelPath};
|
||||||
use crate::prepare::apply_patches;
|
use crate::prepare::apply_patches;
|
||||||
use crate::rustc_info::{get_default_sysroot, get_file_name};
|
use crate::rustc_info::{get_default_sysroot, get_file_name};
|
||||||
use crate::utils::{
|
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};
|
use crate::{config, CodegenBackend, SysrootKind};
|
||||||
|
|
||||||
|
@ -24,8 +24,7 @@ pub(crate) fn build_sysroot(
|
||||||
|
|
||||||
let dist_dir = RelPath::DIST.to_path(dirs);
|
let dist_dir = RelPath::DIST.to_path(dirs);
|
||||||
|
|
||||||
remove_dir_if_exists(&dist_dir);
|
ensure_empty_dir(&dist_dir);
|
||||||
fs::create_dir_all(&dist_dir).unwrap();
|
|
||||||
fs::create_dir_all(dist_dir.join("bin")).unwrap();
|
fs::create_dir_all(dist_dir.join("bin")).unwrap();
|
||||||
fs::create_dir_all(dist_dir.join("lib")).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") {
|
if !config::get_bool("keep_sysroot") {
|
||||||
// Cleanup the deps dir, but keep build scripts and the incremental cache for faster
|
// 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.
|
// 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
|
// Build sysroot
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use crate::utils::remove_dir_if_exists;
|
use crate::utils::ensure_empty_dir;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub(crate) struct Dirs {
|
pub(crate) struct Dirs {
|
||||||
|
@ -64,7 +64,6 @@ impl RelPath {
|
||||||
|
|
||||||
pub(crate) fn ensure_fresh(&self, dirs: &Dirs) {
|
pub(crate) fn ensure_fresh(&self, dirs: &Dirs) {
|
||||||
let path = self.to_path(dirs);
|
let path = self.to_path(dirs);
|
||||||
remove_dir_if_exists(&path);
|
ensure_empty_dir(&path);
|
||||||
fs::create_dir_all(path).unwrap();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ use std::path::{Path, PathBuf};
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
|
|
||||||
use crate::path::{Dirs, RelPath};
|
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) {
|
pub(crate) fn prepare(dirs: &Dirs) {
|
||||||
RelPath::DOWNLOAD.ensure_exists(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");
|
eprintln!("[COPY] {crate_name} source");
|
||||||
|
|
||||||
remove_dir_if_exists(target_dir);
|
ensure_empty_dir(target_dir);
|
||||||
fs::create_dir_all(target_dir).unwrap();
|
|
||||||
copy_dir_recursively(source_dir, target_dir);
|
copy_dir_recursively(source_dir, target_dir);
|
||||||
|
|
||||||
init_git_repo(target_dir);
|
init_git_repo(target_dir);
|
||||||
|
|
|
@ -172,11 +172,33 @@ pub(crate) fn spawn_and_wait(mut cmd: Command) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn remove_dir_if_exists(path: &Path) {
|
/// Create the specified directory if it doesn't exist yet and delete all contents.
|
||||||
match fs::remove_dir_all(&path) {
|
pub(crate) fn ensure_empty_dir(path: &Path) {
|
||||||
Ok(()) => {}
|
fs::create_dir_all(path).unwrap();
|
||||||
Err(err) if err.kind() == io::ErrorKind::NotFound => {}
|
let read_dir = match fs::read_dir(&path) {
|
||||||
Err(err) => panic!("Failed to remove {path}: {err}", path = path.display()),
|
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()),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue