diff --git a/rslib/src/collection/mod.rs b/rslib/src/collection/mod.rs index 79660276d..9bffaf5f7 100644 --- a/rslib/src/collection/mod.rs +++ b/rslib/src/collection/mod.rs @@ -5,7 +5,11 @@ pub(crate) mod timestamps; mod transact; pub(crate) mod undo; -use std::{collections::HashMap, path::PathBuf, sync::Arc}; +use std::{ + collections::HashMap, + path::{Path, PathBuf}, + sync::Arc, +}; use crate::{ browser_table, @@ -65,6 +69,15 @@ pub fn open_test_collection_with_server(server: bool) -> Collection { open_collection(":memory:", "", "", server, tr, log::terminal()).unwrap() } +/// Helper used by syncing to make sure the file can be opened. This should be replaced +/// with a builder in the future. +pub(crate) fn open_and_check_collection(col_path: &Path) -> Result { + use crate::log; + let tr = I18n::template_only(); + let empty = Path::new(""); + open_collection(col_path, &empty, &empty, true, tr, log::terminal()) +} + #[derive(Debug, Default)] pub struct CollectionState { pub(crate) undo: UndoManager, diff --git a/rslib/src/sync/server.rs b/rslib/src/sync/server.rs index 578b4968c..8d680618c 100644 --- a/rslib/src/sync/server.rs +++ b/rslib/src/sync/server.rs @@ -8,6 +8,7 @@ use tempfile::NamedTempFile; use super::ChunkableIds; use crate::{ + collection::open_and_check_collection, prelude::*, storage::open_and_check_sqlite_file, sync::{ @@ -197,12 +198,13 @@ impl SyncServer for LocalServer { col_path = new_file.path(); } - open_and_check_sqlite_file(col_path).map_err(|check_err| { - match fs::remove_file(col_path) { + // ensure it's a valid sqlite file, and a valid collection + open_and_check_sqlite_file(col_path) + .and_then(|_| open_and_check_collection(col_path)) + .map_err(|check_err| match fs::remove_file(col_path) { Ok(_) => check_err, Err(remove_err) => remove_err.into(), - } - })?; + })?; let target_col_path = self.col.col_path.clone(); self.col.close(false)?;