713205a95af2 | atagen | 2026-09-26 09:24 UTC | 1 | #[cfg(feature = "sqlite")] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 2 | use std::path::{Path, PathBuf}; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 3 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 4 | use matrix_sdk::{BoxFuture, SendOutsideWasm, SyncOutsideWasm}; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 5 | use matrix_sdk_store_encryption::StoreCipher; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 6 | use tokio::sync::Mutex; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 7 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 8 | use crate::{AccountError, SessionRecord, StoreConfig, account::account_error}; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 9 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 10 | #[cfg(feature = "sqlite")] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 11 | type RecordLocation = PathBuf; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 12 | #[cfg(feature = "indexeddb")] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 13 | type RecordLocation = String; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 14 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 15 | /// one login's record and the matrix store it belongs to. the engine rotates refresh tokens |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 16 | /// and each works once, so a `save` has to land before the next refresh or the login is gone |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 17 | pub trait SessionStore: SendOutsideWasm + SyncOutsideWasm { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 18 | fn store(&self) -> &StoreConfig; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 19 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 20 | fn load(&self) -> BoxFuture<'_, Result<Option<SessionRecord>, AccountError>>; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 21 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 22 | /// replace the saved record, all or nothing |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 23 | fn save<'a>(&'a self, record: &'a SessionRecord) -> BoxFuture<'a, Result<(), AccountError>>; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 24 | |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 25 | /// swap the record for one that only says who to log in again as, keeping the store. |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 26 | /// `Session::restore` then answers `Restore::SoftLoggedOut`, even after a restart |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 27 | fn soft_log_out(&self) -> BoxFuture<'_, Result<(), AccountError>>; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 28 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 29 | /// drop the record and delete the store, once nothing has the store open |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 30 | fn discard(&self) -> BoxFuture<'_, Result<(), AccountError>>; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 31 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 32 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 33 | /// keeps the record sealed with the store's passphrase, in a file natively and in an |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 34 | /// IndexedDB database of its own in a browser |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 35 | pub struct EncryptedSessionStore { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 36 | record: RecordLocation, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 37 | store: StoreConfig, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 38 | sealer: Mutex<Option<Sealer>>, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 39 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 40 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 41 | struct Sealer { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 42 | cipher: StoreCipher, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 43 | exported: Vec<u8>, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 44 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 45 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 46 | impl EncryptedSessionStore { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 47 | #[cfg(feature = "sqlite")] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 48 | #[must_use] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 49 | pub fn file(record: impl Into<PathBuf>, store: StoreConfig) -> Self { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 50 | Self::new(record.into(), store) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 51 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 52 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 53 | /// the record lives beside the store, in `<name>::macltok-session` |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 54 | #[cfg(feature = "indexeddb")] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 55 | #[must_use] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 56 | pub fn indexeddb(store: StoreConfig) -> Self { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 57 | let record = format!("{}::macltok-session", store.indexeddb_name()); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 58 | Self::new(record, store) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 59 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 60 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 61 | fn new(record: RecordLocation, store: StoreConfig) -> Self { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 62 | Self { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 63 | record, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 64 | store, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 65 | sealer: Mutex::new(None), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 66 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 67 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 68 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 69 | async fn load_record(&self) -> Result<Option<SessionRecord>, AccountError> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 70 | let mut sealer = self.sealer.lock().await; |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 71 | self.read(&mut sealer).await |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 72 | } |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 73 | |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 74 | async fn save_record(&self, record: &SessionRecord) -> Result<(), AccountError> { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 75 | let mut sealer = self.sealer.lock().await; |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 76 | self.write(&mut sealer, record).await |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 77 | } |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 78 | |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 79 | async fn soft_log_out_record(&self) -> Result<(), AccountError> { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 80 | let mut sealer = self.sealer.lock().await; |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 81 | match self.read(&mut sealer).await? { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 82 | Some(record) => self.write(&mut sealer, &record.soft_logged_out()).await, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 83 | None => Ok(()), |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 84 | } |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 85 | } |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 86 | |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 87 | async fn discard_all(&self) -> Result<(), AccountError> { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 88 | let mut sealer = self.sealer.lock().await; |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 89 | backend::remove(&self.record) |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 90 | .await |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 91 | .map_err(|error| account_error("removing the session record", error))?; |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 92 | *sealer = None; |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 93 | self.store.delete().await |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 94 | } |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 95 | |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 96 | async fn read( |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 97 | &self, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 98 | sealer: &mut Option<Sealer>, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 99 | ) -> Result<Option<SessionRecord>, AccountError> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 100 | let Some(sealed) = backend::read(&self.record) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 101 | .await |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 102 | .map_err(|error| account_error("reading the session record", error))? |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 103 | else { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 104 | return Ok(None); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 105 | }; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 106 | let (opened, record) = unseal(self.store.passphrase(), &sealed)?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 107 | *sealer = Some(opened); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 108 | Ok(Some(record)) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 109 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 110 | |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 111 | async fn write( |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 112 | &self, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 113 | sealer: &mut Option<Sealer>, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 114 | record: &SessionRecord, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 115 | ) -> Result<(), AccountError> { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 116 | let sealer = match sealer { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 117 | Some(sealer) => sealer, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 118 | empty => empty.insert(Sealer::new(self.store.passphrase())?), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 119 | }; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 120 | let sealed = sealer.seal(record)?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 121 | backend::write(&self.record, &sealed) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 122 | .await |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 123 | .map_err(|error| account_error("writing the session record", error)) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 124 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 125 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 126 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 127 | impl SessionStore for EncryptedSessionStore { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 128 | fn store(&self) -> &StoreConfig { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 129 | &self.store |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 130 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 131 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 132 | fn load(&self) -> BoxFuture<'_, Result<Option<SessionRecord>, AccountError>> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 133 | Box::pin(self.load_record()) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 134 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 135 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 136 | fn save<'a>(&'a self, record: &'a SessionRecord) -> BoxFuture<'a, Result<(), AccountError>> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 137 | Box::pin(self.save_record(record)) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 138 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 139 | |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 140 | fn soft_log_out(&self) -> BoxFuture<'_, Result<(), AccountError>> { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 141 | Box::pin(self.soft_log_out_record()) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 142 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 143 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 144 | fn discard(&self) -> BoxFuture<'_, Result<(), AccountError>> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 145 | Box::pin(self.discard_all()) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 146 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 147 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 148 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 149 | impl Sealer { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 150 | fn new(passphrase: &str) -> Result<Self, AccountError> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 151 | let cipher = StoreCipher::new() |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 152 | .map_err(|error| account_error("creating the session record cipher", error))?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 153 | let exported = cipher |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 154 | .export(passphrase) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 155 | .map_err(|error| account_error("sealing the session record cipher", error))?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 156 | Ok(Self { cipher, exported }) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 157 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 158 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 159 | fn seal(&self, record: &SessionRecord) -> Result<Vec<u8>, AccountError> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 160 | let value = self |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 161 | .cipher |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 162 | .encrypt_value(record) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 163 | .map_err(|error| account_error("sealing the session record", error))?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 164 | let length = u32::try_from(self.exported.len()) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 165 | .expect("an exported store cipher is a few hundred bytes"); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 166 | let mut sealed = Vec::with_capacity(4 + self.exported.len() + value.len()); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 167 | sealed.extend_from_slice(&length.to_be_bytes()); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 168 | sealed.extend_from_slice(&self.exported); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 169 | sealed.extend_from_slice(&value); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 170 | Ok(sealed) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 171 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 172 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 173 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 174 | fn unseal(passphrase: &str, sealed: &[u8]) -> Result<(Sealer, SessionRecord), AccountError> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 175 | let truncated = || { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 176 | account_error( |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 177 | "opening the session record", |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 178 | std::io::Error::from(std::io::ErrorKind::UnexpectedEof), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 179 | ) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 180 | }; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 181 | let (length, rest) = sealed.split_first_chunk::<4>().ok_or_else(truncated)?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 182 | let (exported, value) = rest |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 183 | .split_at_checked(u32::from_be_bytes(*length) as usize) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 184 | .ok_or_else(truncated)?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 185 | let cipher = StoreCipher::import(passphrase, exported) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 186 | .map_err(|error| account_error("opening the session record cipher", error))?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 187 | let record = cipher |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 188 | .decrypt_value(value) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 189 | .map_err(|error| account_error("opening the session record", error))?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 190 | let sealer = Sealer { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 191 | cipher, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 192 | exported: exported.to_vec(), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 193 | }; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 194 | Ok((sealer, record)) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 195 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 196 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 197 | #[cfg(feature = "sqlite")] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 198 | mod backend { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 199 | use std::{ |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 200 | fs::{self, File, OpenOptions}, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 201 | io::{self, ErrorKind, Write}, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 202 | path::{Path, PathBuf}, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 203 | }; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 204 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 205 | pub(super) async fn read(path: &Path) -> io::Result<Option<Vec<u8>>> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 206 | match fs::read(path) { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 207 | Ok(sealed) => Ok(Some(sealed)), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 208 | Err(error) if error.kind() == ErrorKind::NotFound => Ok(None), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 209 | Err(error) => Err(error), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 210 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 211 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 212 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 213 | pub(super) async fn write(path: &Path, sealed: &[u8]) -> io::Result<()> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 214 | let mut temporary = path.as_os_str().to_owned(); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 215 | temporary.push(".tmp"); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 216 | let temporary = PathBuf::from(temporary); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 217 | let parent = match path.parent() { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 218 | Some(parent) if !parent.as_os_str().is_empty() => parent, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 219 | _ => Path::new("."), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 220 | }; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 221 | fs::create_dir_all(parent)?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 222 | let written = replace(path, &temporary, parent, sealed); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 223 | if written.is_err() { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 224 | let _ = fs::remove_file(&temporary); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 225 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 226 | written |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 227 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 228 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 229 | fn replace(path: &Path, temporary: &Path, parent: &Path, sealed: &[u8]) -> io::Result<()> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 230 | let mut file = private_file(temporary)?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 231 | file.write_all(sealed)?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 232 | file.sync_all()?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 233 | fs::rename(temporary, path)?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 234 | sync_directory(parent) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 235 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 236 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 237 | pub(super) async fn remove(path: &Path) -> io::Result<()> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 238 | match fs::remove_file(path) { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 239 | Err(error) if error.kind() != ErrorKind::NotFound => Err(error), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 240 | _ => Ok(()), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 241 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 242 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 243 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 244 | #[cfg(unix)] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 245 | fn private_file(path: &Path) -> io::Result<File> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 246 | use std::os::unix::fs::OpenOptionsExt; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 247 | OpenOptions::new() |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 248 | .write(true) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 249 | .create(true) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 250 | .truncate(true) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 251 | .mode(0o600) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 252 | .open(path) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 253 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 254 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 255 | #[cfg(not(unix))] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 256 | fn private_file(path: &Path) -> io::Result<File> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 257 | OpenOptions::new() |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 258 | .write(true) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 259 | .create(true) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 260 | .truncate(true) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 261 | .open(path) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 262 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 263 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 264 | /// a rename isn't durable until its directory is synced |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 265 | #[cfg(unix)] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 266 | fn sync_directory(path: &Path) -> io::Result<()> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 267 | File::open(path)?.sync_all() |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 268 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 269 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 270 | #[cfg(not(unix))] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 271 | fn sync_directory(_: &Path) -> io::Result<()> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 272 | Ok(()) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 273 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 274 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 275 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 276 | #[cfg(feature = "indexeddb")] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 277 | mod backend { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 278 | use indexed_db_futures::{ |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 279 | Build, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 280 | database::Database, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 281 | error::{Error, OpenDbError}, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 282 | prelude::*, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 283 | transaction::{Transaction, TransactionMode}, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 284 | typed_array::{Uint8Array, Uint8ArraySlice}, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 285 | }; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 286 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 287 | const RECORDS: &str = "records"; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 288 | const KEY: &str = "record"; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 289 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 290 | async fn open(name: &str) -> Result<Database, OpenDbError> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 291 | Database::open(name) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 292 | .with_version(1u32) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 293 | .with_on_upgrade_needed(|_, transaction: &Transaction<'_>| -> Result<(), Error> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 294 | transaction.db().create_object_store(RECORDS).build()?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 295 | Ok(()) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 296 | }) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 297 | .build()? |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 298 | .await |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 299 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 300 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 301 | pub(super) async fn read(name: &str) -> Result<Option<Vec<u8>>, OpenDbError> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 302 | let database = open(name).await?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 303 | let sealed: Option<Uint8Array> = database |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 304 | .transaction(RECORDS) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 305 | .with_mode(TransactionMode::Readonly) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 306 | .build()? |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 307 | .object_store(RECORDS)? |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 308 | .get(KEY) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 309 | .await?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 310 | Ok(sealed.map(Into::into)) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 311 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 312 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 313 | pub(super) async fn write(name: &str, sealed: &[u8]) -> Result<(), OpenDbError> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 314 | let database = open(name).await?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 315 | let transaction = database |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 316 | .transaction(RECORDS) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 317 | .with_mode(TransactionMode::Readwrite) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 318 | .build()?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 319 | transaction |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 320 | .object_store(RECORDS)? |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 321 | .put(Uint8ArraySlice::new(sealed)) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 322 | .with_key(KEY) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 323 | .build()?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 324 | Ok(transaction.commit().await?) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 325 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 326 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 327 | pub(super) async fn remove(name: &str) -> Result<(), OpenDbError> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 328 | Ok(Database::delete_by_name(name)?.await?) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 329 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 330 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 331 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 332 | #[cfg(feature = "sqlite")] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 333 | pub(crate) fn delete_sqlite_store(path: &Path) -> std::io::Result<()> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 334 | use std::{fs, io::ErrorKind}; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 335 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 336 | // matrix-sdk-sqlite 0.18's database names, which it keeps private |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 337 | const DATABASES: [&str; 4] = [ |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 338 | "matrix-sdk-state.sqlite3", |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 339 | "matrix-sdk-crypto.sqlite3", |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 340 | "matrix-sdk-event-cache.sqlite3", |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 341 | "matrix-sdk-media.sqlite3", |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 342 | ]; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 343 | for database in DATABASES { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 344 | for suffix in ["", "-wal", "-shm", "-journal"] { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 345 | match fs::remove_file(path.join(format!("{database}{suffix}"))) { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 346 | Err(error) if error.kind() != ErrorKind::NotFound => return Err(error), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 347 | _ => {} |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 348 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 349 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 350 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 351 | match fs::remove_dir(path) { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 352 | Err(error) if error.kind() != ErrorKind::NotFound => Err(error), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 353 | _ => Ok(()), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 354 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 355 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 356 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 357 | #[cfg(feature = "indexeddb")] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 358 | pub(crate) async fn delete_indexeddb_store( |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 359 | name: &str, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 360 | ) -> Result<(), indexed_db_futures::error::OpenDbError> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 361 | use indexed_db_futures::database::Database; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 362 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 363 | // the databases matrix-sdk-indexeddb 0.18 opens under one store name |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 364 | for suffix in [ |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 365 | "", |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 366 | "::matrix-sdk-state", |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 367 | "::matrix-sdk-crypto", |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 368 | "::matrix-sdk-crypto-meta", |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 369 | "::event_cache", |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 370 | "::media", |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 371 | ] { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 372 | Database::delete_by_name(&format!("{name}{suffix}"))?.await?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 373 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 374 | Ok(()) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 375 | } |