File tree Expand file tree Collapse file tree 2 files changed +19
-1
lines changed
compiler/rustc_interface/src Expand file tree Collapse file tree 2 files changed +19
-1
lines changed Original file line number Diff line number Diff line change @@ -983,7 +983,7 @@ fn encode_and_write_metadata(
983983 . unwrap_or_else ( |err| tcx. sess . fatal ( & format ! ( "couldn't create a temp dir: {}" , err) ) ) ;
984984 let metadata_tmpdir = MaybeTempDir :: new ( metadata_tmpdir, tcx. sess . opts . cg . save_temps ) ;
985985 let metadata_filename = emit_metadata ( tcx. sess , & metadata, & metadata_tmpdir) ;
986- if let Err ( e) = fs :: rename ( & metadata_filename, & out_filename) {
986+ if let Err ( e) = util :: non_durable_rename ( & metadata_filename, & out_filename) {
987987 tcx. sess . fatal ( & format ! ( "failed to write {}: {}" , out_filename. display( ) , e) ) ;
988988 }
989989 if tcx. sess . opts . json_artifact_notifications {
Original file line number Diff line number Diff line change @@ -670,6 +670,24 @@ pub fn build_output_filenames(
670670 }
671671}
672672
673+ #[ cfg( not( target_os = "linux" ) ) ]
674+ pub fn non_durable_rename ( src : & Path , dst : & Path ) -> std:: io:: Result < ( ) > {
675+ std:: fs:: rename ( src, dst)
676+ }
677+
678+ /// This function attempts to bypass the auto_da_alloc heuristic implemented by some filesystems
679+ /// such as btrfs and ext4. When renaming over a file that already exists then they will "helpfully"
680+ /// write back the source file before committing the rename in case a developer forgot some of
681+ /// the fsyncs in the open/write/fsync(file)/rename/fsync(dir) dance for atomic file updates.
682+ ///
683+ /// To avoid triggering this heuristic we delete the destination first, if it exists.
684+ /// The cost of an extra syscall is much lower than getting descheduled for the sync IO.
685+ #[ cfg( target_os = "linux" ) ]
686+ pub fn non_durable_rename ( src : & Path , dst : & Path ) -> std:: io:: Result < ( ) > {
687+ let _ = std:: fs:: remove_file ( dst) ;
688+ std:: fs:: rename ( src, dst)
689+ }
690+
673691// Note: Also used by librustdoc, see PR #43348. Consider moving this struct elsewhere.
674692//
675693// FIXME: Currently the `everybody_loops` transformation is not applied to:
You can’t perform that action at this time.
0 commit comments