melib/imap: reset imap cache on init error

feature/readline-command-parsing
Manos Pitsidianakis 2 years ago
parent 7924aa8bfe
commit 2224a7100f

@ -33,7 +33,7 @@ pub use connection::*;
mod watch; mod watch;
pub use watch::*; pub use watch::*;
mod cache; mod cache;
use cache::ModSequence; use cache::{ImapCacheReset, ModSequence};
pub mod managesieve; pub mod managesieve;
mod untagged; mod untagged;
@ -301,14 +301,26 @@ impl MailBackend for ImapType {
if self.uid_store.keep_offline_cache { if self.uid_store.keep_offline_cache {
match cache::Sqlite3Cache::get(self.uid_store.clone()).chain_err_summary(|| { match cache::Sqlite3Cache::get(self.uid_store.clone()).chain_err_summary(|| {
format!( format!(
"Could not initialize cache for IMAP account {}", "Could not initialize cache for IMAP account {}. Resetting database.",
self.uid_store.account_name self.uid_store.account_name
) )
}) { }) {
Ok(v) => Some(v), Ok(v) => Some(v),
Err(err) => { Err(err) => {
(self.uid_store.event_consumer)(self.uid_store.account_hash, err.into()); (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 { } else {

@ -93,6 +93,12 @@ pub trait ImapCache: Send + core::fmt::Debug {
) -> Result<Option<Vec<u8>>>; ) -> Result<Option<Vec<u8>>>;
} }
pub trait ImapCacheReset: Send + core::fmt::Debug {
fn reset_db(uid_store: &UIDStore) -> Result<()>
where
Self: Sized;
}
#[cfg(feature = "sqlite3")] #[cfg(feature = "sqlite3")]
pub use sqlite3_m::*; 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 { impl ImapCache for Sqlite3Cache {
fn reset(&mut self) -> Result<()> { 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<()>> { 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 { impl ImapCache for DefaultCache {
fn reset(&mut self) -> Result<()> { 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<()>> { fn mailbox_state(&mut self, _mailbox_hash: MailboxHash) -> Result<Option<()>> {

Loading…
Cancel
Save