Skip to content

Commit 9ef4afb

Browse files
authored
Rollup merge of rust-lang#128977 - jieyouxu:writable-file, r=Kobzol
Only try to modify file times of a writable file - First commit fixes a failure that I was running into locally trying to build a stage 1 `./x build library --stage 1` on windows due to trying to modify file times of a read-only file. - Second commit introduces a `set_file_times` helper which opens a given path as a file in r+w mode and then sets file times. This should hopefully make setting file times less error prone, since trying to set file times on read-only file also happened in rust-lang#127850. (And apparently it only fails locally on Windows or something weird like that.)
2 parents 03c2854 + 13c36f1 commit 9ef4afb

File tree

4 files changed

+32
-10
lines changed

4 files changed

+32
-10
lines changed

src/bootstrap/src/core/download.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -703,9 +703,7 @@ download-rustc = false
703703
let file_times = fs::FileTimes::new().set_accessed(now).set_modified(now);
704704

705705
let llvm_config = llvm_root.join("bin").join(exe("llvm-config", self.build));
706-
let llvm_config_file = t!(File::options().write(true).open(llvm_config));
707-
708-
t!(llvm_config_file.set_times(file_times));
706+
t!(crate::utils::helpers::set_file_times(llvm_config, file_times));
709707

710708
if self.should_fix_bins_and_dylibs() {
711709
let llvm_lib = llvm_root.join("lib");

src/bootstrap/src/lib.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ use crate::core::builder;
3737
use crate::core::builder::{Builder, Kind};
3838
use crate::core::config::{flags, DryRun, LldMode, LlvmLibunwind, Target, TargetSelection};
3939
use crate::utils::exec::{command, BehaviorOnFailure, BootstrapCommand, CommandOutput, OutputMode};
40-
use crate::utils::helpers::{self, dir_is_empty, exe, libdir, mtime, output, symlink_dir};
40+
use crate::utils::helpers::{
41+
self, dir_is_empty, exe, libdir, mtime, output, set_file_times, symlink_dir,
42+
};
4143

4244
mod core;
4345
mod utils;
@@ -1777,9 +1779,8 @@ Executed at: {executed_at}"#,
17771779
}
17781780
}
17791781
if let Ok(()) = fs::hard_link(&src, dst) {
1780-
// Attempt to "easy copy" by creating a hard link
1781-
// (symlinks don't work on windows), but if that fails
1782-
// just fall back to a slow `copy` operation.
1782+
// Attempt to "easy copy" by creating a hard link (symlinks are priviledged on windows),
1783+
// but if that fails just fall back to a slow `copy` operation.
17831784
} else {
17841785
if let Err(e) = fs::copy(&src, dst) {
17851786
panic!("failed to copy `{}` to `{}`: {}", src.display(), dst.display(), e)
@@ -1790,8 +1791,7 @@ Executed at: {executed_at}"#,
17901791
.set_accessed(t!(metadata.accessed()))
17911792
.set_modified(t!(metadata.modified()));
17921793

1793-
let dst_file = t!(fs::File::open(dst));
1794-
t!(dst_file.set_times(file_times));
1794+
t!(set_file_times(dst, file_times));
17951795
}
17961796
}
17971797

src/bootstrap/src/utils/helpers.rs

+6
Original file line numberDiff line numberDiff line change
@@ -544,3 +544,9 @@ pub fn get_closest_merge_base_commit(
544544

545545
Ok(output_result(git.as_command_mut())?.trim().to_owned())
546546
}
547+
548+
/// Sets the file times for a given file at `path`.
549+
pub fn set_file_times<P: AsRef<Path>>(path: P, times: fs::FileTimes) -> io::Result<()> {
550+
let f = fs::File::options().write(true).open(path)?;
551+
f.set_times(times)
552+
}

src/bootstrap/src/utils/helpers/tests.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ use std::io::Write;
33
use std::path::PathBuf;
44

55
use crate::utils::helpers::{
6-
check_cfg_arg, extract_beta_rev, hex_encode, make, program_out_of_date, symlink_dir,
6+
check_cfg_arg, extract_beta_rev, hex_encode, make, program_out_of_date, set_file_times,
7+
symlink_dir,
78
};
89
use crate::{Config, Flags};
910

@@ -92,3 +93,20 @@ fn test_symlink_dir() {
9293
#[cfg(not(windows))]
9394
fs::remove_file(link_path).unwrap();
9495
}
96+
97+
#[test]
98+
fn test_set_file_times_sanity_check() {
99+
let config = Config::parse(&["check".to_owned(), "--config=/does/not/exist".to_owned()]);
100+
let tempfile = config.tempdir().join(".tmp-file");
101+
102+
{
103+
File::create(&tempfile).unwrap().write_all(b"dummy value").unwrap();
104+
assert!(tempfile.exists());
105+
}
106+
107+
// This might only fail on Windows (if file is default read-only then we try to modify file
108+
// times).
109+
let now = std::time::SystemTime::now();
110+
let file_times = fs::FileTimes::new().set_accessed(now).set_modified(now);
111+
set_file_times(&tempfile, file_times).unwrap();
112+
}

0 commit comments

Comments
 (0)