Skip to content

Commit 281513e

Browse files
committed
od: remove unsafe, GNU stdin compat on wasi
1 parent 6276737 commit 281513e

3 files changed

Lines changed: 14 additions & 8 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/uu/od/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ path = "src/od.rs"
2121
byteorder = { workspace = true }
2222
clap = { workspace = true }
2323
half = { workspace = true }
24+
rustix = { workspace = true, features = ["stdio"] }
2425
uucore = { workspace = true, features = ["parser-size"] }
2526
fluent = { workspace = true }
2627
libc.workspace = true

src/uu/od/src/multifile_reader.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
use std::fs::File;
88
use std::io;
9-
#[cfg(unix)]
10-
use std::os::fd::{AsRawFd, FromRawFd};
119

1210
use uucore::display::Quotable;
1311
use uucore::show_error;
@@ -56,20 +54,26 @@ impl MultifileReader<'_> {
5654
// For performance reasons we do still do buffered reads from stdin, but
5755
// the buffering is done elsewhere and in a way that is aware of the `-N`
5856
// limit.
59-
let stdin = io::stdin();
60-
#[cfg(unix)]
57+
#[cfg(any(unix, target_os = "wasi"))]
6158
{
62-
let stdin_raw_fd = stdin.as_raw_fd();
63-
let stdin_file = unsafe { File::from_raw_fd(stdin_raw_fd) };
64-
self.curr_file = Some(Box::new(stdin_file));
59+
// no need to have unsafe for unix
60+
struct RawReader<'a>(rustix::fd::BorrowedFd<'a>);
61+
impl io::Read for RawReader<'_> {
62+
fn read(&mut self, b: &mut [u8]) -> io::Result<usize> {
63+
rustix::io::read(self.0, b).map_err(Into::into)
64+
}
65+
}
66+
let stdin = RawReader(rustix::stdio::stdin());
67+
self.curr_file = Some(Box::new(stdin));
6568
}
6669

6770
// For non-unix platforms we don't have GNU compatibility requirements, so
6871
// we don't need to prevent stdin buffering. This is sub-optimal (since
6972
// there will still be additional buffering further up the stack), but
7073
// doesn't seem worth worrying about at this time.
71-
#[cfg(not(unix))]
74+
#[cfg(not(any(unix, target_os = "wasi")))]
7275
{
76+
let stdin = io::stdin();
7377
self.curr_file = Some(Box::new(stdin));
7478
}
7579
break;

0 commit comments

Comments
 (0)