coven/bootstrap_unfinished.rs
1//! What lists a store's unfinished first installation for the app to
2//! resume, reclaiming one that nothing refers to, and discarding one that
3//! cannot resume (§13.4, §11.6).
4
5use super::*;
6
7/// What lists a store's unfinished first installation for its call to
8/// resume: a join's saved code or a restore's record, never both (§13.4).
9pub(super) enum Kept {
10 Join(SecretText),
11 Restore(SecretText),
12 Nothing,
13}
14
15/// Begin a call's first installation of `id`. One an earlier call left,
16/// which no saved code or record lists, is removed first: a join that
17/// stopped before it saved its code, or after it deleted it, holds a saved
18/// request that only the code it was made for opens, and keeping it would
19/// fail every later invite of the store (§13.4).
20pub(super) async fn begin_installation(
21 builder: &CovenBuilder,
22 id: StoreId,
23 entries: &StoreKeychain,
24) -> Result<BootstrapStore, BootstrapError> {
25 let pending = builder.directories.begin_bootstrap(id, builder.ids.as_ref()).await?;
26 if !pending.resumed() || entries.invite_code()?.is_some() || entries.restore_record()?.is_some()
27 {
28 return Ok(pending);
29 }
30 tracing::info!(store = %id, "removing an unfinished installation nothing refers to");
31 pending.cancel()?;
32 drop(pending);
33 Ok(builder.directories.begin_bootstrap(id, builder.ids.as_ref()).await?)
34}
35
36/// Read what lists the store's unfinished first installation. One that keeps
37/// neither a join's saved code nor a restore's record is what a crash or a
38/// dropped call left before either could write to storage: nothing refers
39/// to it, so once no call holds it, its space is reclaimed. Read again under
40/// the installation's lock, so a call that began meanwhile is never touched
41/// (§13.4, §11.6). An entry coven can't read fails the call.
42pub(super) async fn kept(
43 builder: &CovenBuilder,
44 entries: &StoreKeychain,
45 store: StoreId,
46) -> Result<Kept, BootstrapError> {
47 let read = || -> Result<Kept, BootstrapError> {
48 if let Some(code) = entries.invite_code()? {
49 return Ok(Kept::Join(code));
50 }
51 Ok(entries.restore_record()?.map_or(Kept::Nothing, |(code, _)| Kept::Restore(code)))
52 };
53 match read()? {
54 Kept::Nothing => {}
55 kept => return Ok(kept),
56 }
57 // A call holds it: what it keeps by now is what lists it.
58 let Some(lock) = builder.directories.lock_unfinished_first(store).await? else {
59 return read();
60 };
61 let kept = read()?;
62 if let Kept::Nothing = kept {
63 tracing::info!(%store, "reclaiming an unfinished installation nothing refers to");
64 let bugs = builder.directories.bugs().clone();
65 crate::task::blocking_completing("reclaim an unfinished installation", bugs, move || {
66 lock.remove_directory()
67 })
68 .await
69 .map_err(coven_foundation::files::FileError::from)
70 .and_then(|removed| removed)
71 .map_err(BootstrapDirectoryError::from)?;
72 }
73 Ok(kept)
74}
75
76impl CovenBuilder {
77 /// Delete the store's unfinished first installation from this device: its
78 /// directory and this device's entries for it, including a join's saved
79 /// code and sign-in or a restore's record. For an installation that can't
80 /// resume, such as one whose settings can't be read
81 /// (`BootstrapDirectoryError::Settings`). A join whose request was sent
82 /// stays pending in the store until its invite expires or is declined,
83 /// and a restore past its registration leaves this device registered
84 /// with the store, as `Coven::discard_store` does. The person's saved
85 /// restore code and S3 key stay. Fails with
86 /// `BootstrapError::NoUnfinishedInstallation` when the store has no
87 /// unfinished first installation, or a call holds it; repeating after a
88 /// failure part way finishes the deletion.
89 pub async fn discard_unfinished_installation(
90 &self,
91 store: StoreId,
92 ) -> Result<(), BootstrapError> {
93 let keys = StoreKeychain::new(self.keychain()?, store);
94 let lock = self
95 .directories
96 .lock_unfinished_first(store)
97 .await?
98 .ok_or(BootstrapError::NoUnfinishedInstallation(store))?;
99 let bugs = self.directories.bugs().clone();
100 crate::task::blocking_completing("discard an unfinished installation", bugs, move || {
101 keys.delete_device_entries()?;
102 lock.remove_directory()?;
103 Ok::<(), BootstrapError>(())
104 })
105 .await?
106 }
107}