From 2224a7100f9bc6c44bc66117a88556003e74186e Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Thu, 1 Dec 2022 21:06:33 +0200 Subject: [PATCH] melib/imap: reset imap cache on init error --- melib/src/backends/imap.rs | 18 +++++++++++++++--- melib/src/backends/imap/cache.rs | 22 ++++++++++++++++++++-- 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>>; } +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> { @@ -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> {