@@ -413,21 +413,44 @@ impl AsyncWriter {
413
413
#[ cfg( feature = "mmap" ) ]
414
414
fn make_mmap ( tmpfile : & mut NamedTempFile , size : Option < usize > ) -> Result < Option < MmapMut > > {
415
415
if let Some ( size @ 0 ..=MAX_MMAP_SIZE ) = size {
416
- tmpfile
417
- . as_file_mut ( )
418
- . set_len ( size as u64 )
419
- . with_context ( || {
420
- format ! (
421
- "Failed to configure file length for temp file at {}" ,
422
- tmpfile. path( ) . display( )
423
- )
424
- } ) ?;
416
+ allocate_file ( tmpfile. as_file ( ) , size) . with_context ( || {
417
+ format ! (
418
+ "Failed to configure file length for temp file at {}" ,
419
+ tmpfile. path( ) . display( )
420
+ )
421
+ } ) ?;
425
422
Ok ( unsafe { MmapMut :: map_mut ( tmpfile. as_file ( ) ) . ok ( ) } )
426
423
} else {
427
424
Ok ( None )
428
425
}
429
426
}
430
427
428
+ #[ cfg( feature = "mmap" ) ]
429
+ #[ cfg( target_os = "linux" ) ]
430
+ fn allocate_file ( file : & std:: fs:: File , size : usize ) -> std:: io:: Result < ( ) > {
431
+ use std:: io:: { Error , ErrorKind } ;
432
+ use std:: os:: fd:: AsRawFd ;
433
+
434
+ let fd = file. as_raw_fd ( ) ;
435
+ match unsafe { libc:: posix_fallocate64 ( fd, 0 , size as i64 ) } {
436
+ 0 => Ok ( ( ) ) ,
437
+ libc:: ENOSPC => Err ( Error :: new (
438
+ ErrorKind :: Other , // ErrorKind::StorageFull is unstable
439
+ "cannot allocate file: no space left on device" ,
440
+ ) ) ,
441
+ err => Err ( Error :: new (
442
+ ErrorKind :: Other ,
443
+ format ! ( "posix_fallocate64 failed with code {err}" ) ,
444
+ ) ) ,
445
+ }
446
+ }
447
+
448
+ #[ cfg( feature = "mmap" ) ]
449
+ #[ cfg( not( target_os = "linux" ) ) ]
450
+ fn allocate_file ( file : & std:: fs:: File , size : usize ) -> std:: io:: Result < ( ) > {
451
+ file. set_len ( size as u64 )
452
+ }
453
+
431
454
#[ cfg( not( feature = "mmap" ) ) ]
432
455
fn make_mmap ( _: & mut NamedTempFile , _: Option < usize > ) -> Result < Option < MmapMut > > {
433
456
Ok ( None )
0 commit comments