Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make export from sled to sqlite a build feature #270

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),

Expand All @@ -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),

Expand Down
4 changes: 4 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand All @@ -857,6 +860,7 @@ impl ApplicationSettings {
layout_json.push("layout.json");

let settings = ApplicationSettings {
#[cfg(feature = "sled-export")]
sled_dir,
layout_json,
session_json,
Expand Down
24 changes: 22 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -72,11 +75,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;

Expand Down Expand Up @@ -705,6 +710,7 @@ impl Application {
}
}

#[cfg(feature = "sled-export")]
fn gen_passphrase() -> String {
rand::thread_rng()
.sample_iter(&Alphanumeric)
Expand All @@ -720,6 +726,7 @@ fn read_response(question: &str) -> String {
input
}

#[cfg(feature = "sled-export")]
fn read_yesno(question: &str) -> Option<char> {
read_response(question).chars().next().map(|c| c.to_ascii_lowercase())
}
Expand All @@ -732,7 +739,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()))?;

Expand Down Expand Up @@ -782,6 +795,7 @@ fn print_exit<T: Display, N>(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<Option<(temp_dir::TempDir, String)>> {
Expand Down Expand Up @@ -832,6 +846,7 @@ async fn check_import_keys(
Ok(Some((tmpdir, passphrase)))
}

#[cfg(feature = "sled-export")]
async fn login_upgrade(
keydir: TempDir,
passphrase: String,
Expand Down Expand Up @@ -965,6 +980,7 @@ fn restore_tty(enable_enhanced_keys: bool) {

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.
Expand All @@ -978,12 +994,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() {
Expand Down
1 change: 1 addition & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),

Expand Down
Loading