From e23b22857fce00e6bfae0d0070c509babbb67d58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= Date: Tue, 15 Mar 2022 14:02:45 +0100 Subject: [PATCH] sled: Make store opening error more specific --- crates/matrix-sdk-sled/src/cryptostore.rs | 8 +++--- crates/matrix-sdk-sled/src/lib.rs | 31 +++++++++++++++++++---- crates/matrix-sdk-sled/src/state_store.rs | 7 ++++- 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/crates/matrix-sdk-sled/src/cryptostore.rs b/crates/matrix-sdk-sled/src/cryptostore.rs index d35d1bd74..ff3323315 100644 --- a/crates/matrix-sdk-sled/src/cryptostore.rs +++ b/crates/matrix-sdk-sled/src/cryptostore.rs @@ -49,6 +49,8 @@ use sled::{ }; use tracing::debug; +use super::OpenStoreError; + /// This needs to be 32 bytes long since AES-GCM requires it, otherwise we will /// panic once we try to pickle a Signing object. const DEFAULT_PICKLE: &str = "DEFAULT_PICKLE_PASSPHRASE_123456"; @@ -213,7 +215,7 @@ impl SledStore { pub fn open_with_passphrase( path: impl AsRef, passphrase: Option<&str>, - ) -> Result { + ) -> Result { let path = path.as_ref().join("matrix-sdk-crypto"); let db = Config::new() .temporary(false) @@ -226,7 +228,7 @@ impl SledStore { /// Create a sled based cryptostore using the given sled database. /// The given passphrase will be used to encrypt private data. - pub fn open_with_database(db: Db, passphrase: Option<&str>) -> Result { + pub fn open_with_database(db: Db, passphrase: Option<&str>) -> Result { SledStore::open_helper(db, None, passphrase) } @@ -383,7 +385,7 @@ impl SledStore { db: Db, path: Option, passphrase: Option<&str>, - ) -> Result { + ) -> Result { let account = db.open_tree("account")?; let private_identity = db.open_tree("private_identity")?; diff --git a/crates/matrix-sdk-sled/src/lib.rs b/crates/matrix-sdk-sled/src/lib.rs index 89b56b5ca..f499260c8 100644 --- a/crates/matrix-sdk-sled/src/lib.rs +++ b/crates/matrix-sdk-sled/src/lib.rs @@ -1,6 +1,10 @@ use std::path::Path; -use matrix_sdk_base::store::StoreConfig; +use matrix_sdk_base::store::{StoreConfig, StoreError}; +#[cfg(feature = "encryption")] +use matrix_sdk_crypto::store::CryptoStoreError; +use sled::Error as SledError; +use thiserror::Error; #[cfg(feature = "encryption")] mod cryptostore; @@ -16,14 +20,14 @@ pub use state_store::SledStore as StateStore; fn open_stores_with_path( path: impl AsRef, passphrase: Option<&str>, -) -> Result<(Box, Box), anyhow::Error> { +) -> Result<(Box, Box), OpenStoreError> { if let Some(passphrase) = passphrase { let state_store = StateStore::open_with_passphrase(path, passphrase)?; - let crypto_store = state_store.get_crypto_store(Some(passphrase))?; + let crypto_store = state_store.open_crypto_store(Some(passphrase))?; Ok((Box::new(state_store), Box::new(crypto_store))) } else { let state_store = StateStore::open_with_path(path)?; - let crypto_store = state_store.get_crypto_store(None)?; + let crypto_store = state_store.open_crypto_store(None)?; Ok((Box::new(state_store), Box::new(crypto_store))) } } @@ -34,7 +38,7 @@ fn open_stores_with_path( pub fn make_store_config( path: impl AsRef, passphrase: Option<&str>, -) -> Result { +) -> Result { #[cfg(feature = "encryption")] { let (state_store, crypto_store) = open_stores_with_path(path, passphrase)?; @@ -52,3 +56,20 @@ pub fn make_store_config( Ok(StoreConfig::new().state_store(Box::new(state_store))) } } + +/// All the errors that can occur when opening a sled store. +#[derive(Error, Debug)] +pub enum OpenStoreError { + /// An error occurred with the state store implementation. + #[error(transparent)] + State(#[from] StoreError), + + /// An error occurred with the crypto store implementation. + #[cfg(feature = "encryption")] + #[error(transparent)] + Crypto(#[from] CryptoStoreError), + + /// An error occurred with sled. + #[error(transparent)] + Sled(#[from] SledError), +} diff --git a/crates/matrix-sdk-sled/src/state_store.rs b/crates/matrix-sdk-sled/src/state_store.rs index db9838ece..195638e5a 100644 --- a/crates/matrix-sdk-sled/src/state_store.rs +++ b/crates/matrix-sdk-sled/src/state_store.rs @@ -59,6 +59,8 @@ use sled::{ use tokio::task::spawn_blocking; use tracing::{info, warn}; +#[cfg(feature = "encryption")] +use super::OpenStoreError; #[cfg(feature = "encryption")] pub use crate::CryptoStore; @@ -384,7 +386,10 @@ impl SledStore { /// Open a `CryptoStore` that uses the same database as this store. /// /// The given passphrase will be used to encrypt private data. - pub fn get_crypto_store(&self, passphrase: Option<&str>) -> Result { + pub fn open_crypto_store( + &self, + passphrase: Option<&str>, + ) -> Result { CryptoStore::open_with_database(self.inner.clone(), passphrase) }