@@ -31,7 +31,7 @@ use std::io;
3131// https://github.com/rust-lang/rust/issues/25505#issuecomment-102876737
3232pub 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
5959pub 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 ( ( ) ) ,
0 commit comments