Skip to content

Commit 90c113e

Browse files
committed
1 parent ea21b10 commit 90c113e

5 files changed

Lines changed: 41 additions & 18 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.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ rayon = "1.10"
393393
regex = "1.10.4"
394394
rstest = "0.27.0"
395395
rstest_reuse = "0.7.0"
396+
rustc_version = "0.4.1"
396397
rustc-hash = "2.1.1"
397398
rustix = { version = "1.1.4", default-features = false }
398399
self_cell = "1.0.4"
@@ -451,6 +452,7 @@ renamed_and_removed_lints = { level = "allow", priority = -1 }
451452
# https://doc.rust-lang.org/nightly/rustc/check-cfg/cargo-specifics.html
452453
unexpected_cfgs = { level = "warn", check-cfg = [
453454
'cfg(fuzzing)',
455+
'cfg(nightly)',
454456
'cfg(pgo_training)',
455457
'cfg(target_os, values("cygwin"))',
456458
'cfg(wasi_runner)',

src/uu/dd/Cargo.toml

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

44+
[build-dependencies]
45+
rustc_version = { workspace = true }
46+
4447
[lints]
4548
workspace = true
4649

src/uu/dd/build.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
if rustc_version::version_meta().unwrap().channel == rustc_version::Channel::Nightly {
8+
// Allows conditional compilation with `#[cfg(nightly)]`.
9+
println!("cargo:rustc-cfg=nightly");
10+
}
11+
}

src/uu/dd/src/dd.rs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
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, virtio, wlen, wstat, zram, oconv canonicalized FADV DONTNEED ESPIPE SPIPE bufferedoutput, SETFL
77

8+
// TODO: Remove once https://github.com/rust-lang/rust/issues/71213 is stabilized.
9+
#![cfg_attr(all(nightly, target_os = "wasi"), feature(wasi_ext))]
10+
811
mod blocks;
912
mod bufferedoutput;
1013
mod conversion_tables;
@@ -28,17 +31,18 @@ use uucore::translate;
2831
use std::cmp;
2932
use std::env;
3033
use std::ffi::OsString;
31-
#[cfg(unix)]
34+
#[cfg(any(unix, all(nightly, target_os = "wasi")))]
3235
use std::fs::Metadata;
3336
use std::fs::{File, OpenOptions};
3437
use std::io::{self, Read, Seek, SeekFrom, Write};
38+
#[cfg(any(unix, all(nightly, target_os = "wasi")))]
39+
use std::os::fd::{AsRawFd, FromRawFd};
40+
#[cfg(unix)]
41+
use std::os::unix::fs::FileTypeExt;
3542
#[cfg(any(target_os = "linux", target_os = "android"))]
3643
use std::os::unix::fs::OpenOptionsExt;
37-
#[cfg(unix)]
38-
use std::os::unix::{
39-
fs::FileTypeExt,
40-
io::{AsRawFd, FromRawFd},
41-
};
44+
#[cfg(all(nightly, target_os = "wasi"))]
45+
use std::os::wasi::fs::FileTypeExt;
4246
#[cfg(windows)]
4347
use std::os::windows::{fs::MetadataExt, io::AsHandle};
4448
use std::path::Path;
@@ -50,9 +54,11 @@ use std::time::{Duration, Instant};
5054
use clap::{Arg, Command};
5155
use num_integer::Integer;
5256
use uucore::display::Quotable;
53-
use uucore::error::{FromIo, UResult};
5457
#[cfg(unix)]
55-
use uucore::error::{USimpleError, set_exit_code};
58+
use uucore::error::USimpleError;
59+
#[cfg(any(unix, all(nightly, target_os = "wasi")))]
60+
use uucore::error::set_exit_code;
61+
use uucore::error::{FromIo, UResult};
5662
#[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd"))]
5763
use uucore::show_if_err;
5864
use uucore::{format_usage, show_error};
@@ -213,14 +219,14 @@ impl AlignedBuf {
213219
/// fine-grained access to reading from stdin.
214220
enum Source {
215221
/// Input from stdin.
216-
#[cfg(not(unix))]
222+
#[cfg(not(any(unix, all(nightly, target_os = "wasi"))))]
217223
Stdin(io::Stdin),
218224

219225
/// Input from a file.
220226
File(File),
221227

222228
/// Input from stdin, opened from its file descriptor.
223-
#[cfg(unix)]
229+
#[cfg(any(unix, all(nightly, target_os = "wasi")))]
224230
StdinFile(File),
225231

226232
/// Input from a named pipe, also known as a FIFO.
@@ -236,7 +242,7 @@ impl Source {
236242
/// the [`File`] parameter. You can use this instead of
237243
/// `Source::Stdin` to allow reading from stdin without consuming
238244
/// the entire contents of stdin when this process terminates.
239-
#[cfg(unix)]
245+
#[cfg(any(unix, all(nightly, target_os = "wasi")))]
240246
fn stdin_as_file() -> Self {
241247
let fd = io::stdin().as_raw_fd();
242248
let f = unsafe { File::from_raw_fd(fd) };
@@ -245,7 +251,7 @@ impl Source {
245251

246252
fn skip(&mut self, n: u64, ibs: usize) -> io::Result<u64> {
247253
match self {
248-
#[cfg(not(unix))]
254+
#[cfg(not(any(unix, all(nightly, target_os = "wasi"))))]
249255
Self::Stdin(stdin) => {
250256
let m = uucore::io::read_and_discard(stdin, n, ibs)?;
251257
if m < n {
@@ -256,7 +262,7 @@ impl Source {
256262
}
257263
Ok(m)
258264
}
259-
#[cfg(unix)]
265+
#[cfg(any(unix, all(nightly, target_os = "wasi")))]
260266
Self::StdinFile(f) => {
261267
if let Ok(Some(len)) = try_get_len_of_block_device(f)
262268
&& len < n
@@ -335,10 +341,10 @@ impl Source {
335341
impl Read for Source {
336342
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
337343
match self {
338-
#[cfg(not(unix))]
344+
#[cfg(not(any(unix, all(nightly, target_os = "wasi"))))]
339345
Self::Stdin(stdin) => stdin.read(buf),
340346
Self::File(f) => f.read(buf),
341-
#[cfg(unix)]
347+
#[cfg(any(unix, all(nightly, target_os = "wasi")))]
342348
Self::StdinFile(f) => f.read(buf),
343349
#[cfg(unix)]
344350
Self::Fifo(f) => f.read(buf),
@@ -376,9 +382,9 @@ impl<'a> Input<'a> {
376382
_ => Source::Stdin(io::stdin()),
377383
}
378384
};
379-
#[cfg(all(not(unix), not(windows)))]
385+
#[cfg(all(not(unix), not(windows), not(all(nightly, target_os = "wasi"))))]
380386
let mut src = Source::Stdin(io::stdin());
381-
#[cfg(unix)]
387+
#[cfg(any(unix, all(nightly, target_os = "wasi")))]
382388
let mut src = Source::stdin_as_file();
383389
#[cfg(unix)]
384390
if let Source::StdinFile(f) = &src
@@ -1538,7 +1544,7 @@ fn is_stdout_redirected_to_seekable_file() -> bool {
15381544
}
15391545

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

0 commit comments

Comments
 (0)