composefs/
filesystem_ops.rs

1//! High-level filesystem operations for composefs trees.
2//!
3//! This module provides convenience methods for common operations on
4//! FileSystem objects, including computing image IDs, committing to
5//! repositories, and generating dumpfiles.
6
7use anyhow::Result;
8
9use crate::{
10    dumpfile::write_dumpfile,
11    erofs::writer::mkfs_erofs,
12    fsverity::{compute_verity, FsVerityHashValue},
13    repository::Repository,
14    tree::FileSystem,
15};
16
17impl<ObjectID: FsVerityHashValue> FileSystem<ObjectID> {
18    /// Commits this filesystem as an EROFS image to the repository.
19    ///
20    /// Ensures the root directory stat is computed, generates an EROFS filesystem image,
21    /// and writes it to the repository with the optional name. Returns the fsverity digest
22    /// of the committed image.
23    pub fn commit_image(
24        &mut self,
25        repository: &Repository<ObjectID>,
26        image_name: Option<&str>,
27    ) -> Result<ObjectID> {
28        self.ensure_root_stat();
29        repository.write_image(image_name, &mkfs_erofs(self))
30    }
31
32    /// Computes the fsverity digest for this filesystem as an EROFS image.
33    ///
34    /// Ensures the root directory stat is computed, generates the EROFS image,
35    /// and returns its fsverity digest without writing to a repository.
36    pub fn compute_image_id(&mut self) -> ObjectID {
37        self.ensure_root_stat();
38        compute_verity(&mkfs_erofs(self))
39    }
40
41    /// Prints this filesystem in dumpfile format to stdout.
42    ///
43    /// Ensures the root directory stat is computed and serializes the entire
44    /// filesystem tree to stdout in composefs dumpfile text format.
45    pub fn print_dumpfile(&mut self) -> Result<()> {
46        self.ensure_root_stat();
47        write_dumpfile(&mut std::io::stdout(), self)
48    }
49}