@@ -4,6 +4,8 @@ use std::convert::From;
4
4
use std:: fs:: { remove_file, File } ;
5
5
use std:: io:: { Read , Write } ;
6
6
use std:: path:: Path ;
7
+ #[ cfg( feature = "reflink" ) ]
8
+ use super :: RefLinkUsage ;
7
9
8
10
// Options and flags which can be used to configure how a file will be copied or moved.
9
11
#[ derive( Debug , Copy , Clone ) ]
@@ -14,6 +16,9 @@ pub struct CopyOptions {
14
16
pub skip_exist : bool ,
15
17
/// Sets buffer size for copy/move work only with receipt information about process work.
16
18
pub buffer_size : usize ,
19
+ /// Controls the usage of reflinks on filesystems supporting it.
20
+ #[ cfg( feature = "reflink" ) ]
21
+ pub reflink : RefLinkUsage ,
17
22
}
18
23
19
24
impl CopyOptions {
@@ -32,6 +37,8 @@ impl CopyOptions {
32
37
overwrite : false ,
33
38
skip_exist : false ,
34
39
buffer_size : 64000 , //64kb
40
+ #[ cfg( feature = "reflink" ) ]
41
+ reflink : RefLinkUsage :: Never ,
35
42
}
36
43
}
37
44
@@ -66,6 +73,8 @@ impl From<&super::dir::CopyOptions> for CopyOptions {
66
73
overwrite : dir_options. overwrite ,
67
74
skip_exist : dir_options. skip_exist ,
68
75
buffer_size : dir_options. buffer_size ,
76
+ #[ cfg( feature = "reflink" ) ]
77
+ reflink : dir_options. reflink ,
69
78
}
70
79
}
71
80
}
@@ -136,7 +145,24 @@ where
136
145
}
137
146
}
138
147
139
- Ok ( std:: fs:: copy ( from, to) ?)
148
+ Ok (
149
+ #[ cfg( not( feature = "reflink" ) ) ]
150
+ { std:: fs:: copy ( from, to) ? } ,
151
+
152
+ #[ cfg( feature = "reflink" ) ]
153
+ match options. reflink {
154
+ RefLinkUsage :: Never => std:: fs:: copy ( from, to) ?,
155
+
156
+ #[ cfg( feature = "reflink" ) ]
157
+ RefLinkUsage :: Auto => reflink:: reflink_or_copy ( from, to) ?. unwrap_or ( 0 ) ,
158
+
159
+ #[ cfg( feature = "reflink" ) ]
160
+ RefLinkUsage :: Always => {
161
+ reflink:: reflink ( from, to) ?;
162
+ 0
163
+ } ,
164
+ }
165
+ )
140
166
}
141
167
142
168
/// Copies the contents of one file to another file with information about progress.
@@ -205,6 +231,18 @@ where
205
231
err ! ( & msg, ErrorKind :: AlreadyExists ) ;
206
232
}
207
233
}
234
+
235
+ #[ cfg( feature = "reflink" ) ]
236
+ if options. reflink != RefLinkUsage :: Never {
237
+ match reflink:: reflink ( & from, & to) {
238
+ Ok ( ( ) ) => return Ok ( 0 ) ,
239
+ Err ( e) if options. reflink == RefLinkUsage :: Always => {
240
+ return Err ( :: std:: convert:: From :: from ( e) ) ;
241
+ } ,
242
+ Err ( _) => { /* continue with plain copy */ }
243
+ }
244
+ }
245
+
208
246
let mut file_from = File :: open ( from) ?;
209
247
let mut buf = vec ! [ 0 ; options. buffer_size] ;
210
248
let file_size = file_from. metadata ( ) ?. len ( ) ;
0 commit comments