From be80920107599bda393cb8db120f360952946ec6 Mon Sep 17 00:00:00 2001 From: hcaumeil <78665596+hcaumeil@users.noreply.github.com> Date: Fri, 19 Apr 2024 12:24:33 +0100 Subject: [PATCH] Make export from sled to sqlite a build feature --- Cargo.toml | 5 +++-- src/base.rs | 3 ++- src/config.rs | 4 ++++ src/main.rs | 24 ++++++++++++++++++++++-- src/tests.rs | 1 + 5 files changed, 32 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e0913a2..6e6f122 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ build = "build.rs" [features] default = ["bundled"] +sled-export = ["dep:temp-dir", "dep:sled"] bundled = ["matrix-sdk/bundled-sqlite", "rustls-tls"] native-tls = ["matrix-sdk/native-tls"] rustls-tls = ["matrix-sdk/rustls-tls"] @@ -50,8 +51,8 @@ regex = "^1.5" rpassword = "^7.2" serde = "^1.0" serde_json = "^1.0" -sled = "0.34.7" -temp-dir = "0.1.12" +sled = { version = "0.34.7", optional = true } +temp-dir = { version = "0.1.12", optional = true } thiserror = "^1.0.37" toml = "^0.8.12" tracing = "~0.1.36" diff --git a/src/base.rs b/src/base.rs index f576d34..6914596 100644 --- a/src/base.rs +++ b/src/base.rs @@ -608,6 +608,7 @@ pub enum IambError { FailedKeyImport(#[from] matrix_sdk::encryption::RoomKeyImportError), /// A failure related to the cryptographic store. + #[cfg(feature = "sled-export")] #[error("Cannot export keys from sled: {0}")] UpgradeSled(#[from] crate::sled_export::SledMigrationError), @@ -619,7 +620,7 @@ pub enum IambError { #[error("Matrix client error: {0}")] Matrix(#[from] matrix_sdk::Error), - /// A failure in the sled storage. + /// A failure in the storage. #[error("Matrix client storage error: {0}")] Store(#[from] matrix_sdk::StoreError), diff --git a/src/config.rs b/src/config.rs index 4d31fff..70ea363 100644 --- a/src/config.rs +++ b/src/config.rs @@ -748,6 +748,7 @@ pub struct ApplicationSettings { pub layout_json: PathBuf, pub session_json: PathBuf, pub session_json_old: PathBuf, + #[cfg(feature = "sled-export")] pub sled_dir: PathBuf, pub sqlite_dir: PathBuf, pub profile_name: String, @@ -836,7 +837,9 @@ impl ApplicationSettings { profile_data_dir.push("profiles"); profile_data_dir.push(profile_name.as_str()); + #[cfg(feature = "sled-export")] let mut sled_dir = profile_dir.clone(); + #[cfg(feature = "sled-export")] sled_dir.push("matrix"); let mut sqlite_dir = profile_data_dir.clone(); @@ -857,6 +860,7 @@ impl ApplicationSettings { layout_json.push("layout.json"); let settings = ApplicationSettings { + #[cfg(feature = "sled-export")] sled_dir, layout_json, session_json, diff --git a/src/main.rs b/src/main.rs index 739c9aa..e2e5cd0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,11 +27,14 @@ use std::sync::Arc; use std::time::{Duration, Instant}; use clap::Parser; +#[cfg(feature = "sled-export")] use matrix_sdk::crypto::encrypt_room_key_export; use matrix_sdk::ruma::api::client::error::ErrorKind; use matrix_sdk::ruma::OwnedUserId; use modalkit::keybindings::InputBindings; +#[cfg(feature = "sled-export")] use rand::{distributions::Alphanumeric, Rng}; +#[cfg(feature = "sled-export")] use temp_dir::TempDir; use tokio::sync::Mutex as AsyncMutex; use tracing_subscriber::FmtSubscriber; @@ -69,11 +72,13 @@ mod keybindings; mod message; mod notifications; mod preview; -mod sled_export; mod util; mod windows; mod worker; +#[cfg(feature = "sled-export")] +mod sled_export; + #[cfg(test)] mod tests; @@ -711,6 +716,7 @@ impl Application { } } +#[cfg(feature = "sled-export")] fn gen_passphrase() -> String { rand::thread_rng() .sample_iter(&Alphanumeric) @@ -726,6 +732,7 @@ fn read_response(question: &str) -> String { input } +#[cfg(feature = "sled-export")] fn read_yesno(question: &str) -> Option { read_response(question).chars().next().map(|c| c.to_ascii_lowercase()) } @@ -738,7 +745,13 @@ async fn login(worker: &Requester, settings: &ApplicationSettings) -> IambResult return Ok(()); } - if settings.session_json_old.is_file() && !settings.sled_dir.is_dir() { + #[cfg(not(feature = "sled-export"))] + let check_old_session = settings.session_json_old.is_file(); + + #[cfg(feature = "sled-export")] + let check_old_session = settings.session_json_old.is_file() && !settings.sled_dir.is_dir(); + + if check_old_session { let session = settings.read_session(&settings.session_json_old)?; worker.login(LoginStyle::SessionRestore(session.into()))?; @@ -788,6 +801,7 @@ fn print_exit(v: T) -> N { // We can't access the OlmMachine directly, so write the keys to a temporary // file first, and then import them later. +#[cfg(feature = "sled-export")] async fn check_import_keys( settings: &ApplicationSettings, ) -> IambResult> { @@ -838,6 +852,7 @@ async fn check_import_keys( Ok(Some((tmpdir, passphrase))) } +#[cfg(feature = "sled-export")] async fn login_upgrade( keydir: TempDir, passphrase: String, @@ -907,6 +922,7 @@ async fn login_normal( async fn run(settings: ApplicationSettings) -> IambResult<()> { // Get old keys the first time we run w/ the upgraded SDK. + #[cfg(feature = "sled-export")] let import_keys = check_import_keys(&settings).await?; // Set up client state. @@ -920,12 +936,16 @@ async fn run(settings: ApplicationSettings) -> IambResult<()> { let store = Arc::new(AsyncMutex::new(store)); worker.init(store.clone()); + #[cfg(feature = "sled-export")] let res = if let Some((keydir, pass)) = import_keys { login_upgrade(keydir, pass, &worker, &settings, &store).await } else { login_normal(&worker, &settings, &store).await }; + #[cfg(not(feature = "sled-export"))] + let res = login_normal(&worker, &settings, &store).await; + match res { Err(UIError::Application(IambError::Matrix(e))) => { if let Some(ErrorKind::UnknownToken { .. }) = e.client_api_error_kind() { diff --git a/src/tests.rs b/src/tests.rs index b385992..0df9f85 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -204,6 +204,7 @@ pub fn mock_settings() -> ApplicationSettings { layout_json: PathBuf::new(), session_json: PathBuf::new(), session_json_old: PathBuf::new(), + #[cfg(feature = "sled-export")] sled_dir: PathBuf::new(), sqlite_dir: PathBuf::new(),