From: Timo Sirainen Date: Sun, 4 Oct 2015 18:49:08 +0000 (+0300) Subject: lib-index: When writing new index, rotate the log file first before writing it. X-Git-Tag: 2.2.20.rc1~170 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=44d77d20cbe158306fcc2d4b49aa883b8f8dbc83;p=thirdparty%2Fdovecot%2Fcore.git lib-index: When writing new index, rotate the log file first before writing it. This way the index contains the new log's seq+offset instead of having to recreate the index almost immediately afterwards. --- diff --git a/src/lib-index/mail-index-write.c b/src/lib-index/mail-index-write.c index e8eb7c06ed..dcf146b2ac 100644 --- a/src/lib-index/mail-index-write.c +++ b/src/lib-index/mail-index-write.c @@ -118,13 +118,29 @@ static int mail_index_recreate(struct mail_index *index) void mail_index_write(struct mail_index *index, bool want_rotate) { struct mail_index_map *map = index->map; - const struct mail_index_header *hdr = &map->hdr; + struct mail_index_header *hdr = &map->hdr; i_assert(index->log_sync_locked); if (index->readonly) return; + /* rotate the .log before writing index, so the index will point to + the latest log. */ + if (want_rotate && + hdr->log_file_seq == index->log->head->hdr.file_seq && + hdr->log_file_tail_offset == hdr->log_file_head_offset) { + if (mail_transaction_log_rotate(index->log, FALSE) == 0) { + struct mail_transaction_log_file *file = + index->log->head; + i_assert(file->hdr.prev_file_seq == hdr->log_file_seq); + i_assert(file->hdr.prev_file_offset == hdr->log_file_head_offset); + hdr->log_file_seq = file->hdr.file_seq; + hdr->log_file_head_offset = + hdr->log_file_tail_offset = file->hdr.hdr_size; + } + } + if (!MAIL_INDEX_IS_IN_MEMORY(index)) { if (mail_index_recreate(index) < 0) { (void)mail_index_move_to_memory(index); @@ -135,9 +151,4 @@ void mail_index_write(struct mail_index *index, bool want_rotate) index->last_read_log_file_seq = hdr->log_file_seq; index->last_read_log_file_head_offset = hdr->log_file_head_offset; index->last_read_log_file_tail_offset = hdr->log_file_tail_offset; - - if (want_rotate && - hdr->log_file_seq == index->log->head->hdr.file_seq && - hdr->log_file_tail_offset == hdr->log_file_head_offset) - (void)mail_transaction_log_rotate(index->log, FALSE); }