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>>
impl<ObjectID: FsVerityHashValue> FileSystem<RegularFile<ObjectID>>
Sourcepub fn commit_images(
&self,
repository: &Repository<ObjectID>,
image_name: Option<&str>,
) -> Result<HashMap<FormatVersion, ObjectID>>
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().
Sourcepub fn commit_image(
&self,
repository: &Repository<ObjectID>,
image_name: Option<&str>,
) -> Result<ObjectID>
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().
Sourcepub fn commit_image_version(
&self,
repository: &Repository<ObjectID>,
version: FormatVersion,
image_name: Option<&str>,
) -> Result<ObjectID>
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().
Sourcepub fn compute_image_bytes(
&self,
version: FormatVersion,
) -> (Box<[u8]>, ObjectID)
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().
Sourcepub fn compute_image_id(&self, version: FormatVersion) -> ObjectID
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().
Sourcepub fn print_dumpfile(&self) -> Result<()>
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>
impl<T> FileSystem<T>
Sourcepub fn new(root_stat: Stat) -> Self
pub fn new(root_stat: Stat) -> Self
Creates a new filesystem with a root directory having the given metadata.
Sourcepub fn set_root_stat(&mut self, stat: Stat)
pub fn set_root_stat(&mut self, stat: Stat)
Sets the metadata for the root directory.
Sourcepub fn push_leaf(&mut self, stat: Stat, content: LeafContent<T>) -> LeafId
pub fn push_leaf(&mut self, stat: Stat, content: LeafContent<T>) -> LeafId
Pushes a new leaf into the leaves table and returns its LeafId.
Sourcepub fn copy_root_metadata_from_usr(&mut self) -> Result<(), ImageError>
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.
Sourcepub fn for_each_stat<F>(&self, f: F)
pub fn for_each_stat<F>(&self, f: F)
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.
Sourcepub fn for_each_stat_mut<F>(&mut self, f: F)
pub fn for_each_stat_mut<F>(&mut self, f: F)
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.
Sourcepub fn filter_xattrs<F>(&mut self, predicate: F)
pub fn filter_xattrs<F>(&mut self, predicate: F)
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).
Sourcepub fn canonicalize_run(&mut self) -> Result<(), ImageError>
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).
Sourcepub fn transform_for_oci(
&mut self,
options: &OciTransformOptions,
) -> Result<(), ImageError>
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:
Self::copy_root_metadata_from_usr- copies/usrmetadata to root directorySelf::canonicalize_run- empties/rundirectorySelf::filter_xattrs- filters xattrs according tooptions.xattrs(strips host-leaked xattrs like build-time security.selinux; keeps security.capability, and alsouser.*xattrs whenXattrFiltering::KeepUserXattrsis selected)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.
Sourcepub fn try_map_regular<U, E>(
self,
f: impl FnMut(&T) -> Result<U, E>,
) -> Result<FileSystem<U>, E>
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.
Sourcepub fn compact(&mut self)
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.
Sourcepub fn nlinks(&self) -> Vec<u32>
pub fn nlinks(&self) -> Vec<u32>
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).
Sourcepub fn fsck(&self) -> Result<(), ImageError>
pub fn fsck(&self) -> Result<(), ImageError>
Verify internal consistency of the filesystem.
Checks that:
- All
LeafIdindices 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.
Sourcepub fn as_dir(&self) -> DirectoryRef<'_, T>
pub fn as_dir(&self) -> DirectoryRef<'_, T>
Returns a DirectoryRef for the root directory.