Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 37 additions & 12 deletions src/uu/paste/src/paste.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ use std::iter::Cycle;
use std::path::Path;
use std::rc::Rc;
use std::slice::Iter;
use uucore::error::{UResult, USimpleError};
use uucore::display::Quotable;
use uucore::error::{FromIo, UError, UResult, USimpleError};
use uucore::format_usage;
use uucore::i18n::charmap::mb_char_len;
use uucore::line_ending::LineEnding;
Expand Down Expand Up @@ -107,8 +108,9 @@ fn paste(
)
} else {
let path = Path::new(&filename);
let file = File::open(path)?;
InputSource::File(BufReader::new(file))
let file = File::open(path)
.map_err_context(|| filename.as_os_str().maybe_quote().to_string())?;
InputSource::File(BufReader::new(file), filename)
};

input_source_vec.push(input_source);
Expand Down Expand Up @@ -136,14 +138,24 @@ fn paste(
let mut delimiter_state = DelimiterState::new(&unescaped_and_encoded_delimiters);

let mut output = Vec::new();
let mut read_error: Option<Box<dyn UError>> = None;

if serial {
for input_source in &mut input_source_vec {
output.clear();

while let read @ 1.. = input_source.read_until(line_ending_byte, &mut output)? {
remove_trailing_line_ending_byte(line_ending_byte, read, &mut output);
delimiter_state.write_delimiter(&mut output);
loop {
match input_source.read_until(line_ending_byte, &mut output) {
Ok(0) => break,
Ok(read) => {
remove_trailing_line_ending_byte(line_ending_byte, read, &mut output);
delimiter_state.write_delimiter(&mut output);
}
Err(error) => {
read_error.get_or_insert(error);
break;
}
}
}

delimiter_state.remove_trailing_delimiter(&mut output);
Expand All @@ -168,14 +180,19 @@ fn paste(
if eof[i] {
eof_count += 1;
} else {
match input_source.read_until(line_ending_byte, &mut output)? {
0 => {
match input_source.read_until(line_ending_byte, &mut output) {
Ok(0) => {
eof[i] = true;
eof_count += 1;
}
read => {
Ok(read) => {
remove_trailing_line_ending_byte(line_ending_byte, read, &mut output);
}
Err(error) => {
read_error.get_or_insert(error);
eof[i] = true;
eof_count += 1;
}
}
}

Expand All @@ -200,6 +217,10 @@ fn paste(
}
}

if let Some(error) = read_error {
return Err(error);
}

Ok(())
}

Expand Down Expand Up @@ -376,14 +397,16 @@ impl<'a> DelimiterState<'a> {
}

enum InputSource {
File(BufReader<File>),
File(BufReader<File>, OsString),
StandardInput(Rc<RefCell<Stdin>>),
}

impl InputSource {
fn read(&mut self, buf: &mut [u8]) -> UResult<usize> {
let us = match self {
Self::File(bu) => bu.read(buf)?,
Self::File(reader, name) => reader
.read(buf)
.map_err_context(|| name.as_os_str().maybe_quote().to_string())?,
Self::StandardInput(rc) => rc
.try_borrow()
.map_err(|bo| {
Expand All @@ -398,7 +421,9 @@ 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::File(reader, name) => reader
.read_until(byte, buf)
.map_err_context(|| name.as_os_str().maybe_quote().to_string())?,
Self::StandardInput(rc) => rc
.try_borrow()
.map_err(|bo| {
Expand Down
21 changes: 21 additions & 0 deletions tests/by-util/test_paste.rs
Original file line number Diff line number Diff line change
Expand Up @@ -609,3 +609,24 @@ fn test_crlf_delimiter_not_eaten_by_an_empty_line() {
.succeeds()
.stdout_is(expected);
}

#[test]
fn test_missing_file_error_includes_filename() {
new_ucmd!()
.arg("missing.txt")
.fails_with_code(1)
.stderr_is("paste: missing.txt: No such file or directory\n");
}

#[test]
fn test_directory_error_includes_filename() {
let (at, mut ucmd) = at_and_ucmd!();
at.write("a.txt", "a\n");
at.mkdir("d");

let result = ucmd.args(&["a.txt", "d"]).fails_with_code(1);
result.stderr_contains("paste: d:");
if !cfg!(windows) {
result.stdout_is("a\t\n");
}
}
Comment thread
Logic769 marked this conversation as resolved.
Resolved
Loading