Fix a couple of TOCTOU occurences

This commit is contained in:
bjorn3 2023-01-19 14:51:43 +00:00
parent 7095783268
commit 4e87f13054
4 changed files with 16 additions and 15 deletions

View 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

View 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();
}
}

View 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));

View 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();