Skip to content

Commit

Permalink
basenc: ignore Interrupted errors
Browse files Browse the repository at this point in the history
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
  • Loading branch information
andrewliebenow committed Oct 11, 2024
1 parent 3564b5c commit f4599d0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
34 changes: 17 additions & 17 deletions src/uu/base32/src/base_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ pub fn get_input(config: &Config) -> UResult<Box<dyn Read>> {
}
}

pub fn handle_input<R: Read>(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();
Expand Down Expand Up @@ -377,13 +377,13 @@ pub mod fast_encode {
}

fn write_to_output(
line_wrapping_option: &mut Option<LineWrapping>,
line_wrapping: &mut Option<LineWrapping>,
encoded_buffer: &mut VecDeque<u8>,
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)?;
Expand All @@ -393,9 +393,9 @@ pub mod fast_encode {
}
// End of helper functions

pub fn fast_encode<R: Read, W: Write>(
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<usize>,
) -> UResult<()> {
Expand Down Expand Up @@ -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)));
Expand All @@ -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(())
Expand Down Expand Up @@ -606,9 +606,9 @@ pub mod fast_decode {
}
// End of helper functions

pub fn fast_decode<R: Read, W: Write>(
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<()> {
Expand Down Expand Up @@ -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)));
Expand All @@ -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(())
Expand Down
10 changes: 5 additions & 5 deletions src/uu/paste/src/paste.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ fn parse_delimiters(delimiters: &str) -> UResult<Box<[Box<[u8]>]>> {
let mut add_single_char_delimiter = |vec: &mut Vec<Box<[u8]>>, 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::<Box<[u8]>>::with_capacity(delimiters.len());
Expand Down Expand Up @@ -311,7 +311,7 @@ impl<'a> DelimiterState<'a> {
DelimiterState::MultipleDelimiters {
current_delimiter, ..
} => current_delimiter.len(),
_ => {
DelimiterState::NoDelimiters => {
return;
}
};
Expand Down Expand Up @@ -350,7 +350,7 @@ impl<'a> DelimiterState<'a> {

*current_delimiter = bo;
}
_ => {}
DelimiterState::NoDelimiters => {}
}
}
}
Expand All @@ -363,8 +363,8 @@ enum InputSource {
impl InputSource {
fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> UResult<usize> {
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()
Expand Down

0 comments on commit f4599d0

Please sign in to comment.