changeset 297:ef6ae9e1b585 HEAD

some cleanups + fd leaks when recaching mailbox.
author Timo Sirainen <tss@iki.fi>
date Mon, 23 Sep 2002 14:11:58 +0300
parents d66aa1f1fb2d
children 5509d87fe68b
files src/lib-index/mail-index-update-cache.c src/lib-index/mbox/mbox-fsck.c src/lib-index/mbox/mbox-lock.c src/lib-index/mbox/mbox-rebuild.c src/lib-index/mbox/mbox-rewrite.c
diffstat 5 files changed, 40 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-index/mail-index-update-cache.c	Mon Sep 23 13:42:20 2002 +0300
+++ b/src/lib-index/mail-index-update-cache.c	Mon Sep 23 14:11:58 2002 +0300
@@ -1,13 +1,17 @@
 /* Copyright (C) 2002 Timo Sirainen */
 
 #include "lib.h"
+#include "iobuffer.h"
 #include "mail-index.h"
 
+#include <unistd.h>
+
 static int cache_record(MailIndex *index, MailIndexRecord *rec,
 			MailField cache_fields)
 {
 	MailIndexUpdate *update;
 	IOBuffer *inbuf;
+	int failed;
 
 	inbuf = index->open_mail(index, rec);
 	if (inbuf == NULL)
@@ -17,7 +21,12 @@
 
 	update = index->update_begin(index, rec);
 	mail_index_update_headers(update, inbuf, cache_fields, NULL, NULL);
-	return index->update_end(update);
+	failed = !index->update_end(update);
+
+	(void)close(inbuf->fd);
+	io_buffer_destroy(inbuf);
+
+	return !failed;
 }
 
 int mail_index_update_cache(MailIndex *index)
--- a/src/lib-index/mbox/mbox-fsck.c	Mon Sep 23 13:42:20 2002 +0300
+++ b/src/lib-index/mbox/mbox-fsck.c	Mon Sep 23 14:11:58 2002 +0300
@@ -285,10 +285,8 @@
 	/* open the mbox file. we don't really need to open it read-write,
 	   but fcntl() locking requires it. */
 	fd = open(index->mbox_path, O_RDWR);
-	if (fd == -1) {
-		mbox_set_syscall_error(index, "open()");
-		return FALSE;
-	}
+	if (fd == -1)
+		return mbox_set_syscall_error(index, "open()");
 
 	inbuf = io_buffer_create_mmap(fd, default_pool,
 				      MAIL_MMAP_BLOCK_SIZE, 0);
@@ -302,7 +300,9 @@
 		(void)mbox_unlock(index, index->mbox_path, fd);
 	}
 
-	(void)close(fd);
+	if (close(fd) < 0)
+		mbox_set_syscall_error(index, "close()");
+
 	io_buffer_destroy(inbuf);
 
 	if (failed)
--- a/src/lib-index/mbox/mbox-lock.c	Mon Sep 23 13:42:20 2002 +0300
+++ b/src/lib-index/mbox/mbox-lock.c	Mon Sep 23 14:11:58 2002 +0300
@@ -33,8 +33,7 @@
 static int mbox_lock_flock(MailIndex *index, const char *path, int fd, int set)
 {
 	if (flock(fd, set ? LOCK_EX : LOCK_UN) == -1) {
-		index_set_error(index, "flock() mbox lock failed for file "
-				"%s: %m", path);
+		index_file_set_syscall_error(index, path, "flock()");
 		return FALSE;
 	}
 
@@ -54,8 +53,7 @@
 
 	while (fcntl(fd, F_SETLKW, &fl) == -1) {
 		if (errno != EINTR) {
-			index_set_error(index, "fcntl() mbox lock "
-					"failed for file %s: %m", path);
+			index_file_set_syscall_error(index, path, "fcntl()");
 			return FALSE;
 		}
 	}
@@ -75,9 +73,7 @@
 		if (unlink(path) == 0 || errno == ENOENT)
 			return TRUE;
 
-		index_set_error(index, "unlink() failed for dotlock file "
-				"%s: %m", path);
-		return FALSE;
+		return index_file_set_syscall_error(index, path, "unlink()");
 	}
 
 	/* don't bother with the temp files as we'd just leave them lying
@@ -89,13 +85,11 @@
 
 		if (stat(path, &st) == 0) {
 			/* lock exists, see if it's too old */
-			if (now > st.st_ctime + STALE_LOCK_TIMEOUT) {
-				if (unlink(path) == -1 && errno != ENOENT) {
-					index_set_error(index, "unlink() failed"
-							" for dotlock file "
-							"%s: %m", path);
-					break;
-				}
+			if (now > st.st_ctime + STALE_LOCK_TIMEOUT && 
+			    unlink(path) < 0 && errno != ENOENT) {
+				index_file_set_syscall_error(index, path,
+							     "unlink()");
+				break;
 			}
 
 			usleep(LOCK_RANDOM_USLEEP_TIME);
@@ -103,15 +97,17 @@
 		}
 
 		fd = open(path, O_WRONLY | O_EXCL | O_CREAT, 0);
-		if (fd >= 0) {
+		if (fd != -1) {
 			/* got it */
-			(void)close(fd);
+			if (close(fd) < 0) {
+				index_file_set_syscall_error(index, path,
+							     "close()");
+			}
 			return TRUE;
 		}
 
 		if (errno != EEXIST) {
-			index_set_error(index, "Can't create dotlock file "
-					"%s: %m", path);
+			index_file_set_syscall_error(index, path, "open()");
 			return FALSE;
 		}
 	} while (now < max_wait_time);
--- a/src/lib-index/mbox/mbox-rebuild.c	Mon Sep 23 13:42:20 2002 +0300
+++ b/src/lib-index/mbox/mbox-rebuild.c	Mon Sep 23 14:11:58 2002 +0300
@@ -51,7 +51,8 @@
 
 	/* lock the mailbox so we can be sure no-one interrupts us. */
 	if (!mbox_lock(index, index->mbox_path, fd)) {
-		(void)close(fd);
+		if (close(fd) < 0)
+			mbox_set_syscall_error(index, "close()");
 		return FALSE;
 	}
 
@@ -61,14 +62,14 @@
 	failed = !mbox_index_append(index, inbuf);
 
 	(void)mbox_unlock(index, index->mbox_path, fd);
-	(void)close(fd);
+	if (close(fd) < 0) mbox_set_syscall_error(index, "close()");
 	io_buffer_destroy(inbuf);
 
 	if (failed)
 		return FALSE;
 
 	/* update sync stamp */
-	if (stat(index->mbox_path, &st) == -1)
+	if (stat(index->mbox_path, &st) < 0)
 		return mbox_set_syscall_error(index, "fstat()");
 
 	index->file_sync_stamp = st.st_mtime;
--- a/src/lib-index/mbox/mbox-rewrite.c	Mon Sep 23 13:42:20 2002 +0300
+++ b/src/lib-index/mbox/mbox-rewrite.c	Mon Sep 23 14:11:58 2002 +0300
@@ -293,7 +293,8 @@
 
 	out_fd = mail_index_create_temp_file(index, &path);
 	if (out_fd == -1) {
-		(void)close(in_fd);
+		if (close(in_fd) < 0)
+			mbox_set_syscall_error(index, "close()");
 		io_buffer_destroy(inbuf);
 		return FALSE;
 	}
@@ -362,9 +363,12 @@
 		}
 	}
 
-	(void)close(out_fd);
-	(void)close(in_fd);
 	(void)unlink(path);
+
+	if (close(out_fd) < 0)
+		index_file_set_syscall_error(index, path, "close()");
+	if (close(in_fd) < 0)
+		mbox_set_syscall_error(index, "close()");
 	io_buffer_destroy(outbuf);
 	io_buffer_destroy(inbuf);
 	return failed;