coreutils_rs/common/
io.rs1use std::fs::{self, File};
2use std::io::{self, Read};
3use std::ops::Deref;
4use std::path::Path;
5
6use memmap2::Mmap;
7
8const MMAP_THRESHOLD: u64 = 64 * 1024; pub enum FileData {
15 Mmap(Mmap),
16 Owned(Vec<u8>),
17}
18
19impl Deref for FileData {
20 type Target = [u8];
21
22 fn deref(&self) -> &[u8] {
23 match self {
24 FileData::Mmap(m) => m,
25 FileData::Owned(v) => v,
26 }
27 }
28}
29
30pub fn read_file(path: &Path) -> io::Result<FileData> {
32 let metadata = fs::metadata(path)?;
33
34 if metadata.len() >= MMAP_THRESHOLD {
35 let file = File::open(path)?;
36 let mmap = unsafe { Mmap::map(&file)? };
38 #[cfg(target_os = "linux")]
39 {
40 let _ = mmap.advise(memmap2::Advice::Sequential);
41 }
42 Ok(FileData::Mmap(mmap))
43 } else {
44 Ok(FileData::Owned(fs::read(path)?))
45 }
46}
47
48pub fn file_size(path: &Path) -> io::Result<u64> {
50 Ok(fs::metadata(path)?.len())
51}
52
53pub fn read_stdin() -> io::Result<Vec<u8>> {
55 let mut buf = Vec::new();
56 io::stdin().lock().read_to_end(&mut buf)?;
57 Ok(buf)
58}