Skip to main content

Filesystem

Struct Filesystem 

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

RAII wrapper around an ext2_filsys handle.

Drop flushes and closes the filesystem, preventing resource leaks even when operations fail or panic.

§Example

use std::path::Path;
use bux_e2fs::{Filesystem, CreateOptions};

let mut fs = Filesystem::create(
    Path::new("/tmp/image.raw"),
    512 * 1024 * 1024,
    &CreateOptions::default(),
).unwrap();
fs.populate(Path::new("/tmp/rootfs")).unwrap();
fs.add_journal().unwrap();
// Drop closes the filesystem automatically.

Implementations§

Source§

impl Filesystem

Source

pub fn create( path: &Path, size_bytes: u64, opts: &CreateOptions, ) -> Result<Self>

Creates a new ext4 filesystem image at path.

Equivalent to mke2fs -t ext4 -b <block_size> -m <reserved> <path> <size>.

Source

pub fn open(path: &Path) -> Result<Self>

Opens an existing ext4 image for read-write operations.

Source

pub fn populate(&mut self, source_dir: &Path) -> Result<()>

Populates the filesystem from a host directory.

Recursively copies all files, directories, symlinks, and permissions from source_dir into the image root.

Source

pub fn flush(&mut self) -> Result<()>

Flushes all pending changes to disk without closing the filesystem.

Source

pub fn write_file(&mut self, host_path: &Path, guest_path: &str) -> Result<()>

Writes a single host file into the filesystem image.

Equivalent to debugfs -w -R "write <host_path> <guest_path>".

Source

pub fn mkdir(&mut self, name: &str) -> Result<()>

Creates a directory inside the filesystem image.

Creates a symlink inside the filesystem image.

Source

pub fn add_journal(&mut self) -> Result<()>

Adds an ext3/4 journal (size auto-calculated by libext2fs).

Creates a directory entry linking name to inode ino in directory dir.

Source

pub fn read_inode(&self, ino: u32) -> Result<ext2_inode>

Reads the on-disk inode structure for the given inode number.

Source

pub fn write_inode(&mut self, ino: u32, inode: &ext2_inode) -> Result<()>

Writes the inode structure back to the filesystem.

Source

pub fn write_new_inode(&mut self, ino: u32, inode: &ext2_inode) -> Result<()>

Writes a freshly allocated inode (initializes the on-disk slot).

Source

pub fn alloc_inode(&mut self, dir: u32, mode: u16) -> Result<u32>

Allocates a new inode number near dir with the given POSIX mode.

Updates the inode bitmap and allocation statistics automatically. Requires bitmaps to be loaded (always true after create).

Source

pub fn alloc_block(&mut self, goal: u64) -> Result<u64>

Allocates a new block near goal.

Updates the block bitmap and allocation statistics automatically. Requires bitmaps to be loaded (always true after create).

Trait Implementations§

Source§

impl Debug for Filesystem

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Drop for Filesystem

Source§

fn drop(&mut self)

Executes the destructor for this type. 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.