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 /// Generates an EROFS filesystem image and writes it to the repository
21 /// with the optional name. Returns the fsverity digest of the committed image.
22 ///
23 /// Note: Callers should ensure root metadata is set before calling this,
24 /// typically via `copy_root_metadata_from_usr()` or `set_root_stat()`.
25 pub fn commit_image(
26 &self,
27 repository: &Repository<ObjectID>,
28 image_name: Option<&str>,
29 ) -> Result<ObjectID> {
30 repository.write_image(image_name, &mkfs_erofs(self))
31 }
32
33 /// Computes the fsverity digest for this filesystem as an EROFS image.
34 ///
35 /// Generates the EROFS image and returns its fsverity digest without
36 /// writing to a repository.
37 ///
38 /// Note: Callers should ensure root metadata is set before calling this,
39 /// typically via `copy_root_metadata_from_usr()` or `set_root_stat()`.
40 pub fn compute_image_id(&self) -> ObjectID {
41 compute_verity(&mkfs_erofs(self))
42 }
43
44 /// Prints this filesystem in dumpfile format to stdout.
45 ///
46 /// Serializes the entire filesystem tree to stdout in composefs dumpfile
47 /// text format.
48 ///
49 /// Note: Callers should ensure root metadata is set before calling this,
50 /// typically via `copy_root_metadata_from_usr()` or `set_root_stat()`.
51 pub fn print_dumpfile(&self) -> Result<()> {
52 write_dumpfile(&mut std::io::stdout(), self)
53 }
54}