sled: Make store opening error more specific

This commit is contained in:
Kévin Commaille
2022-03-15 14:02:45 +01:00
parent 3bf5388da0
commit e23b22857f
3 changed files with 37 additions and 9 deletions

View File

@@ -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<Path>,
passphrase: Option<&str>,
) -> Result<Self, anyhow::Error> {
) -> Result<Self, OpenStoreError> {
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<Self, anyhow::Error> {
pub fn open_with_database(db: Db, passphrase: Option<&str>) -> Result<Self, OpenStoreError> {
SledStore::open_helper(db, None, passphrase)
}
@@ -383,7 +385,7 @@ impl SledStore {
db: Db,
path: Option<PathBuf>,
passphrase: Option<&str>,
) -> Result<Self, anyhow::Error> {
) -> Result<Self, OpenStoreError> {
let account = db.open_tree("account")?;
let private_identity = db.open_tree("private_identity")?;

View File

@@ -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<Path>,
passphrase: Option<&str>,
) -> Result<(Box<StateStore>, Box<CryptoStore>), anyhow::Error> {
) -> Result<(Box<StateStore>, Box<CryptoStore>), 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<Path>,
passphrase: Option<&str>,
) -> Result<StoreConfig, anyhow::Error> {
) -> Result<StoreConfig, OpenStoreError> {
#[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),
}

View File

@@ -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<CryptoStore, anyhow::Error> {
pub fn open_crypto_store(
&self,
passphrase: Option<&str>,
) -> Result<CryptoStore, OpenStoreError> {
CryptoStore::open_with_database(self.inner.clone(), passphrase)
}