Skip to content

Commit f7726da

Browse files
committed
dd: enable WASI support for StdinFile on nightly
1 parent 19c7f64 commit f7726da

6 files changed

Lines changed: 47 additions & 21 deletions

File tree

.vscode/cspell.dictionaries/workspace.wordlist.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ doctest
55
# * crates
66
aho-corasick
77
blake2b_simd
8+
rsconf
89
rustix
910

1011
# * uutils project

Cargo.lock

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,7 @@ regex = "1.10.4"
449449
rlimit = "0.11.0"
450450
rstest = "0.26.0"
451451
rstest_reuse = "0.7.0"
452+
rustc_version = "0.4.1"
452453
rustc-hash = "2.1.1"
453454
rust-ini = "0.21.0"
454455
# raw-backend's linux_execfn is not supported on linux < 6.4 if /proc is not mounted. So use-libc-auxv is needed

src/uu/dd/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ divan = { workspace = true }
4444
tempfile = { workspace = true }
4545
uucore = { workspace = true, features = ["benchmark"] }
4646

47+
[build-dependencies]
48+
rustc_version = { workspace = true }
49+
4750
[[bench]]
4851
name = "dd_bench"
4952
harness = false

src/uu/dd/build.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// This file is part of the uutils coreutils package.
2+
//
3+
// For the full copyright and license information, please view the LICENSE
4+
// file that was distributed with this source code.
5+
6+
fn main() {
7+
println!("cargo:rustc-check-cfg=cfg(nightly)");
8+
9+
let is_nightly =
10+
rustc_version::version_meta().unwrap().channel == rustc_version::Channel::Nightly;
11+
12+
if is_nightly {
13+
println!("cargo:rustc-cfg=nightly");
14+
}
15+
}

src/uu/dd/src/dd.rs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
// spell-checker:ignore fname, ftype, tname, fpath, specfile, testfile, unspec, ifile, ofile, outfile, fullblock, urand, fileio, atoe, atoibm, behaviour, bmax, bremain, cflags, creat, ctable, ctty, datastructures, doesnt, etoa, fileout, fname, gnudd, iconvflags, iseek, nocache, noctty, noerror, nofollow, nolinks, nonblock, oconvflags, oseek, outfile, parseargs, rlen, rmax, rremain, rsofar, rstat, sigusr, wlen, wstat oconv canonicalized fadvise Fadvise FADV DONTNEED ESPIPE bufferedoutput, SETFL
77

8+
#![cfg_attr(all(target_os = "wasi", nightly), feature(wasi_ext))]
9+
810
mod blocks;
911
mod bufferedoutput;
1012
mod conversion_tables;
@@ -31,19 +33,20 @@ use uucore::translate;
3133
use std::cmp;
3234
use std::env;
3335
use std::ffi::OsString;
34-
#[cfg(unix)]
36+
#[cfg(any(unix, all(target_os = "wasi", nightly)))]
3537
use std::fs::Metadata;
3638
use std::fs::{File, OpenOptions};
3739
use std::io::{self, Read, Seek, SeekFrom, Write};
3840
#[cfg(any(target_os = "linux", target_os = "android"))]
3941
use std::os::fd::AsFd;
42+
#[cfg(any(unix, all(target_os = "wasi", nightly)))]
43+
use std::os::fd::{AsRawFd, FromRawFd};
44+
#[cfg(unix)]
45+
use std::os::unix::fs::FileTypeExt;
4046
#[cfg(any(target_os = "linux", target_os = "android"))]
4147
use std::os::unix::fs::OpenOptionsExt;
42-
#[cfg(unix)]
43-
use std::os::unix::{
44-
fs::FileTypeExt,
45-
io::{AsRawFd, FromRawFd},
46-
};
48+
#[cfg(all(target_os = "wasi", nightly))]
49+
use std::os::wasi::fs::FileTypeExt;
4750
#[cfg(windows)]
4851
use std::os::windows::{fs::MetadataExt, io::AsHandle};
4952
use std::path::Path;
@@ -60,9 +63,11 @@ use nix::{
6063
fcntl::{PosixFadviseAdvice, posix_fadvise},
6164
};
6265
use uucore::display::Quotable;
63-
use uucore::error::{FromIo, UResult};
6466
#[cfg(unix)]
65-
use uucore::error::{USimpleError, set_exit_code};
67+
use uucore::error::USimpleError;
68+
#[cfg(any(unix, all(target_os = "wasi", nightly)))]
69+
use uucore::error::set_exit_code;
70+
use uucore::error::{FromIo, UResult};
6671
#[cfg(target_os = "linux")]
6772
use uucore::show_if_err;
6873
use uucore::{format_usage, show_error};
@@ -212,14 +217,14 @@ fn read_and_discard<R: Read>(reader: &mut R, n: u64, buf_size: usize) -> io::Res
212217
/// fine-grained access to reading from stdin.
213218
enum Source {
214219
/// Input from stdin.
215-
#[cfg(not(unix))]
220+
#[cfg(not(any(unix, all(target_os = "wasi", nightly))))]
216221
Stdin(io::Stdin),
217222

218223
/// Input from a file.
219224
File(File),
220225

221226
/// Input from stdin, opened from its file descriptor.
222-
#[cfg(unix)]
227+
#[cfg(any(unix, all(target_os = "wasi", nightly)))]
223228
StdinFile(File),
224229

225230
/// Input from a named pipe, also known as a FIFO.
@@ -235,7 +240,7 @@ impl Source {
235240
/// the [`File`] parameter. You can use this instead of
236241
/// `Source::Stdin` to allow reading from stdin without consuming
237242
/// the entire contents of stdin when this process terminates.
238-
#[cfg(unix)]
243+
#[cfg(any(unix, all(target_os = "wasi", nightly)))]
239244
fn stdin_as_file() -> Self {
240245
let fd = io::stdin().as_raw_fd();
241246
let f = unsafe { File::from_raw_fd(fd) };
@@ -244,7 +249,7 @@ impl Source {
244249

245250
fn skip(&mut self, n: u64, ibs: usize) -> io::Result<u64> {
246251
match self {
247-
#[cfg(not(unix))]
252+
#[cfg(not(any(unix, all(target_os = "wasi", nightly))))]
248253
Self::Stdin(stdin) => {
249254
let m = read_and_discard(stdin, n, ibs)?;
250255
if m < n {
@@ -255,7 +260,7 @@ impl Source {
255260
}
256261
Ok(m)
257262
}
258-
#[cfg(unix)]
263+
#[cfg(any(unix, all(target_os = "wasi", nightly)))]
259264
Self::StdinFile(f) => {
260265
if let Ok(Some(len)) = try_get_len_of_block_device(f)
261266
&& len < n
@@ -332,10 +337,10 @@ impl Source {
332337
impl Read for Source {
333338
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
334339
match self {
335-
#[cfg(not(unix))]
340+
#[cfg(not(any(unix, all(target_os = "wasi", nightly))))]
336341
Self::Stdin(stdin) => stdin.read(buf),
337342
Self::File(f) => f.read(buf),
338-
#[cfg(unix)]
343+
#[cfg(any(unix, all(target_os = "wasi", nightly)))]
339344
Self::StdinFile(f) => f.read(buf),
340345
#[cfg(unix)]
341346
Self::Fifo(f) => f.read(buf),
@@ -379,9 +384,9 @@ impl<'a> Input<'a> {
379384
Source::Stdin(io::stdin())
380385
}
381386
};
382-
#[cfg(all(not(unix), not(windows)))]
387+
#[cfg(all(not(unix), not(windows), not(all(target_os = "wasi", nightly))))]
383388
let mut src = Source::Stdin(io::stdin());
384-
#[cfg(unix)]
389+
#[cfg(any(unix, all(target_os = "wasi", nightly)))]
385390
let mut src = Source::stdin_as_file();
386391
#[cfg(unix)]
387392
if let Source::StdinFile(f) = &src
@@ -1491,7 +1496,7 @@ fn is_stdout_redirected_to_seekable_file() -> bool {
14911496
}
14921497

14931498
/// Try to get the len if it is a block device
1494-
#[cfg(unix)]
1499+
#[cfg(any(unix, all(target_os = "wasi", nightly)))]
14951500
fn try_get_len_of_block_device(file: &mut File) -> io::Result<Option<u64>> {
14961501
let ftype = file.metadata()?.file_type();
14971502
if !ftype.is_block_device() {

0 commit comments

Comments
 (0)