Skip to content

Commit

Permalink
Use an helper to move the files
Browse files Browse the repository at this point in the history
In case the source is not in the same filesystem.
  • Loading branch information
lu-zero committed May 10, 2024
1 parent cf77474 commit 9092ac7
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
6 changes: 4 additions & 2 deletions src/bootstrap/src/core/build_steps/dist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ use crate::core::build_steps::tool::{self, Tool};
use crate::core::builder::{Builder, Kind, RunConfig, ShouldRun, Step};
use crate::core::config::TargetSelection;
use crate::utils::channel::{self, Info};
use crate::utils::helpers::{exe, is_dylib, output, t, target_supports_cranelift_backend, timeit};
use crate::utils::helpers::{
exe, is_dylib, move_file, output, t, target_supports_cranelift_backend, timeit,
};
use crate::utils::tarball::{GeneratedTarball, OverlayKind, Tarball};
use crate::{Compiler, DependencyType, Mode, LLVM_TOOLS};

Expand Down Expand Up @@ -2024,7 +2026,7 @@ impl Step for Extended {
builder.run(&mut cmd);

if !builder.config.dry_run() {
t!(fs::rename(exe.join(&filename), distdir(builder).join(&filename)));
t!(move_file(exe.join(&filename), distdir(builder).join(&filename)));
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/bootstrap/src/core/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use build_helper::ci::CiEnv;
use xz2::bufread::XzDecoder;

use crate::core::config::RustfmtMetadata;
use crate::utils::helpers::{check_run, exe, program_out_of_date};
use crate::utils::helpers::{check_run, exe, move_file, program_out_of_date};
use crate::{core::build_steps::llvm::detect_llvm_sha, utils::helpers::hex_encode};
use crate::{t, Config};

Expand Down Expand Up @@ -209,7 +209,7 @@ impl Config {
None => panic!("no protocol in {url}"),
}
t!(
std::fs::rename(&tempfile, dest_path),
move_file(&tempfile, dest_path),
format!("failed to rename {tempfile:?} to {dest_path:?}")
);
}
Expand Down Expand Up @@ -313,7 +313,7 @@ impl Config {
if src_path.is_dir() && dst_path.exists() {
continue;
}
t!(fs::rename(src_path, dst_path));
t!(move_file(src_path, dst_path));
}
let dst_dir = dst.join(directory_prefix);
if dst_dir.exists() {
Expand Down
11 changes: 11 additions & 0 deletions src/bootstrap/src/utils/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,17 @@ pub fn symlink_dir(config: &Config, original: &Path, link: &Path) -> io::Result<
}
}

/// Rename a file if from and to are in the same filesystem or
/// copy and remove the file otherwise
pub fn move_file<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<()> {
if fs::rename(&from, &to).is_err() {
std::fs::copy(&from, &to)?;
std::fs::remove_file(&from)?;
}

Ok(())
}

pub fn forcing_clang_based_tests() -> bool {
if let Some(var) = env::var_os("RUSTBUILD_FORCE_CLANG_BASED_TESTS") {
match &var.to_string_lossy().to_lowercase()[..] {
Expand Down
4 changes: 2 additions & 2 deletions src/bootstrap/src/utils/tarball.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::{
use crate::core::builder::Builder;
use crate::core::{build_steps::dist::distdir, builder::Kind};
use crate::utils::channel;
use crate::utils::helpers::t;
use crate::utils::helpers::{move_file, t};

#[derive(Copy, Clone)]
pub(crate) enum OverlayKind {
Expand Down Expand Up @@ -284,7 +284,7 @@ impl<'a> Tarball<'a> {
// name, not "image". We rename the image directory just before passing
// into rust-installer.
let dest = self.temp_dir.join(self.package_name());
t!(std::fs::rename(&self.image_dir, &dest));
t!(move_file(&self.image_dir, &dest));

self.run(|this, cmd| {
let distdir = distdir(this.builder);
Expand Down

0 comments on commit 9092ac7

Please sign in to comment.