@@ -7,24 +7,25 @@ use crate::sync::atomic::{AtomicBool, Ordering};
7
7
use crate :: sys:: cvt;
8
8
use crate :: sys_common:: AsInner ;
9
9
10
- use libc:: { c_int, c_void, ssize_t } ;
10
+ use libc:: { c_int, c_void} ;
11
11
12
12
#[ derive( Debug ) ]
13
13
pub struct FileDesc {
14
14
fd : c_int ,
15
15
}
16
16
17
- fn max_len ( ) -> usize {
18
- // The maximum read limit on most posix-like systems is `SSIZE_MAX`,
19
- // with the man page quoting that if the count of bytes to read is
20
- // greater than `SSIZE_MAX` the result is "unspecified".
21
- //
22
- // On macOS, however, apparently the 64-bit libc is either buggy or
23
- // intentionally showing odd behavior by rejecting any read with a size
24
- // larger than or equal to INT_MAX. To handle both of these the read
25
- // size is capped on both platforms.
26
- if cfg ! ( target_os = "macos" ) { <c_int >:: MAX as usize - 1 } else { <ssize_t >:: MAX as usize }
27
- }
17
+ // The maximum read limit on most POSIX-like systems is `SSIZE_MAX`,
18
+ // with the man page quoting that if the count of bytes to read is
19
+ // greater than `SSIZE_MAX` the result is "unspecified".
20
+ //
21
+ // On macOS, however, apparently the 64-bit libc is either buggy or
22
+ // intentionally showing odd behavior by rejecting any read with a size
23
+ // larger than or equal to INT_MAX. To handle both of these the read
24
+ // size is capped on both platforms.
25
+ #[ cfg( target_os = "macos" ) ]
26
+ const READ_LIMIT : usize = c_int:: MAX as usize - 1 ;
27
+ #[ cfg( not( target_os = "macos" ) ) ]
28
+ const READ_LIMIT : usize = libc:: ssize_t:: MAX as usize ;
28
29
29
30
impl FileDesc {
30
31
pub fn new ( fd : c_int ) -> FileDesc {
@@ -44,7 +45,7 @@ impl FileDesc {
44
45
45
46
pub fn read ( & self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
46
47
let ret = cvt ( unsafe {
47
- libc:: read ( self . fd , buf. as_mut_ptr ( ) as * mut c_void , cmp:: min ( buf. len ( ) , max_len ( ) ) )
48
+ libc:: read ( self . fd , buf. as_mut_ptr ( ) as * mut c_void , cmp:: min ( buf. len ( ) , READ_LIMIT ) )
48
49
} ) ?;
49
50
Ok ( ret as usize )
50
51
}
@@ -92,7 +93,7 @@ impl FileDesc {
92
93
cvt_pread64 (
93
94
self . fd ,
94
95
buf. as_mut_ptr ( ) as * mut c_void ,
95
- cmp:: min ( buf. len ( ) , max_len ( ) ) ,
96
+ cmp:: min ( buf. len ( ) , READ_LIMIT ) ,
96
97
offset as i64 ,
97
98
)
98
99
. map ( |n| n as usize )
@@ -101,7 +102,7 @@ impl FileDesc {
101
102
102
103
pub fn write ( & self , buf : & [ u8 ] ) -> io:: Result < usize > {
103
104
let ret = cvt ( unsafe {
104
- libc:: write ( self . fd , buf. as_ptr ( ) as * const c_void , cmp:: min ( buf. len ( ) , max_len ( ) ) )
105
+ libc:: write ( self . fd , buf. as_ptr ( ) as * const c_void , cmp:: min ( buf. len ( ) , READ_LIMIT ) )
105
106
} ) ?;
106
107
Ok ( ret as usize )
107
108
}
@@ -144,7 +145,7 @@ impl FileDesc {
144
145
cvt_pwrite64 (
145
146
self . fd ,
146
147
buf. as_ptr ( ) as * const c_void ,
147
- cmp:: min ( buf. len ( ) , max_len ( ) ) ,
148
+ cmp:: min ( buf. len ( ) , READ_LIMIT ) ,
148
149
offset as i64 ,
149
150
)
150
151
. map ( |n| n as usize )
0 commit comments