Skip to content

Commit 0a62bc4

Browse files
authored
Rollup merge of rust-lang#39439 - king6cong:move, r=alexcrichton
rename other than copy/remove
2 parents 9c8cdb2 + 5a21f42 commit 0a62bc4

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

src/librustc/util/fs.rs

+32-5
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use std::io;
3131
// https://github.com/rust-lang/rust/issues/25505#issuecomment-102876737
3232
pub fn fix_windows_verbatim_for_gcc(p: &Path) -> PathBuf {
3333
if !cfg!(windows) {
34-
return p.to_path_buf()
34+
return p.to_path_buf();
3535
}
3636
let mut components = p.components();
3737
let prefix = match components.next() {
@@ -58,7 +58,7 @@ pub fn fix_windows_verbatim_for_gcc(p: &Path) -> PathBuf {
5858

5959
pub enum LinkOrCopy {
6060
Link,
61-
Copy
61+
Copy,
6262
}
6363

6464
/// Copy `p` into `q`, preferring to use hard-linking if possible. If
@@ -76,7 +76,35 @@ pub fn link_or_copy<P: AsRef<Path>, Q: AsRef<Path>>(p: P, q: Q) -> io::Result<Li
7676
Err(_) => {
7777
match fs::copy(p, q) {
7878
Ok(_) => Ok(LinkOrCopy::Copy),
79-
Err(e) => Err(e)
79+
Err(e) => Err(e),
80+
}
81+
}
82+
}
83+
}
84+
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
93+
/// filesystem), fallback to copy & remove
94+
pub fn rename_or_copy_remove<P: AsRef<Path>, Q: AsRef<Path>>(p: P,
95+
q: Q)
96+
-> io::Result<RenameOrCopyRemove> {
97+
let p = p.as_ref();
98+
let q = q.as_ref();
99+
match fs::rename(p, q) {
100+
Ok(()) => Ok(RenameOrCopyRemove::Rename),
101+
Err(_) => {
102+
match fs::copy(p, q) {
103+
Ok(_) => {
104+
fs::remove_file(p)?;
105+
Ok(RenameOrCopyRemove::CopyRemove)
106+
}
107+
Err(e) => Err(e),
80108
}
81109
}
82110
}
@@ -93,8 +121,7 @@ pub fn create_dir_racy(path: &Path) -> io::Result<()> {
93121
}
94122
match path.parent() {
95123
Some(p) => try!(create_dir_racy(p)),
96-
None => return Err(io::Error::new(io::ErrorKind::Other,
97-
"failed to create whole tree")),
124+
None => return Err(io::Error::new(io::ErrorKind::Other, "failed to create whole tree")),
98125
}
99126
match fs::create_dir(path) {
100127
Ok(()) => Ok(()),

src/librustc_driver/driver.rs

+2-2
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,10 +1085,9 @@ 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::copy(&f,
1088+
rename_or_copy_remove(&f,
10881089
f.with_file_name(format!("{}.0.o",
10891090
f.file_stem().unwrap().to_string_lossy()))).unwrap();
1090-
fs::remove_file(f).unwrap();
10911091
}
10921092

10931093
// Remove assembly source, unless --save-temps was specified

0 commit comments

Comments
 (0)