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

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ regex = "1.10.4"
same-file = "1.0.6"
unicode-width = "0.2.0"

[target.'cfg(any(target_os = "linux", target_os = "android"))'.dependencies]
rustix = { version = "1.1.5", features = ["fs"] }

[dev-dependencies]
assert_cmd = "2.0.14"
divan = { version = "5.0.0", package = "codspeed-divan-compat" }
Expand Down
36 changes: 36 additions & 0 deletions fuzz/Cargo.lock

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

24 changes: 18 additions & 6 deletions src/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ use std::iter::Peekable;
use std::process::ExitCode;
use std::{cmp, fs, io};

#[cfg(unix)]
use std::os::fd::{AsRawFd, FromRawFd};

#[cfg(unix)]
use std::os::unix::fs::MetadataExt;

#[cfg(target_os = "windows")]
use std::os::windows::fs::MetadataExt;

Expand All @@ -43,8 +37,26 @@ fn usage_string(executable: &str) -> String {
format!("Usage: {executable} <from> <to>")
}

#[cfg(any(target_os = "linux", target_os = "android"))]
fn is_stdout_dev_null() -> bool {
// as_fd().try_clone_to_owned() needs additional overhead of fcntl

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

the unix version below doesn't need fcntl either (from_raw_fd + forget)
could you please do the same with MetadataExt::rdev() + FileTypeExt::is_char_device() and drop rustix?

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.

Is needs unsafe. Is it OK? clap-rs already pulls rustix.

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 think std does not have a feature to check major/minor.

use rustix::fs;
let stdout = io::stdout();
let Ok(stat) = fs::fstat(stdout) else {
return false;
};
let dev = stat.st_rdev;
Comment thread
oech3 marked this conversation as resolved.

let is_char_device = fs::FileType::from_raw_mode(stat.st_mode).is_char_device();
// /dev/null's major/minor is frozen on Linux. So we don't need to open new `/dev/null` and compare
is_char_device && fs::major(dev) == 1 && fs::minor(dev) == 3
}

#[cfg(unix)]
#[cfg(not(any(target_os = "linux", target_os = "android")))]
fn is_stdout_dev_null() -> bool {
use std::os::fd::{AsRawFd, FromRawFd};
use std::os::unix::fs::MetadataExt;
let Ok(dev_null) = fs::metadata("/dev/null") else {
return false;
};
Expand Down
Loading