Skip to main content

capsule_library/
album_store.rs

1//! Persistence of the sealed album-key store at `{root}/.library/albums.cbor`.
2//!
3//! The store's format, sealing and escrow are `capsule_core::crypto::keys::AlbumStore`; this
4//! module owns only the file. It is an extension trait so the call sites keep reading
5//! `AlbumStore::load(..)` / `store.save(..)`.
6
7use std::fs;
8use std::path::{Path, PathBuf};
9
10use capsule_core::crypto::keys::{ALBUM_STORE_FILE, AlbumStore, AlbumStoreError, MasterKey};
11
12use crate::utils::paths::tmp_path;
13
14/// Reading and durably replacing an [`AlbumStore`] on disk.
15pub trait AlbumStoreFile: Sized {
16    /// `{root}/.library/albums.cbor`.
17    fn path(root: &Path) -> PathBuf;
18
19    /// Load and unseal the store at `{root}/.library/albums.cbor`.
20    ///
21    /// `Ok(None)` means the file does not exist — a **library written before album keys were
22    /// durable**, whose album keys were session-scoped and were therefore never persisted. Such
23    /// a library opens with zero albums; its assets are recoverable only through a backup artifact (see
24    /// [`Workspace::import_backup`](crate::lifecycle::Workspace::import_backup)). This is not a
25    /// regression — those assets were already undecryptable across a restart before the store
26    /// existed — but it is a real, permanent limitation of libraries created before it.
27    fn load(root: &Path, master: &MasterKey) -> Result<Option<Self>, AlbumStoreError>;
28
29    /// Seal and durably replace the store. Temp-then-rename, so a crash mid-write leaves the
30    /// previous good keystore intact rather than a truncated one.
31    fn save(&self, root: &Path, master: &MasterKey) -> Result<(), AlbumStoreError>;
32}
33
34impl AlbumStoreFile for AlbumStore {
35    fn path(root: &Path) -> PathBuf {
36        root.join(".library").join(ALBUM_STORE_FILE)
37    }
38
39    #[tracing::instrument(skip_all, fields(root = %root.display()))]
40    fn load(root: &Path, master: &MasterKey) -> Result<Option<Self>, AlbumStoreError> {
41        let path = <Self as AlbumStoreFile>::path(root);
42        if !path.exists() {
43            tracing::debug!(path = %path.display(), "album store: absent");
44            return Ok(None);
45        }
46        let sealed = fs::read(&path).map_err(|e| AlbumStoreError::Io(e.to_string()))?;
47        AlbumStore::open_sealed(&sealed, master).map(Some)
48    }
49
50    #[tracing::instrument(skip_all, fields(root = %root.display(), albums = self.albums().len()))]
51    fn save(&self, root: &Path, master: &MasterKey) -> Result<(), AlbumStoreError> {
52        let sealed = self.seal(master)?;
53        let path = <Self as AlbumStoreFile>::path(root);
54        if let Some(parent) = path.parent() {
55            fs::create_dir_all(parent).map_err(|e| AlbumStoreError::Io(e.to_string()))?;
56        }
57        let tmp = tmp_path(&path);
58        fs::write(&tmp, &sealed).map_err(|e| AlbumStoreError::Io(e.to_string()))?;
59        fs::rename(&tmp, &path).map_err(|e| AlbumStoreError::Io(e.to_string()))?;
60        tracing::info!(
61            path = %path.display(),
62            sealed_bytes = sealed.len(),
63            "album store: persisted"
64        );
65        Ok(())
66    }
67}
68
69#[cfg(test)]
70mod tests;