Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ pub fn run() {
#[cfg(feature = "matrix-crypto")]
matrix_crypto::engine_wipe,
#[cfg(feature = "matrix-crypto")]
matrix_crypto::push::engine_decrypt_push,
matrix_crypto::engine_store_exists,
share_inbox::share_inbox_drain,
share_inbox::share_inbox_read,
share_inbox::share_inbox_clear,
Expand Down
50 changes: 0 additions & 50 deletions src-tauri/src/matrix_crypto/jni_push.rs

This file was deleted.

49 changes: 45 additions & 4 deletions src-tauri/src/matrix_crypto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ pub mod cross_signing;
pub mod devices;
pub mod dispatch;
pub mod events;
#[cfg(target_os = "android")]
pub mod jni_push;
pub mod message_flow;
pub mod push;
pub mod requests;
pub mod rooms;
pub mod verification;
Expand Down Expand Up @@ -152,6 +149,10 @@ pub fn store_subpath(user_id: &str, device_id: &str) -> PathBuf {
PathBuf::from("matrix-crypto").join(account)
}

fn store_db_path(dir: &Path) -> PathBuf {
dir.join("matrix-sdk-crypto.sqlite3")
}

/// Per-account store directory. Resolved here rather than passed in so the
/// webview never has to know an absolute path, and so the native notification
/// handler can derive the same location independently.
Expand All @@ -170,6 +171,23 @@ fn store_dir(
Ok(base.join(store_subpath(user_id, device_id)))
}

async fn store_exists_at(dir: &Path) -> Result<bool, String> {
match tokio::fs::metadata(store_db_path(dir)).await {
Ok(metadata) => Ok(metadata.is_file()),
Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(false),
Err(error) => Err(format!("checking crypto store failed: {error}")),
}
}

#[tauri::command]
pub async fn engine_store_exists(
app: tauri::AppHandle<crate::BrowserEngine>,
user_id: String,
device_id: String,
) -> Result<bool, String> {
store_exists_at(&store_dir(&app, &user_id, &device_id)?).await
}

pub(super) static OPEN_GUARD: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(());

/// Opens a store and registers its machine, replacing any machine already open for the
Expand Down Expand Up @@ -199,7 +217,7 @@ pub(super) async fn open_machine_locked(
tokio::fs::create_dir_all(dir)
.await
.map_err(|e| e.to_string())?;
let db_path = dir.join("matrix-sdk-crypto.sqlite3");
let db_path = store_db_path(dir);

let account = account_key(user_id, device_id);
engines().close_account(&account)?;
Expand Down Expand Up @@ -329,6 +347,29 @@ mod tests {
);
}

#[tokio::test]
async fn store_exists_does_not_create_a_missing_store() {
let dir =
std::env::temp_dir().join(format!("sable-store-exists-missing-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);

assert!(!store_exists_at(&dir).await.unwrap());
assert!(!dir.exists());
}

#[tokio::test]
async fn store_exists_only_accepts_the_crypto_database_file() {
let dir =
std::env::temp_dir().join(format!("sable-store-exists-present-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(store_db_path(&dir), b"placeholder").unwrap();

assert!(store_exists_at(&dir).await.unwrap());

let _ = std::fs::remove_dir_all(&dir);
}

#[tokio::test]
async fn outgoing_room_key_requests_stay_disabled() {
let user: &matrix_sdk::ruma::UserId = "@gossip:example.org".try_into().unwrap();
Expand Down
Loading
Loading