mirror of
https://github.com/chatmail/core.git
synced 2026-05-18 22:36:29 +03:00
Compare commits
2 Commits
v1.142.8
...
link2xt/no
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6dd2f16df1 | ||
|
|
0cdf9b8f9c |
@@ -1,7 +1,6 @@
|
|||||||
//! # Account manager module.
|
//! # Account manager module.
|
||||||
|
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
use std::future::Future;
|
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
use anyhow::{ensure, Context as _, Result};
|
use anyhow::{ensure, Context as _, Result};
|
||||||
@@ -166,12 +165,13 @@ impl Accounts {
|
|||||||
.remove(&id)
|
.remove(&id)
|
||||||
.with_context(|| format!("no account with id {id}"))?;
|
.with_context(|| format!("no account with id {id}"))?;
|
||||||
ctx.stop_io().await;
|
ctx.stop_io().await;
|
||||||
|
ctx.sql.close().await;
|
||||||
drop(ctx);
|
drop(ctx);
|
||||||
|
|
||||||
if let Some(cfg) = self.config.get_account(id) {
|
if let Some(cfg) = self.config.get_account(id) {
|
||||||
let account_path = self.dir.join(cfg.dir);
|
let account_path = self.dir.join(cfg.dir);
|
||||||
|
|
||||||
try_many_times(|| fs::remove_dir_all(&account_path))
|
fs::remove_dir_all(&account_path)
|
||||||
.await
|
.await
|
||||||
.context("failed to remove account data")?;
|
.context("failed to remove account data")?;
|
||||||
}
|
}
|
||||||
@@ -207,10 +207,10 @@ impl Accounts {
|
|||||||
fs::create_dir_all(self.dir.join(&account_config.dir))
|
fs::create_dir_all(self.dir.join(&account_config.dir))
|
||||||
.await
|
.await
|
||||||
.context("failed to create dir")?;
|
.context("failed to create dir")?;
|
||||||
try_many_times(|| fs::rename(&dbfile, &new_dbfile))
|
fs::rename(&dbfile, &new_dbfile)
|
||||||
.await
|
.await
|
||||||
.context("failed to rename dbfile")?;
|
.context("failed to rename dbfile")?;
|
||||||
try_many_times(|| fs::rename(&blobdir, &new_blobdir))
|
fs::rename(&blobdir, &new_blobdir)
|
||||||
.await
|
.await
|
||||||
.context("failed to rename blobdir")?;
|
.context("failed to rename blobdir")?;
|
||||||
if walfile.exists() {
|
if walfile.exists() {
|
||||||
@@ -235,7 +235,7 @@ impl Accounts {
|
|||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
let account_path = std::path::PathBuf::from(&account_config.dir);
|
let account_path = std::path::PathBuf::from(&account_config.dir);
|
||||||
try_many_times(|| fs::remove_dir_all(&account_path))
|
fs::remove_dir_all(&account_path)
|
||||||
.await
|
.await
|
||||||
.context("failed to remove account data")?;
|
.context("failed to remove account data")?;
|
||||||
self.config.remove_account(account_config.id).await?;
|
self.config.remove_account(account_config.id).await?;
|
||||||
@@ -620,37 +620,6 @@ impl Config {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Spend up to 1 minute trying to do the operation.
|
|
||||||
///
|
|
||||||
/// Even if Delta Chat itself does not hold the file lock,
|
|
||||||
/// there may be other processes such as antivirus,
|
|
||||||
/// or the filesystem may be network-mounted.
|
|
||||||
///
|
|
||||||
/// Without this workaround removing account may fail on Windows with an error
|
|
||||||
/// "The process cannot access the file because it is being used by another process. (os error 32)".
|
|
||||||
async fn try_many_times<F, Fut, T>(f: F) -> std::result::Result<(), T>
|
|
||||||
where
|
|
||||||
F: Fn() -> Fut,
|
|
||||||
Fut: Future<Output = std::result::Result<(), T>>,
|
|
||||||
{
|
|
||||||
let mut counter = 0;
|
|
||||||
loop {
|
|
||||||
counter += 1;
|
|
||||||
|
|
||||||
if let Err(err) = f().await {
|
|
||||||
if counter > 60 {
|
|
||||||
return Err(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Wait 1 second and try again.
|
|
||||||
tokio::time::sleep(std::time::Duration::from_millis(1000)).await;
|
|
||||||
} else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Configuration of a single account.
|
/// Configuration of a single account.
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
|
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
|
||||||
struct AccountConfig {
|
struct AccountConfig {
|
||||||
|
|||||||
@@ -126,7 +126,7 @@ impl Sql {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Closes all underlying Sqlite connections.
|
/// Closes all underlying Sqlite connections.
|
||||||
async fn close(&self) {
|
pub(crate) async fn close(&self) {
|
||||||
let _ = self.pool.write().await.take();
|
let _ = self.pool.write().await.take();
|
||||||
// drop closes the connection
|
// drop closes the connection
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user