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
3 changes: 3 additions & 0 deletions src/uu/tee/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ clap = { workspace = true }
uucore = { workspace = true, features = ["libc", "parser", "signals"] }
fluent = { workspace = true }

[target.'cfg(unix)'.dependencies]
rustix = { workspace = true }

[dev-dependencies]
divan = { workspace = true }
rustix = { workspace = true, features = ["stdio", "fs"] }
Expand Down
35 changes: 23 additions & 12 deletions src/uu/tee/src/tee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,21 +197,16 @@ impl MultiWriter {

fn write_flush(&mut self, buf: &[u8]) -> Result<()> {
let mode = self.output_error_mode;
self.writers.retain_mut(|writer| {
let res = (|| {
writer.inner.write_all(buf)?;
writer.inner.flush()
})();
match res {
self.writers
.retain_mut(|writer| match writer.inner.write_all(buf) {
Ok(()) => true,
Err(e) => {
if let Err(e) = process_error(mode, e, writer, &mut self.ignored_errors) {
self.aborted.get_or_insert(e);
}
false
}
}
});
});
self.aborted.take().map_or(
if self.writers.is_empty() {
// This error kind will never be raised by the standard
Expand Down Expand Up @@ -254,18 +249,34 @@ enum Writer {
Stdout(std::io::Stdout),
}

// raw syscall avoids buffering which is POSIX requirement
// throughput is better than flush after write_all by unknown reason
// since there is no rustix::io::write_all, we remove buffering by impl from write_all
#[cfg(unix)]
impl Write for Writer {
fn write(&mut self, buf: &[u8]) -> Result<usize> {
match self {
Self::File(f) => f.write(buf),
Self::Stdout(s) => s.write(buf),
Self::File(f) => Ok(rustix::io::write(f, buf)?),
Self::Stdout(s) => Ok(rustix::io::write(s, buf)?),
}
}

fn flush(&mut self) -> Result<()> {
Ok(())
}
}

#[cfg(not(unix))]
impl Writer {
// override write_all to avoid calling many flush
pub fn write_all(&mut self, buf: &[u8]) -> Result<()> {
match self {
Self::File(f) => f.flush(),
Self::Stdout(s) => s.flush(),
// File does not have line buffering
Self::File(f) => f.write_all(buf),
Self::Stdout(s) => {
s.write_all(buf)?;
s.flush() // todo: investigate how to remove flush overhead
}
}
}
}
Expand Down
Loading