Skip to content

Commit e7cf641

Browse files
cgwaltersjeckersb
authored andcommitted
repo: Add some docstrings
Prep for some more changes. Signed-off-by: Colin Walters <[email protected]>
1 parent 8d1e570 commit e7cf641

File tree

1 file changed

+41
-3
lines changed

1 file changed

+41
-3
lines changed

crates/composefs/src/repository.rs

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,13 @@ impl<ObjectID: FsVerityHashValue> Drop for Repository<ObjectID> {
6969
}
7070

7171
impl<ObjectID: FsVerityHashValue> Repository<ObjectID> {
72+
/// Return the objects directory.
7273
pub fn objects_dir(&self) -> ErrnoResult<&OwnedFd> {
7374
self.objects
7475
.get_or_try_init(|| ensure_dir_and_openat(&self.repository, "objects", OFlags::PATH))
7576
}
7677

78+
/// Open a repository at the target directory and path.
7779
pub fn open_path(dirfd: impl AsFd, path: impl AsRef<Path>) -> Result<Self> {
7880
let path = path.as_ref();
7981

@@ -92,12 +94,14 @@ impl<ObjectID: FsVerityHashValue> Repository<ObjectID> {
9294
})
9395
}
9496

97+
/// Open the default user-owned composefs repository.
9598
pub fn open_user() -> Result<Self> {
9699
let home = std::env::var("HOME").with_context(|| "$HOME must be set when in user mode")?;
97100

98101
Self::open_path(CWD, PathBuf::from(home).join(".var/lib/composefs"))
99102
}
100103

104+
/// Open the default system-global composefs repository.
101105
pub fn open_system() -> Result<Self> {
102106
Self::open_path(CWD, PathBuf::from("/sysroot/composefs".to_string()))
103107
}
@@ -114,6 +118,7 @@ impl<ObjectID: FsVerityHashValue> Repository<ObjectID> {
114118
tokio::task::spawn_blocking(move || self_.ensure_object(&data)).await?
115119
}
116120

121+
/// Given a blob of data, store it in the repository.
117122
pub fn ensure_object(&self, data: &[u8]) -> Result<ObjectID> {
118123
let dirfd = self.objects_dir()?;
119124
let id: ObjectID = compute_verity(data);
@@ -216,6 +221,10 @@ impl<ObjectID: FsVerityHashValue> Repository<ObjectID> {
216221
Ok(fd)
217222
}
218223

224+
/// By default fsverity is required to be enabled on the target
225+
/// filesystem. Setting this disables verification of digests
226+
/// and an instance of [`Self`] can be used on a filesystem
227+
/// without fsverity support.
219228
pub fn set_insecure(&mut self, insecure: bool) -> &mut Self {
220229
self.insecure = insecure;
221230
self
@@ -236,6 +245,8 @@ impl<ObjectID: FsVerityHashValue> Repository<ObjectID> {
236245
format!("objects/{}", id.to_object_pathname())
237246
}
238247

248+
/// Check if the provided splitstream is present in the repository;
249+
/// if so, return its fsverity digest.
239250
pub fn has_stream(&self, sha256: &Sha256Digest) -> Result<Option<ObjectID>> {
240251
let stream_path = format!("streams/{}", hex::encode(sha256));
241252

@@ -260,7 +271,7 @@ impl<ObjectID: FsVerityHashValue> Repository<ObjectID> {
260271
}
261272
}
262273

263-
/// Basically the same as has_stream() except that it performs expensive verification
274+
/// Similar to [`Self::has_stream`] but performs more expensive verification.
264275
pub fn check_stream(&self, sha256: &Sha256Digest) -> Result<Option<ObjectID>> {
265276
let stream_path = format!("streams/{}", hex::encode(sha256));
266277
match self.openat(&stream_path, OFlags::RDONLY) {
@@ -301,6 +312,8 @@ impl<ObjectID: FsVerityHashValue> Repository<ObjectID> {
301312
}
302313
}
303314

315+
/// Write the given splitstream to the repository with the
316+
/// provided name.
304317
pub fn write_stream(
305318
&self,
306319
writer: SplitStreamWriter<ObjectID>,
@@ -374,6 +387,7 @@ impl<ObjectID: FsVerityHashValue> Repository<ObjectID> {
374387
Ok(object_id)
375388
}
376389

390+
/// Open a splitstream with the given name.
377391
pub fn open_stream(
378392
&self,
379393
name: &str,
@@ -392,6 +406,8 @@ impl<ObjectID: FsVerityHashValue> Repository<ObjectID> {
392406
SplitStreamReader::new(file)
393407
}
394408

409+
/// Given an object identifier (a digest), return a read-only file descriptor
410+
/// for its contents. The fsverity digest is verified (if the repository is not in `insecure` mode).
395411
pub fn open_object(&self, id: &ObjectID) -> Result<OwnedFd> {
396412
self.open_with_verity(&Self::format_object_path(id), id)
397413
}
@@ -412,7 +428,13 @@ impl<ObjectID: FsVerityHashValue> Repository<ObjectID> {
412428
Ok(())
413429
}
414430

415-
/// this function is not safe for untrusted users
431+
/// Write `data into the repository as an image with the given `name`.
432+
///
433+
/// The fsverity digest is returned.
434+
///
435+
/// # Integrity
436+
///
437+
/// This function is not safe for untrusted users.
416438
pub fn write_image(&self, name: Option<&str>, data: &[u8]) -> Result<ObjectID> {
417439
let object_id = self.ensure_object(data)?;
418440

@@ -429,7 +451,13 @@ impl<ObjectID: FsVerityHashValue> Repository<ObjectID> {
429451
Ok(object_id)
430452
}
431453

432-
/// this function is not safe for untrusted users
454+
/// Import the data from the provided read into the repository as an image.
455+
///
456+
/// The fsverity digest is returned.
457+
///
458+
/// # Integrity
459+
///
460+
/// This function is not safe for untrusted users.
433461
pub fn import_image<R: Read>(&self, name: &str, image: &mut R) -> Result<ObjectID> {
434462
let mut data = vec![];
435463
image.read_to_end(&mut data)?;
@@ -460,6 +488,8 @@ impl<ObjectID: FsVerityHashValue> Repository<ObjectID> {
460488
}
461489
}
462490

491+
/// Create a detached mount of an image. This file descriptor can then
492+
/// be attached via e.g. `move_mount`.
463493
pub fn mount(&self, name: &str) -> Result<OwnedFd> {
464494
let (image, enable_verity) = self.open_image(name)?;
465495
Ok(composefs_fsmount(
@@ -470,6 +500,7 @@ impl<ObjectID: FsVerityHashValue> Repository<ObjectID> {
470500
)?)
471501
}
472502

503+
/// Mount the image with the provided digest at the target path.
473504
pub fn mount_at(&self, name: &str, mountpoint: impl AsRef<Path>) -> Result<()> {
474505
Ok(mount_at(
475506
self.mount(name)?,
@@ -540,6 +571,7 @@ impl<ObjectID: FsVerityHashValue> Repository<ObjectID> {
540571
Ok(())
541572
}
542573

574+
/// Open the provided path in the repository.
543575
fn openat(&self, name: &str, flags: OFlags) -> ErrnoResult<OwnedFd> {
544576
// Unconditionally add CLOEXEC as we always want it.
545577
openat(
@@ -599,13 +631,19 @@ impl<ObjectID: FsVerityHashValue> Repository<ObjectID> {
599631
Ok(objects)
600632
}
601633

634+
/// Given an image, return the set of all objects referenced by it.
602635
pub fn objects_for_image(&self, name: &str) -> Result<HashSet<ObjectID>> {
603636
let (image, _) = self.open_image(name)?;
604637
let mut data = vec![];
605638
std::fs::File::from(image).read_to_end(&mut data)?;
606639
Ok(crate::erofs::reader::collect_objects(&data)?)
607640
}
608641

642+
/// Perform a garbage collection operation.
643+
///
644+
/// # Locking
645+
///
646+
/// An exclusive lock is held for the duration of this operation.
609647
pub fn gc(&self) -> Result<()> {
610648
flock(&self.repository, FlockOperation::LockExclusive)?;
611649

0 commit comments

Comments
 (0)