Skip to main content

coreutils_rs/common/
io.rs

1use std::fs::{self, File};
2use std::io::{self, Read};
3use std::ops::Deref;
4use std::path::Path;
5
6#[cfg(target_os = "linux")]
7use std::sync::atomic::{AtomicBool, Ordering};
8
9use memmap2::{Mmap, MmapOptions};
10
11/// Holds file data — either zero-copy mmap or an owned Vec.
12/// Dereferences to `&[u8]` for transparent use.
13pub enum FileData {
14    Mmap(Mmap),
15    Owned(Vec<u8>),
16}
17
18impl Deref for FileData {
19    type Target = [u8];
20
21    fn deref(&self) -> &[u8] {
22        match self {
23            FileData::Mmap(m) => m,
24            FileData::Owned(v) => v,
25        }
26    }
27}
28
29/// Threshold below which we use read() instead of mmap.
30/// For files under 1MB, read() is faster since mmap has setup/teardown overhead
31/// (page table creation for up to 256 pages, TLB flush on munmap) that exceeds
32/// the zero-copy benefit.
33const MMAP_THRESHOLD: u64 = 1024 * 1024;
34
35/// Track whether O_NOATIME is supported to avoid repeated failed open() attempts.
36/// After the first EPERM, we never try O_NOATIME again (saves one syscall per file).
37#[cfg(target_os = "linux")]
38static NOATIME_SUPPORTED: AtomicBool = AtomicBool::new(true);
39
40/// Open a file with O_NOATIME on Linux to avoid atime inode writes.
41/// Caches whether O_NOATIME works to avoid double-open on every file.
42#[cfg(target_os = "linux")]
43fn open_noatime(path: &Path) -> io::Result<File> {
44    use std::os::unix::fs::OpenOptionsExt;
45    if NOATIME_SUPPORTED.load(Ordering::Relaxed) {
46        match fs::OpenOptions::new()
47            .read(true)
48            .custom_flags(libc::O_NOATIME)
49            .open(path)
50        {
51            Ok(f) => return Ok(f),
52            Err(ref e) if e.raw_os_error() == Some(libc::EPERM) => {
53                // O_NOATIME requires file ownership or CAP_FOWNER — disable globally
54                NOATIME_SUPPORTED.store(false, Ordering::Relaxed);
55            }
56            Err(e) => return Err(e), // Real error, propagate
57        }
58    }
59    File::open(path)
60}
61
62#[cfg(not(target_os = "linux"))]
63fn open_noatime(path: &Path) -> io::Result<File> {
64    File::open(path)
65}
66
67/// Read a file with zero-copy mmap for large files or read() for small files.
68/// Opens once with O_NOATIME, uses fstat for metadata to save a syscall.
69pub fn read_file(path: &Path) -> io::Result<FileData> {
70    let file = open_noatime(path)?;
71    let metadata = file.metadata()?;
72    let len = metadata.len();
73
74    if len > 0 && metadata.file_type().is_file() {
75        // Small files: exact-size read from already-open fd.
76        // Uses read_full into pre-sized buffer instead of read_to_end,
77        // which avoids the grow-and-probe pattern (saves 1-2 extra read() syscalls).
78        if len < MMAP_THRESHOLD {
79            let mut buf = vec![0u8; len as usize];
80            let n = read_full(&mut &file, &mut buf)?;
81            buf.truncate(n);
82            return Ok(FileData::Owned(buf));
83        }
84
85        // SAFETY: Read-only mapping. MADV_SEQUENTIAL lets the kernel
86        // prefetch ahead of our sequential access pattern.
87        match unsafe { MmapOptions::new().populate().map(&file) } {
88            Ok(mmap) => {
89                #[cfg(target_os = "linux")]
90                {
91                    let _ = mmap.advise(memmap2::Advice::Sequential);
92                    // HUGEPAGE reduces TLB misses for large files (2MB+ = 1+ huge page).
93                    // With 4KB pages, a 100MB file needs 25,600 TLB entries; with 2MB
94                    // huge pages it needs only 50, reducing TLB miss overhead by ~500x.
95                    if len >= 2 * 1024 * 1024 {
96                        let _ = mmap.advise(memmap2::Advice::HugePage);
97                    }
98                }
99                Ok(FileData::Mmap(mmap))
100            }
101            Err(_) => {
102                // mmap failed — fall back to read
103                let mut buf = Vec::with_capacity(len as usize);
104                let mut reader = file;
105                reader.read_to_end(&mut buf)?;
106                Ok(FileData::Owned(buf))
107            }
108        }
109    } else if len > 0 {
110        // Non-regular file (special files) — read from open fd
111        let mut buf = Vec::new();
112        let mut reader = file;
113        reader.read_to_end(&mut buf)?;
114        Ok(FileData::Owned(buf))
115    } else {
116        Ok(FileData::Owned(Vec::new()))
117    }
118}
119
120/// Get file size without reading it (for byte-count-only optimization).
121pub fn file_size(path: &Path) -> io::Result<u64> {
122    Ok(fs::metadata(path)?.len())
123}
124
125/// Read all bytes from stdin into a Vec.
126/// On Linux, uses raw libc::read() to bypass Rust's StdinLock/BufReader overhead.
127/// Uses a direct read() loop into a pre-allocated buffer instead of read_to_end(),
128/// which avoids Vec's grow-and-probe pattern (extra read() calls and memcpy).
129/// Callers should enlarge the pipe buffer via fcntl(F_SETPIPE_SZ) before calling.
130/// Uses the full spare capacity for each read() to minimize syscalls.
131pub fn read_stdin() -> io::Result<Vec<u8>> {
132    #[cfg(target_os = "linux")]
133    return read_stdin_raw();
134
135    #[cfg(not(target_os = "linux"))]
136    read_stdin_generic()
137}
138
139/// Raw libc::read() implementation for Linux — bypasses Rust's StdinLock
140/// and BufReader layers entirely. StdinLock uses an internal 8KB BufReader
141/// which adds an extra memcpy for every read; raw read() goes directly
142/// from the kernel pipe buffer to our Vec.
143///
144/// Note: callers (ftac, ftr, fbase64) are expected to enlarge the pipe
145/// buffer via fcntl(F_SETPIPE_SZ) before calling this function. We don't
146/// do it here to avoid accidentally shrinking a previously enlarged pipe.
147#[cfg(target_os = "linux")]
148fn read_stdin_raw() -> io::Result<Vec<u8>> {
149    const PREALLOC: usize = 16 * 1024 * 1024;
150    const READ_BUF: usize = 8 * 1024 * 1024;
151
152    let mut buf: Vec<u8> = Vec::with_capacity(PREALLOC);
153
154    loop {
155        let spare_cap = buf.capacity() - buf.len();
156        if spare_cap < READ_BUF {
157            buf.reserve(PREALLOC);
158        }
159        let spare_cap = buf.capacity() - buf.len();
160        let start = buf.len();
161
162        // SAFETY: we read into the uninitialized spare capacity and extend
163        // set_len only by the number of bytes actually read.
164        let ret = unsafe {
165            libc::read(
166                0,
167                buf.as_mut_ptr().add(start) as *mut libc::c_void,
168                spare_cap,
169            )
170        };
171        if ret < 0 {
172            let err = io::Error::last_os_error();
173            if err.kind() == io::ErrorKind::Interrupted {
174                continue;
175            }
176            return Err(err);
177        }
178        if ret == 0 {
179            break;
180        }
181        unsafe { buf.set_len(start + ret as usize) };
182    }
183
184    Ok(buf)
185}
186
187/// Generic read_stdin for non-Linux platforms.
188#[cfg(not(target_os = "linux"))]
189fn read_stdin_generic() -> io::Result<Vec<u8>> {
190    const PREALLOC: usize = 16 * 1024 * 1024;
191    const READ_BUF: usize = 4 * 1024 * 1024;
192
193    let mut stdin = io::stdin().lock();
194    let mut buf: Vec<u8> = Vec::with_capacity(PREALLOC);
195
196    loop {
197        let spare_cap = buf.capacity() - buf.len();
198        if spare_cap < READ_BUF {
199            buf.reserve(PREALLOC);
200        }
201        let spare_cap = buf.capacity() - buf.len();
202
203        let start = buf.len();
204        unsafe { buf.set_len(start + spare_cap) };
205        match stdin.read(&mut buf[start..start + spare_cap]) {
206            Ok(0) => {
207                buf.truncate(start);
208                break;
209            }
210            Ok(n) => {
211                buf.truncate(start + n);
212            }
213            Err(e) if e.kind() == io::ErrorKind::Interrupted => {
214                buf.truncate(start);
215                continue;
216            }
217            Err(e) => return Err(e),
218        }
219    }
220
221    Ok(buf)
222}
223
224/// Read as many bytes as possible into buf, retrying on partial reads.
225/// Ensures the full buffer is filled (or EOF reached), avoiding the
226/// probe-read overhead of read_to_end.
227/// Fast path: regular file reads usually return the full buffer on the first call.
228#[inline]
229fn read_full(reader: &mut impl Read, buf: &mut [u8]) -> io::Result<usize> {
230    // Fast path: first read() usually fills the entire buffer for regular files
231    let n = reader.read(buf)?;
232    if n == buf.len() || n == 0 {
233        return Ok(n);
234    }
235    // Slow path: partial read — retry to fill buffer (pipes, slow devices)
236    let mut total = n;
237    while total < buf.len() {
238        match reader.read(&mut buf[total..]) {
239            Ok(0) => break,
240            Ok(n) => total += n,
241            Err(e) if e.kind() == io::ErrorKind::Interrupted => continue,
242            Err(e) => return Err(e),
243        }
244    }
245    Ok(total)
246}