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
1 change: 1 addition & 0 deletions .vscode/cspell.dictionaries/workspace.wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ doctest
# * crates
aho-corasick
blake2b_simd
rsconf
rustix

# * uutils project
Expand Down
31 changes: 21 additions & 10 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -446,8 +446,10 @@ rand_chacha = { version = "0.10.0" }
rayon = "1.10"
regex = "1.10.4"
rlimit = "0.11.0"
rsconf = "0.3.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"
# binary name of coreutils can be hijacked by overriding getauxval via LD_PRELOAD
Expand Down
4 changes: 4 additions & 0 deletions src/uu/dd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ divan = { workspace = true }
tempfile = { workspace = true }
uucore = { workspace = true, features = ["benchmark"] }

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

[[bench]]
name = "dd_bench"
harness = false
10 changes: 10 additions & 0 deletions src/uu/dd/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// 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() {
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.

why that?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

wasi-ext is not yet stabilised, we need to detect if we are compiling with 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.

Can we use cfg_aliases to avoid new dep?

let is_nightly =
rustc_version::version_meta().unwrap().channel == rustc_version::Channel::Nightly;
rsconf::declare_cfg("nightly", is_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.

Is RUSTC_BOOTSTRAP=1 supported?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

There is no way to detect whether a certain unstable feature is enabled; we have to check for nightly compiler. So RUSTC_BOOTSTRAP=1 won't be supported.

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.

Just manually catching?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I don't know, I'm open to suggestions.

Comment on lines +7 to +9
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Suggested change
let is_nightly =
rustc_version::version_meta().unwrap().channel == rustc_version::Channel::Nightly;
rsconf::declare_cfg("nightly", is_nightly);
println!("cargo:rustc-check-cfg=cfg(nightly)");
if rustc_version::version_meta().unwrap().channel == rustc_version::Channel::Nightly {
println!("cargo:rustc-cfg=nightly");
}

I guess we can lose the rsconf build dependency if that's an issue?

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.

Yes. Because we can reduce build time.

}
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 @@ -211,14 +216,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 @@ -234,7 +239,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 @@ -243,7 +248,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 @@ -254,7 +259,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 @@ -331,10 +336,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 @@ -378,9 +383,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 @@ -1490,7 +1495,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