@@ -67,7 +67,7 @@ impl Mmap {
67
67
pub fn accessible_reserved (
68
68
mut accessible_size : usize ,
69
69
mapping_size : usize ,
70
- backing_file : Option < std:: fs:: File > ,
70
+ mut backing_file : Option < std:: fs:: File > ,
71
71
memory_type : MmapType ,
72
72
) -> Result < Self , String > {
73
73
use std:: os:: fd:: IntoRawFd ;
@@ -76,14 +76,27 @@ impl Mmap {
76
76
assert_le ! ( accessible_size, mapping_size) ;
77
77
assert_eq ! ( mapping_size & ( page_size - 1 ) , 0 ) ;
78
78
assert_eq ! ( accessible_size & ( page_size - 1 ) , 0 ) ;
79
- let memory_fd = backing_file. map_or ( -1 , |fd| fd. into_raw_fd ( ) ) ;
80
79
81
80
// Mmap may return EINVAL if the size is zero, so just
82
81
// special-case that.
83
82
if mapping_size == 0 {
84
83
return Ok ( Self :: new ( ) ) ;
85
84
}
86
85
86
+ // If there is a backing file, reise the file so that its at least
87
+ // `mapping_size` bytes.
88
+ if let Some ( backing_file) = & mut backing_file {
89
+ let len = backing_file. metadata ( ) . map_err ( |e| e. to_string ( ) ) ?. len ( ) as usize ;
90
+ if len < mapping_size {
91
+ backing_file
92
+ . set_len ( mapping_size as u64 )
93
+ . map_err ( |e| e. to_string ( ) ) ?;
94
+ }
95
+ accessible_size = accessible_size. max ( len) . min ( mapping_size) ;
96
+ }
97
+
98
+ let memory_fd = backing_file. map_or ( -1 , |fd| fd. into_raw_fd ( ) ) ;
99
+
87
100
// Compute the flags
88
101
let mut flags = match memory_fd {
89
102
fd if fd < 0 => libc:: MAP_ANON ,
@@ -94,18 +107,6 @@ impl Mmap {
94
107
MmapType :: Shared => libc:: MAP_SHARED ,
95
108
} ;
96
109
97
- // Resize the file so that its size is at least `mapping_size`.
98
- if memory_fd != -1 {
99
- let len = unsafe { libc:: lseek ( memory_fd, 0 , libc:: SEEK_END ) } ;
100
- if len < mapping_size as i64 {
101
- let r = unsafe { libc:: ftruncate64 ( memory_fd, mapping_size as libc:: off64_t ) } ;
102
- if r != 0 {
103
- return Err ( io:: Error :: last_os_error ( ) . to_string ( ) ) ;
104
- }
105
- }
106
- accessible_size = accessible_size. max ( len as usize ) . min ( mapping_size) ;
107
- }
108
-
109
110
Ok ( if accessible_size == mapping_size {
110
111
// Allocate a single read-write region at once.
111
112
let ptr = unsafe {
0 commit comments