check for invalid collections in full_upload()

This commit is contained in:
Damien Elmes 2021-09-13 11:50:13 +10:00
parent 93148211e0
commit 251c5ae080
2 changed files with 20 additions and 5 deletions

View file

@ -5,7 +5,11 @@ pub(crate) mod timestamps;
mod transact; mod transact;
pub(crate) mod undo; pub(crate) mod undo;
use std::{collections::HashMap, path::PathBuf, sync::Arc}; use std::{
collections::HashMap,
path::{Path, PathBuf},
sync::Arc,
};
use crate::{ use crate::{
browser_table, browser_table,
@ -65,6 +69,15 @@ pub fn open_test_collection_with_server(server: bool) -> Collection {
open_collection(":memory:", "", "", server, tr, log::terminal()).unwrap() 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<Collection> {
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)] #[derive(Debug, Default)]
pub struct CollectionState { pub struct CollectionState {
pub(crate) undo: UndoManager, pub(crate) undo: UndoManager,

View file

@ -8,6 +8,7 @@ use tempfile::NamedTempFile;
use super::ChunkableIds; use super::ChunkableIds;
use crate::{ use crate::{
collection::open_and_check_collection,
prelude::*, prelude::*,
storage::open_and_check_sqlite_file, storage::open_and_check_sqlite_file,
sync::{ sync::{
@ -197,11 +198,12 @@ impl SyncServer for LocalServer {
col_path = new_file.path(); col_path = new_file.path();
} }
open_and_check_sqlite_file(col_path).map_err(|check_err| { // ensure it's a valid sqlite file, and a valid collection
match fs::remove_file(col_path) { 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, Ok(_) => check_err,
Err(remove_err) => remove_err.into(), Err(remove_err) => remove_err.into(),
}
})?; })?;
let target_col_path = self.col.col_path.clone(); let target_col_path = self.col.col_path.clone();