Skip to main content

FileBuffer

Struct FileBuffer 

Source
pub struct FileBuffer { /* private fields */ }
Expand description

A memory-mapped file buffer for efficient file access

This struct provides safe access to file contents through memory mapping, which avoids loading the entire file into memory while providing fast random access to file data.

§Examples

use libmagic_rs::io::FileBuffer;
use std::path::Path;

let buffer = FileBuffer::new(Path::new("example.bin"))?;
let data = buffer.as_slice();
println!("File size: {} bytes", data.len());

Implementations§

Source§

impl FileBuffer

Source

pub const MAX_FILE_SIZE: u64

Maximum file size that can be processed (1 GB)

This limit prevents memory exhaustion attacks and ensures reasonable processing times. Files larger than this are likely not suitable for magic rule evaluation and may indicate malicious input.

Source

pub fn new(path: &Path) -> Result<Self, IoError>

Creates a new memory-mapped file buffer

§Arguments
  • path - Path to the file to be mapped
§Returns

Returns a FileBuffer on success, or an IoError if the file cannot be opened or mapped.

§Errors

This function will return an error if:

  • The file does not exist or cannot be opened
  • The file cannot be memory-mapped
  • The file is empty
  • The file is larger than the maximum allowed size
  • File metadata cannot be read
§Examples
use libmagic_rs::io::FileBuffer;
use std::path::Path;

let buffer = FileBuffer::new(Path::new("example.bin"))?;
Source

pub fn from_path_and_metadata( path: &Path, metadata: &Metadata, ) -> Result<Self, IoError>

Creates a new FileBuffer using caller-supplied metadata.

This is a performance-focused alternative to FileBuffer::new for callers that have already called std::fs::metadata on path (for example, to check the empty-file case before constructing the buffer). It skips the internal std::fs::canonicalize + second metadata round-trip that FileBuffer::new performs, eliminating two redundant syscalls on the hot path of MagicDatabase::evaluate_file.

§Security

This constructor deliberately skips std::fs::canonicalize for performance. Symlink resolution and path canonicalization are the caller’s responsibility. In adversarial environments (untrusted file paths), prefer FileBuffer::new or [MagicDatabase::evaluate_buffer] instead.

The caller is responsible for having read metadata via a path that makes sense for their security model. The same structural checks (regular file, non-empty, under MAX_FILE_SIZE) are still applied against the supplied metadata.

§Errors

Returns the same IoError variants as FileBuffer::new for validation failures, file open failures, and mmap failures.

Source

pub fn as_slice(&self) -> &[u8]

Returns the file contents as a byte slice

This provides safe access to the memory-mapped file data without copying the contents.

§Examples
use libmagic_rs::io::FileBuffer;
use std::path::Path;

let buffer = FileBuffer::new(Path::new("example.bin"))?;
let data = buffer.as_slice();
println!("First byte: 0x{:02x}", data[0]);
Source

pub fn path(&self) -> &Path

Returns the path of the file

§Examples
use libmagic_rs::io::FileBuffer;
use std::path::Path;

let buffer = FileBuffer::new(Path::new("example.bin"))?;
println!("File path: {}", buffer.path().display());
Source

pub fn len(&self) -> usize

Returns the size of the file in bytes

§Examples
use libmagic_rs::io::FileBuffer;
use std::path::Path;

let buffer = FileBuffer::new(Path::new("example.bin"))?;
println!("File size: {} bytes", buffer.len());
Source

pub fn is_empty(&self) -> bool

Returns true if the file is empty

Note: This should never return true for a successfully created FileBuffer, as empty files are rejected during construction.

§Examples
use libmagic_rs::io::FileBuffer;
use std::path::Path;

let buffer = FileBuffer::new(Path::new("example.bin"))?;
assert!(!buffer.is_empty()); // Should always be false for valid buffers

Trait Implementations§

Source§

impl Debug for FileBuffer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.