pub struct File<'a> { /* private fields */ }Expand description
Represents a file in the PFS.
Use read_at() for positional reads (thread-safe, &self),
or as_slice() for zero-copy access when available.
Files may be compressed, in which case you should use
pfsc::PfscImage as an Image adapter.
§Example
let pfs = orbis_pfs::open_slice(&data, None)?;
let root = pfs.root().open()?;
if let Some(orbis_pfs::directory::DirEntry::File(file)) = root.get(b"example.txt") {
let mut contents = vec![0u8; file.len() as usize];
file.read_at(0, &mut contents)?;
println!("File contents: {}", String::from_utf8_lossy(&contents));
}Implementations§
Source§impl<'a> File<'a>
impl<'a> File<'a>
pub fn mode(&self) -> u16
pub fn flags(&self) -> u32
pub fn len(&self) -> u64
pub fn compressed_len(&self) -> u64
Sourcepub fn ctime(&self) -> u64
pub fn ctime(&self) -> u64
Returns the last metadata change time as seconds since the Unix epoch.
Sourcepub fn birthnsec(&self) -> u32
pub fn birthnsec(&self) -> u32
Returns the sub-second nanosecond component of birthtime().
pub fn uid(&self) -> u32
pub fn gid(&self) -> u32
pub fn is_compressed(&self) -> bool
pub fn is_empty(&self) -> bool
Sourcepub fn as_slice(&self) -> Option<&'a [u8]>
pub fn as_slice(&self) -> Option<&'a [u8]>
Returns the file contents as a borrowed slice without a copy.
This returns Some only when all of the following are true:
- The PFS was opened via
open_slice()with an unencrypted image - The file is not compressed
- The file’s blocks are laid out contiguously in the image
For compressed files, use pfsc::PfscImage instead.
When this returns None, use read_at() as a fallback.
Sourcepub fn read_at(&self, offset: u64, buf: &mut [u8]) -> Result<usize>
pub fn read_at(&self, offset: u64, buf: &mut [u8]) -> Result<usize>
Reads file data at the given offset without modifying any cursor.
This is the primary read method. It takes &self (not &mut self)
and requires no synchronization, making it safe to call from multiple
threads concurrently.
Sourcepub fn reader(&self) -> FileReader<'a> ⓘ
pub fn reader(&self) -> FileReader<'a> ⓘ
Creates a FileReader that implements Read and Seek.
This is useful when you need to pass a PFS file to APIs that expect
standard I/O traits (e.g. io::copy, decompressors, parsers).
Each reader maintains its own cursor position. Multiple readers can exist concurrently for the same file.
§Example
use std::io::Read;
let pfs = orbis_pfs::open_slice(&data, None)?;
let root = pfs.root().open()?;
if let Some(orbis_pfs::directory::DirEntry::File(file)) = root.get(b"example.txt") {
let mut reader = file.reader();
let mut contents = String::new();
reader.read_to_string(&mut contents)?;
}Sourcepub fn into_image(self) -> PfsFileImage<'a>
pub fn into_image(self) -> PfsFileImage<'a>
Converts this file handle into a PfsFileImage for use as an
Image source (e.g. to open a nested PFS or wrap in
PfscImage).