Skip to main content

FileSystem

Struct FileSystem 

Source
pub struct FileSystem<T> {
    pub root: Directory<T>,
    pub leaves: Vec<Leaf<T>>,
}
Expand description

A complete filesystem tree with a root directory and a flat table of leaves.

Leaf nodes (non-directory files) are stored in a flat Vec and referenced by LeafId indices from the directory tree. This design is Send + Sync, supports hardlinks via shared LeafId, and avoids reference counting.

Fields§

§root: Directory<T>

The root directory of the filesystem.

§leaves: Vec<Leaf<T>>

Table of all leaf nodes; LeafId indexes into this vector.

Implementations§

Source§

impl<ObjectID: FsVerityHashValue> FileSystem<RegularFile<ObjectID>>

Source

pub fn commit_images( &self, repository: &Repository<ObjectID>, image_name: Option<&str>, ) -> Result<HashMap<FormatVersion, ObjectID>>

Commits this filesystem as EROFS images for each format version in the repository config.

Returns a map from FormatVersion to the fsverity digest of the stored image for that version.

The image_name named ref (if provided) is assigned to the default version from repository.format_config(). All extra versions are stored anonymously (no named ref).

Note: Callers should ensure root metadata is set before calling this, typically via copy_root_metadata_from_usr() or set_root_stat().

Source

pub fn commit_image( &self, repository: &Repository<ObjectID>, image_name: Option<&str>, ) -> Result<ObjectID>

Commits this filesystem as an EROFS image to the repository.

Generates an EROFS filesystem image using the repository’s configured EROFS format version and writes it with the optional name. Returns the fsverity digest of the committed image for the default format version.

Note: Callers should ensure root metadata is set before calling this, typically via copy_root_metadata_from_usr() or set_root_stat().

Source

pub fn commit_image_version( &self, repository: &Repository<ObjectID>, version: FormatVersion, image_name: Option<&str>, ) -> Result<ObjectID>

Commits this filesystem as an EROFS image of the given version, regardless of the repository’s configured default/extra format versions.

Useful when a caller needs a non-default format version committed — e.g. one the repository wasn’t configured to generate — but doesn’t already know the resulting digest. Callers that already have (or want) the digest before deciding whether to persist should use Self::compute_image_bytes followed by Repository::write_image instead, to avoid serializing the tree twice.

Note: Callers should ensure root metadata is set before calling this, typically via copy_root_metadata_from_usr() or set_root_stat().

Source

pub fn compute_image_bytes( &self, version: FormatVersion, ) -> (Box<[u8]>, ObjectID)

Computes the EROFS image bytes for this filesystem at version, along with their fsverity digest.

Useful for callers that need to know the digest before deciding whether to persist the image (e.g. matching against an expected digest via composefs_oci::boot::find_matching_boot_image): they can pass the returned bytes to Repository::write_image directly instead of regenerating them via Self::commit_image_version, which would otherwise serialize the same tree a second time.

Note: Callers should ensure root metadata is set before calling this, typically via copy_root_metadata_from_usr() or set_root_stat().

Source

pub fn compute_image_id(&self, version: FormatVersion) -> ObjectID

Computes the fsverity digest for this filesystem as an EROFS image.

The digest depends on the EROFS format version: V1 and V2 produce different on-disk layouts and therefore different digests. Callers must supply the version explicitly so that the digest matches what is actually stored (or will be stored) in the repository.

Note: Callers should ensure root metadata is set before calling this, typically via copy_root_metadata_from_usr() or set_root_stat().

Source

pub fn print_dumpfile(&self) -> Result<()>

Prints this filesystem in dumpfile format to stdout.

Serializes the entire filesystem tree to stdout in composefs dumpfile text format.

Note: Callers should ensure root metadata is set before calling this, typically via copy_root_metadata_from_usr() or set_root_stat().

Source§

impl<T> FileSystem<T>

Source

pub fn new(root_stat: Stat) -> Self

Creates a new filesystem with a root directory having the given metadata.

Source

pub fn set_root_stat(&mut self, stat: Stat)

Sets the metadata for the root directory.

Source

pub fn push_leaf(&mut self, stat: Stat, content: LeafContent<T>) -> LeafId

Pushes a new leaf into the leaves table and returns its LeafId.

Source

pub fn copy_root_metadata_from_usr(&mut self) -> Result<(), ImageError>

Copies metadata from /usr to the root directory.

OCI container layer tars often don’t include a root directory entry, and when they do, container runtimes typically ignore it. This makes root metadata non-deterministic. This method provides a way to derive consistent root metadata by copying it from /usr, which is always present in standard filesystem layouts.

The copied metadata includes:

  • Mode (permissions)
  • Modification time
  • User ID (uid)
  • Group ID (gid)
  • Extended attributes (xattrs)

NOTE: If changing this behavior, also update doc/oci.md.

§Errors

Returns an error if /usr does not exist or is not a directory.

Source

pub fn for_each_stat<F>(&self, f: F)
where F: Fn(&Stat),

Applies a function to every Stat in the filesystem tree.

This visits the root directory and all descendant directories via the tree, and each leaf stat exactly once via the flat leaves table.

Source

pub fn for_each_stat_mut<F>(&mut self, f: F)
where F: FnMut(&mut Stat),

Applies a function to every Stat in the filesystem tree, mutably.

This visits each directory stat via the tree, and each leaf stat exactly once via the flat leaves table. No dedup needed since each leaf appears exactly once in the table regardless of how many directory entries reference it.

Source

pub fn filter_xattrs<F>(&mut self, predicate: F)
where F: Fn(&OsStr) -> bool,

Filters extended attributes across the entire filesystem tree.

Retains only xattrs whose names match the given predicate. This is useful for stripping build-time xattrs that shouldn’t leak into the final image (e.g., security.selinux labels from the build host).

Source

pub fn canonicalize_run(&mut self) -> Result<(), ImageError>

Empties the /run directory if present, using /usr’s mtime.

/run is a tmpfs at runtime and should always be empty in container images. This also works around podman/buildah’s RUN --mount behavior where bind mount targets leave directory stubs in the filesystem that shouldn’t be part of the image content.

The mtime is set to match /usr for consistency with Self::copy_root_metadata_from_usr.

NOTE: If changing this behavior, also update doc/oci.md.

§Errors

Returns an error if /usr does not exist (needed to get the mtime).

Source

pub fn transform_for_oci( &mut self, options: &OciTransformOptions, ) -> Result<(), ImageError>

Transforms the filesystem for OCI container image consistency.

This applies the standard transformations needed to ensure consistent composefs digests between build-time (mounted filesystem) and install-time (OCI tar layers) views:

  1. Self::copy_root_metadata_from_usr - copies /usr metadata to root directory
  2. Self::canonicalize_run - empties /run directory
  3. Self::filter_xattrs - filters xattrs according to options.xattrs (strips host-leaked xattrs like build-time security.selinux; keeps security.capability, and also user.* xattrs when XattrFiltering::KeepUserXattrs is selected)
  4. Self::compact - removes leaves orphaned by the steps above (e.g. files that were only referenced from the now-emptied /run)

This is the recommended single entry point for OCI container processing. Because the cleanup is built in, callers get a valid (fsck-clean) filesystem regardless of whether they came from the OCI tar layers or a mounted root.

NOTE: If changing this behavior, also update doc/oci.md.

§Errors

Returns an error if /usr does not exist.

Source

pub fn try_map_regular<U, E>( self, f: impl FnMut(&T) -> Result<U, E>, ) -> Result<FileSystem<U>, E>

Converts FileSystem<T> to FileSystem<U> by mapping regular file content.

Applies f to each LeafContent::Regular(T) to produce LeafContent::Regular(U). All other leaf content variants (symlinks, devices, etc.) are passed through unchanged.

Because hardlinks are index-based, directory tree indices pass through unchanged. The mapping function is called exactly once per unique leaf.

Source

pub fn compact(&mut self)

Removes unreferenced leaves and remaps all LeafIds.

After removing entries from the tree, some leaves may become unreferenced. This method compacts the leaves table by removing dead entries and updating all LeafIds in the tree accordingly.

Compute nlink counts for all leaves at once.

Returns a Vec indexed by LeafId where each entry is the number of directory entries referencing that leaf (i.e. the hard link count).

Source

pub fn fsck(&self) -> Result<(), ImageError>

Verify internal consistency of the filesystem.

Checks that:

  • All LeafId indices in the directory tree are within bounds of the leaves table
  • All leaves in the table are referenced by at least one directory entry (no orphans)

Returns Ok(()) if the filesystem is consistent, or an error describing the first inconsistency found.

Source

pub fn as_dir(&self) -> DirectoryRef<'_, T>

Returns a DirectoryRef for the root directory.

Source

pub fn leaf(&self, id: LeafId) -> &Leaf<T>

Returns a reference to the leaf with the given id.

Source

pub fn leaf_mut(&mut self, id: LeafId) -> &mut Leaf<T>

Returns a mutable reference to the leaf with the given id.

Trait Implementations§

Source§

impl<T: Clone> Clone for FileSystem<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for FileSystem<T>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for FileSystem<T>
where Directory<T>: Freeze, Vec<Leaf<T>>: Freeze,

§

impl<T> RefUnwindSafe for FileSystem<T>

§

impl<T> Send for FileSystem<T>
where Directory<T>: Send, Vec<Leaf<T>>: Send,

§

impl<T> Sync for FileSystem<T>
where Directory<T>: Sync, Vec<Leaf<T>>: Sync,

§

impl<T> Unpin for FileSystem<T>
where Directory<T>: Unpin, Vec<Leaf<T>>: Unpin,

§

impl<T> UnsafeUnpin for FileSystem<T>

§

impl<T> UnwindSafe for FileSystem<T>

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.