Skip to content

Commit c3fa16e

Browse files
committed
Fix a couple of TOCTOU occurences
1 parent 248a67a commit c3fa16e

File tree

4 files changed

+28
-19
lines changed

4 files changed

+28
-19
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;
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+
utils::remove_dir_if_exists(&path);
6767
fs::create_dir_all(path).unwrap();
6868
}
6969
}

build_system/prepare.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,17 @@ use std::fs;
33
use std::path::{Path, PathBuf};
44
use std::process::{Command, Stdio};
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;
11-
use super::utils::{copy_dir_recursively, git_command, retry_spawn_and_wait, spawn_and_wait};
8+
use super::rustc_info::{get_default_sysroot, get_rustc_version};
9+
use super::utils::{
10+
copy_dir_recursively, git_command, remove_file_if_exists, retry_spawn_and_wait, spawn_and_wait,
11+
};
1212

1313
pub(crate) fn prepare(dirs: &Dirs, vendor: bool) {
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();
14+
RelPath::DOWNLOAD.ensure_fresh(dirs);
1815

19-
if Path::new(".cargo/config.toml").exists() {
20-
std::fs::remove_file(".cargo/config.toml").unwrap();
21-
}
16+
remove_file_if_exists(Path::new(".cargo/config.toml"));
2217

2318
let mut cargo_workspaces = vec![super::build_backend::CG_CLIF.manifest_path(dirs)];
2419

build_system/utils.rs

Lines changed: 17 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

@@ -237,6 +237,22 @@ pub(crate) fn spawn_and_wait_with_input(mut cmd: Command, input: String) -> Stri
237237
String::from_utf8(output.stdout).unwrap()
238238
}
239239

240+
pub(crate) fn remove_file_if_exists(path: &Path) {
241+
match fs::remove_file(&path) {
242+
Ok(()) => {}
243+
Err(err) if err.kind() == io::ErrorKind::NotFound => {}
244+
Err(err) => panic!("Failed to remove {path}: {err}", path = path.display()),
245+
}
246+
}
247+
248+
pub(crate) fn remove_dir_if_exists(path: &Path) {
249+
match fs::remove_dir_all(&path) {
250+
Ok(()) => {}
251+
Err(err) if err.kind() == io::ErrorKind::NotFound => {}
252+
Err(err) => panic!("Failed to remove {path}: {err}", path = path.display()),
253+
}
254+
}
255+
240256
pub(crate) fn copy_dir_recursively(from: &Path, to: &Path) {
241257
for entry in fs::read_dir(from).unwrap() {
242258
let entry = entry.unwrap();

0 commit comments

Comments
 (0)