changeset 22062:7d4ec76b7d94

lib-index: mail_transaction_log_file_get_highest_modseq_at() - return error string This can help figuring out why some error happened when more context is provided in the caller's error messages.
author Timo Sirainen <timo.sirainen@dovecot.fi>
date Wed, 17 May 2017 16:28:58 +0300
parents e8d999dd8043
children de0cf622817b
files src/lib-index/mail-transaction-log-file.c src/lib-index/mail-transaction-log-private.h src/lib-index/mail-transaction-log-view.c src/lib-index/test-mail-transaction-log-view.c
diffstat 4 files changed, 22 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-index/mail-transaction-log-file.c	Wed May 17 16:21:54 2017 +0300
+++ b/src/lib-index/mail-transaction-log-file.c	Wed May 17 16:28:58 2017 +0300
@@ -1171,7 +1171,8 @@
 
 static int
 log_get_synced_record(struct mail_transaction_log_file *file, uoff_t *offset,
-		      const struct mail_transaction_header **hdr_r)
+		      const struct mail_transaction_header **hdr_r,
+		      const char **error_r)
 {
 	const struct mail_transaction_header *hdr;
 	uint32_t trans_size;
@@ -1184,10 +1185,11 @@
 	trans_size = mail_index_offset_to_uint32(hdr->size);
 	if (trans_size < sizeof(*hdr) ||
 	    *offset - file->buffer_offset + trans_size > file->buffer->used) {
-		mail_transaction_log_file_set_corrupted(file,
+		*error_r = t_strdup_printf(
 			"Transaction log corrupted unexpectedly at "
 			"%"PRIuUOFF_T": Invalid size %u (type=%x)",
 			*offset, trans_size, hdr->type);
+		mail_transaction_log_file_set_corrupted(file, "%s", *error_r);
 		return -1;
 	}
 	*offset += trans_size;
@@ -1197,7 +1199,8 @@
 
 int mail_transaction_log_file_get_highest_modseq_at(
 		struct mail_transaction_log_file *file,
-		uoff_t offset, uint64_t *highest_modseq_r)
+		uoff_t offset, uint64_t *highest_modseq_r,
+		const char **error_r)
 {
 	const struct mail_transaction_header *hdr;
 	struct modseq_cache *cache;
@@ -1230,7 +1233,7 @@
 
 	ret = mail_transaction_log_file_map(file, cur_offset, offset, &reason);
 	if (ret <= 0) {
-		mail_index_set_error(file->log->index,
+		*error_r = t_strdup_printf(
 			"Failed to map transaction log %s for getting modseq "
 			"at offset=%"PRIuUOFF_T" with start_offset=%"PRIuUOFF_T": %s",
 			file->filepath, offset, cur_offset, reason);
@@ -1240,7 +1243,7 @@
 	i_assert(cur_offset >= file->buffer_offset);
 	i_assert(cur_offset + file->buffer->used >= offset);
 	while (cur_offset < offset) {
-		if (log_get_synced_record(file, &cur_offset, &hdr) < 0)
+		if (log_get_synced_record(file, &cur_offset, &hdr, error_r) < 0)
 			return- 1;
 		mail_transaction_update_modseq(hdr, hdr + 1, &cur_modseq,
 			MAIL_TRANSACTION_LOG_HDR_VERSION(&file->hdr));
@@ -1313,8 +1316,11 @@
 
 	i_assert(cur_offset >= file->buffer_offset);
 	while (cur_offset < file->sync_offset) {
-		if (log_get_synced_record(file, &cur_offset, &hdr) < 0)
+		if (log_get_synced_record(file, &cur_offset, &hdr, &reason) < 0) {
+			mail_index_set_error(file->log->index,
+				"%s: %s", file->filepath, reason);
 			return -1;
+		}
 		mail_transaction_update_modseq(hdr, hdr + 1, &cur_modseq,
 			MAIL_TRANSACTION_LOG_HDR_VERSION(&file->hdr));
 		if (cur_modseq >= modseq)
--- a/src/lib-index/mail-transaction-log-private.h	Wed May 17 16:21:54 2017 +0300
+++ b/src/lib-index/mail-transaction-log-private.h	Wed May 17 16:28:58 2017 +0300
@@ -153,7 +153,8 @@
 				    unsigned int version);
 int mail_transaction_log_file_get_highest_modseq_at(
 		struct mail_transaction_log_file *file,
-		uoff_t offset, uint64_t *highest_modseq_r);
+		uoff_t offset, uint64_t *highest_modseq_r,
+		const char **error_r);
 int mail_transaction_log_file_get_modseq_next_offset(
 		struct mail_transaction_log_file *file,
 		uint64_t modseq, uoff_t *next_offset_r);
--- a/src/lib-index/mail-transaction-log-view.c	Wed May 17 16:21:54 2017 +0300
+++ b/src/lib-index/mail-transaction-log-view.c	Wed May 17 16:28:58 2017 +0300
@@ -336,10 +336,8 @@
 	view->broken = FALSE;
 
 	if (mail_transaction_log_file_get_highest_modseq_at(view->cur,
-				view->cur_offset, &view->prev_modseq) < 0) {
-		*reason_r = "Failed to get modseq";
+				view->cur_offset, &view->prev_modseq, reason_r) < 0)
 		return -1;
-	}
 
 	i_assert(view->cur_offset <= view->cur->sync_offset);
 	return 1;
@@ -401,8 +399,12 @@
 	view->broken = FALSE;
 
 	if (mail_transaction_log_file_get_highest_modseq_at(view->cur,
-				view->cur_offset, &view->prev_modseq) < 0)
+			view->cur_offset, &view->prev_modseq, &reason) < 0) {
+		mail_index_set_error(file->log->index,
+			"Failed to get modseq in %s for all-view: %s",
+			file->filepath, reason);
 		return -1;
+	}
 	return 0;
 }
 
--- a/src/lib-index/test-mail-transaction-log-view.c	Wed May 17 16:21:54 2017 +0300
+++ b/src/lib-index/test-mail-transaction-log-view.c	Wed May 17 16:28:58 2017 +0300
@@ -49,7 +49,8 @@
 
 int mail_transaction_log_file_get_highest_modseq_at(
 		struct mail_transaction_log_file *file ATTR_UNUSED,
-		uoff_t offset ATTR_UNUSED, uint64_t *highest_modseq_r)
+		uoff_t offset ATTR_UNUSED, uint64_t *highest_modseq_r,
+		const char **error_r ATTR_UNUSED)
 {
 	*highest_modseq_r = 0;
 	return 0;