@@ -35,6 +35,8 @@ lazy_static! {
35
35
pub enum SendError {
36
36
#[ error( "timed out reading a reply from device" ) ]
37
37
Timeout ,
38
+ #[ error( "Did not write correct number of bytes. Got {:?}. Want {}" , written, expected) ]
39
+ ShortWrite { written : usize , expected : usize } ,
38
40
}
39
41
40
42
#[ derive( Debug , Error ) ]
@@ -171,7 +173,6 @@ async fn read_with_timeout<T: AsyncRead + Unpin>(
171
173
}
172
174
}
173
175
174
- #[ allow( clippy:: unused_io_amount) ] // TODO(https://fxbug.dev/42177040)
175
176
pub async fn send_with_listener < T : AsyncRead + AsyncWrite + Unpin > (
176
177
cmd : Command ,
177
178
interface : & mut T ,
@@ -180,24 +181,28 @@ pub async fn send_with_listener<T: AsyncRead + AsyncWrite + Unpin>(
180
181
let _lock = SEND_LOCK . lock ( ) . await ;
181
182
let bytes = Vec :: < u8 > :: try_from ( & cmd) ?;
182
183
tracing:: debug!( "Fastboot: writing command {cmd:?}: {}" , String :: from_utf8_lossy( & bytes) ) ;
183
- interface. write ( & bytes) . await ?;
184
+ let written = interface. write ( & bytes) . await ?;
185
+ if written < bytes. len ( ) {
186
+ return Err ( anyhow ! ( SendError :: ShortWrite { written, expected: bytes. len( ) } ) ) ;
187
+ }
184
188
read ( interface, listener) . await
185
189
}
186
190
187
191
#[ tracing:: instrument( skip( interface) ) ]
188
- #[ allow( clippy:: unused_io_amount) ] // TODO(https://fxbug.dev/42177040)
189
192
pub async fn send < T : AsyncRead + AsyncWrite + Unpin > (
190
193
cmd : Command ,
191
194
interface : & mut T ,
192
195
) -> Result < Reply > {
193
196
let _lock = SEND_LOCK . lock ( ) . await ;
194
197
let bytes = Vec :: < u8 > :: try_from ( & cmd) ?;
195
198
tracing:: debug!( "Fastboot: writing command {cmd:?}: {}" , String :: from_utf8_lossy( & bytes) ) ;
196
- interface. write ( & bytes) . await ?;
199
+ let written = interface. write ( & bytes) . await ?;
200
+ if written < bytes. len ( ) {
201
+ return Err ( anyhow ! ( SendError :: ShortWrite { written, expected: bytes. len( ) } ) ) ;
202
+ }
197
203
read_and_log_info ( interface) . await
198
204
}
199
205
200
- #[ allow( clippy:: unused_io_amount) ] // TODO(https://fxbug.dev/42177040)
201
206
pub async fn send_with_timeout < T : AsyncRead + AsyncWrite + Unpin > (
202
207
cmd : Command ,
203
208
interface : & mut T ,
@@ -206,7 +211,10 @@ pub async fn send_with_timeout<T: AsyncRead + AsyncWrite + Unpin>(
206
211
let _lock = SEND_LOCK . lock ( ) . await ;
207
212
let bytes = Vec :: < u8 > :: try_from ( & cmd) ?;
208
213
tracing:: debug!( "Fastboot: writing command {cmd:?}: {}" , String :: from_utf8_lossy( & bytes) ) ;
209
- interface. write ( & bytes) . await ?;
214
+ let written = interface. write ( & bytes) . await ?;
215
+ if written < bytes. len ( ) {
216
+ return Err ( anyhow ! ( SendError :: ShortWrite { written, expected: bytes. len( ) } ) ) ;
217
+ }
210
218
read_with_timeout ( interface, & LogInfoListener { } , timeout) . await
211
219
}
212
220
@@ -263,7 +271,13 @@ pub async fn upload_with_read_timeout<T: AsyncRead + AsyncWrite + Unpin, R: Read
263
271
listener. on_error ( & err) . await ?;
264
272
bail ! ( err) ;
265
273
}
266
- _ => {
274
+ Ok ( written) => {
275
+ if written < n {
276
+ return Err ( anyhow ! ( SendError :: ShortWrite {
277
+ written,
278
+ expected: n
279
+ } ) ) ;
280
+ }
267
281
listener. on_progress ( n. try_into ( ) . unwrap ( ) ) . await ?;
268
282
tracing:: trace!( "fastboot: wrote {} bytes" , n) ;
269
283
}
0 commit comments