]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-index: Code cleanup - avoid code duplication.
authorTimo Sirainen <tss@iki.fi>
Mon, 6 Oct 2014 23:54:35 +0000 (02:54 +0300)
committerTimo Sirainen <tss@iki.fi>
Mon, 6 Oct 2014 23:54:35 +0000 (02:54 +0300)
src/lib-index/mail-cache.c

index 1cf77d8d1e5953739bbede5195a0b27de51584a8..6cb4bd9a35787ef9facf159ab0bbebd4949480c3 100644 (file)
@@ -95,6 +95,34 @@ static void mail_cache_init_file_cache(struct mail_cache *cache)
        cache->st_dev = st.st_dev;
 }
 
+static int mail_cache_try_open(struct mail_cache *cache)
+{
+       const void *data;
+
+       cache->opened = TRUE;
+
+       if (MAIL_INDEX_IS_IN_MEMORY(cache->index))
+               return 0;
+
+       cache->fd = nfs_safe_open(cache->filepath,
+                                 cache->index->readonly ? O_RDONLY : O_RDWR);
+       if (cache->fd == -1) {
+               if (errno == ENOENT) {
+                       cache->need_compress_file_seq = 0;
+                       return 0;
+               }
+
+               mail_cache_set_syscall_error(cache, "open()");
+               return -1;
+       }
+
+       mail_cache_init_file_cache(cache);
+
+       if (mail_cache_map(cache, 0, 0, &data) < 0)
+               return -1;
+       return 1;
+}
+
 static bool mail_cache_need_reopen(struct mail_cache *cache)
 {
        struct stat st;
@@ -143,34 +171,14 @@ static bool mail_cache_need_reopen(struct mail_cache *cache)
        return FALSE;
 }
 
-int mail_cache_reopen(struct mail_cache *cache)
+static int mail_cache_reopen_now(struct mail_cache *cache)
 {
        struct mail_index_view *view;
        const struct mail_index_ext *ext;
-       const void *data;
-
-       i_assert(!cache->locked);
-
-       if (!mail_cache_need_reopen(cache)) {
-               /* reopening does no good */
-               return 0;
-       }
 
        mail_cache_file_close(cache);
 
-       cache->fd = nfs_safe_open(cache->filepath,
-                                 cache->index->readonly ? O_RDONLY : O_RDWR);
-       if (cache->fd == -1) {
-               if (errno == ENOENT)
-                       cache->need_compress_file_seq = 0;
-               else
-                       mail_cache_set_syscall_error(cache, "open()");
-               return -1;
-       }
-
-       mail_cache_init_file_cache(cache);
-
-       if (mail_cache_map(cache, 0, 0, &data) < 0)
+       if (mail_cache_try_open(cache) <= 0)
                return -1;
 
        if (mail_cache_header_fields_read(cache) < 0)
@@ -193,6 +201,17 @@ int mail_cache_reopen(struct mail_cache *cache)
        return 1;
 }
 
+int mail_cache_reopen(struct mail_cache *cache)
+{
+       i_assert(!cache->locked);
+
+       if (!mail_cache_need_reopen(cache)) {
+               /* reopening does no good */
+               return 0;
+       }
+       return mail_cache_reopen_now(cache);
+}
+
 static void mail_cache_update_need_compress(struct mail_cache *cache)
 {
        const struct mail_cache_header *hdr = cache->hdr;
@@ -460,34 +479,6 @@ int mail_cache_map(struct mail_cache *cache, size_t offset, size_t size,
                                     cache->mmap_base, FALSE);
 }
 
-static int mail_cache_try_open(struct mail_cache *cache)
-{
-       const void *data;
-
-       cache->opened = TRUE;
-
-       if (MAIL_INDEX_IS_IN_MEMORY(cache->index))
-               return 0;
-
-       cache->fd = nfs_safe_open(cache->filepath,
-                                 cache->index->readonly ? O_RDONLY : O_RDWR);
-       if (cache->fd == -1) {
-               if (errno == ENOENT) {
-                       cache->need_compress_file_seq = 0;
-                       return 0;
-               }
-
-               mail_cache_set_syscall_error(cache, "open()");
-               return -1;
-       }
-
-       mail_cache_init_file_cache(cache);
-
-       if (mail_cache_map(cache, 0, 0, &data) < 0)
-               return -1;
-       return 1;
-}
-
 int mail_cache_open_and_verify(struct mail_cache *cache)
 {
        int ret;