changeset 8060:4bfe7618743e HEAD

Index opening: If there's broken index and no .log, don't crash.
author Timo Sirainen <tss@iki.fi>
date Mon, 04 Aug 2008 01:41:39 +0300
parents 25ff4c990c23
children 7f1389badb4c
files src/lib-index/mail-index-fsck.c
diffstat 1 files changed, 30 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-index/mail-index-fsck.c	Thu Jul 24 22:18:29 2008 +0300
+++ b/src/lib-index/mail-index-fsck.c	Mon Aug 04 01:41:39 2008 +0300
@@ -4,7 +4,7 @@
 #include "ioloop.h"
 #include "array.h"
 #include "mail-index-private.h"
-#include "mail-transaction-log.h"
+#include "mail-transaction-log-private.h"
 
 static void mail_index_fsck_error(struct mail_index *index,
 				  const char *fmt, ...) ATTR_FORMAT(2, 3);
@@ -26,17 +26,12 @@
 	}
 
 static void
-mail_index_fsck_header(struct mail_index *index, struct mail_index_map *map,
-		       struct mail_index_header *hdr)
+mail_index_fsck_log_pos(struct mail_index *index, struct mail_index_map *map,
+			struct mail_index_header *hdr)
 {
 	uint32_t file_seq;
 	uoff_t file_offset;
 
-	/* mail_index_map_check_header() has already checked that the index
-	   isn't completely broken. */
-	if (hdr->uid_validity == 0 && hdr->next_uid != 1)
-		hdr->uid_validity = ioloop_time;
-
 	mail_transaction_log_get_head(index->log, &file_seq, &file_offset);
 	if (hdr->log_file_seq < file_seq) {
 		hdr->log_file_head_offset = hdr->log_file_tail_offset =
@@ -57,6 +52,19 @@
 	}
 }
 
+static void
+mail_index_fsck_header(struct mail_index *index, struct mail_index_map *map,
+		       struct mail_index_header *hdr)
+{
+	/* mail_index_map_check_header() has already checked that the index
+	   isn't completely broken. */
+	if (hdr->uid_validity == 0 && hdr->next_uid != 1)
+		hdr->uid_validity = ioloop_time;
+
+	if (index->log->head != NULL)
+		mail_index_fsck_log_pos(index, map, hdr);
+}
+
 static bool
 array_has_name(const ARRAY_TYPE(const_string) *names, const char *name)
 {
@@ -396,12 +404,13 @@
 {
 	struct mail_index_header hdr;
 
-	/* Remember the log head position. If we go back in the index's head
-	   offset, ignore errors in the log up to this offset. */
-	mail_transaction_log_get_head(index->log,
-				      &index->fsck_log_head_file_seq,
-				      &index->fsck_log_head_file_offset);
-
+	if (index->log->head != NULL) {
+		/* Remember the log head position. If we go back in the index's
+		   head offset, ignore errors in the log up to this offset. */
+		mail_transaction_log_get_head(index->log,
+			&index->fsck_log_head_file_seq,
+			&index->fsck_log_head_file_offset);
+	}
 	hdr = map->hdr;
 
 	mail_index_fsck_header(index, map, &hdr);
@@ -420,6 +429,13 @@
 
 	i_warning("fscking index file %s", index->filepath);
 
+	if (index->log->head == NULL) {
+		/* we're trying to open the index files, but there wasn't
+		   any .log file. this should be rare, so just fsck it without
+		   locking. */
+		orig_locked = TRUE;
+	}
+
 	if (!orig_locked) {
 		if (mail_transaction_log_sync_lock(index->log, &file_seq,
 						   &file_offset) < 0)