From: Timo Sirainen Date: Mon, 30 Mar 2020 12:45:57 +0000 (+0300) Subject: lib-index: mail_cache_sync_reset_id() - Rewrite to simplify X-Git-Tag: 2.3.11.2~399 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2d98c7f343bad6a1d544cac247fdde108a3795df;p=thirdparty%2Fdovecot%2Fcore.git lib-index: mail_cache_sync_reset_id() - Rewrite to simplify This doesn't change any functionality. --- diff --git a/src/lib-index/mail-cache.c b/src/lib-index/mail-cache.c index 25759488cd..76a456a854 100644 --- a/src/lib-index/mail-cache.c +++ b/src/lib-index/mail-cache.c @@ -662,30 +662,40 @@ static void mail_cache_unlock_file(struct mail_cache *cache) file_unlock(&cache->file_lock); } -static int mail_cache_sync_reset_id(struct mail_cache *cache) +static bool mail_cache_verify_reset_id(struct mail_cache *cache) { const struct mail_index_ext *ext; struct mail_index_view *iview; uint32_t reset_id; - int i; - /* now verify that the index reset_id matches the cache's file_seq */ - for (i = 0; ; i++) { - iview = mail_index_view_open(cache->index); - ext = mail_index_view_get_ext(iview, cache->ext_id); - reset_id = ext == NULL ? 0 : ext->reset_id; - mail_index_view_close(&iview); + iview = mail_index_view_open(cache->index); + ext = mail_index_view_get_ext(iview, cache->ext_id); + reset_id = ext == NULL ? 0 : ext->reset_id; + mail_index_view_close(&iview); - if (cache->hdr->file_seq == reset_id) - break; - /* mismatch. try refreshing index once. if that doesn't help, - we can't use the cache. */ - if (i > 0 || cache->index->mapping) - return 0; - if (mail_index_refresh(cache->index) < 0) - return -1; + return cache->hdr->file_seq == reset_id; +} + +static int mail_cache_sync_reset_id(struct mail_cache *cache) +{ + /* verify that the index reset_id matches the cache's file_seq */ + if (mail_cache_verify_reset_id(cache)) + return 1; + + /* Mismatch. See if we can get it synced. */ + if (cache->index->mapping) { + /* Syncing is already locked, and we're in the middle of + mapping the index. The cache is unusable. */ + i_assert(cache->index->log_sync_locked); + return 0; } - return 1; + + /* See if reset_id changes after refreshing the index. */ + if (mail_index_refresh(cache->index) < 0) + return -1; + if (mail_cache_verify_reset_id(cache)) + return 1; + return 0; } static int