changeset 5947:7ebe0593f488 HEAD

Don't create cache file until something is actually being added to it.
author Timo Sirainen <tss@iki.fi>
date Thu, 12 Jul 2007 02:33:49 +0300
parents 19bc2a4b3669
children a0b3228be6d2
files src/lib-index/mail-cache-compress.c src/lib-index/mail-cache-fields.c src/lib-index/mail-cache-private.h src/lib-index/mail-cache-sync-update.c src/lib-index/mail-cache-transaction.c src/lib-index/mail-cache.c
diffstat 6 files changed, 39 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- 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 @@
 		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:
--- 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 @@
 	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);
--- 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 @@
 
 /* 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);
 
--- 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 @@
 		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;
 		}
--- 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 @@
 	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 @@
 {
 	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 @@
 			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 @@
 	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) {
--- 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 @@
 	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 @@
 			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 @@
 		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 @@
 	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 *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 @@
 		(void)file_dotlock_delete(&cache->dotlock);
 }
 
-int mail_cache_lock(struct mail_cache *cache)
+int mail_cache_lock(struct mail_cache *cache, bool require_same_reset_id)
 {
-	struct mail_index_view *view;
 	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 @@
 	    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) {
+	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 @@
 			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 @@
 		}
 	}
 
-	mail_index_view_close(&view);
 	i_assert((ret <= 0 && !cache->locked) || (ret > 0 && cache->locked));
 	return ret;
 }