Skip to content

Commit 768f6a9

Browse files
committed
add and use rename_or_copy_remove fn that fallback to copy & remove
1 parent 5e41ec2 commit 768f6a9

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/librustc/util/fs.rs

+25
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,31 @@ pub fn link_or_copy<P: AsRef<Path>, Q: AsRef<Path>>(p: P, q: Q) -> io::Result<Li
8282
}
8383
}
8484

85+
#[derive(Debug)]
86+
pub enum RenameOrCopyRemove {
87+
Rename,
88+
CopyRemove
89+
}
90+
91+
/// Rename `p` into `q`, preferring to use `rename` if possible.
92+
/// If `rename` fails (rename may fail for reasons such as crossing filesystem), fallback to copy & remove
93+
pub fn rename_or_copy_remove<P: AsRef<Path>, Q: AsRef<Path>>(p: P, q: Q) -> io::Result<RenameOrCopyRemove> {
94+
let p = p.as_ref();
95+
let q = q.as_ref();
96+
match fs::rename(p, q) {
97+
Ok(()) => Ok(RenameOrCopyRemove::Rename),
98+
Err(_) => {
99+
match fs::copy(p, q) {
100+
Ok(_) => {
101+
fs::remove_file(p)?;
102+
Ok(RenameOrCopyRemove::CopyRemove)
103+
},
104+
Err(e) => Err(e)
105+
}
106+
}
107+
}
108+
}
109+
85110
// Like std::fs::create_dir_all, except handles concurrent calls among multiple
86111
// threads or processes.
87112
pub fn create_dir_racy(path: &Path) -> io::Result<()> {

src/librustc_driver/driver.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use rustc::middle::privacy::AccessLevels;
2222
use rustc::ty::{self, TyCtxt, Resolutions, GlobalArenas};
2323
use rustc::util::common::time;
2424
use rustc::util::nodemap::{NodeSet, NodeMap};
25+
use rustc::util::fs::rename_or_copy_remove;
2526
use rustc_borrowck as borrowck;
2627
use rustc_incremental::{self, IncrementalHashesMap};
2728
use rustc_incremental::ich::Fingerprint;
@@ -1084,7 +1085,7 @@ pub fn phase_5_run_llvm_passes(sess: &Session,
10841085
// are going to build an executable
10851086
if sess.opts.output_types.contains_key(&OutputType::Exe) {
10861087
let f = outputs.path(OutputType::Object);
1087-
fs::rename(&f,
1088+
rename_or_copy_remove(&f,
10881089
f.with_file_name(format!("{}.0.o",
10891090
f.file_stem().unwrap().to_string_lossy()))).unwrap();
10901091
}

0 commit comments

Comments
 (0)