Skip to content

Commit 8fbdc04

Browse files
committed
Auto merge of #128977 - jieyouxu:writable-file, r=Kobzol
Only try to modify file times of a writable file on Windows Introduces a `set_file_times` helper which opens a given path as a file in r+w mode on Windows and then sets file times. Previously the file was open as read-only for Windows which caused permission errors locally. This should hopefully make setting file times less error prone, since trying to set file times on read-only file on Windows also happened in #127850. try-job: dist-loongarch64-musl try-job: x86_64-msvc
2 parents d626fbd + 26fae1e commit 8fbdc04

File tree

4 files changed

+45
-11
lines changed

4 files changed

+45
-11
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

+8-7
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;
@@ -1792,21 +1794,20 @@ Executed at: {executed_at}"#,
17921794
}
17931795
}
17941796
if let Ok(()) = fs::hard_link(&src, dst) {
1795-
// Attempt to "easy copy" by creating a hard link
1796-
// (symlinks don't work on windows), but if that fails
1797-
// just fall back to a slow `copy` operation.
1797+
// Attempt to "easy copy" by creating a hard link (symlinks are priviledged on windows),
1798+
// but if that fails just fall back to a slow `copy` operation.
17981799
} else {
17991800
if let Err(e) = fs::copy(&src, dst) {
18001801
panic!("failed to copy `{}` to `{}`: {}", src.display(), dst.display(), e)
18011802
}
18021803
t!(fs::set_permissions(dst, metadata.permissions()));
18031804

1805+
// Restore file times because changing permissions on e.g. Linux using `chmod` can cause
1806+
// file access time to change.
18041807
let file_times = fs::FileTimes::new()
18051808
.set_accessed(t!(metadata.accessed()))
18061809
.set_modified(t!(metadata.modified()));
1807-
1808-
let dst_file = t!(fs::File::open(dst));
1809-
t!(dst_file.set_times(file_times));
1810+
t!(set_file_times(dst, file_times));
18101811
}
18111812
}
18121813

src/bootstrap/src/utils/helpers.rs

+12
Original file line numberDiff line numberDiff line change
@@ -544,3 +544,15 @@ 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+
// Windows requires file to be writable to modify file times. But on Linux CI the file does not
551+
// need to be writable to modify file times and might be read-only.
552+
let f = if cfg!(windows) {
553+
fs::File::options().write(true).open(path)?
554+
} else {
555+
fs::File::open(path)?
556+
};
557+
f.set_times(times)
558+
}

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

+24-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,25 @@ 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 =
100+
Config::parse(Flags::parse(&["check".to_owned(), "--config=/does/not/exist".to_owned()]));
101+
let tempfile = config.tempdir().join(".tmp-file");
102+
103+
{
104+
File::create(&tempfile).unwrap().write_all(b"dummy value").unwrap();
105+
assert!(tempfile.exists());
106+
}
107+
108+
// This might only fail on Windows (if file is default read-only then we try to modify file
109+
// times).
110+
let unix_epoch = std::time::SystemTime::UNIX_EPOCH;
111+
let target_time = fs::FileTimes::new().set_accessed(unix_epoch).set_modified(unix_epoch);
112+
set_file_times(&tempfile, target_time).unwrap();
113+
114+
let found_metadata = fs::metadata(tempfile).unwrap();
115+
assert_eq!(found_metadata.accessed().unwrap(), unix_epoch);
116+
assert_eq!(found_metadata.modified().unwrap(), unix_epoch)
117+
}

0 commit comments

Comments
 (0)