mirror of
https://github.com/chatmail/core.git
synced 2026-04-27 18:36:30 +03:00
Resultify sql.open()
This commit is contained in:
@@ -136,10 +136,7 @@ impl Context {
|
|||||||
let ctx = Context {
|
let ctx = Context {
|
||||||
inner: Arc::new(inner),
|
inner: Arc::new(inner),
|
||||||
};
|
};
|
||||||
ensure!(
|
ctx.sql.open(&ctx, &ctx.dbfile, false).await?;
|
||||||
ctx.sql.open(&ctx, &ctx.dbfile, false).await,
|
|
||||||
"Failed opening sqlite database"
|
|
||||||
);
|
|
||||||
|
|
||||||
Ok(ctx)
|
Ok(ctx)
|
||||||
}
|
}
|
||||||
|
|||||||
32
src/imex.rs
32
src/imex.rs
@@ -6,6 +6,7 @@ use std::{
|
|||||||
ffi::OsStr,
|
ffi::OsStr,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use anyhow::Context as _;
|
||||||
use async_std::path::{Path, PathBuf};
|
use async_std::path::{Path, PathBuf};
|
||||||
use async_std::{
|
use async_std::{
|
||||||
fs::{self, File},
|
fs::{self, File},
|
||||||
@@ -118,7 +119,11 @@ async fn cleanup_aborted_imex(context: &Context, what: ImexMode) {
|
|||||||
dc_delete_files_in_dir(context, context.get_blobdir()).await;
|
dc_delete_files_in_dir(context, context.get_blobdir()).await;
|
||||||
}
|
}
|
||||||
if what == ImexMode::ExportBackup || what == ImexMode::ImportBackup {
|
if what == ImexMode::ExportBackup || what == ImexMode::ImportBackup {
|
||||||
context.sql.open(context, context.get_dbfile(), false).await;
|
context
|
||||||
|
.sql
|
||||||
|
.open(context, context.get_dbfile(), false)
|
||||||
|
.await
|
||||||
|
.ok();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -166,7 +171,7 @@ pub async fn has_backup_old(context: &Context, dir_name: impl AsRef<Path>) -> Re
|
|||||||
let name = name.to_string_lossy();
|
let name = name.to_string_lossy();
|
||||||
if name.starts_with("delta-chat") && name.ends_with(".bak") {
|
if name.starts_with("delta-chat") && name.ends_with(".bak") {
|
||||||
let sql = Sql::new();
|
let sql = Sql::new();
|
||||||
if sql.open(context, &path, true).await {
|
if sql.open(context, &path, true).await.is_ok() {
|
||||||
let curr_backup_time = sql
|
let curr_backup_time = sql
|
||||||
.get_raw_config_int(context, "backup_time")
|
.get_raw_config_int(context, "backup_time")
|
||||||
.await
|
.await
|
||||||
@@ -520,13 +525,11 @@ async fn import_backup(context: &Context, backup_to_import: impl AsRef<Path>) ->
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ensure!(
|
|
||||||
context
|
context
|
||||||
.sql
|
.sql
|
||||||
.open(&context, &context.get_dbfile(), false)
|
.open(&context, &context.get_dbfile(), false)
|
||||||
.await,
|
.await
|
||||||
"could not re-open db"
|
.context("Could not re-open db")?;
|
||||||
);
|
|
||||||
|
|
||||||
delete_and_reset_all_device_msgs(&context).await?;
|
delete_and_reset_all_device_msgs(&context).await?;
|
||||||
|
|
||||||
@@ -558,13 +561,11 @@ async fn import_backup_old(context: &Context, backup_to_import: impl AsRef<Path>
|
|||||||
);
|
);
|
||||||
/* error already logged */
|
/* error already logged */
|
||||||
/* re-open copied database file */
|
/* re-open copied database file */
|
||||||
ensure!(
|
|
||||||
context
|
context
|
||||||
.sql
|
.sql
|
||||||
.open(&context, &context.get_dbfile(), false)
|
.open(&context, &context.get_dbfile(), false)
|
||||||
.await,
|
.await
|
||||||
"could not re-open db"
|
.context("Could not re-open db")?;
|
||||||
);
|
|
||||||
|
|
||||||
delete_and_reset_all_device_msgs(&context).await?;
|
delete_and_reset_all_device_msgs(&context).await?;
|
||||||
|
|
||||||
@@ -743,7 +744,7 @@ async fn export_backup_old(context: &Context, dir: impl AsRef<Path>) -> Result<(
|
|||||||
context
|
context
|
||||||
.sql
|
.sql
|
||||||
.open(&context, &context.get_dbfile(), false)
|
.open(&context, &context.get_dbfile(), false)
|
||||||
.await;
|
.await?;
|
||||||
|
|
||||||
if !copied {
|
if !copied {
|
||||||
bail!(
|
bail!(
|
||||||
@@ -753,11 +754,14 @@ async fn export_backup_old(context: &Context, dir: impl AsRef<Path>) -> Result<(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
let dest_sql = Sql::new();
|
let dest_sql = Sql::new();
|
||||||
ensure!(
|
dest_sql
|
||||||
dest_sql.open(context, &dest_path_filename, false).await,
|
.open(context, &dest_path_filename, false)
|
||||||
|
.await
|
||||||
|
.context(format!(
|
||||||
"could not open exported database {}",
|
"could not open exported database {}",
|
||||||
dest_path_string
|
dest_path_string
|
||||||
);
|
))?;
|
||||||
|
|
||||||
let res = match add_files_to_export(context, &dest_sql).await {
|
let res = match add_files_to_export(context, &dest_sql).await {
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
dc_delete_file(context, &dest_path_filename).await;
|
dc_delete_file(context, &dest_path_filename).await;
|
||||||
|
|||||||
20
src/sql.rs
20
src/sql.rs
@@ -14,6 +14,7 @@ use crate::constants::{ShowEmails, DC_CHAT_ID_TRASH};
|
|||||||
use crate::context::Context;
|
use crate::context::Context;
|
||||||
use crate::dc_tools::*;
|
use crate::dc_tools::*;
|
||||||
use crate::ephemeral::start_ephemeral_timers;
|
use crate::ephemeral::start_ephemeral_timers;
|
||||||
|
use crate::error::format_err;
|
||||||
use crate::param::*;
|
use crate::param::*;
|
||||||
use crate::peerstate::*;
|
use crate::peerstate::*;
|
||||||
|
|
||||||
@@ -78,18 +79,23 @@ impl Sql {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// return true on success, false on failure
|
// return true on success, false on failure
|
||||||
pub async fn open<T: AsRef<Path>>(&self, context: &Context, dbfile: T, readonly: bool) -> bool {
|
pub async fn open<T: AsRef<Path>>(
|
||||||
match open(context, self, dbfile, readonly).await {
|
&self,
|
||||||
Ok(_) => true,
|
context: &Context,
|
||||||
Err(err) => match err.downcast_ref::<Error>() {
|
dbfile: T,
|
||||||
Some(Error::SqlAlreadyOpen) => false,
|
readonly: bool,
|
||||||
|
) -> crate::error::Result<()> {
|
||||||
|
let res = open(context, self, dbfile, readonly).await;
|
||||||
|
if let Err(err) = &res {
|
||||||
|
match err.downcast_ref::<Error>() {
|
||||||
|
Some(Error::SqlAlreadyOpen) => {}
|
||||||
_ => {
|
_ => {
|
||||||
self.close().await;
|
self.close().await;
|
||||||
false
|
|
||||||
}
|
}
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
res.map_err(|e| format_err!("Could not open db: {}", e))
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn execute<S: AsRef<str>>(
|
pub async fn execute<S: AsRef<str>>(
|
||||||
&self,
|
&self,
|
||||||
|
|||||||
Reference in New Issue
Block a user