[dovecot-cvs] dovecot/src/lib-storage/index/mbox mbox-lock.c, 1.4, 1.5 mbox-mail.c, 1.4, 1.5 mbox-storage.c, 1.77, 1.78 mbox-sync-private.h, 1.11, 1.12 mbox-sync.c, 1.21, 1.22 mbox-transaction.c, 1.1, 1.2

cras at procontrol.fi cras at procontrol.fi
Tue Jun 15 04:15:46 EEST 2004


Update of /home/cvs/dovecot/src/lib-storage/index/mbox
In directory talvi:/tmp/cvs-serv12670/lib-storage/index/mbox

Modified Files:
	mbox-lock.c mbox-mail.c mbox-storage.c mbox-sync-private.h 
	mbox-sync.c mbox-transaction.c 
Log Message:
lock the mbox before reading it.



Index: mbox-lock.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib-storage/index/mbox/mbox-lock.c,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- mbox-lock.c	14 Jun 2004 22:44:56 -0000	1.4
+++ mbox-lock.c	15 Jun 2004 01:15:44 -0000	1.5
@@ -409,6 +409,7 @@
 
 int mbox_unlock(struct index_mailbox *ibox, unsigned int lock_id)
 {
+	i_assert(ibox->mbox_locks > 0);
 	i_assert(ibox->mbox_lock_id == lock_id);
 
 	if (--ibox->mbox_locks > 0)

Index: mbox-mail.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib-storage/index/mbox/mbox-mail.c,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- mbox-mail.c	14 Jun 2004 23:35:17 -0000	1.4
+++ mbox-mail.c	15 Jun 2004 01:15:44 -0000	1.5
@@ -5,6 +5,7 @@
 #include "index-mail.h"
 #include "mbox-storage.h"
 #include "mbox-file.h"
+#include "mbox-sync-private.h"
 #include "istream-raw-mbox.h"
 
 #include <fcntl.h>
@@ -16,17 +17,23 @@
 	struct index_mailbox *ibox = mail->ibox;
 	const void *data;
 
+	if (ibox->mbox_lock_type == F_UNLCK) {
+		if (mbox_sync(ibox, FALSE, TRUE) < 0)
+			return -1;
+
+		i_assert(ibox->mbox_lock_type != F_UNLCK);
+                mail->ibox->mbox_mail_lock_id = ibox->mbox_lock_id;
+	}
+
+	if (mbox_file_open_stream(ibox) < 0)
+		return -1;
+
 	if (mail_index_lookup_extra(ibox->view, mail->mail.seq,
 				    ibox->mbox_extra_idx, &data) < 0) {
 		mail_storage_set_index_error(ibox);
 		return -1;
 	}
 
-	// FIXME: lock the file. sync if needed.
-
-	if (mbox_file_open_stream(ibox) < 0)
-		return -1;
-
 	istream_raw_mbox_seek(ibox->mbox_stream, *((const uint64_t *)data));
 	return 0;
 }

Index: mbox-storage.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib-storage/index/mbox/mbox-storage.c,v
retrieving revision 1.77
retrieving revision 1.78
diff -u -d -r1.77 -r1.78
--- mbox-storage.c	14 Jun 2004 04:30:32 -0000	1.77
+++ mbox-storage.c	15 Jun 2004 01:15:44 -0000	1.78
@@ -390,6 +390,14 @@
 	return 0; // FIXME
 }
 
+static void mbox_mail_deinit(struct index_mail *mail)
+{
+	if (mail->ibox->mbox_mail_lock_id != 0) {
+		(void)mbox_unlock(mail->ibox, mail->ibox->mbox_mail_lock_id);
+                mail->ibox->mbox_mail_lock_id = 0;
+	}
+}
+
 static struct mailbox *
 mbox_open(struct index_storage *storage, const char *name,
 	  enum mailbox_open_flags flags)
@@ -427,6 +435,7 @@
 	ibox->mbox_extra_idx = mbox_extra_idx;
 
 	ibox->get_recent_count = mbox_get_recent_count;
+	ibox->mail_deinit = mbox_mail_deinit;
 	ibox->mail_interface = &mbox_mail;
 
 	return &ibox->box;

Index: mbox-sync-private.h
===================================================================
RCS file: /home/cvs/dovecot/src/lib-storage/index/mbox/mbox-sync-private.h,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -d -r1.11 -r1.12
--- mbox-sync-private.h	14 Jun 2004 22:44:56 -0000	1.11
+++ mbox-sync-private.h	15 Jun 2004 01:15:44 -0000	1.12
@@ -83,7 +83,7 @@
 	off_t expunged_space, space_diff;
 };
 
-int mbox_sync(struct index_mailbox *ibox, int last_commit);
+int mbox_sync(struct index_mailbox *ibox, int last_commit, int lock);
 void mbox_sync_parse_next_mail(struct istream *input,
 			       struct mbox_sync_mail_context *ctx,
 			       int rewriting);

Index: mbox-sync.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib-storage/index/mbox/mbox-sync.c,v
retrieving revision 1.21
retrieving revision 1.22
diff -u -d -r1.21 -r1.22
--- mbox-sync.c	14 Jun 2004 23:35:17 -0000	1.21
+++ mbox-sync.c	15 Jun 2004 01:15:44 -0000	1.22
@@ -864,12 +864,7 @@
 	struct mbox_sync_mail_context mail_ctx;
 	struct stat st;
 	uint32_t min_msg_count;
-	int ret, lock_type;
-
-	lock_type = mail_index_sync_have_more(sync_ctx->index_sync_ctx) ?
-		F_WRLCK : F_RDLCK;
-	if (mbox_sync_lock(sync_ctx, lock_type) < 0)
-		return -1;
+	int ret;
 
 	if (fstat(sync_ctx->fd, &st) < 0) {
 		mbox_set_syscall_error(sync_ctx->ibox, "stat()");
@@ -949,17 +944,26 @@
 		(uint64_t)st.st_size != hdr->sync_size;
 }
 
-int mbox_sync(struct index_mailbox *ibox, int last_commit)
+int mbox_sync(struct index_mailbox *ibox, int last_commit, int lock)
 {
 	struct mail_index_sync_ctx *index_sync_ctx;
 	struct mail_index_view *sync_view;
 	struct mbox_sync_context sync_ctx;
 	uint32_t seq;
 	uoff_t offset;
-	int ret;
+	unsigned int lock_id = 0;
+	int ret, lock_type;
 
-	if ((ret = mbox_sync_has_changed(ibox)) < 0)
+	if (lock) {
+		if (mbox_lock(ibox, F_RDLCK, &lock_id) <= 0)
+			return -1;
+	}
+
+	if ((ret = mbox_sync_has_changed(ibox)) < 0) {
+		if (lock)
+			(void)mbox_unlock(ibox, lock_id);
 		return -1;
+	}
 	if (ret == 0 && !last_commit)
 		return 0;
 
@@ -994,6 +998,15 @@
 	ret = mail_index_get_header(sync_view, &sync_ctx.hdr);
 	i_assert(ret == 0);
 
+	lock_type = mail_index_sync_have_more(index_sync_ctx) ?
+		F_WRLCK : F_RDLCK;
+	if (lock_type == F_WRLCK && lock) {
+		(void)mbox_unlock(ibox, lock_id);
+		lock_id = 0;
+	}
+	if (mbox_sync_lock(&sync_ctx, lock_type) < 0)
+		return -1;
+
 	if (mbox_sync_do(&sync_ctx) < 0)
 		ret = -1;
 
@@ -1009,7 +1022,7 @@
 	if (mail_index_sync_end(index_sync_ctx) < 0)
 		ret = -1;
 
-	if (sync_ctx.lock_id != 0) {
+	if (sync_ctx.lock_id != 0 && (ret < 0 || !lock)) {
 		/* FIXME: drop to read locking and keep it MBOX_SYNC_SECS+1
 		   to make sure we notice changes made by others */
 		if (mbox_unlock(ibox, sync_ctx.lock_id) < 0)
@@ -1031,7 +1044,7 @@
 	    ibox->sync_last_check + MAILBOX_FULL_SYNC_INTERVAL <= ioloop_time) {
 		ibox->sync_last_check = ioloop_time;
 
-		if (mbox_sync(ibox, FALSE) < 0)
+		if (mbox_sync(ibox, FALSE, FALSE) < 0)
 			return -1;
 	}
 

Index: mbox-transaction.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib-storage/index/mbox/mbox-transaction.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- mbox-transaction.c	6 May 2004 01:22:25 -0000	1.1
+++ mbox-transaction.c	15 Jun 2004 01:15:44 -0000	1.2
@@ -37,7 +37,7 @@
 	}
 
 	if (ret == 0) {
-		if (mbox_sync(ibox, TRUE) < 0)
+		if (mbox_sync(ibox, TRUE, FALSE) < 0)
 			ret = -1;
 	}
 



More information about the dovecot-cvs mailing list