changeset 21172:5ef269356b27

lib-index: Improve errors - return reason in mail_transaction_log_file_open()
author Timo Sirainen <timo.sirainen@dovecot.fi>
date Fri, 18 Nov 2016 16:52:31 +0200
parents d3022316712b
children 369e2eec8992
files src/lib-index/mail-transaction-log-file.c src/lib-index/mail-transaction-log-private.h src/lib-index/mail-transaction-log.c
diffstat 3 files changed, 18 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-index/mail-transaction-log-file.c	Fri Nov 18 16:41:18 2016 +0200
+++ b/src/lib-index/mail-transaction-log-file.c	Fri Nov 18 16:52:31 2016 +0200
@@ -881,7 +881,8 @@
 	return ret;
 }
 
-int mail_transaction_log_file_open(struct mail_transaction_log_file *file)
+int mail_transaction_log_file_open(struct mail_transaction_log_file *file,
+				   const char **reason_r)
 {
 	struct mail_index *index = file->log->index;
         unsigned int i;
@@ -900,10 +901,13 @@
 			index->readonly = TRUE;
 		}
 		if (file->fd == -1) {
-			if (errno == ENOENT)
+			if (errno == ENOENT) {
+				*reason_r = "File doesn't exist";
 				return 0;
+			}
 
 			log_file_set_syscall_error(file, "open()");
+			*reason_r = t_strdup_printf("open() failed: %m");
 			return -1;
                 }
 
@@ -915,6 +919,7 @@
 			   renamed to .log.2 and we're trying to reopen it.
 			   also possible that hit a race condition where .log
 			   and .log.2 are linked. */
+			*reason_r = "File is already open";
 			return 0;
 		} else {
 			ret = mail_transaction_log_file_read_hdr(file,
@@ -932,11 +937,13 @@
 			} else {
 				i_unlink_if_exists(file->filepath);
 			}
+			*reason_r = "File is corrupted";
 			return 0;
 		}
 		if (errno != ESTALE ||
 		    i == MAIL_INDEX_ESTALE_RETRY_COUNT) {
 			/* syscall error */
+			*reason_r = t_strdup_printf("fstat() failed: %m");
 			return -1;
 		}
 
--- a/src/lib-index/mail-transaction-log-private.h	Fri Nov 18 16:41:18 2016 +0200
+++ b/src/lib-index/mail-transaction-log-private.h	Fri Nov 18 16:52:31 2016 +0200
@@ -116,7 +116,10 @@
 				const char *path);
 void mail_transaction_log_file_free(struct mail_transaction_log_file **file);
 
-int mail_transaction_log_file_open(struct mail_transaction_log_file *file);
+/* Returns 1 if log was opened, 0 if it didn't exist or was already open,
+   -1 if error. */
+int mail_transaction_log_file_open(struct mail_transaction_log_file *file,
+				   const char **reason_r);
 int mail_transaction_log_file_create(struct mail_transaction_log_file *file,
 				     bool reset);
 int mail_transaction_log_file_lock(struct mail_transaction_log_file *file);
--- a/src/lib-index/mail-transaction-log.c	Fri Nov 18 16:41:18 2016 +0200
+++ b/src/lib-index/mail-transaction-log.c	Fri Nov 18 16:52:31 2016 +0200
@@ -59,6 +59,7 @@
 int mail_transaction_log_open(struct mail_transaction_log *log)
 {
 	struct mail_transaction_log_file *file;
+	const char *reason;
 	int ret;
 
 	i_free(log->filepath);
@@ -79,7 +80,7 @@
 		return 0;
 
 	file = mail_transaction_log_file_alloc(log, log->filepath);
-	if ((ret = mail_transaction_log_file_open(file)) <= 0) {
+	if ((ret = mail_transaction_log_file_open(file, &reason)) <= 0) {
 		/* leave the file for _create() */
 		log->open_file = file;
 		return ret;
@@ -287,6 +288,7 @@
 {
         struct mail_transaction_log_file *file;
 	struct stat st;
+	const char *reason;
 
 	i_assert(log->head != NULL);
 
@@ -330,7 +332,7 @@
 	}
 
 	file = mail_transaction_log_file_alloc(log, log->filepath);
-	if (mail_transaction_log_file_open(file) <= 0) {
+	if (mail_transaction_log_file_open(file, &reason) <= 0) {
 		mail_transaction_log_file_free(&file);
 		return -1;
 	}
@@ -415,12 +417,8 @@
 
 	/* see if we have it in log.2 file */
 	file = mail_transaction_log_file_alloc(log, log->filepath2);
-	if ((ret = mail_transaction_log_file_open(file)) <= 0) {
+	if ((ret = mail_transaction_log_file_open(file, reason_r)) <= 0) {
 		mail_transaction_log_file_free(&file);
-		if (ret < 0)
-			*reason_r = "Failed to open .log.2";
-		else
-			*reason_r = ".log.2 doesn't exist";
 		return ret;
 	}