src/sled_export.rs
58 lines · 2001 bytes
1//! # sled -> sqlite migration code
2//!
3//! Before the 0.0.9 release, iamb used matrix-sdk@0.6.2, which used [sled]
4//! for storing information, including room keys. In matrix-sdk@0.7.0,
5//! the SDK switched to using SQLite. This module takes care of opening
6//! sled, exporting the inbound group sessions used for decryption,
7//! and importing them into SQLite.
8//!
9//! This code will eventually be removed once people have been given enough
10//! time to upgrade off of pre-0.0.9 versions.
11//!
12//! [sled]: https://docs.rs/sled/0.34.7/sled/index.html
13use sled::{Config, IVec};
14use std::path::Path;
15
16use crate::base::IambError;
17use matrix_sdk_crypto::olm::{ExportedRoomKey, InboundGroupSession, PickledInboundGroupSession};
18
19#[derive(Debug, thiserror::Error)]
20pub enum SledMigrationError {
21 #[error("sled failure: {0}")]
22 Sled(#[from] sled::Error),
23
24 #[error("deserialization failure: {0}")]
25 Deserialize(#[from] serde_json::Error),
26}
27
28fn group_session_from_slice(
29 (_, bytes): (IVec, IVec),
30) -> Result<PickledInboundGroupSession, SledMigrationError> {
31 serde_json::from_slice(&bytes).map_err(SledMigrationError::from)
32}
33
34async fn export_room_keys_priv(
35 sled_dir: &Path,
36) -> Result<Vec<ExportedRoomKey>, SledMigrationError> {
37 let path = sled_dir.join("matrix-sdk-state");
38 let store = Config::new().temporary(false).path(&path).open()?;
39 let inbound_groups = store.open_tree("inbound_group_sessions")?;
40
41 let mut exported = vec![];
42 let sessions = inbound_groups
43 .iter()
44 .map(|p| p.map_err(SledMigrationError::from).and_then(group_session_from_slice))
45 .collect::<Result<Vec<_>, _>>()?
46 .into_iter()
47 .filter_map(|p| InboundGroupSession::from_pickle(p).ok());
48
49 for session in sessions {
50 exported.push(session.export().await);
51 }
52
53 Ok(exported)
54}
55
56pub async fn export_room_keys(sled_dir: &Path) -> Result<Vec<ExportedRoomKey>, IambError> {
57 export_room_keys_priv(sled_dir).await.map_err(IambError::from)
58}