base: Feature flag the sled state store

master
Damir Jelić 2021-01-22 18:10:17 +01:00
parent cb12bc1584
commit 8028c23f56
4 changed files with 33 additions and 21 deletions

View File

@ -15,10 +15,10 @@ features = ["docs"]
rustdoc-args = ["--cfg", "feature=\"docs\""] rustdoc-args = ["--cfg", "feature=\"docs\""]
[features] [features]
default = ["encryption", "sled_cryptostore", "messages", "native-tls"] default = ["encryption", "sled_cryptostore", "sled_state_store", "native-tls"]
messages = ["matrix-sdk-base/messages"]
encryption = ["matrix-sdk-base/encryption", "dashmap"] encryption = ["matrix-sdk-base/encryption", "dashmap"]
sled_state_store = ["matrix-sdk-base/sled_state_store"]
sled_cryptostore = ["matrix-sdk-base/sled_cryptostore"] sled_cryptostore = ["matrix-sdk-base/sled_cryptostore"]
unstable-synapse-quirks = ["matrix-sdk-base/unstable-synapse-quirks"] unstable-synapse-quirks = ["matrix-sdk-base/unstable-synapse-quirks"]
markdown = ["matrix-sdk-base/markdown"] markdown = ["matrix-sdk-base/markdown"]
@ -26,7 +26,7 @@ native-tls = ["reqwest/native-tls"]
rustls-tls = ["reqwest/rustls-tls"] rustls-tls = ["reqwest/rustls-tls"]
socks = ["reqwest/socks"] socks = ["reqwest/socks"]
docs = ["encryption", "sled_cryptostore", "messages"] docs = ["encryption", "sled_cryptostore", "sled_state_store"]
[dependencies] [dependencies]
dashmap = { version = "4.0.1", optional = true } dashmap = { version = "4.0.1", optional = true }

View File

@ -16,13 +16,13 @@ rustdoc-args = ["--cfg", "feature=\"docs\""]
[features] [features]
default = [] default = []
messages = []
encryption = ["matrix-sdk-crypto"] encryption = ["matrix-sdk-crypto"]
sled_state_store = ["sled", "pbkdf2", "hmac", "sha2", "rand", "chacha20poly1305"]
sled_cryptostore = ["matrix-sdk-crypto/sled_cryptostore"] sled_cryptostore = ["matrix-sdk-crypto/sled_cryptostore"]
unstable-synapse-quirks = ["matrix-sdk-common/unstable-synapse-quirks"] unstable-synapse-quirks = ["matrix-sdk-common/unstable-synapse-quirks"]
markdown = ["matrix-sdk-common/markdown"] markdown = ["matrix-sdk-common/markdown"]
docs = ["encryption", "sled_cryptostore", "messages"] docs = ["encryption", "sled_cryptostore"]
[dependencies] [dependencies]
dashmap= "4.0.1" dashmap= "4.0.1"
@ -35,14 +35,16 @@ matrix-sdk-crypto = { version = "0.2.0", path = "../matrix_sdk_crypto", optional
# Misc dependencies # Misc dependencies
thiserror = "1.0.23" thiserror = "1.0.23"
sled = "0.34.6"
chacha20poly1305 = "0.7.1"
rand = "0.8.2"
zeroize = { version = "1.2.0", features = ["zeroize_derive"] }
pbkdf2 = { version = "0.6.0", default-features = false }
hmac = "0.10.1"
sha2 = "0.9.2"
futures = "0.3.8" futures = "0.3.8"
zeroize = { version = "1.2.0", features = ["zeroize_derive"] }
# Deps for the sled state store
sled = { version = "0.34.6", optional = true }
chacha20poly1305 = { version = "0.7.1", optional = true }
pbkdf2 = { version = "0.6.0", default-features = false, optional = true }
hmac = { version = "0.10.1", optional = true }
sha2 = { version = "0.9.2", optional = true }
rand = { version = "0.8.2", optional = true }
[target.'cfg(not(target_arch = "wasm32"))'.dependencies.tokio] [target.'cfg(not(target_arch = "wasm32"))'.dependencies.tokio]
version = "1.0.1" version = "1.0.1"

View File

@ -279,8 +279,8 @@ impl BaseClient {
/// * `config` - An optional session if the user already has one from a /// * `config` - An optional session if the user already has one from a
/// previous login call. /// previous login call.
pub fn new_with_config(config: BaseClientConfig) -> Result<Self> { pub fn new_with_config(config: BaseClientConfig) -> Result<Self> {
#[cfg(not(target_arch = "wasm32"))] #[cfg(feature = "sled_state_store")]
let (store, sled_store) = if let Some(path) = &config.store_path { let stores = if let Some(path) = &config.store_path {
if config.passphrase.is_some() { if config.passphrase.is_some() {
info!("Opening an encrypted store in path {}", path.display()); info!("Opening an encrypted store in path {}", path.display());
} else { } else {
@ -290,15 +290,15 @@ impl BaseClient {
} else { } else {
Store::open_temporary()? Store::open_temporary()?
}; };
#[cfg(target_arch = "wasm32")] #[cfg(not(feature = "sled_state_store"))]
let store = Store::open_memory_store(); let stores = Store::open_memory_store();
#[cfg(not(target_arch = "wasm32"))] #[cfg(all(feature = "encryption", feature = "sled_state_store"))]
let crypto_store = if config.crypto_store.is_none() { let crypto_store = if config.crypto_store.is_none() {
#[cfg(feature = "sled_cryptostore")] #[cfg(feature = "sled_cryptostore")]
let store: Option<Box<dyn CryptoStore>> = Some(Box::new( let store: Option<Box<dyn CryptoStore>> = Some(Box::new(
matrix_sdk_crypto::store::SledStore::open_with_database( matrix_sdk_crypto::store::SledStore::open_with_database(
sled_store, stores.1,
config.passphrase.as_deref().map(|p| p.as_str()), config.passphrase.as_deref().map(|p| p.as_str()),
) )
.map_err(OlmError::Store)?, .map_err(OlmError::Store)?,
@ -310,9 +310,14 @@ impl BaseClient {
} else { } else {
config.crypto_store config.crypto_store
}; };
#[cfg(target_arch = "wasm32")] #[cfg(all(not(feature = "sled_state_store"), feature = "encryption"))]
let crypto_store = config.crypto_store; let crypto_store = config.crypto_store;
#[cfg(feature = "sled_state_store")]
let store = stores.0;
#[cfg(not(feature = "sled_state_store"))]
let store = stores;
Ok(BaseClient { Ok(BaseClient {
session: store.session.clone(), session: store.session.clone(),
sync_token: store.sync_token.clone(), sync_token: store.sync_token.clone(),
@ -403,6 +408,7 @@ impl BaseClient {
} }
#[cfg(not(feature = "sled_cryptostore"))] #[cfg(not(feature = "sled_cryptostore"))]
{ {
let _ = path;
*olm = Some(OlmMachine::new(&session.user_id, &session.device_id)); *olm = Some(OlmMachine::new(&session.user_id, &session.device_id));
} }
} else { } else {

View File

@ -25,6 +25,7 @@ use matrix_sdk_common::{
locks::RwLock, locks::RwLock,
AsyncTraitDeps, AsyncTraitDeps,
}; };
#[cfg(feature = "sled_state_store")]
use sled::Db; use sled::Db;
use crate::{ use crate::{
@ -34,15 +35,16 @@ use crate::{
}; };
mod memory_store; mod memory_store;
#[cfg(not(target_arch = "wasm32"))] #[cfg(feature = "sled_state_store")]
mod sled_store; mod sled_store;
use self::memory_store::MemoryStore; use self::memory_store::MemoryStore;
#[cfg(not(target_arch = "wasm32"))] #[cfg(feature = "sled_state_store")]
use self::sled_store::SledStore; use self::sled_store::SledStore;
#[derive(Debug, thiserror::Error)] #[derive(Debug, thiserror::Error)]
pub enum StoreError { pub enum StoreError {
#[cfg(feature = "sled_state_store")]
#[error(transparent)] #[error(transparent)]
Sled(#[from] sled::Error), Sled(#[from] sled::Error),
#[error(transparent)] #[error(transparent)]
@ -142,6 +144,7 @@ impl Store {
Self::new(inner) Self::new(inner)
} }
#[cfg(feature = "sled_state_store")]
pub fn open_default(path: impl AsRef<Path>, passphrase: Option<&str>) -> Result<(Self, Db)> { pub fn open_default(path: impl AsRef<Path>, passphrase: Option<&str>) -> Result<(Self, Db)> {
let inner = if let Some(passphrase) = passphrase { let inner = if let Some(passphrase) = passphrase {
SledStore::open_with_passphrase(path, passphrase)? SledStore::open_with_passphrase(path, passphrase)?
@ -152,6 +155,7 @@ impl Store {
Ok((Self::new(Box::new(inner.clone())), inner.inner)) Ok((Self::new(Box::new(inner.clone())), inner.inner))
} }
#[cfg(feature = "sled_state_store")]
pub fn open_temporary() -> Result<(Self, Db)> { pub fn open_temporary() -> Result<(Self, Db)> {
let inner = SledStore::open()?; let inner = SledStore::open()?;