summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorManos Pitsidianakis <el13635@mail.ntua.gr>2022-12-01 21:06:33 +0200
committerManos Pitsidianakis <el13635@mail.ntua.gr>2022-12-01 21:06:33 +0200
commit2224a7100f9bc6c44bc66117a88556003e74186e (patch)
tree8a8b92094c38947b08b611e99fe4a1756214d9c6
parent7924aa8bfe8f0fbcd557bb8bb3a9d3ebeab2220a (diff)
downloadmeli-2224a7100f9bc6c44bc66117a88556003e74186e.zip
melib/imap: reset imap cache on init error
-rw-r--r--melib/src/backends/imap.rs18
-rw-r--r--melib/src/backends/imap/cache.rs22
2 files changed, 35 insertions, 5 deletions
diff --git a/melib/src/backends/imap.rs b/melib/src/backends/imap.rs
index 0cb1550f..88be46dd 100644
--- a/melib/src/backends/imap.rs
+++ b/melib/src/backends/imap.rs
@@ -33,7 +33,7 @@ pub use connection::*;
mod watch;
pub use watch::*;
mod cache;
-use cache::ModSequence;
+use cache::{ImapCacheReset, ModSequence};
pub mod managesieve;
mod untagged;
@@ -301,14 +301,26 @@ impl MailBackend for ImapType {
if self.uid_store.keep_offline_cache {
match cache::Sqlite3Cache::get(self.uid_store.clone()).chain_err_summary(|| {
format!(
- "Could not initialize cache for IMAP account {}",
+ "Could not initialize cache for IMAP account {}. Resetting database.",
self.uid_store.account_name
)
}) {
Ok(v) => Some(v),
Err(err) => {
(self.uid_store.event_consumer)(self.uid_store.account_hash, err.into());
- None
+ match cache::Sqlite3Cache::reset_db(&self.uid_store)
+ .and_then(|()| cache::Sqlite3Cache::get(self.uid_store.clone()))
+ .chain_err_summary(|| "Could not reset IMAP cache database.")
+ {
+ Ok(v) => Some(v),
+ Err(err) => {
+ (self.uid_store.event_consumer)(
+ self.uid_store.account_hash,
+ err.into(),
+ );
+ None
+ }
+ }
}
}
} else {
diff --git a/melib/src/backends/imap/cache.rs b/melib/src/backends/imap/cache.rs
index 7192d8df..5ddb2b68 100644
--- a/melib/src/backends/imap/cache.rs
+++ b/melib/src/backends/imap/cache.rs
@@ -93,6 +93,12 @@ pub trait ImapCache: Send + core::fmt::Debug {
) -> Result<Option<Vec<u8>>>;
}
+pub trait ImapCacheReset: Send + core::fmt::Debug {
+ fn reset_db(uid_store: &UIDStore) -> Result<()>
+ where
+ Self: Sized;
+}
+
#[cfg(feature = "sqlite3")]
pub use sqlite3_m::*;
@@ -185,9 +191,15 @@ mod sqlite3_m {
}
}
+ impl ImapCacheReset for Sqlite3Cache {
+ fn reset_db(uid_store: &UIDStore) -> Result<()> {
+ sqlite3::reset_db(&DB_DESCRIPTION, Some(uid_store.account_name.as_str()))
+ }
+ }
+
impl ImapCache for Sqlite3Cache {
fn reset(&mut self) -> Result<()> {
- sqlite3::reset_db(&DB_DESCRIPTION, Some(self.uid_store.account_name.as_str()))
+ Sqlite3Cache::reset_db(&self.uid_store)
}
fn mailbox_state(&mut self, mailbox_hash: MailboxHash) -> Result<Option<()>> {
@@ -687,9 +699,15 @@ mod default_m {
}
}
+ impl ImapCacheReset for DefaultCache {
+ fn reset_db(uid_store: &UIDStore) -> Result<()> {
+ Err(MeliError::new("melib is not built with any imap cache").set_kind(ErrorKind::Bug))
+ }
+ }
+
impl ImapCache for DefaultCache {
fn reset(&mut self) -> Result<()> {
- Err(MeliError::new("melib is not built with any imap cache").set_kind(ErrorKind::Bug))
+ DefaultCache::reset_db(&self.uid_store)
}
fn mailbox_state(&mut self, _mailbox_hash: MailboxHash) -> Result<Option<()>> {