Skip to content

Commit 4e87f13

Browse files
committed
Fix a couple of TOCTOU occurences
1 parent 7095783 commit 4e87f13

File tree

4 files changed

+16
-15
lines changed

4 files changed

+16
-15
lines changed

build_system/build_sysroot.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::process::{self, Command};
44

55
use super::path::{Dirs, RelPath};
66
use super::rustc_info::{get_file_name, get_rustc_version, get_toolchain_name};
7-
use super::utils::{spawn_and_wait, try_hard_link, CargoProject, Compiler};
7+
use super::utils::{remove_dir_if_exists, spawn_and_wait, try_hard_link, CargoProject, Compiler};
88
use super::SysrootKind;
99

1010
static DIST_DIR: RelPath = RelPath::DIST;
@@ -230,9 +230,7 @@ fn build_clif_sysroot_for_triple(
230230
if !super::config::get_bool("keep_sysroot") {
231231
// Cleanup the deps dir, but keep build scripts and the incremental cache for faster
232232
// recompilation as they are not affected by changes in cg_clif.
233-
if build_dir.join("deps").exists() {
234-
fs::remove_dir_all(build_dir.join("deps")).unwrap();
235-
}
233+
remove_dir_if_exists(&build_dir.join("deps"));
236234
}
237235

238236
// Build sysroot

build_system/path.rs

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

4+
use super::utils::remove_dir_if_exists;
5+
46
#[derive(Debug, Clone)]
57
pub(crate) struct Dirs {
68
pub(crate) source_dir: PathBuf,
@@ -61,9 +63,7 @@ impl RelPath {
6163

6264
pub(crate) fn ensure_fresh(&self, dirs: &Dirs) {
6365
let path = self.to_path(dirs);
64-
if path.exists() {
65-
fs::remove_dir_all(&path).unwrap();
66-
}
66+
remove_dir_if_exists(&path);
6767
fs::create_dir_all(path).unwrap();
6868
}
6969
}

build_system/prepare.rs

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

6-
use crate::build_system::rustc_info::get_default_sysroot;
7-
86
use super::build_sysroot::{BUILD_SYSROOT, ORIG_BUILD_SYSROOT, SYSROOT_RUSTC_VERSION, SYSROOT_SRC};
97
use super::path::{Dirs, RelPath};
10-
use super::rustc_info::get_rustc_version;
8+
use super::rustc_info::{get_default_sysroot, get_rustc_version};
119
use super::utils::{copy_dir_recursively, git_command, retry_spawn_and_wait, spawn_and_wait};
1210

1311
pub(crate) fn prepare(dirs: &Dirs) {
14-
if RelPath::DOWNLOAD.to_path(dirs).exists() {
15-
std::fs::remove_dir_all(RelPath::DOWNLOAD.to_path(dirs)).unwrap();
16-
}
17-
std::fs::create_dir_all(RelPath::DOWNLOAD.to_path(dirs)).unwrap();
12+
RelPath::DOWNLOAD.ensure_fresh(dirs);
1813

1914
spawn_and_wait(super::build_backend::CG_CLIF.fetch("cargo", dirs));
2015

build_system/utils.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::env;
22
use std::fs;
3-
use std::io::Write;
3+
use std::io::{self, Write};
44
use std::path::{Path, PathBuf};
55
use std::process::{self, Command, Stdio};
66

@@ -246,6 +246,14 @@ pub(crate) fn spawn_and_wait_with_input(mut cmd: Command, input: String) -> Stri
246246
String::from_utf8(output.stdout).unwrap()
247247
}
248248

249+
pub(crate) fn remove_dir_if_exists(path: &Path) {
250+
match fs::remove_dir_all(&path) {
251+
Ok(()) => {}
252+
Err(err) if err.kind() == io::ErrorKind::NotFound => {}
253+
Err(err) => panic!("Failed to remove {path}: {err}", path = path.display()),
254+
}
255+
}
256+
249257
pub(crate) fn copy_dir_recursively(from: &Path, to: &Path) {
250258
for entry in fs::read_dir(from).unwrap() {
251259
let entry = entry.unwrap();

0 commit comments

Comments
 (0)