@@ -26,7 +26,7 @@ use std::io::prelude::*;
26
26
#[ cfg( not( target_os = "redox" ) ) ]
27
27
use std:: os:: unix:: fs;
28
28
#[ cfg( not( target_os = "redox" ) ) ]
29
- use tempfile:: { self , NamedTempFile } ;
29
+ use tempfile:: NamedTempFile ;
30
30
31
31
#[ test]
32
32
#[ cfg( not( target_os = "redox" ) ) ]
@@ -227,6 +227,51 @@ fn test_readlink() {
227
227
) ;
228
228
}
229
229
230
+ /// This test creates a temporary file containing the contents
231
+ /// 'foobarbaz' and uses the `copy_file_range` call to transfer
232
+ /// 3 bytes at offset 3 (`bar`) to another empty file at offset 0. The
233
+ /// resulting file is read and should contain the contents `bar`.
234
+ /// The from_offset should be updated by the call to reflect
235
+ /// the 3 bytes read (6).
236
+ #[ cfg( any(
237
+ target_os = "linux" ,
238
+ // Not available until FreeBSD 13.0
239
+ all( target_os = "freebsd" , fbsd14) ,
240
+ target_os = "android"
241
+ ) ) ]
242
+ #[ test]
243
+ // QEMU does not support copy_file_range. Skip under qemu
244
+ #[ cfg_attr( qemu, ignore) ]
245
+ fn test_copy_file_range ( ) {
246
+ use std:: os:: unix:: io:: AsRawFd ;
247
+ use nix:: fcntl:: copy_file_range;
248
+
249
+ const CONTENTS : & [ u8 ] = b"foobarbaz" ;
250
+
251
+ let mut tmp1 = tempfile:: tempfile ( ) . unwrap ( ) ;
252
+ let mut tmp2 = tempfile:: tempfile ( ) . unwrap ( ) ;
253
+
254
+ tmp1. write_all ( CONTENTS ) . unwrap ( ) ;
255
+ tmp1. flush ( ) . unwrap ( ) ;
256
+
257
+ let mut from_offset: i64 = 3 ;
258
+ copy_file_range (
259
+ tmp1. as_raw_fd ( ) ,
260
+ Some ( & mut from_offset) ,
261
+ tmp2. as_raw_fd ( ) ,
262
+ None ,
263
+ 3 ,
264
+ )
265
+ . unwrap ( ) ;
266
+
267
+ let mut res: String = String :: new ( ) ;
268
+ tmp2. rewind ( ) . unwrap ( ) ;
269
+ tmp2. read_to_string ( & mut res) . unwrap ( ) ;
270
+
271
+ assert_eq ! ( res, String :: from( "bar" ) ) ;
272
+ assert_eq ! ( from_offset, 6 ) ;
273
+ }
274
+
230
275
#[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
231
276
mod linux_android {
232
277
use libc:: loff_t;
@@ -237,48 +282,12 @@ mod linux_android {
237
282
use nix:: fcntl:: * ;
238
283
use nix:: unistd:: { close, pipe, read, write} ;
239
284
240
- use tempfile:: tempfile;
241
285
#[ cfg( any( target_os = "linux" ) ) ]
242
286
use tempfile:: NamedTempFile ;
287
+ use tempfile:: tempfile;
243
288
244
289
use crate :: * ;
245
290
246
- /// This test creates a temporary file containing the contents
247
- /// 'foobarbaz' and uses the `copy_file_range` call to transfer
248
- /// 3 bytes at offset 3 (`bar`) to another empty file at offset 0. The
249
- /// resulting file is read and should contain the contents `bar`.
250
- /// The from_offset should be updated by the call to reflect
251
- /// the 3 bytes read (6).
252
- #[ test]
253
- // QEMU does not support copy_file_range. Skip under qemu
254
- #[ cfg_attr( qemu, ignore) ]
255
- fn test_copy_file_range ( ) {
256
- const CONTENTS : & [ u8 ] = b"foobarbaz" ;
257
-
258
- let mut tmp1 = tempfile ( ) . unwrap ( ) ;
259
- let mut tmp2 = tempfile ( ) . unwrap ( ) ;
260
-
261
- tmp1. write_all ( CONTENTS ) . unwrap ( ) ;
262
- tmp1. flush ( ) . unwrap ( ) ;
263
-
264
- let mut from_offset: i64 = 3 ;
265
- copy_file_range (
266
- tmp1. as_raw_fd ( ) ,
267
- Some ( & mut from_offset) ,
268
- tmp2. as_raw_fd ( ) ,
269
- None ,
270
- 3 ,
271
- )
272
- . unwrap ( ) ;
273
-
274
- let mut res: String = String :: new ( ) ;
275
- tmp2. rewind ( ) . unwrap ( ) ;
276
- tmp2. read_to_string ( & mut res) . unwrap ( ) ;
277
-
278
- assert_eq ! ( res, String :: from( "bar" ) ) ;
279
- assert_eq ! ( from_offset, 6 ) ;
280
- }
281
-
282
291
#[ test]
283
292
fn test_splice ( ) {
284
293
const CONTENTS : & [ u8 ] = b"abcdef123456" ;
0 commit comments