dovecot: Don't create cache file until something is actually bei...

dovecot at dovecot.org dovecot at dovecot.org
Thu Jul 12 03:21:47 EEST 2007


details:   http://hg.dovecot.org/dovecot/rev/7ebe0593f488
changeset: 5947:7ebe0593f488
user:      Timo Sirainen <tss at iki.fi>
date:      Thu Jul 12 02:33:49 2007 +0300
description:
Don't create cache file until something is actually being added to it.

diffstat:

6 files changed, 43 insertions(+), 29 deletions(-)
src/lib-index/mail-cache-compress.c    |    2 -
src/lib-index/mail-cache-fields.c      |    2 -
src/lib-index/mail-cache-private.h     |    2 -
src/lib-index/mail-cache-sync-update.c |    2 -
src/lib-index/mail-cache-transaction.c |   18 ++++++++++--
src/lib-index/mail-cache.c             |   46 ++++++++++++++++----------------

diffs (213 lines):

diff -r 19bc2a4b3669 -r 7ebe0593f488 src/lib-index/mail-cache-compress.c
--- a/src/lib-index/mail-cache-compress.c	Thu Jul 12 02:32:44 2007 +0300
+++ b/src/lib-index/mail-cache-compress.c	Thu Jul 12 02:33:49 2007 +0300
@@ -387,7 +387,7 @@ int mail_cache_compress(struct mail_cach
 		return mail_cache_compress_locked(cache, trans, &unlock);
 	}
 
-	switch (mail_cache_lock(cache)) {
+	switch (mail_cache_lock(cache, FALSE)) {
 	case -1:
 		return -1;
 	case 0:
diff -r 19bc2a4b3669 -r 7ebe0593f488 src/lib-index/mail-cache-fields.c
--- a/src/lib-index/mail-cache-fields.c	Thu Jul 12 02:32:44 2007 +0300
+++ b/src/lib-index/mail-cache-fields.c	Thu Jul 12 02:33:49 2007 +0300
@@ -414,7 +414,7 @@ int mail_cache_header_fields_update(stru
 	if (cache->locked)
 		return mail_cache_header_fields_update_locked(cache);
 
-	if (mail_cache_lock(cache) <= 0)
+	if (mail_cache_lock(cache, NULL) <= 0)
 		return -1;
 
 	ret = mail_cache_header_fields_update_locked(cache);
diff -r 19bc2a4b3669 -r 7ebe0593f488 src/lib-index/mail-cache-private.h
--- a/src/lib-index/mail-cache-private.h	Thu Jul 12 02:32:44 2007 +0300
+++ b/src/lib-index/mail-cache-private.h	Thu Jul 12 02:33:49 2007 +0300
@@ -196,7 +196,7 @@ int mail_cache_open_and_verify(struct ma
 
 /* Explicitly lock the cache file. Returns -1 if error, 1 if ok, 0 if we
    couldn't lock */
-int mail_cache_lock(struct mail_cache *cache);
+int mail_cache_lock(struct mail_cache *cache, bool require_same_reset_id);
 /* Returns -1 if cache is / just got corrupted, 0 if ok. */
 int mail_cache_unlock(struct mail_cache *cache);
 
diff -r 19bc2a4b3669 -r 7ebe0593f488 src/lib-index/mail-cache-sync-update.c
--- a/src/lib-index/mail-cache-sync-update.c	Thu Jul 12 02:32:44 2007 +0300
+++ b/src/lib-index/mail-cache-sync-update.c	Thu Jul 12 02:33:49 2007 +0300
@@ -37,7 +37,7 @@ static int mail_cache_handler_init(struc
 		return 0;
 
 	if (!ctx->locked) {
-		if ((ret = mail_cache_lock(cache)) <= 0) {
+		if ((ret = mail_cache_lock(cache, TRUE)) <= 0) {
                         ctx->lock_failed = TRUE;
 			return ret;
 		}
diff -r 19bc2a4b3669 -r 7ebe0593f488 src/lib-index/mail-cache-transaction.c
--- a/src/lib-index/mail-cache-transaction.c	Thu Jul 12 02:32:44 2007 +0300
+++ b/src/lib-index/mail-cache-transaction.c	Thu Jul 12 02:33:49 2007 +0300
@@ -36,6 +36,7 @@ struct mail_cache_transaction_ctx {
 	uint32_t reserved_space_offset, reserved_space;
 	uint32_t last_grow_size;
 
+	unsigned int tried_compression:1;
 	unsigned int changes:1;
 };
 
@@ -69,6 +70,8 @@ static void mail_cache_transaction_reset
 {
 	ctx->cache_file_seq = MAIL_CACHE_IS_UNUSABLE(ctx->cache) ? 0 :
 		ctx->cache->hdr->file_seq;
+	mail_index_ext_set_reset_id(ctx->trans, ctx->cache->ext_id,
+				    ctx->cache_file_seq);
 
 	if (ctx->cache_data)
 		buffer_set_used_size(ctx->cache_data, 0);
@@ -109,7 +112,7 @@ static int mail_cache_transaction_lock(s
 			ctx->cache_file_seq = ctx->cache->hdr->file_seq;
 	}
 
-	if ((ret = mail_cache_lock(ctx->cache)) <= 0)
+	if ((ret = mail_cache_lock(ctx->cache, FALSE)) <= 0)
 		return ret;
 
 	if (ctx->cache_file_seq != ctx->cache->hdr->file_seq)
@@ -669,8 +672,17 @@ static int mail_cache_header_add_field(s
 	uint32_t offset, hdr_offset;
 	int ret = 0;
 
-	if (mail_cache_transaction_lock(ctx) <= 0)
-		return -1;
+	if ((ret = mail_cache_transaction_lock(ctx)) <= 0) {
+		/* create the cache file if it doesn't exist yet */
+		if (ctx->tried_compression)
+			return -1;
+		ctx->tried_compression = TRUE;
+
+		if (mail_cache_compress(cache, ctx->trans) < 0)
+			return -1;
+		if ((ret = mail_cache_transaction_lock(ctx)) <= 0)
+			return ret;
+	}
 
 	/* re-read header to make sure we don't lose any fields. */
 	if (mail_cache_header_fields_read(cache) < 0) {
diff -r 19bc2a4b3669 -r 7ebe0593f488 src/lib-index/mail-cache.c
--- a/src/lib-index/mail-cache.c	Thu Jul 12 02:32:44 2007 +0300
+++ b/src/lib-index/mail-cache.c	Thu Jul 12 02:33:49 2007 +0300
@@ -104,7 +104,7 @@ int mail_cache_reopen(struct mail_cache 
 	cache->fd = nfs_safe_open(cache->filepath, O_RDWR);
 	if (cache->fd == -1) {
 		if (errno == ENOENT)
-			cache->need_compress_file_seq = (uint32_t)-1;
+			cache->need_compress_file_seq = 0;
 		else
 			mail_cache_set_syscall_error(cache, "open()");
 		return -1;
@@ -220,7 +220,7 @@ int mail_cache_map(struct mail_cache *ca
 			cache->need_compress_file_seq =
 				!MAIL_CACHE_IS_UNUSABLE(cache) &&
 				cache->hdr->file_seq != 0 ?
-				cache->hdr->file_seq : (uint32_t)-1;
+				cache->hdr->file_seq : 0;
 			return -1;
 		}
 		return 0;
@@ -263,7 +263,7 @@ int mail_cache_map(struct mail_cache *ca
 		cache->need_compress_file_seq =
 			!MAIL_CACHE_IS_UNUSABLE(cache) &&
 			cache->hdr->file_seq != 0 ?
-			cache->hdr->file_seq : (uint32_t)-1;
+			cache->hdr->file_seq : 0;
 		return -1;
 	}
 
@@ -280,7 +280,7 @@ static int mail_cache_try_open(struct ma
 	cache->fd = nfs_safe_open(cache->filepath, O_RDWR);
 	if (cache->fd == -1) {
 		if (errno == ENOENT) {
-			cache->need_compress_file_seq = (uint32_t)-1;
+			cache->need_compress_file_seq = 0;
 			return 0;
 		}
 
@@ -366,8 +366,8 @@ struct mail_cache *mail_cache_create(str
 	struct mail_cache *cache;
 
 	cache = mail_cache_alloc(index);
-	cache->opened = TRUE;
-	cache->need_compress_file_seq = (uint32_t)-1;
+	if (unlink(cache->filepath) < 0 && errno != ENOENT)
+		mail_cache_set_syscall_error(cache, "unlink()");
 	return cache;
 }
 
@@ -420,10 +420,11 @@ static void mail_cache_unlock_file(struc
 		(void)file_dotlock_delete(&cache->dotlock);
 }
 
-int mail_cache_lock(struct mail_cache *cache)
-{
-	struct mail_index_view *view;
+int mail_cache_lock(struct mail_cache *cache, bool require_same_reset_id)
+{
 	const struct mail_index_ext *ext;
+	struct mail_index_view *iview;
+	uint32_t reset_id;
 	int i, ret;
 
 	i_assert(!cache->locked);
@@ -435,20 +436,21 @@ int mail_cache_lock(struct mail_cache *c
 	    MAIL_INDEX_IS_IN_MEMORY(cache->index))
 		return 0;
 
-	view = mail_index_view_open(cache->index);
-	ext = mail_index_view_get_ext(view, cache->ext_id);
-	if (ext == NULL) {
+	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 (ext == NULL && require_same_reset_id) {
 		/* cache not used */
-		mail_index_view_close(&view);
-		return 0;
-	}
-
-	if (cache->hdr->file_seq != ext->reset_id) {
+		return 0;
+	}
+
+	if (cache->hdr->file_seq != reset_id) {
 		/* we want the latest cache file */
-		if ((ret = mail_cache_reopen(cache)) <= 0) {
-			mail_index_view_close(&view);
+		ret = mail_cache_reopen(cache);
+		if (ret < 0 || (ret == 0 && require_same_reset_id))
 			return ret;
-		}
 	}
 
 	for (i = 0; i < 3; i++) {
@@ -457,7 +459,8 @@ int mail_cache_lock(struct mail_cache *c
 			break;
 		cache->locked = TRUE;
 
-		if (cache->hdr->file_seq == ext->reset_id) {
+		if (cache->hdr->file_seq == reset_id ||
+		    !require_same_reset_id) {
 			/* got it */
 			break;
 		}
@@ -483,7 +486,6 @@ int mail_cache_lock(struct mail_cache *c
 		}
 	}
 
-	mail_index_view_close(&view);
 	i_assert((ret <= 0 && !cache->locked) || (ret > 0 && cache->locked));
 	return ret;
 }


More information about the dovecot-cvs mailing list