Skip to content

Commit 53761c0

Browse files
oech3gabrielhnf
authored andcommitted
tee: catch EINTR at write
1 parent 4166ea0 commit 53761c0

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

  • src/uucore/src/lib/mods

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,31 @@ impl<T: AsFd> io::Write for RawWriter<T> {
5353
}
5454
}
5555

56+
// write_all retries with EINTR which is sometimes not compatible with GNU
57+
#[cfg(any(unix, target_os = "wasi"))]
58+
pub trait AsFdExt {
59+
fn write_all_no_retry(&self, buf: &[u8]) -> io::Result<()>;
60+
}
61+
#[cfg(any(unix, target_os = "wasi"))]
62+
impl<T: AsFd> AsFdExt for T {
63+
fn write_all_no_retry(&self, mut buf: &[u8]) -> io::Result<()> {
64+
let fd = self.as_fd();
65+
while !buf.is_empty() {
66+
match rustix::io::write(fd, buf) {
67+
Ok(0) => {
68+
return Err(io::Error::new(
69+
io::ErrorKind::WriteZero,
70+
"failed to write whole buffer",
71+
));
72+
}
73+
Ok(n) => buf = &buf[n..],
74+
Err(e) => return Err(e.into()),
75+
}
76+
}
77+
Ok(())
78+
}
79+
}
80+
5681
/// abstraction wrapper for native file handle / file descriptor
5782
// todo: remove clone introducing additional syscall dependency
5883
pub struct OwnedFileDescriptorOrHandle {

0 commit comments

Comments
 (0)