mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 03:27:44 +00:00
wc: fix counting files from pseudo-filesystem
This commit is contained in:
parent
9dbff22101
commit
dd311b294b
3 changed files with 19 additions and 1 deletions
|
@ -37,6 +37,8 @@ exponentiate
|
||||||
eval
|
eval
|
||||||
falsey
|
falsey
|
||||||
fileio
|
fileio
|
||||||
|
filesystem
|
||||||
|
filesystems
|
||||||
flamegraph
|
flamegraph
|
||||||
fullblock
|
fullblock
|
||||||
getfacl
|
getfacl
|
||||||
|
|
|
@ -80,7 +80,13 @@ pub(crate) fn count_bytes_fast<T: WordCountable>(handle: &mut T) -> (usize, Opti
|
||||||
if let Ok(stat) = stat::fstat(fd) {
|
if let Ok(stat) = stat::fstat(fd) {
|
||||||
// If the file is regular, then the `st_size` should hold
|
// If the file is regular, then the `st_size` should hold
|
||||||
// the file's size in bytes.
|
// the file's size in bytes.
|
||||||
if (stat.st_mode & S_IFREG) != 0 {
|
// If stat.st_size = 0 then
|
||||||
|
// - either the size is 0
|
||||||
|
// - or the size is unknown.
|
||||||
|
// The second case happens for files in pseudo-filesystems. For
|
||||||
|
// example with /proc/version and /sys/kernel/profiling. So,
|
||||||
|
// if it is 0 we don't report that and instead do a full read.
|
||||||
|
if (stat.st_mode & S_IFREG) != 0 && stat.st_size > 0 {
|
||||||
return (stat.st_size as usize, None);
|
return (stat.st_size as usize, None);
|
||||||
}
|
}
|
||||||
#[cfg(any(target_os = "linux", target_os = "android"))]
|
#[cfg(any(target_os = "linux", target_os = "android"))]
|
||||||
|
|
|
@ -1,3 +1,6 @@
|
||||||
|
#[cfg(all(unix, not(target_os = "macos")))]
|
||||||
|
use pretty_assertions::assert_ne;
|
||||||
|
|
||||||
use crate::common::util::*;
|
use crate::common::util::*;
|
||||||
|
|
||||||
// spell-checker:ignore (flags) lwmcL clmwL ; (path) bogusfile emptyfile manyemptylines moby notrailingnewline onelongemptyline onelongword weirdchars
|
// spell-checker:ignore (flags) lwmcL clmwL ; (path) bogusfile emptyfile manyemptylines moby notrailingnewline onelongemptyline onelongword weirdchars
|
||||||
|
@ -235,3 +238,10 @@ fn test_read_from_nonexistent_file() {
|
||||||
.stderr_contains(MSG)
|
.stderr_contains(MSG)
|
||||||
.stdout_is("");
|
.stdout_is("");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[cfg(all(unix, not(target_os = "macos")))]
|
||||||
|
fn test_files_from_pseudo_filesystem() {
|
||||||
|
let result = new_ucmd!().arg("-c").arg("/proc/version").succeeds();
|
||||||
|
assert_ne!(result.stdout_str(), "0 /proc/version\n");
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue