Skip to content

Commit 44a644f

Browse files
committed
cp: omit pipe() if input is pipe
1 parent c170a18 commit 44a644f

1 file changed

Lines changed: 16 additions & 15 deletions

File tree

  • src/uucore/src/lib/features/buf_copy

src/uucore/src/lib/features/buf_copy/linux.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,22 @@ where
3939
R: Read + AsFd + AsRawFd,
4040
S: Write + AsFd + AsRawFd,
4141
{
42-
// If we're on Linux or Android, try to use the splice() system call
43-
// for faster writing. If it works, we're done.
44-
// todo: bypass broker pipe this if input or output is pipe. We use this mostly for stream.
45-
if !crate::pipes::splice_unbounded_broker(src, dest)? {
46-
return Ok(());
42+
// try to use the splice() system call
43+
// for faster writing. If it works, we're done
44+
// todo: dedup with cat/src/splice.rs
45+
let fallback = match crate::pipes::splice(&src, dest, crate::pipes::MAX_ROOTLESS_PIPE_SIZE) {
46+
Ok(_) => crate::pipes::splice_unbounded(src, dest)?,
47+
// both of in/output are not pipe
48+
_ => crate::pipes::splice_unbounded_broker(src, dest)?,
49+
};
50+
if fallback {
51+
std::io::copy(src, dest)?;
52+
// If the splice() call failed and there has been some data written to
53+
// stdout via while loop above AND there will be second splice() call
54+
// that will succeed, data pushed through splice will be output before
55+
// the data buffered in stdout.lock. Therefore additional explicit flush
56+
// is required here.
57+
dest.flush()?;
4758
}
48-
49-
// If the splice() call failed, fall back on slower writing.
50-
std::io::copy(src, dest)?;
51-
52-
// If the splice() call failed and there has been some data written to
53-
// stdout via while loop above AND there will be second splice() call
54-
// that will succeed, data pushed through splice will be output before
55-
// the data buffered in stdout.lock. Therefore additional explicit flush
56-
// is required here.
57-
dest.flush()?;
5859
Ok(())
5960
}

0 commit comments

Comments
 (0)