From: Timo Sirainen Date: Mon, 30 Mar 2020 12:25:49 +0000 (+0300) Subject: lib-index: mail_cache_open_and_verify() - Separate error and corruption handling X-Git-Tag: 2.3.11.2~403 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=42ea566e4a3c087f2290ef5c2e951da9593af583;p=thirdparty%2Fdovecot%2Fcore.git lib-index: mail_cache_open_and_verify() - Separate error and corruption handling --- diff --git a/src/lib-index/mail-cache.c b/src/lib-index/mail-cache.c index 98cdc19c74..9616dd21f3 100644 --- a/src/lib-index/mail-cache.c +++ b/src/lib-index/mail-cache.c @@ -124,6 +124,8 @@ static void mail_cache_init_file_cache(struct mail_cache *cache) static int mail_cache_try_open(struct mail_cache *cache) { + int ret; + i_assert(!cache->opened); cache->opened = TRUE; @@ -146,9 +148,9 @@ static int mail_cache_try_open(struct mail_cache *cache) mail_cache_init_file_cache(cache); - if (mail_cache_map_all(cache) <= 0) { + if ((ret = mail_cache_map_all(cache)) <= 0) { mail_cache_file_close(cache); - return -1; + return ret; } return 1; } @@ -553,14 +555,25 @@ int mail_cache_open_and_verify(struct mail_cache *cache) { int ret; - if (cache->opened) - return 0; - ret = mail_cache_try_open(cache); - if (ret > 0) - ret = mail_cache_header_fields_read(cache); - if (ret < 0) { - /* failed for some reason - doesn't really matter, - it's disabled for now. */ + if (cache->opened) { + if (!MAIL_CACHE_IS_UNUSABLE(cache)) + return 1; + mail_cache_file_close(cache); + } + if ((ret = mail_cache_try_open(cache)) < 0) { + /* I/O error */ + mail_cache_file_close(cache); + return -1; + } + + if (ret > 0) { + if (mail_cache_header_fields_read(cache) < 0) { + /* corrupted */ + ret = 0; + } + } + if (ret == 0) { + /* cache was corrupted and should have been deleted already. */ mail_cache_file_close(cache); } return ret; diff --git a/src/lib-index/mail-cache.h b/src/lib-index/mail-cache.h index 7e6338e14c..59ca0c26c7 100644 --- a/src/lib-index/mail-cache.h +++ b/src/lib-index/mail-cache.h @@ -80,7 +80,8 @@ int mail_cache_compress_with_trans(struct mail_cache *cache, int mail_cache_compress(struct mail_cache *cache, uint32_t compress_file_seq); /* Returns TRUE if there is at least something in the cache. */ bool mail_cache_exists(struct mail_cache *cache); -/* Open and read cache header. Returns 0 if ok, -1 if error/corrupted. */ +/* Open and read cache header. Returns 1 if ok, 0 if cache doesn't exist or it + was corrupted and just got deleted, -1 if I/O error. */ int mail_cache_open_and_verify(struct mail_cache *cache); struct mail_cache_view *