@@ -41,14 +41,14 @@ async fn read_to_end_uring(mut fd: OwnedFd, mut buf: Vec<u8>) -> io::Result<Vec<
4141 let start_cap = buf. capacity ( ) ;
4242
4343 loop {
44- if buf. len ( ) == buf. capacity ( ) && buf. capacity ( ) == start_cap && buf. len ( ) > PROBE_SIZE {
44+ if buf. len ( ) == buf. capacity ( ) && buf. capacity ( ) == start_cap && buf. len ( ) >= PROBE_SIZE {
4545 // The buffer might be an exact fit. Let's read into a probe buffer
4646 // and see if it returns `Ok(0)`. If so, we've avoided an
4747 // unnecessary increasing of the capacity. But if not, append the
4848 // probe buffer to the primary buffer and let its capacity grow.
49- let ( size_read , r_fd, r_buf) = small_probe_read ( fd, buf, & mut offset) . await ?;
49+ let ( r_fd, r_buf, is_eof ) = small_probe_read ( fd, buf, & mut offset) . await ?;
5050
51- if size_read == 0 {
51+ if is_eof {
5252 return Ok ( r_buf) ;
5353 }
5454
@@ -69,9 +69,9 @@ async fn read_to_end_uring(mut fd: OwnedFd, mut buf: Vec<u8>) -> io::Result<Vec<
6969 let read_len = u32:: try_from ( buf_len) . expect ( "buf_len must always fit in u32" ) ;
7070
7171 // read into spare capacity
72- let ( size_read , r_fd, r_buf) = op_read ( fd, buf, & mut offset, read_len) . await ?;
72+ let ( r_fd, r_buf, is_eof ) = op_read ( fd, buf, & mut offset, read_len) . await ?;
7373
74- if size_read == 0 {
74+ if is_eof {
7575 return Ok ( r_buf) ;
7676 }
7777
@@ -84,7 +84,7 @@ async fn small_probe_read(
8484 fd : OwnedFd ,
8585 mut buf : Vec < u8 > ,
8686 offset : & mut u64 ,
87- ) -> io:: Result < ( u32 , OwnedFd , Vec < u8 > ) > {
87+ ) -> io:: Result < ( OwnedFd , Vec < u8 > , bool ) > {
8888 let read_len = PROBE_SIZE_U32 ;
8989
9090 let mut temp_arr = [ 0 ; PROBE_SIZE ] ;
@@ -97,21 +97,25 @@ async fn small_probe_read(
9797 // than PROBE_SIZE. So we can read into the discarded length
9898 buf. truncate ( back_bytes_len) ;
9999
100- let ( size_read , r_fd, mut r_buf) = op_read ( fd, buf, offset, read_len) . await ?;
100+ let ( r_fd, mut r_buf, is_eof ) = op_read ( fd, buf, offset, read_len) . await ?;
101101 // If `size_read` returns zero due to reasons such as buffer's exact fit,
102102 // then this `try_reserve` does not perform allocation.
103103 r_buf. try_reserve ( PROBE_SIZE ) ?;
104104 r_buf. splice ( back_bytes_len..back_bytes_len, temp_arr) ;
105105
106- Ok ( ( size_read , r_fd, r_buf) )
106+ Ok ( ( r_fd, r_buf, is_eof ) )
107107}
108108
109+ // Takes a amount of length to read and reads until we exhaust the given length
110+ // to read or EOF reached.
111+ //
112+ // Returns size_read, the result and EOF reached or not
109113async fn op_read (
110114 mut fd : OwnedFd ,
111115 mut buf : Vec < u8 > ,
112116 offset : & mut u64 ,
113117 mut read_len : u32 ,
114- ) -> io:: Result < ( u32 , OwnedFd , Vec < u8 > ) > {
118+ ) -> io:: Result < ( OwnedFd , Vec < u8 > , bool ) > {
115119 loop {
116120 let ( res, r_fd, r_buf) = Op :: read ( fd, buf, read_len, * offset) . await ;
117121
@@ -125,8 +129,10 @@ async fn op_read(
125129 * offset += size_read as u64 ;
126130 read_len -= size_read;
127131
128- if read_len == 0 || size_read == 0 {
129- return Ok ( ( size_read, r_fd, r_buf) ) ;
132+ let is_eof = size_read == 0 ;
133+
134+ if read_len == 0 || is_eof {
135+ return Ok ( ( r_fd, r_buf, is_eof) ) ;
130136 }
131137
132138 buf = r_buf;
0 commit comments