Skip to content

Commit cf8e4eb

Browse files
committed
cat: move cfg to uucore
1 parent 055a66b commit cf8e4eb

2 files changed

Lines changed: 19 additions & 30 deletions

File tree

src/uu/cat/src/cat.rs

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -497,43 +497,26 @@ fn print_fast<R: FdReadable>(handle: &mut InputHandle<R>) -> CatResult<()> {
497497
}
498498

499499
#[cfg_attr(any(target_os = "linux", target_os = "android"), inline(never))] // splice fast-path does not require this allocation
500-
#[cfg(any(unix, target_os = "wasi"))]
501500
fn print_unbuffered<R: FdReadable>(
502501
handle: &mut InputHandle<R>,
503502
stdout: io::Stdout,
504503
) -> CatResult<()> {
504+
#[cfg(not(any(unix, target_os = "wasi")))]
505+
let mut stdout = stdout.lock();
505506
// todo: since there is no cost by 0-fill, we could use larger heap buffer for throughput
507+
#[cfg(any(unix, target_os = "wasi"))]
506508
let mut buf = [std::mem::MaybeUninit::<u8>::uninit(); 1024 * 64];
507-
// use raw syscall to remove buffering
508-
loop {
509-
match rustix::io::read(&handle.reader, &mut buf) {
510-
Ok(([], _)) => return Ok(()),
511-
Ok((filled, _)) => {
512-
uucore::io::write_all_raw(&stdout, filled).inspect_err(handle_broken_pipe)?;
513-
}
514-
Err(e) if e.kind() != ErrorKind::Interrupted => return Err(e.into()),
515-
_ => {}
516-
}
517-
}
518-
}
519-
520-
#[cfg(not(any(unix, target_os = "wasi")))]
521-
fn print_unbuffered<R: FdReadable>(
522-
handle: &mut InputHandle<R>,
523-
stdout: io::Stdout,
524-
) -> CatResult<()> {
525-
let mut stdout = stdout.lock();
509+
#[cfg(not(any(unix, target_os = "wasi")))]
526510
let mut buf = [0; 1024 * 64];
527511
loop {
528-
match handle.reader.read(&mut buf) {
529-
Ok(0) => return Ok(()),
530-
Ok(n) => {
531-
stdout
532-
.write_all(&buf[..n])
533-
.inspect_err(handle_broken_pipe)?;
534-
// we cannot use rustix::io on Windows
535-
// really bad workaround for unbuffered write <https://github.com/uutils/coreutils/issues/12188>
536-
stdout.flush().inspect_err(handle_broken_pipe)?;
512+
#[cfg(any(unix, target_os = "wasi"))]
513+
let res = rustix::io::read(&handle.reader, &mut buf).map(|(f, _)| f);
514+
#[cfg(not(any(unix, target_os = "wasi")))]
515+
let res = handle.reader.read(&mut buf).map(|n| &buf[..n]);
516+
match res {
517+
Ok([]) => return Ok(()),
518+
Ok(filled) => {
519+
uucore::io::write_all_raw(&stdout, filled).inspect_err(handle_broken_pipe)?;
537520
}
538521
Err(e) if e.kind() != ErrorKind::Interrupted => return Err(e.into()),
539522
_ => {}

src/uucore/src/lib/mods/io.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type NativeType = OwnedHandle;
3030
#[cfg(not(windows))]
3131
type NativeType = OwnedFd;
3232

33-
// io::write_all but no buffering
33+
// io::write_all-like function without io::stdout's buffering
3434
#[inline]
3535
#[cfg(any(unix, target_os = "wasi"))]
3636
pub fn write_all_raw(output: impl AsFd, buf: &[u8]) -> io::Result<()> {
@@ -46,6 +46,12 @@ pub fn write_all_raw(output: impl AsFd, buf: &[u8]) -> io::Result<()> {
4646
Ok(())
4747
}
4848

49+
#[cfg(not(any(unix, target_os = "wasi")))]
50+
pub fn write_all_raw<W: io::Write>(mut output: W, buf: &[u8]) -> io::Result<()> {
51+
output.write_all(&buf)?;
52+
output.flush() // workaround for unbuffered write <https://github.com/uutils/coreutils/issues/12188>
53+
}
54+
4955
/// abstraction wrapper for native file handle / file descriptor
5056
// todo: remove clone introducing additional syscall dependency
5157
pub struct OwnedFileDescriptorOrHandle {

0 commit comments

Comments
 (0)