From 5e41ec2cccb86db1a9677d2bfb9d2f623755f3de Mon Sep 17 00:00:00 2001 From: king6cong Date: Wed, 1 Feb 2017 11:49:24 +0800 Subject: [PATCH 1/3] rename other than copy/remove --- src/librustc_driver/driver.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/librustc_driver/driver.rs b/src/librustc_driver/driver.rs index a04a5b106b8f1..6fe6541c1d919 100644 --- a/src/librustc_driver/driver.rs +++ b/src/librustc_driver/driver.rs @@ -1084,10 +1084,9 @@ pub fn phase_5_run_llvm_passes(sess: &Session, // are going to build an executable if sess.opts.output_types.contains_key(&OutputType::Exe) { let f = outputs.path(OutputType::Object); - fs::copy(&f, + fs::rename(&f, f.with_file_name(format!("{}.0.o", f.file_stem().unwrap().to_string_lossy()))).unwrap(); - fs::remove_file(f).unwrap(); } // Remove assembly source, unless --save-temps was specified From 768f6a9b57c83030919adb30565918f1b520596f Mon Sep 17 00:00:00 2001 From: king6cong Date: Fri, 3 Feb 2017 17:42:01 +0800 Subject: [PATCH 2/3] add and use rename_or_copy_remove fn that fallback to copy & remove --- src/librustc/util/fs.rs | 25 +++++++++++++++++++++++++ src/librustc_driver/driver.rs | 3 ++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/librustc/util/fs.rs b/src/librustc/util/fs.rs index c290d8f893e9e..7f8a69de60723 100644 --- a/src/librustc/util/fs.rs +++ b/src/librustc/util/fs.rs @@ -82,6 +82,31 @@ pub fn link_or_copy, Q: AsRef>(p: P, q: Q) -> io::Result
  • , Q: AsRef>(p: P, q: Q) -> io::Result { + let p = p.as_ref(); + let q = q.as_ref(); + match fs::rename(p, q) { + Ok(()) => Ok(RenameOrCopyRemove::Rename), + Err(_) => { + match fs::copy(p, q) { + Ok(_) => { + fs::remove_file(p)?; + Ok(RenameOrCopyRemove::CopyRemove) + }, + Err(e) => Err(e) + } + } + } +} + // Like std::fs::create_dir_all, except handles concurrent calls among multiple // threads or processes. pub fn create_dir_racy(path: &Path) -> io::Result<()> { diff --git a/src/librustc_driver/driver.rs b/src/librustc_driver/driver.rs index 6fe6541c1d919..33bf4d5276adf 100644 --- a/src/librustc_driver/driver.rs +++ b/src/librustc_driver/driver.rs @@ -22,6 +22,7 @@ use rustc::middle::privacy::AccessLevels; use rustc::ty::{self, TyCtxt, Resolutions, GlobalArenas}; use rustc::util::common::time; use rustc::util::nodemap::{NodeSet, NodeMap}; +use rustc::util::fs::rename_or_copy_remove; use rustc_borrowck as borrowck; use rustc_incremental::{self, IncrementalHashesMap}; use rustc_incremental::ich::Fingerprint; @@ -1084,7 +1085,7 @@ pub fn phase_5_run_llvm_passes(sess: &Session, // are going to build an executable if sess.opts.output_types.contains_key(&OutputType::Exe) { let f = outputs.path(OutputType::Object); - fs::rename(&f, + rename_or_copy_remove(&f, f.with_file_name(format!("{}.0.o", f.file_stem().unwrap().to_string_lossy()))).unwrap(); } From 5a21f42cb7f193663fa7a3e95f27e4bf805b5be2 Mon Sep 17 00:00:00 2001 From: king6cong Date: Sat, 4 Feb 2017 10:21:26 +0800 Subject: [PATCH 3/3] run rustfmt for librustc/util/fs.rs --- src/librustc/util/fs.rs | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/librustc/util/fs.rs b/src/librustc/util/fs.rs index 7f8a69de60723..3b4b3998c5745 100644 --- a/src/librustc/util/fs.rs +++ b/src/librustc/util/fs.rs @@ -31,7 +31,7 @@ use std::io; // https://github.com/rust-lang/rust/issues/25505#issuecomment-102876737 pub fn fix_windows_verbatim_for_gcc(p: &Path) -> PathBuf { if !cfg!(windows) { - return p.to_path_buf() + return p.to_path_buf(); } let mut components = p.components(); let prefix = match components.next() { @@ -58,7 +58,7 @@ pub fn fix_windows_verbatim_for_gcc(p: &Path) -> PathBuf { pub enum LinkOrCopy { Link, - Copy + Copy, } /// Copy `p` into `q`, preferring to use hard-linking if possible. If @@ -76,7 +76,7 @@ pub fn link_or_copy, Q: AsRef>(p: P, q: Q) -> io::Result
  • { match fs::copy(p, q) { Ok(_) => Ok(LinkOrCopy::Copy), - Err(e) => Err(e) + Err(e) => Err(e), } } } @@ -85,12 +85,15 @@ pub fn link_or_copy, Q: AsRef>(p: P, q: Q) -> io::Result
  • , Q: AsRef>(p: P, q: Q) -> io::Result { +/// If `rename` fails (rename may fail for reasons such as crossing +/// filesystem), fallback to copy & remove +pub fn rename_or_copy_remove, Q: AsRef>(p: P, + q: Q) + -> io::Result { let p = p.as_ref(); let q = q.as_ref(); match fs::rename(p, q) { @@ -100,8 +103,8 @@ pub fn rename_or_copy_remove, Q: AsRef>(p: P, q: Q) -> io:: Ok(_) => { fs::remove_file(p)?; Ok(RenameOrCopyRemove::CopyRemove) - }, - Err(e) => Err(e) + } + Err(e) => Err(e), } } } @@ -118,8 +121,7 @@ pub fn create_dir_racy(path: &Path) -> io::Result<()> { } match path.parent() { Some(p) => try!(create_dir_racy(p)), - None => return Err(io::Error::new(io::ErrorKind::Other, - "failed to create whole tree")), + None => return Err(io::Error::new(io::ErrorKind::Other, "failed to create whole tree")), } match fs::create_dir(path) { Ok(()) => Ok(()),