From caf08dd279114ba6d7355cd90ea2fdd9050336df Mon Sep 17 00:00:00 2001 From: Andrew Liebenow Date: Fri, 11 Oct 2024 10:04:21 -0500 Subject: [PATCH] basenc: ignore Interrupted errors Mirror behavior of `std::io::Read`'s `read_to_end` function ([link][1]): continue reading when errors with kind `std::io::ErrorKind::Interrupted` are encountered. Also: clean up a few other things. [1]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_to_end --- src/uu/base32/src/base_common.rs | 34 ++++++++++++++++---------------- src/uu/paste/src/paste.rs | 10 +++++----- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/uu/base32/src/base_common.rs b/src/uu/base32/src/base_common.rs index f6b88f55157..130fe86264b 100644 --- a/src/uu/base32/src/base_common.rs +++ b/src/uu/base32/src/base_common.rs @@ -160,7 +160,7 @@ pub fn get_input(config: &Config) -> UResult> { } } -pub fn handle_input(input: &mut R, format: Format, config: Config) -> UResult<()> { +pub fn handle_input(input: &mut dyn Read, format: Format, config: Config) -> UResult<()> { let supports_fast_decode_and_encode = get_supports_fast_decode_and_encode(format); let supports_fast_decode_and_encode_ref = supports_fast_decode_and_encode.as_ref(); @@ -377,13 +377,13 @@ pub mod fast_encode { } fn write_to_output( - line_wrapping_option: &mut Option, + line_wrapping: &mut Option, encoded_buffer: &mut VecDeque, output: &mut dyn Write, is_cleanup: bool, ) -> io::Result<()> { // Write all data in `encoded_buffer` to `output` - if let &mut Some(ref mut li) = line_wrapping_option { + if let &mut Some(ref mut li) = line_wrapping { write_with_line_breaks(li, encoded_buffer, output, is_cleanup)?; } else { write_without_line_breaks(encoded_buffer, output, is_cleanup)?; @@ -393,9 +393,9 @@ pub mod fast_encode { } // End of helper functions - pub fn fast_encode( - input: &mut R, - mut output: W, + pub fn fast_encode( + input: &mut dyn Read, + output: &mut dyn Write, supports_fast_decode_and_encode: &dyn SupportsFastDecodeAndEncode, wrap: Option, ) -> UResult<()> { @@ -475,14 +475,14 @@ pub mod fast_encode { assert!(leftover_buffer.len() < encode_in_chunks_of_size); // Write all data in `encoded_buffer` to `output` - write_to_output(&mut line_wrapping, &mut encoded_buffer, &mut output, false)?; + write_to_output(&mut line_wrapping, &mut encoded_buffer, output, false)?; } Err(er) => { let kind = er.kind(); if kind == ErrorKind::Interrupted { - // TODO - // Retry reading? + // Retry reading + continue; } return Err(USimpleError::new(1, format_read_error(kind))); @@ -499,7 +499,7 @@ pub mod fast_encode { // Write all data in `encoded_buffer` to output // `is_cleanup` triggers special cleanup-only logic - write_to_output(&mut line_wrapping, &mut encoded_buffer, &mut output, true)?; + write_to_output(&mut line_wrapping, &mut encoded_buffer, output, true)?; } Ok(()) @@ -606,9 +606,9 @@ pub mod fast_decode { } // End of helper functions - pub fn fast_decode( - input: &mut R, - mut output: &mut W, + pub fn fast_decode( + input: &mut dyn Read, + output: &mut dyn Write, supports_fast_decode_and_encode: &dyn SupportsFastDecodeAndEncode, ignore_garbage: bool, ) -> UResult<()> { @@ -711,14 +711,14 @@ pub mod fast_decode { assert!(leftover_buffer.len() < decode_in_chunks_of_size); // Write all data in `decoded_buffer` to `output` - write_to_output(&mut decoded_buffer, &mut output)?; + write_to_output(&mut decoded_buffer, output)?; } Err(er) => { let kind = er.kind(); if kind == ErrorKind::Interrupted { - // TODO - // Retry reading? + // Retry reading + continue; } return Err(USimpleError::new(1, format_read_error(kind))); @@ -734,7 +734,7 @@ pub mod fast_decode { .decode_into_vec(&leftover_buffer, &mut decoded_buffer)?; // Write all data in `decoded_buffer` to `output` - write_to_output(&mut decoded_buffer, &mut output)?; + write_to_output(&mut decoded_buffer, output)?; } Ok(()) diff --git a/src/uu/paste/src/paste.rs b/src/uu/paste/src/paste.rs index 9d26197813b..456639ba972 100644 --- a/src/uu/paste/src/paste.rs +++ b/src/uu/paste/src/paste.rs @@ -200,7 +200,7 @@ fn parse_delimiters(delimiters: &str) -> UResult]>> { let mut add_single_char_delimiter = |vec: &mut Vec>, ch: char| { let delimiter_encoded = ch.encode_utf8(&mut buffer); - vec.push(Box::from(delimiter_encoded.as_bytes())); + vec.push(Box::<[u8]>::from(delimiter_encoded.as_bytes())); }; let mut vec = Vec::>::with_capacity(delimiters.len()); @@ -311,7 +311,7 @@ impl<'a> DelimiterState<'a> { DelimiterState::MultipleDelimiters { current_delimiter, .. } => current_delimiter.len(), - _ => { + DelimiterState::NoDelimiters => { return; } }; @@ -350,7 +350,7 @@ impl<'a> DelimiterState<'a> { *current_delimiter = bo; } - _ => {} + DelimiterState::NoDelimiters => {} } } } @@ -363,8 +363,8 @@ enum InputSource { impl InputSource { fn read_until(&mut self, byte: u8, buf: &mut Vec) -> UResult { let us = match self { - Self::File(bu) => bu.read_until(byte, buf)?, - Self::StandardInput(rc) => rc + InputSource::File(bu) => bu.read_until(byte, buf)?, + InputSource::StandardInput(rc) => rc .try_borrow() .map_err(|bo| USimpleError::new(1, format!("{bo}")))? .lock()