@@ -232,3 +232,46 @@ mod test_posix_fadvise {
232
232
assert_eq ! ( errno, Errno :: ESPIPE as i32 ) ;
233
233
}
234
234
}
235
+
236
+ #[ cfg( any( target_os = "linux" ,
237
+ target_os = "android" ,
238
+ target_os = "emscripten" ,
239
+ target_os = "fuchsia" ,
240
+ any( target_os = "wasi" , target_env = "wasi" ) ,
241
+ target_env = "freebsd" ) ) ]
242
+ mod test_posix_fallocate {
243
+
244
+ use tempfile:: NamedTempFile ;
245
+ use std:: { io:: Read , os:: unix:: io:: { RawFd , AsRawFd } } ;
246
+ use nix:: errno:: Errno ;
247
+ use nix:: fcntl:: * ;
248
+ use nix:: unistd:: pipe;
249
+
250
+ #[ test]
251
+ fn test_success ( ) {
252
+ const len: usize = 100 ;
253
+ let mut tmp = NamedTempFile :: new ( ) . unwrap ( ) ;
254
+ let fd = tmp. as_raw_fd ( ) ;
255
+ let res = posix_fallocate ( fd, 0 , len as i64 ) . unwrap ( ) ;
256
+ let mut data = [ 1u8 ; len] ;
257
+ assert_eq ! ( tmp. read( & mut data) . expect( "read failure" ) , len) ;
258
+ assert_eq ! ( & data as & [ u8 ] , & [ 0u8 ; len] as & [ u8 ] ) ;
259
+ assert_eq ! ( res, 0 ) ;
260
+ }
261
+
262
+ #[ test]
263
+ fn test_errno ( ) {
264
+ let ( rd, _wr) = pipe ( ) . unwrap ( ) ;
265
+ let errno = posix_fallocate ( rd as RawFd , 0 , 100 ) . unwrap ( ) ;
266
+ match Errno :: from_i32 ( errno) {
267
+ Errno :: EBADF
268
+ | Errno :: EFBIG
269
+ | Errno :: EINTR
270
+ | Errno :: EINVAL
271
+ | Errno :: ENODEV
272
+ | Errno :: ENOSPC
273
+ | Errno :: ESPIPE => ( ) ,
274
+ _ => panic ! ( "errno does not match posix_fallocate spec" ) ,
275
+ }
276
+ }
277
+ }
0 commit comments