# HG changeset patch # User Timo Sirainen # Date 1193574673 -7200 # Node ID f3ca0b9e3628f5b9e2b60c6c856523f9b9eb62c0 # Parent ce7f9d33d566c251bcd3a64bba83213559ad9824 Fixed a race condition causing cache corruption when creating/compressing cache file. diff -r ce7f9d33d566 -r f3ca0b9e3628 src/lib-index/mail-cache-transaction.c --- a/src/lib-index/mail-cache-transaction.c Sun Oct 28 13:27:46 2007 +0200 +++ b/src/lib-index/mail-cache-transaction.c Sun Oct 28 14:31:13 2007 +0200 @@ -74,7 +74,7 @@ mail_index_ext_set_reset_id(ctx->trans, ctx->cache->ext_id, ctx->cache_file_seq); - if (ctx->cache_data) + if (ctx->cache_data != NULL) buffer_set_used_size(ctx->cache_data, 0); if (array_is_created(&ctx->cache_data_seq)) array_clear(&ctx->cache_data_seq); @@ -116,8 +116,11 @@ if (ctx->cache_file_seq == 0) { if (!ctx->cache->opened) (void)mail_cache_open_and_verify(ctx->cache); - if (!MAIL_CACHE_IS_UNUSABLE(ctx->cache)) + if (!MAIL_CACHE_IS_UNUSABLE(ctx->cache)) { + i_assert(ctx->cache_data == NULL || + ctx->cache_data->used == 0); ctx->cache_file_seq = ctx->cache->hdr->file_seq; + } } if ((ret = mail_cache_lock(ctx->cache, FALSE)) <= 0) @@ -507,15 +510,6 @@ buffer_set_used_size(ctx->cache_data, ctx->prev_pos); } - if (ctx->cache_file_seq == 0) { - if (!ctx->cache->opened) - (void)mail_cache_open_and_verify(ctx->cache); - if (MAIL_CACHE_IS_UNUSABLE(ctx->cache)) - return -1; - - ctx->cache_file_seq = ctx->cache->hdr->file_seq; - } - if (ctx->cache_file_seq != ctx->cache->hdr->file_seq) { /* cache file reopened - need to abort */ mail_cache_transaction_reset(ctx); @@ -788,6 +782,15 @@ (MAIL_CACHE_DECISION_NO | MAIL_CACHE_DECISION_FORCED)) return; + if (ctx->cache_file_seq == 0) { + if (!ctx->cache->opened) + (void)mail_cache_open_and_verify(ctx->cache); + if (MAIL_CACHE_IS_UNUSABLE(ctx->cache)) + return; + + ctx->cache_file_seq = ctx->cache->hdr->file_seq; + } + file_field = ctx->cache->field_file_map[field_idx]; if (file_field == (uint32_t)-1) { /* we'll have to add this field to headers */