]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-index: mail_cache_open_and_verify() - Separate error and corruption handling
authorTimo Sirainen <timo.sirainen@open-xchange.com>
Mon, 30 Mar 2020 12:25:49 +0000 (15:25 +0300)
committerTimo Sirainen <timo.sirainen@open-xchange.com>
Wed, 15 Apr 2020 09:41:42 +0000 (12:41 +0300)
src/lib-index/mail-cache.c
src/lib-index/mail-cache.h

index 98cdc19c742d897f9d5270845d90632a8e683aa2..9616dd21f3f69f1319148e686de43821478758e8 100644 (file)
@@ -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;
index 7e6338e14cfb283c25992b92199136955936f840..59ca0c26c7e6a96a5689654b523366b4c75007b5 100644 (file)
@@ -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 *