]> git.lizzy.rs Git - rust.git/commitdiff
Fix a couple of TOCTOU occurences
authorbjorn3 <17426603+bjorn3@users.noreply.github.com>
Thu, 19 Jan 2023 14:51:43 +0000 (14:51 +0000)
committerbjorn3 <17426603+bjorn3@users.noreply.github.com>
Thu, 19 Jan 2023 15:58:40 +0000 (15:58 +0000)
build_system/build_sysroot.rs
build_system/path.rs
build_system/prepare.rs
build_system/utils.rs

index f52d34ffcd63f872918e3d8dd9b3569b5ac32f70..bd04fdbe304a3031cf7995133a306a04744ff118 100644 (file)
@@ -4,7 +4,7 @@
 
 use super::path::{Dirs, RelPath};
 use super::rustc_info::{get_file_name, get_rustc_version, get_toolchain_name};
-use super::utils::{spawn_and_wait, try_hard_link, CargoProject, Compiler};
+use super::utils::{remove_dir_if_exists, spawn_and_wait, try_hard_link, CargoProject, Compiler};
 use super::SysrootKind;
 
 static DIST_DIR: RelPath = RelPath::DIST;
@@ -230,9 +230,7 @@ fn build_clif_sysroot_for_triple(
     if !super::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.
-        if build_dir.join("deps").exists() {
-            fs::remove_dir_all(build_dir.join("deps")).unwrap();
-        }
+        remove_dir_if_exists(&build_dir.join("deps"));
     }
 
     // Build sysroot
index 35ab6f111fef40a2fedd4909a2f08af3769c8f20..3290723005dd92b939486489e4acda090b0a8ac0 100644 (file)
@@ -1,6 +1,8 @@
 use std::fs;
 use std::path::PathBuf;
 
+use super::utils::remove_dir_if_exists;
+
 #[derive(Debug, Clone)]
 pub(crate) struct Dirs {
     pub(crate) source_dir: PathBuf,
@@ -61,9 +63,7 @@ pub(crate) fn ensure_exists(&self, dirs: &Dirs) {
 
     pub(crate) fn ensure_fresh(&self, dirs: &Dirs) {
         let path = self.to_path(dirs);
-        if path.exists() {
-            fs::remove_dir_all(&path).unwrap();
-        }
+        remove_dir_if_exists(&path);
         fs::create_dir_all(path).unwrap();
     }
 }
index bc6c3223dc234b8ed207e0492f5198bcae249df3..f25a81dc23459f8e93e4204eb32efb2c80df12e8 100644 (file)
@@ -3,18 +3,13 @@
 use std::path::{Path, PathBuf};
 use std::process::Command;
 
-use crate::build_system::rustc_info::get_default_sysroot;
-
 use super::build_sysroot::{BUILD_SYSROOT, ORIG_BUILD_SYSROOT, SYSROOT_RUSTC_VERSION, SYSROOT_SRC};
 use super::path::{Dirs, RelPath};
-use super::rustc_info::get_rustc_version;
+use super::rustc_info::{get_default_sysroot, get_rustc_version};
 use super::utils::{copy_dir_recursively, git_command, retry_spawn_and_wait, spawn_and_wait};
 
 pub(crate) fn prepare(dirs: &Dirs) {
-    if RelPath::DOWNLOAD.to_path(dirs).exists() {
-        std::fs::remove_dir_all(RelPath::DOWNLOAD.to_path(dirs)).unwrap();
-    }
-    std::fs::create_dir_all(RelPath::DOWNLOAD.to_path(dirs)).unwrap();
+    RelPath::DOWNLOAD.ensure_fresh(dirs);
 
     spawn_and_wait(super::build_backend::CG_CLIF.fetch("cargo", dirs));
 
index 21bfb1b1f00f58454461b87ae1de1e67efd32a27..da2a94a0a4ff84a3b307c22213e1813fd03ca55f 100644 (file)
@@ -1,6 +1,6 @@
 use std::env;
 use std::fs;
-use std::io::Write;
+use std::io::{self, Write};
 use std::path::{Path, PathBuf};
 use std::process::{self, Command, Stdio};
 
@@ -246,6 +246,14 @@ pub(crate) fn spawn_and_wait_with_input(mut cmd: Command, input: String) -> Stri
     String::from_utf8(output.stdout).unwrap()
 }
 
+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()),
+    }
+}
+
 pub(crate) fn copy_dir_recursively(from: &Path, to: &Path) {
     for entry in fs::read_dir(from).unwrap() {
         let entry = entry.unwrap();