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
7 changes: 4 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ regex = "1.10.4"
rlimit = "0.11.0"
rstest = "0.26.0"
rstest_reuse = "0.7.0"
rustc_version = "0.4.1"
rustc-hash = "2.1.1"
rust-ini = "0.21.0"
# raw-backend's linux_execfn is not supported on linux < 6.4 if /proc is not mounted. So use-libc-auxv is needed
Expand Down
3 changes: 3 additions & 0 deletions src/uu/dd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ divan = { workspace = true }
tempfile = { workspace = true }
uucore = { workspace = true, features = ["benchmark"] }

[build-dependencies]
rustc_version = { workspace = true }

[[bench]]
name = "dd_bench"
harness = false
15 changes: 15 additions & 0 deletions src/uu/dd/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// This file is part of the uutils coreutils package.
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

fn main() {
Comment thread
xtqqczze marked this conversation as resolved.
println!("cargo:rustc-check-cfg=cfg(nightly)");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still not a fan of this
esp without any comment on the why


let is_nightly =
rustc_version::version_meta().unwrap().channel == rustc_version::Channel::Nightly;

if is_nightly {
println!("cargo:rustc-cfg=nightly");
}
}
41 changes: 23 additions & 18 deletions src/uu/dd/src/dd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

// 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

#![cfg_attr(all(target_os = "wasi", nightly), feature(wasi_ext))]

mod blocks;
mod bufferedoutput;
mod conversion_tables;
Expand All @@ -31,19 +33,20 @@ use uucore::translate;
use std::cmp;
use std::env;
use std::ffi::OsString;
#[cfg(unix)]
#[cfg(any(unix, all(target_os = "wasi", nightly)))]
use std::fs::Metadata;
use std::fs::{File, OpenOptions};
use std::io::{self, Read, Seek, SeekFrom, Write};
#[cfg(any(target_os = "linux", target_os = "android"))]
use std::os::fd::AsFd;
#[cfg(any(unix, all(target_os = "wasi", nightly)))]
use std::os::fd::{AsRawFd, FromRawFd};
#[cfg(unix)]
use std::os::unix::fs::FileTypeExt;
#[cfg(any(target_os = "linux", target_os = "android"))]
use std::os::unix::fs::OpenOptionsExt;
#[cfg(unix)]
use std::os::unix::{
fs::FileTypeExt,
io::{AsRawFd, FromRawFd},
};
#[cfg(all(target_os = "wasi", nightly))]
use std::os::wasi::fs::FileTypeExt;
#[cfg(windows)]
use std::os::windows::{fs::MetadataExt, io::AsHandle};
use std::path::Path;
Expand All @@ -60,9 +63,11 @@ use nix::{
fcntl::{PosixFadviseAdvice, posix_fadvise},
};
use uucore::display::Quotable;
use uucore::error::{FromIo, UResult};
#[cfg(unix)]
use uucore::error::{USimpleError, set_exit_code};
use uucore::error::USimpleError;
#[cfg(any(unix, all(target_os = "wasi", nightly)))]
use uucore::error::set_exit_code;
use uucore::error::{FromIo, UResult};
#[cfg(target_os = "linux")]
use uucore::show_if_err;
use uucore::{format_usage, show_error};
Expand Down Expand Up @@ -212,14 +217,14 @@ fn read_and_discard<R: Read>(reader: &mut R, n: u64, buf_size: usize) -> io::Res
/// fine-grained access to reading from stdin.
enum Source {
/// Input from stdin.
#[cfg(not(unix))]
#[cfg(not(any(unix, all(target_os = "wasi", nightly))))]
Stdin(io::Stdin),

/// Input from a file.
File(File),

/// Input from stdin, opened from its file descriptor.
#[cfg(unix)]
#[cfg(any(unix, all(target_os = "wasi", nightly)))]
StdinFile(File),

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

fn skip(&mut self, n: u64, ibs: usize) -> io::Result<u64> {
match self {
#[cfg(not(unix))]
#[cfg(not(any(unix, all(target_os = "wasi", nightly))))]
Self::Stdin(stdin) => {
let m = read_and_discard(stdin, n, ibs)?;
if m < n {
Expand All @@ -255,7 +260,7 @@ impl Source {
}
Ok(m)
}
#[cfg(unix)]
#[cfg(any(unix, all(target_os = "wasi", nightly)))]
Self::StdinFile(f) => {
if let Ok(Some(len)) = try_get_len_of_block_device(f)
&& len < n
Expand Down Expand Up @@ -332,10 +337,10 @@ impl Source {
impl Read for Source {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
match self {
#[cfg(not(unix))]
#[cfg(not(any(unix, all(target_os = "wasi", nightly))))]
Self::Stdin(stdin) => stdin.read(buf),
Self::File(f) => f.read(buf),
#[cfg(unix)]
#[cfg(any(unix, all(target_os = "wasi", nightly)))]
Self::StdinFile(f) => f.read(buf),
#[cfg(unix)]
Self::Fifo(f) => f.read(buf),
Expand Down Expand Up @@ -379,9 +384,9 @@ impl<'a> Input<'a> {
Source::Stdin(io::stdin())
}
};
#[cfg(all(not(unix), not(windows)))]
#[cfg(all(not(unix), not(windows), not(all(target_os = "wasi", nightly))))]
let mut src = Source::Stdin(io::stdin());
#[cfg(unix)]
#[cfg(any(unix, all(target_os = "wasi", nightly)))]
let mut src = Source::stdin_as_file();
#[cfg(unix)]
if let Source::StdinFile(f) = &src
Expand Down Expand Up @@ -1491,7 +1496,7 @@ fn is_stdout_redirected_to_seekable_file() -> bool {
}

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