Skip to content

Commit 6fbe4d9

Browse files
committed
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.
1 parent b7272c2 commit 6fbe4d9

File tree

4 files changed

+34
-15
lines changed

4 files changed

+34
-15
lines changed

build_system/build_sysroot.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::path::{Dirs, RelPath};
66
use crate::prepare::apply_patches;
77
use crate::rustc_info::{get_default_sysroot, get_file_name};
88
use crate::utils::{
9-
remove_dir_if_exists, spawn_and_wait, try_hard_link, CargoProject, Compiler, LogGroup,
9+
ensure_empty_dir, spawn_and_wait, try_hard_link, CargoProject, Compiler, LogGroup,
1010
};
1111
use crate::{config, CodegenBackend, SysrootKind};
1212

@@ -24,8 +24,7 @@ pub(crate) fn build_sysroot(
2424

2525
let dist_dir = RelPath::DIST.to_path(dirs);
2626

27-
remove_dir_if_exists(&dist_dir);
28-
fs::create_dir_all(&dist_dir).unwrap();
27+
ensure_empty_dir(&dist_dir);
2928
fs::create_dir_all(dist_dir.join("bin")).unwrap();
3029
fs::create_dir_all(dist_dir.join("lib")).unwrap();
3130

@@ -223,7 +222,7 @@ fn build_clif_sysroot_for_triple(
223222
if !config::get_bool("keep_sysroot") {
224223
// Cleanup the deps dir, but keep build scripts and the incremental cache for faster
225224
// recompilation as they are not affected by changes in cg_clif.
226-
remove_dir_if_exists(&build_dir.join("deps"));
225+
ensure_empty_dir(&build_dir.join("deps"));
227226
}
228227

229228
// Build sysroot

build_system/path.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::fs;
22
use std::path::PathBuf;
33

4-
use crate::utils::remove_dir_if_exists;
4+
use crate::utils::ensure_empty_dir;
55

66
#[derive(Debug, Clone)]
77
pub(crate) struct Dirs {
@@ -64,7 +64,6 @@ impl RelPath {
6464

6565
pub(crate) fn ensure_fresh(&self, dirs: &Dirs) {
6666
let path = self.to_path(dirs);
67-
remove_dir_if_exists(&path);
68-
fs::create_dir_all(path).unwrap();
67+
ensure_empty_dir(&path);
6968
}
7069
}

build_system/prepare.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::path::{Path, PathBuf};
55
use std::process::Command;
66

77
use crate::path::{Dirs, RelPath};
8-
use crate::utils::{copy_dir_recursively, remove_dir_if_exists, spawn_and_wait};
8+
use crate::utils::{copy_dir_recursively, ensure_empty_dir, spawn_and_wait};
99

1010
pub(crate) fn prepare(dirs: &Dirs) {
1111
RelPath::DOWNLOAD.ensure_exists(dirs);
@@ -202,8 +202,7 @@ pub(crate) fn apply_patches(dirs: &Dirs, crate_name: &str, source_dir: &Path, ta
202202

203203
eprintln!("[COPY] {crate_name} source");
204204

205-
remove_dir_if_exists(target_dir);
206-
fs::create_dir_all(target_dir).unwrap();
205+
ensure_empty_dir(target_dir);
207206
copy_dir_recursively(source_dir, target_dir);
208207

209208
init_git_repo(target_dir);

build_system/utils.rs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,33 @@ pub(crate) fn spawn_and_wait(mut cmd: Command) {
172172
}
173173
}
174174

175-
pub(crate) fn remove_dir_if_exists(path: &Path) {
176-
match fs::remove_dir_all(&path) {
177-
Ok(()) => {}
178-
Err(err) if err.kind() == io::ErrorKind::NotFound => {}
179-
Err(err) => panic!("Failed to remove {path}: {err}", path = path.display()),
175+
/// Create the specified directory if it doesn't exist yet and delete all contents.
176+
pub(crate) fn ensure_empty_dir(path: &Path) {
177+
fs::create_dir_all(path).unwrap();
178+
let read_dir = match fs::read_dir(&path) {
179+
Ok(read_dir) => read_dir,
180+
Err(err) if err.kind() == io::ErrorKind::NotFound => {
181+
return;
182+
}
183+
Err(err) => {
184+
panic!("Failed to read contents of {path}: {err}", path = path.display())
185+
}
186+
};
187+
for entry in read_dir {
188+
let entry = entry.unwrap();
189+
if entry.file_type().unwrap().is_dir() {
190+
match fs::remove_dir_all(entry.path()) {
191+
Ok(()) => {}
192+
Err(err) if err.kind() == io::ErrorKind::NotFound => {}
193+
Err(err) => panic!("Failed to remove {path}: {err}", path = entry.path().display()),
194+
}
195+
} else {
196+
match fs::remove_file(entry.path()) {
197+
Ok(()) => {}
198+
Err(err) if err.kind() == io::ErrorKind::NotFound => {}
199+
Err(err) => panic!("Failed to remove {path}: {err}", path = entry.path().display()),
200+
}
201+
}
180202
}
181203
}
182204

0 commit comments

Comments
 (0)