Auto merge of #8185 - ehuss:allow-mtime-failure, r=alexcrichton

Allow failure when setting file mtime.

Some filesystems do not allow changing file mtimes (most recent example is a Windows mount in WSL). Since these operations are not critical to Cargo's operation, errors should probably just be ignored. (Currently they are used for mid-build modification protection, and the unstable mtime-on-use).

Fixes #8184
This commit is contained in:
bors 2020-04-30 19:24:28 +00:00
commit ba832aca75
4 changed files with 25 additions and 9 deletions

View File

@ -387,12 +387,9 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
// state informing what variables were discovered via our script as // state informing what variables were discovered via our script as
// well. // well.
paths::write(&output_file, &output.stdout)?; paths::write(&output_file, &output.stdout)?;
log::debug!( // This mtime shift allows Cargo to detect if a source file was
"rewinding custom script output mtime {:?} to {}", // modified in the middle of the build.
output_file, paths::set_file_time_no_err(output_file, timestamp);
timestamp
);
filetime::set_file_times(output_file, timestamp, timestamp)?;
paths::write(&err_file, &output.stderr)?; paths::write(&err_file, &output.stderr)?;
paths::write(&root_output_file, util::path2bytes(&script_out_dir)?)?; paths::write(&root_output_file, util::path2bytes(&script_out_dir)?)?;
let parsed_output = let parsed_output =

View File

@ -1537,7 +1537,7 @@ fn compare_old_fingerprint(
// update the mtime so other cleaners know we used it // update the mtime so other cleaners know we used it
let t = FileTime::from_system_time(SystemTime::now()); let t = FileTime::from_system_time(SystemTime::now());
debug!("mtime-on-use forcing {:?} to {}", loc, t); debug!("mtime-on-use forcing {:?} to {}", loc, t);
filetime::set_file_times(loc, t, t)?; paths::set_file_time_no_err(loc, t);
} }
let new_hash = new_fingerprint.hash(); let new_hash = new_fingerprint.hash();

View File

@ -321,8 +321,9 @@ fn rustc(cx: &mut Context<'_, '_>, unit: &Unit, exec: &Arc<dyn Executor>) -> Car
rustc_dep_info_loc.display() rustc_dep_info_loc.display()
)) ))
})?; })?;
debug!("rewinding mtime of {:?} to {}", dep_info_loc, timestamp); // This mtime shift allows Cargo to detect if a source file was
filetime::set_file_times(dep_info_loc, timestamp, timestamp)?; // modified in the middle of the build.
paths::set_file_time_no_err(dep_info_loc, timestamp);
} }
Ok(()) Ok(())

View File

@ -393,3 +393,21 @@ fn _link_or_copy(src: &Path, dst: &Path) -> CargoResult<()> {
})?; })?;
Ok(()) Ok(())
} }
/// Changes the filesystem mtime (and atime if possible) for the given file.
///
/// This intentionally does not return an error, as this is sometimes not
/// supported on network filesystems. For the current uses in Cargo, this is a
/// "best effort" approach, and errors shouldn't be propagated.
pub fn set_file_time_no_err<P: AsRef<Path>>(path: P, time: FileTime) {
let path = path.as_ref();
match filetime::set_file_times(path, time, time) {
Ok(()) => log::debug!("set file mtime {} to {}", path.display(), time),
Err(e) => log::warn!(
"could not set mtime of {} to {}: {:?}",
path.display(),
time,
e
),
}
}