Something went wrong. Try again.
XNU but rewritten in Rust
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384//! VFS core traits: `Vnode` and `Filesystem`.
/// A single filesystem node (file or device).pub trait Vnode: Send + Sync { /// Read up to `buf.len()` bytes at `offset` into `buf`. /// /// Returns the number of bytes actually read (may be less than `buf.len()` /// at end-of-file), or a negative errno on error. fn read(&self, offset: u64, buf: &mut [u8]) -> Result<usize, i32>;
/// Size of the file in bytes (0 for device nodes). fn size(&self) -> u64;
/// `true` if this node represents a character device (affects `stat` mode). fn is_char_device(&self) -> bool { false }}
/// A mounted filesystem that can look up paths.pub trait Filesystem: Send + Sync { /// Return a `Vnode` for `path`, or `None` if the path does not exist in /// this filesystem. fn lookup(&self, path: &str) -> Option<liballoc::sync::Arc<dyn Vnode>>;}
/// Read the entire contents of `node` into a freshly allocated `Vec`.////// Used by exec paths that need all file bytes for Mach-O parsing./// Returns `None` if any read fails or the file is too large to fit in memory.pub fn read_all(node: &dyn Vnode) -> Option<liballoc::vec::Vec<u8>> { let size = node.size() as usize; let mut buf = liballoc::vec![0u8; size]; let mut total = 0usize; while total < size { match node.read(total as u64, &mut buf[total..]) { Ok(0) => break, Ok(n) => total += n, Err(_) => return None, } } buf.truncate(total); Some(buf)}
/// Read just enough bytes from `node` to parse all Mach-O load commands.////// Avoids a full `read_all` for large binaries: only the header region (a few/// KiB) is needed by `loader::parse` to extract metadata. `Segment::data`/// slices will be empty; segments should be streamed later from the vnode.////// Returns `None` if the node is unreadable or not a recognisable Mach-O.pub fn read_macho_header(node: &dyn Vnode) -> Option<liballoc::vec::Vec<u8>> { const INITIAL: usize = 4096; const MAX: usize = 8 * 1024 * 1024;
let mut buf = liballoc::vec![0u8; INITIAL]; let n = node.read(0, &mut buf).ok()?; buf.truncate(n);
let arm64_off = ::loader::fat::arm64_slice_offset(&buf).unwrap_or(0); let arm64_hdr = buf.get(arm64_off..)?; let needed = ::loader::header_region_size(arm64_hdr) .map(|s| arm64_off + s) .unwrap_or(INITIAL);
if needed <= buf.len() { buf.truncate(needed); return Some(buf); }
let needed = needed.min(MAX); let mut full = liballoc::vec![0u8; needed]; let mut done = 0usize; while done < needed { match node.read(done as u64, &mut full[done..]) { Ok(0) | Err(_) => break, Ok(n) => done += n, } } full.truncate(done); Some(full)}