changeset 365:cb405d2f5fd5 HEAD

mbox locking cleanups
author Timo Sirainen <tss@iki.fi>
date Sun, 06 Oct 2002 08:52:43 +0300
parents ea958a5b9de1
children 0da2b09461aa
files src/lib-index/mbox/mbox-fsck.c src/lib-index/mbox/mbox-lock.c src/lib-index/mbox/mbox-lock.h src/lib-index/mbox/mbox-rebuild.c src/lib-index/mbox/mbox-rewrite.c src/lib-storage/index/mbox/mbox-expunge.c src/lib-storage/index/mbox/mbox-save.c
diffstat 7 files changed, 47 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-index/mbox/mbox-fsck.c	Sun Oct 06 08:44:27 2002 +0300
+++ b/src/lib-index/mbox/mbox-fsck.c	Sun Oct 06 08:52:43 2002 +0300
@@ -293,11 +293,11 @@
 	if (inbuf == NULL)
 		return FALSE;
 
-	if (!mbox_lock(index, index->mbox_path, index->mbox_fd, FALSE))
+	if (!mbox_lock_read(index))
 		failed = TRUE;
 	else {
 		failed = !mbox_index_fsck_buf(index, inbuf);
-		(void)mbox_unlock(index, index->mbox_path, index->mbox_fd);
+		(void)mbox_unlock(index);
 	}
 	io_buffer_unref(inbuf);
 
--- a/src/lib-index/mbox/mbox-lock.c	Sun Oct 06 08:44:27 2002 +0300
+++ b/src/lib-index/mbox/mbox-lock.c	Sun Oct 06 08:52:43 2002 +0300
@@ -29,8 +29,7 @@
 #define STALE_LOCK_TIMEOUT (60*10)
 
 #ifdef USE_FLOCK
-static int mbox_lock_flock(MailIndex *index, const char *path, int fd,
-			   int lock_type)
+static int mbox_lock_flock(MailIndex *index, int lock_type)
 {
 	if (lock_type == F_WRLCK)
 		lock_type = LOCK_EX;
@@ -39,16 +38,16 @@
 	else
 		lock_type = LOCK_UN;
 
-	if (flock(fd, lock_type) < 0)
-		return index_file_set_syscall_error(index, path, "flock()");
+	if (flock(index->mbox_fd, lock_type) < 0)
+		return index_file_set_syscall_error(index, index->mbox_path,
+						    "flock()");
 
 	return TRUE;
 }
 
 #else
 
-static int mbox_lock_fcntl(MailIndex *index, const char *path, int fd,
-			   int lock_type)
+static int mbox_lock_fcntl(MailIndex *index, int lock_type)
 {
 	struct flock fl;
 
@@ -57,9 +56,10 @@
 	fl.l_start = 0;
 	fl.l_len = 0;
 
-	while (fcntl(fd, F_SETLKW, &fl) == -1) {
+	while (fcntl(index->mbox_fd, F_SETLKW, &fl) == -1) {
 		if (errno != EINTR) {
-			index_file_set_syscall_error(index, path, "fcntl()");
+			index_file_set_syscall_error(index, index->mbox_path,
+						     "fcntl()");
 			return FALSE;
 		}
 	}
@@ -123,47 +123,58 @@
 	return FALSE;
 }
 
-int mbox_lock(MailIndex *index, const char *path, int fd, int exclusive)
+static int mbox_lock(MailIndex *index, int exclusive)
 {
 	int lock_type;
 
-	i_assert(fd >= 0);
+	i_assert(index->mbox_fd != -1);
 
 	if (++index->mbox_locks > 1)
 		return TRUE;
 
+	if (exclusive) {
+		if (!mbox_lock_dotlock(index, index->mbox_path, TRUE))
+			return FALSE;
+	}
+
         lock_type = exclusive ? F_WRLCK : F_RDLCK;
 #ifdef USE_FLOCK
-	if (!mbox_lock_flock(index, path, fd, lock_type))
+	if (!mbox_lock_flock(index, lock_type))
 		return FALSE;
 #else
-	if (!mbox_lock_fcntl(index, path, fd, lock_type))
+	if (!mbox_lock_fcntl(index, lock_type))
 		return FALSE;
 #endif
-	if (exclusive) {
-		if (!mbox_lock_dotlock(index, path, TRUE))
-			return FALSE;
-	}
 
 	return TRUE;
 }
 
-int mbox_unlock(MailIndex *index, const char *path, int fd)
+int mbox_lock_read(MailIndex *index)
+{
+	return mbox_lock(index, FALSE);
+}
+
+int mbox_lock_write(MailIndex *index)
 {
-	i_assert(fd >= 0);
+	return mbox_lock(index, TRUE);
+}
+
+int mbox_unlock(MailIndex *index)
+{
+	i_assert(index->mbox_fd != -1);
 	i_assert(index->mbox_locks > 0);
 
 	if (--index->mbox_locks > 0)
 		return TRUE;
 
 #ifdef USE_FLOCK
-	if (!mbox_lock_flock(index, path, fd, F_UNLCK))
+	if (!mbox_lock_flock(index, F_UNLCK))
 		return FALSE;
 #else
-	if (!mbox_lock_fcntl(index, path, fd, F_UNLCK))
+	if (!mbox_lock_fcntl(index, F_UNLCK))
 		return FALSE;
 #endif
-	if (!mbox_lock_dotlock(index, path, FALSE))
+	if (!mbox_lock_dotlock(index, index->mbox_path, FALSE))
 		return FALSE;
 
 	return TRUE;
--- a/src/lib-index/mbox/mbox-lock.h	Sun Oct 06 08:44:27 2002 +0300
+++ b/src/lib-index/mbox/mbox-lock.h	Sun Oct 06 08:52:43 2002 +0300
@@ -1,7 +1,8 @@
 #ifndef __MBOX_LOCK_H
 #define __MBOX_LOCK_H
 
-int mbox_lock(MailIndex *index, const char *path, int fd, int exclusive);
-int mbox_unlock(MailIndex *index, const char *path, int fd);
+int mbox_lock_read(MailIndex *index);
+int mbox_lock_write(MailIndex *index);
+int mbox_unlock(MailIndex *index);
 
 #endif
--- a/src/lib-index/mbox/mbox-rebuild.c	Sun Oct 06 08:44:27 2002 +0300
+++ b/src/lib-index/mbox/mbox-rebuild.c	Sun Oct 06 08:52:43 2002 +0300
@@ -47,14 +47,14 @@
 		return FALSE;
 
 	/* lock the mailbox so we can be sure no-one interrupts us. */
-	if (!mbox_lock(index, index->mbox_path, index->mbox_fd, FALSE)) {
+	if (!mbox_lock_read(index)) {
 		io_buffer_unref(inbuf);
 		return FALSE;
 	}
 
 	mbox_skip_empty_lines(inbuf);
 	failed = !mbox_index_append(index, inbuf);
-	(void)mbox_unlock(index, index->mbox_path, index->mbox_fd);
+	(void)mbox_unlock(index);
 
 	io_buffer_unref(inbuf);
 
--- a/src/lib-index/mbox/mbox-rewrite.c	Sun Oct 06 08:44:27 2002 +0300
+++ b/src/lib-index/mbox/mbox-rewrite.c	Sun Oct 06 08:52:43 2002 +0300
@@ -390,7 +390,7 @@
 		if (inbuf == NULL)
 			break;
 
-		if (!mbox_lock(index, index->mbox_path, index->mbox_fd, TRUE))
+		if (!mbox_lock_write(index))
 			break;
 		locked = TRUE;
 
@@ -413,10 +413,8 @@
 	} while (0);
 
 	if (!rewrite) {
-		if (locked) {
-			(void)mbox_unlock(index, index->mbox_path,
-					  index->mbox_fd);
-		}
+		if (locked)
+			(void)mbox_unlock(index);
 		if (inbuf != NULL)
 			io_buffer_unref(inbuf);
 		return !failed;
@@ -529,7 +527,7 @@
 		}
 	}
 
-	(void)mbox_unlock(index, index->mbox_path, index->mbox_fd);
+	(void)mbox_unlock(index);
 	(void)unlink(path);
 
 	if (close(tmp_fd) < 0)
--- a/src/lib-storage/index/mbox/mbox-expunge.c	Sun Oct 06 08:44:27 2002 +0300
+++ b/src/lib-storage/index/mbox/mbox-expunge.c	Sun Oct 06 08:52:43 2002 +0300
@@ -121,8 +121,7 @@
 	if (inbuf == NULL)
 		return FALSE;
 
-	if (!mbox_lock(ibox->index, ibox->index->mbox_path,
-		       ibox->index->mbox_fd, TRUE)) {
+	if (!mbox_lock_write(ibox->index)) {
 		io_buffer_unref(inbuf);
 		return FALSE;
 	}
@@ -147,8 +146,7 @@
 		failed = TRUE;
 	}
 
-	(void)mbox_unlock(ibox->index, ibox->index->mbox_path,
-			  ibox->index->mbox_fd);
+	(void)mbox_unlock(ibox->index);
 	io_buffer_unref(outbuf);
 
 	return !failed;
--- a/src/lib-storage/index/mbox/mbox-save.c	Sun Oct 06 08:44:27 2002 +0300
+++ b/src/lib-storage/index/mbox/mbox-save.c	Sun Oct 06 08:52:43 2002 +0300
@@ -173,7 +173,7 @@
 	io_buffer_unref(inbuf);
 	fd = ibox->index->mbox_fd;
 
-	if (!mbox_lock(ibox->index, ibox->index->mbox_path, fd, TRUE)) {
+	if (!mbox_lock_write(ibox->index)) {
 		(void)close(fd);
 		return mail_storage_set_index_error(ibox);
 	}
@@ -203,6 +203,6 @@
 		}
 	}
 
-	(void)mbox_unlock(ibox->index, ibox->index->mbox_path, fd);
+	(void)mbox_unlock(ibox->index);
 	return !failed;
 }