changeset 9697:6e4b79ca75cc HEAD

index: Code cleanups.
author Timo Sirainen <tss@iki.fi>
date Thu, 30 Jul 2009 18:13:25 -0400
parents 126132cb1c19
children 37bdd5ce828f
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, 75 insertions(+), 131 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-index/mail-transaction-log-file.c	Thu Jul 30 17:52:41 2009 -0400
+++ b/src/lib-index/mail-transaction-log-file.c	Thu Jul 30 18:13:25 2009 -0400
@@ -15,6 +15,14 @@
 #define LOG_PREFETCH 1024
 #define MEMORY_LOG_NAME "(in-memory transaction log file)"
 
+static int
+log_file_set_syscall_error(struct mail_transaction_log_file *file,
+			   const char *function)
+{
+	return mail_index_file_set_syscall_error(file->log->index,
+						 file->filepath, function);
+}
+
 void
 mail_transaction_log_file_set_corrupted(struct mail_transaction_log_file *file,
 					const char *fmt, ...)
@@ -25,13 +33,11 @@
 	file->hdr.indexid = 0;
 	if (!MAIL_TRANSACTION_LOG_FILE_IN_MEMORY(file)) {
 		/* indexid=0 marks the log file as corrupted */
+		unsigned int offset =
+			offsetof(struct mail_transaction_log_header, indexid);
 		if (pwrite_full(file->fd, &file->hdr.indexid,
-				sizeof(file->hdr.indexid),
-				offsetof(struct mail_transaction_log_header,
-					 indexid)) < 0) {
-			mail_index_file_set_syscall_error(file->log->index,
-				file->filepath, "pwrite()");
-		}
+				sizeof(file->hdr.indexid), offset) < 0)
+			log_file_set_syscall_error(file, "pwrite()");
 	}
 
 	va_start(va, fmt);
@@ -82,19 +88,13 @@
 		buffer_free(&file->buffer);
 
 	if (file->mmap_base != NULL) {
-		if (munmap(file->mmap_base, file->mmap_size) < 0) {
-			mail_index_file_set_syscall_error(file->log->index,
-							  file->filepath,
-							  "munmap()");
-		}
+		if (munmap(file->mmap_base, file->mmap_size) < 0)
+			log_file_set_syscall_error(file, "munmap()");
 	}
 
 	if (file->fd != -1) {
-		if (close(file->fd) < 0) {
-			mail_index_file_set_syscall_error(file->log->index,
-							  file->filepath,
-							  "close()");
-		}
+		if (close(file->fd) < 0)
+			log_file_set_syscall_error(file, "close()");
 	}
 
 	i_free(file->filepath);
@@ -263,9 +263,7 @@
 		return 0;
 	}
 	if (ret < 0) {
-		mail_index_file_set_syscall_error(file->log->index,
-						  file->filepath,
-						  "file_dotlock_create()");
+		log_file_set_syscall_error(file, "file_dotlock_create()");
 		return -1;
 	}
 
@@ -287,8 +285,7 @@
 
 	ret = file_dotlock_delete(&file->log->dotlock);
 	if (ret < 0) {
-		mail_index_file_set_syscall_error(file->log->index,
-			file->filepath, "file_dotlock_delete()");
+		log_file_set_syscall_error(file, "file_dotlock_delete()");
 		return -1;
 	}
 
@@ -325,9 +322,7 @@
 		return 0;
 	}
 	if (ret < 0) {
-		mail_index_file_set_syscall_error(file->log->index,
-						  file->filepath,
-						  "mail_index_wait_lock_fd()");
+		log_file_set_syscall_error(file, "mail_index_wait_lock_fd()");
 		return -1;
 	}
 
@@ -391,11 +386,8 @@
 
 	ret = mail_transaction_log_file_read_header(file);
 	if (ret < 0) {
-                if (errno != ESTALE || !ignore_estale) {
-                        mail_index_file_set_syscall_error(file->log->index,
-                                                          file->filepath,
-                                                          "pread()");
-                }
+                if (errno != ESTALE || !ignore_estale)
+			log_file_set_syscall_error(file, "pread()");
 		return -1;
 	}
 	if (file->hdr.major_version != MAIL_TRANSACTION_LOG_MAJOR_VERSION) {
@@ -486,10 +478,8 @@
 	struct stat st;
 
 	if (fstat(file->fd, &st) < 0) {
-                if (errno != ESTALE || !ignore_estale) {
-			mail_index_file_set_syscall_error(file->log->index,
-				file->filepath, "fstat()");
-                }
+                if (errno != ESTALE || !ignore_estale)
+			log_file_set_syscall_error(file, "fstat()");
 		return -1;
 	}
 
@@ -540,11 +530,8 @@
 	if (reset)
 		rename_existing = FALSE;
 	else if (nfs_safe_stat(file->filepath, &st) < 0) {
-		if (errno != ENOENT) {
-			mail_index_file_set_syscall_error(index, file->filepath,
-							  "stat()");
-			return -1;
-		}
+		if (errno != ENOENT)
+			return log_file_set_syscall_error(file, "stat()");
 		rename_existing = FALSE;
 	} else if (st.st_ino == file->st_ino &&
 		   CMP_DEV_T(st.st_dev, file->st_dev) &&
@@ -559,8 +546,7 @@
 		fd = nfs_safe_open(file->filepath, O_RDWR);
 		if (fd == -1) {
 			if (errno != ENOENT) {
-				mail_index_file_set_syscall_error(index,
-					file->filepath, "open()");
+				log_file_set_syscall_error(file, "open()");
 				return -1;
 			}
 		} else {
@@ -574,10 +560,8 @@
 				return 0;
 			}
 			file->fd = -1;
-			if (close(fd) < 0) {
-				mail_index_file_set_syscall_error(index,
-					file->filepath, "close()");
-			}
+			if (close(fd) < 0)
+				log_file_set_syscall_error(file, "close()");
 		}
 		rename_existing = FALSE;
 	}
@@ -593,20 +577,14 @@
 		file->hdr.prev_file_offset = 0;
 	}
 
-	if (write_full(new_fd, &file->hdr, sizeof(file->hdr)) < 0) {
-		mail_index_file_set_syscall_error(index, file->filepath,
-						  "write_full()");
-		return -1;
-	}
+	if (write_full(new_fd, &file->hdr, sizeof(file->hdr)) < 0)
+		return log_file_set_syscall_error(file, "write_full()");
 
 	if ((file->log->flags & MAIL_INDEX_OPEN_FLAG_NFS_FLUSH) != 0) {
 		/* the header isn't important, so don't bother calling
 		   fdatasync() unless NFS is used */
-		if (fdatasync(new_fd) < 0) {
-			mail_index_file_set_syscall_error(index, file->filepath,
-							  "fdatasync()");
-			return -1;
-		}
+		if (fdatasync(new_fd) < 0)
+			return log_file_set_syscall_error(file, "fdatasync()");
 	}
 
 	file->fd = new_fd;
@@ -672,11 +650,8 @@
 			       file->filepath, 0, &dotlock);
 	umask(old_mask);
 
-	if (fd == -1) {
-		mail_index_file_set_syscall_error(index, file->filepath,
-						  "file_dotlock_open()");
-		return -1;
-	}
+	if (fd == -1)
+		return log_file_set_syscall_error(file, "file_dotlock_open()");
 	mail_index_fchown(index, fd, file_dotlock_get_lock_path(dotlock));
 
         /* either fd gets used or the dotlock gets deleted and returned fd
@@ -702,9 +677,7 @@
 			if (errno == ENOENT)
 				return 0;
 
-			mail_index_file_set_syscall_error(file->log->index,
-				file->filepath, "open()");
-			return -1;
+			return log_file_set_syscall_error(file, "open()");
                 }
 
 		ignore_estale = i < MAIL_INDEX_ESTALE_RETRY_COUNT;
@@ -1158,12 +1131,8 @@
 		   Without this check we might see partial transactions,
 		   sometimes causing "Extension record updated without intro
 		   prefix" errors. */
-		if (fstat(file->fd, &st) < 0) {
-			mail_index_file_set_syscall_error(file->log->index,
-							  file->filepath,
-							  "fstat()");
-			return -1;
-		}
+		if (fstat(file->fd, &st) < 0)
+			return log_file_set_syscall_error(file, "fstat()");
 		if ((uoff_t)st.st_size != file->last_size) {
 			file->last_size = st.st_size;
 			return 0;
@@ -1235,9 +1204,7 @@
 		/* log file was deleted in NFS server, fail silently */
 		return 0;
 	} else {
-		mail_index_file_set_syscall_error(file->log->index,
-						  file->filepath, "pread()");
-		return -1;
+		return log_file_set_syscall_error(file, "pread()");
 	}
 }
 
@@ -1268,10 +1235,7 @@
 			/* log file was deleted in NFS server, fail silently */
 			return 0;
 		}
-
-		mail_index_file_set_syscall_error(file->log->index,
-						  file->filepath, "pread()");
-		return -1;
+		return log_file_set_syscall_error(file, "pread()");
 	}
 	return 1;
 }
@@ -1390,17 +1354,13 @@
 	if (file->mmap_base == MAP_FAILED) {
 		file->mmap_base = NULL;
 		file->mmap_size = 0;
-		mail_index_file_set_syscall_error(file->log->index,
-						  file->filepath, "mmap()");
-		return -1;
+		return log_file_set_syscall_error(file, "mmap()");
 	}
 
 	if (file->mmap_size > mmap_get_page_size()) {
 		if (madvise(file->mmap_base, file->mmap_size,
-			    MADV_SEQUENTIAL) < 0) {
-			mail_index_file_set_syscall_error(file->log->index,
-				file->filepath, "madvise()");
-		}
+			    MADV_SEQUENTIAL) < 0)
+			log_file_set_syscall_error(file, "madvise()");
 	}
 
 	buffer_create_const_data(&file->mmap_buffer,
@@ -1416,10 +1376,8 @@
 	if (file->mmap_base == NULL)
 		return;
 
-	if (munmap(file->mmap_base, file->mmap_size) < 0) {
-		mail_index_file_set_syscall_error(file->log->index,
-						  file->filepath, "munmap()");
-	}
+	if (munmap(file->mmap_base, file->mmap_size) < 0)
+		log_file_set_syscall_error(file, "munmap()");
 	file->mmap_base = NULL;
 	file->mmap_size = 0;
 	buffer_free(&file->buffer);
@@ -1437,11 +1395,8 @@
 	i_assert(file->buffer_offset == 0 || file->mmap_base == NULL);
 	i_assert(file->mmap_size == 0 || file->mmap_base != NULL);
 
-	if (fstat(file->fd, &st) < 0) {
-		mail_index_file_set_syscall_error(file->log->index,
-						  file->filepath, "fstat()");
-		return -1;
-	}
+	if (fstat(file->fd, &st) < 0)
+		return log_file_set_syscall_error(file, "fstat()");
 	file->last_size = st.st_size;
 
 	if ((uoff_t)st.st_size < file->sync_offset) {
@@ -1562,11 +1517,8 @@
 		file->buffer = buf;
 
 		/* and lose the mmap */
-		if (munmap(file->mmap_base, file->mmap_size) < 0) {
-			mail_index_file_set_syscall_error(file->log->index,
-							  file->filepath,
-							  "munmap()");
-		}
+		if (munmap(file->mmap_base, file->mmap_size) < 0)
+			log_file_set_syscall_error(file, "munmap()");
 		file->mmap_base = NULL;
 	} else if (file->buffer_offset != 0) {
 		/* we don't have the full log in the memory. read it. */
@@ -1574,13 +1526,10 @@
 	}
 	file->last_size = 0;
 
-	if (close(file->fd) < 0) {
-		mail_index_file_set_syscall_error(file->log->index,
-						  file->filepath, "close()");
-	}
+	if (close(file->fd) < 0)
+		log_file_set_syscall_error(file, "close()");
 	file->fd = -1;
 
 	i_free(file->filepath);
-	file->filepath = i_strconcat(file->log->index->filepath,
-				     MAIL_TRANSACTION_LOG_SUFFIX, NULL);
+	file->filepath = i_strdup(file->log->filepath);
 }
--- a/src/lib-index/mail-transaction-log-private.h	Thu Jul 30 17:52:41 2009 -0400
+++ b/src/lib-index/mail-transaction-log-private.h	Thu Jul 30 18:13:25 2009 -0400
@@ -77,6 +77,7 @@
 	struct mail_index *index;
         struct mail_transaction_log_view *views;
 	enum mail_index_open_flags flags;
+	char *filepath, *filepath2;
 
 	/* files is a linked list of all the opened log files. the list is
 	   sorted by the log file sequence, so that transaction views can use
--- a/src/lib-index/mail-transaction-log.c	Thu Jul 30 17:52:41 2009 -0400
+++ b/src/lib-index/mail-transaction-log.c	Thu Jul 30 18:13:25 2009 -0400
@@ -36,6 +36,9 @@
 
 	log = i_new(struct mail_transaction_log, 1);
 	log->index = index;
+	log->filepath = i_strconcat(index->filepath,
+				    MAIL_TRANSACTION_LOG_SUFFIX, NULL);
+	log->filepath2 = i_strconcat(log->filepath, ".2", NULL);
 
 	log->dotlock_settings.timeout = MAIL_TRANSCATION_LOG_LOCK_TIMEOUT;
 	log->dotlock_settings.stale_timeout =
@@ -49,22 +52,19 @@
 static void mail_transaction_log_2_unlink_old(struct mail_transaction_log *log)
 {
 	struct stat st;
-	const char *path;
 
-	path = t_strconcat(log->index->filepath,
-			   MAIL_TRANSACTION_LOG_SUFFIX".2", NULL);
-	if (stat(path, &st) < 0) {
+	if (stat(log->filepath2, &st) < 0) {
 		if (errno != ENOENT && errno != ESTALE) {
 			mail_index_set_error(log->index,
-				"stat(%s) failed: %m", path);
+				"stat(%s) failed: %m", log->filepath2);
 		}
 		return;
 	}
 
 	if (st.st_mtime + MAIL_TRANSACTION_LOG2_STALE_SECS <= ioloop_time) {
-		if (unlink(path) < 0 && errno != ENOENT) {
+		if (unlink(log->filepath2) < 0 && errno != ENOENT) {
 			mail_index_set_error(log->index,
-				"unlink(%s) failed: %m", path);
+				"unlink(%s) failed: %m", log->filepath2);
 		}
 	}
 }
@@ -72,7 +72,6 @@
 int mail_transaction_log_open(struct mail_transaction_log *log)
 {
 	struct mail_transaction_log_file *file;
-	const char *path;
 	int ret;
 
 	log->flags = log->index->flags;
@@ -91,10 +90,7 @@
 	if (MAIL_INDEX_IS_IN_MEMORY(log->index))
 		return 0;
 
-	path = t_strconcat(log->index->filepath,
-			   MAIL_TRANSACTION_LOG_SUFFIX, NULL);
-
-	file = mail_transaction_log_file_alloc(log, path);
+	file = mail_transaction_log_file_alloc(log, log->filepath);
 	if ((ret = mail_transaction_log_file_open(file, FALSE)) <= 0) {
 		/* leave the file for _create() */
 		log->open_file = file;
@@ -108,7 +104,6 @@
 int mail_transaction_log_create(struct mail_transaction_log *log, bool reset)
 {
 	struct mail_transaction_log_file *file;
-	const char *path;
 
 	if (MAIL_INDEX_IS_IN_MEMORY(log->index)) {
 		file = mail_transaction_log_file_alloc_in_memory(log);
@@ -116,11 +111,7 @@
 		return 0;
 	}
 
-	path = t_strconcat(log->index->filepath,
-			   MAIL_TRANSACTION_LOG_SUFFIX, NULL);
-
-	file = mail_transaction_log_file_alloc(log, path);
-
+	file = mail_transaction_log_file_alloc(log, log->filepath);
 	if (log->open_file != NULL) {
 		/* remember what file we tried to open. if someone else created
 		   a new file, use it instead of recreating it */
@@ -160,6 +151,8 @@
 
 	mail_transaction_log_close(log);
 	log->index->log = NULL;
+	i_free(log->filepath);
+	i_free(log->filepath2);
 	i_free(log);
 }
 
@@ -174,6 +167,12 @@
 		mail_transaction_log_close(log);
 	}
 
+	i_free(log->filepath);
+	i_free(log->filepath2);
+	log->filepath = i_strconcat(log->index->filepath,
+				    MAIL_TRANSACTION_LOG_SUFFIX, NULL);
+	log->filepath2 = i_strconcat(log->filepath, ".2", NULL);
+
 	if (log->head != NULL)
 		mail_transaction_log_file_move_to_memory(log->head);
 	else {
@@ -288,20 +287,18 @@
 {
         struct mail_transaction_log_file *file;
 	struct stat st;
-	const char *path;
 
 	i_assert(log->head != NULL);
 
 	if (MAIL_TRANSACTION_LOG_FILE_IN_MEMORY(log->head))
 		return 0;
 
-	path = t_strconcat(log->index->filepath,
-			   MAIL_TRANSACTION_LOG_SUFFIX, NULL);
 	if (nfs_flush && (log->flags & MAIL_INDEX_OPEN_FLAG_NFS_FLUSH) != 0)
-		nfs_flush_file_handle_cache(path);
-	if (nfs_safe_stat(path, &st) < 0) {
+		nfs_flush_file_handle_cache(log->filepath);
+	if (nfs_safe_stat(log->filepath, &st) < 0) {
 		if (errno != ENOENT) {
-			mail_index_file_set_syscall_error(log->index, path,
+			mail_index_file_set_syscall_error(log->index,
+							  log->filepath,
 							  "stat()");
 			return -1;
 		}
@@ -325,7 +322,7 @@
 		return 0;
 	}
 
-	file = mail_transaction_log_file_alloc(log, path);
+	file = mail_transaction_log_file_alloc(log, log->filepath);
 	if (mail_transaction_log_file_open(file, FALSE) <= 0) {
 		mail_transaction_log_file_free(&file);
 		return -1;
@@ -363,7 +360,6 @@
 				   struct mail_transaction_log_file **file_r)
 {
 	struct mail_transaction_log_file *file;
-	const char *path;
 	int ret;
 
 	if (file_seq > log->head->hdr.file_seq) {
@@ -399,9 +395,7 @@
 		return 0;
 
 	/* see if we have it in log.2 file */
-	path = t_strconcat(log->index->filepath,
-			   MAIL_TRANSACTION_LOG_SUFFIX".2", NULL);
-	file = mail_transaction_log_file_alloc(log, path);
+	file = mail_transaction_log_file_alloc(log, log->filepath2);
 	if ((ret = mail_transaction_log_file_open(file, TRUE)) <= 0) {
 		mail_transaction_log_file_free(&file);
 		return ret;