dovecot-2.2: lib-index: Code cleanup - avoid code duplication.

dovecot at dovecot.org dovecot at dovecot.org
Mon Oct 6 23:55:07 UTC 2014


details:   http://hg.dovecot.org/dovecot-2.2/rev/a10eea380073
changeset: 17901:a10eea380073
user:      Timo Sirainen <tss at iki.fi>
date:      Tue Oct 07 02:54:35 2014 +0300
description:
lib-index: Code cleanup - avoid code duplication.

diffstat:

 src/lib-index/mail-cache.c |  91 ++++++++++++++++++++-------------------------
 1 files changed, 41 insertions(+), 50 deletions(-)

diffs (128 lines):

diff -r 6aeb2a7e5303 -r a10eea380073 src/lib-index/mail-cache.c
--- a/src/lib-index/mail-cache.c	Tue Oct 07 02:35:41 2014 +0300
+++ b/src/lib-index/mail-cache.c	Tue Oct 07 02:54:35 2014 +0300
@@ -95,6 +95,34 @@
 	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 @@
 	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 @@
 	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 @@
 				     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;


More information about the dovecot-cvs mailing list