1
Fork 0
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:
Terts Diepraam 2022-01-28 18:17:40 +01:00
parent 9dbff22101
commit dd311b294b
3 changed files with 19 additions and 1 deletions

View file

@ -37,6 +37,8 @@ exponentiate
eval
falsey
fileio
filesystem
filesystems
flamegraph
fullblock
getfacl

View file

@ -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 the file is regular, then the `st_size` should hold
// 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);
}
#[cfg(any(target_os = "linux", target_os = "android"))]

View file

@ -1,3 +1,6 @@
#[cfg(all(unix, not(target_os = "macos")))]
use pretty_assertions::assert_ne;
use crate::common::util::*;
// 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)
.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");
}