@@ -7,24 +7,25 @@ use crate::sync::atomic::{AtomicBool, Ordering};
77use crate :: sys:: cvt;
88use crate :: sys_common:: AsInner ;
99
10- use libc:: { c_int, c_void, ssize_t } ;
10+ use libc:: { c_int, c_void} ;
1111
1212#[ derive( Debug ) ]
1313pub struct FileDesc {
1414 fd : c_int ,
1515}
1616
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 ;
2829
2930impl FileDesc {
3031 pub fn new ( fd : c_int ) -> FileDesc {
@@ -44,7 +45,7 @@ impl FileDesc {
4445
4546 pub fn read ( & self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
4647 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 ) )
4849 } ) ?;
4950 Ok ( ret as usize )
5051 }
@@ -92,7 +93,7 @@ impl FileDesc {
9293 cvt_pread64 (
9394 self . fd ,
9495 buf. as_mut_ptr ( ) as * mut c_void ,
95- cmp:: min ( buf. len ( ) , max_len ( ) ) ,
96+ cmp:: min ( buf. len ( ) , READ_LIMIT ) ,
9697 offset as i64 ,
9798 )
9899 . map ( |n| n as usize )
@@ -101,7 +102,7 @@ impl FileDesc {
101102
102103 pub fn write ( & self , buf : & [ u8 ] ) -> io:: Result < usize > {
103104 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 ) )
105106 } ) ?;
106107 Ok ( ret as usize )
107108 }
@@ -144,7 +145,7 @@ impl FileDesc {
144145 cvt_pwrite64 (
145146 self . fd ,
146147 buf. as_ptr ( ) as * const c_void ,
147- cmp:: min ( buf. len ( ) , max_len ( ) ) ,
148+ cmp:: min ( buf. len ( ) , READ_LIMIT ) ,
148149 offset as i64 ,
149150 )
150151 . map ( |n| n as usize )
0 commit comments