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
6 changes: 3 additions & 3 deletions src/uu/cp/src/platform/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ fn check_dest_is_fifo(dest: &Path) -> bool {
}

/// Copy the contents of a stream from `source` to `dest`.
fn copy_stream<P>(source: P, dest: P) -> std::io::Result<u64>
fn copy_stream<P>(source: P, dest: P) -> std::io::Result<()>
where
P: AsRef<Path>,
{
Expand Down Expand Up @@ -250,10 +250,10 @@ where
dst_file.set_len(0)?;
}

let num_bytes_copied = buf_copy::copy_stream(&mut src_file, &mut dst_file)
buf_copy::copy_stream(&mut src_file, &mut dst_file)
.map_err(|e| std::io::Error::other(format!("{e}")))?;

Ok(num_bytes_copied)
Ok(())
}

/// Copies `source` to `dest` using copy-on-write if possible.
Expand Down
6 changes: 2 additions & 4 deletions src/uucore/src/lib/features/buf_copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,8 @@ mod tests {
let thread = thread::spawn(move || {
pipe_write.write_all(data).unwrap();
});
let result = copy_stream(&mut pipe_read, &mut dest_file).unwrap();
copy_stream(&mut pipe_read, &mut dest_file).unwrap();
thread.join().unwrap();
assert_eq!(result, data.len() as u64);

// We would have been at the end already, so seek again to the start.
dest_file.seek(SeekFrom::Start(0)).unwrap();
Expand All @@ -104,13 +103,12 @@ mod tests {
src_file.sync_all().unwrap();

let mut src_file = File::open(&src_path).unwrap();
let bytes_copied = copy_stream(&mut src_file, &mut dest_file).unwrap();
copy_stream(&mut src_file, &mut dest_file).unwrap();

let mut dest_file = File::open(&dest_path).unwrap();
let mut buf = Vec::new();
dest_file.read_to_end(&mut buf).unwrap();

assert_eq!(bytes_copied as usize, data.len());
assert_eq!(buf, data);
}
}
13 changes: 4 additions & 9 deletions src/uucore/src/lib/features/buf_copy/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,7 @@ impl From<rustix::io::Errno> for Error {
/// # Arguments
/// * `source` - `Read` implementor to copy data from.
/// * `dest` - `Write` implementor to copy data to.
///
/// # Returns
///
/// Result of operation and bytes successfully written (as a `u64`) when
/// operation is successful.
pub fn copy_stream<R, S>(src: &mut R, dest: &mut S) -> UResult<u64>
pub fn copy_stream<R, S>(src: &mut R, dest: &mut S) -> UResult<()>
where
R: Read + AsFd + AsRawFd,
S: Write + AsFd + AsRawFd,
Expand All @@ -62,19 +57,19 @@ where
// for faster writing. If it works, we're done.
let result = splice_write(src, &dest.as_fd())?;
if !result.1 {
return Ok(result.0);
return Ok(());
}

// If the splice() call failed, fall back on slower writing.
let result = std::io::copy(src, dest)?;
std::io::copy(src, dest)?;

// If the splice() call failed and there has been some data written to
// stdout via while loop above AND there will be second splice() call
// that will succeed, data pushed through splice will be output before
// the data buffered in stdout.lock. Therefore additional explicit flush
// is required here.
dest.flush()?;
Ok(result)
Ok(())
}

/// Write from source `handle` into destination `write_fd` using Linux-specific
Expand Down
11 changes: 3 additions & 8 deletions src/uucore/src/lib/features/buf_copy/other.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,11 @@ use crate::error::UResult;
/// # Arguments
/// * `source` - `Read` implementor to copy data from.
/// * `dest` - `Write` implementor to copy data to.
///
/// # Returns
///
/// Result of operation and bytes successfully written (as a `u64`) when
/// operation is successful.
pub fn copy_stream<R, S>(src: &mut R, dest: &mut S) -> UResult<u64>
pub fn copy_stream<R, S>(src: &mut R, dest: &mut S) -> UResult<()>
where
R: Read,
S: Write,
{
let result = std::io::copy(src, dest)?;
Ok(result)
std::io::copy(src, dest)?;
Ok(())
}
Loading