@@ -31,7 +31,7 @@ use std::io;
31
31
// https://github.com/rust-lang/rust/issues/25505#issuecomment-102876737
32
32
pub fn fix_windows_verbatim_for_gcc ( p : & Path ) -> PathBuf {
33
33
if !cfg ! ( windows) {
34
- return p. to_path_buf ( )
34
+ return p. to_path_buf ( ) ;
35
35
}
36
36
let mut components = p. components ( ) ;
37
37
let prefix = match components. next ( ) {
@@ -58,7 +58,7 @@ pub fn fix_windows_verbatim_for_gcc(p: &Path) -> PathBuf {
58
58
59
59
pub enum LinkOrCopy {
60
60
Link ,
61
- Copy
61
+ Copy ,
62
62
}
63
63
64
64
/// 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
76
76
Err ( _) => {
77
77
match fs:: copy ( p, q) {
78
78
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) ,
80
108
}
81
109
}
82
110
}
@@ -93,8 +121,7 @@ pub fn create_dir_racy(path: &Path) -> io::Result<()> {
93
121
}
94
122
match path. parent ( ) {
95
123
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" ) ) ,
98
125
}
99
126
match fs:: create_dir ( path) {
100
127
Ok ( ( ) ) => Ok ( ( ) ) ,
0 commit comments