diff options
| author | bjorn3 <17426603+bjorn3@users.noreply.github.com> | 2024-09-14 17:34:24 +0000 |
|---|---|---|
| committer | bjorn3 <17426603+bjorn3@users.noreply.github.com> | 2024-09-14 17:34:24 +0000 |
| commit | 6fbe4d90b83dad1823a5d0944b8e9b25d764d24b (patch) | |
| tree | 2a4c4aec8c5a065b163ca8dd35ce4f120a4dc64a | |
| parent | b7272c236a2d308cd1ee0792d60d95cbf6797f46 (diff) | |
| download | rust-6fbe4d90b83dad1823a5d0944b8e9b25d764d24b.tar.gz rust-6fbe4d90b83dad1823a5d0944b8e9b25d764d24b.zip | |
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.
| -rw-r--r-- | build_system/build_sysroot.rs | 7 | ||||
| -rw-r--r-- | build_system/path.rs | 5 | ||||
| -rw-r--r-- | build_system/prepare.rs | 5 | ||||
| -rw-r--r-- | build_system/utils.rs | 32 |
4 files changed, 34 insertions, 15 deletions
diff --git a/build_system/build_sysroot.rs b/build_system/build_sysroot.rs index ce6a3d28835..6a4f2a6c72c 100644 --- a/build_system/build_sysroot.rs +++ b/build_system/build_sysroot.rs @@ -6,7 +6,7 @@ use crate::path::{Dirs, RelPath}; 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 diff --git a/build_system/path.rs b/build_system/path.rs index 8572815fc55..35e7e81c528 100644 --- a/build_system/path.rs +++ b/build_system/path.rs @@ -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 @@ impl RelPath { 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); } } diff --git a/build_system/prepare.rs b/build_system/prepare.rs index ea03870f6e5..b9cfcd99a77 100644 --- a/build_system/prepare.rs +++ b/build_system/prepare.rs @@ -5,7 +5,7 @@ use std::path::{Path, PathBuf}; 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); diff --git a/build_system/utils.rs b/build_system/utils.rs index 89264f04299..22a9487a202 100644 --- a/build_system/utils.rs +++ b/build_system/utils.rs @@ -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()), + } + } } } |
