[dovecot-cvs] dovecot/src/lib-index/mbox mbox-rewrite.c,1.36,1.37 mbox-sync-full.c,1.3,1.4 mbox-sync.c,1.19,1.20

cras at procontrol.fi cras at procontrol.fi
Mon Nov 4 06:47:42 EET 2002


Update of /home/cvs/dovecot/src/lib-index/mbox
In directory danu:/tmp/cvs-serv26855/lib-index/mbox

Modified Files:
	mbox-rewrite.c mbox-sync-full.c mbox-sync.c 
Log Message:
And more locking/syncing fixes. Now it's finally beginning to look sane
again.



Index: mbox-rewrite.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib-index/mbox/mbox-rewrite.c,v
retrieving revision 1.36
retrieving revision 1.37
diff -u -d -r1.36 -r1.37
--- mbox-rewrite.c	2 Nov 2002 22:47:33 -0000	1.36
+++ mbox-rewrite.c	4 Nov 2002 04:47:40 -0000	1.37
@@ -424,7 +424,9 @@
 	tmp_fd = -1; inbuf = NULL;
 	failed = TRUE; rewrite = FALSE;
 	do {
-		/* make sync() lock the file to prevent race conditions */
+		if (!index->set_lock(index, MAIL_LOCK_EXCLUSIVE))
+			break;
+
 		if (!index->sync_and_lock(index, MAIL_LOCK_EXCLUSIVE, NULL))
 			break;
 
@@ -475,6 +477,13 @@
 			if (!mbox_mail_get_location(index, rec, &offset,
 						    &hdr_size, &body_size)) {
 				/* fsck should have fixed it */
+				failed = TRUE;
+				break;
+			}
+
+			if (offset < inbuf->v_offset) {
+				index_set_corrupted(index,
+						    "Invalid message offset");
 				failed = TRUE;
 				break;
 			}

Index: mbox-sync-full.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib-index/mbox/mbox-sync-full.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- mbox-sync-full.c	2 Nov 2002 20:10:21 -0000	1.3
+++ mbox-sync-full.c	4 Nov 2002 04:47:40 -0000	1.4
@@ -266,11 +266,7 @@
 	IBuffer *inbuf;
 	int failed;
 
-	if (index->lock_type == MAIL_LOCK_SHARED)
-		(void)mail_index_set_lock(index, MAIL_LOCK_UNLOCK);
-
-	if (!index->set_lock(index, MAIL_LOCK_EXCLUSIVE))
-		return FALSE;
+	i_assert(index->lock_type == MAIL_LOCK_EXCLUSIVE);
 
 	inbuf = mbox_get_inbuf(index, 0, MAIL_LOCK_SHARED);
 	if (inbuf == NULL)

Index: mbox-sync.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib-index/mbox/mbox-sync.c,v
retrieving revision 1.19
retrieving revision 1.20
diff -u -d -r1.19 -r1.20
--- mbox-sync.c	2 Nov 2002 22:51:11 -0000	1.19
+++ mbox-sync.c	4 Nov 2002 04:47:40 -0000	1.20
@@ -41,7 +41,32 @@
 	return offset;
 }
 
-int mbox_index_sync(MailIndex *index, MailLockType lock_type, int *changes)
+static int mbox_lock_and_sync_full(MailIndex *index,
+				   MailLockType data_lock_type)
+{
+        MailLockType lock_type;
+
+	/* syncing needs exclusive index lock and shared
+	   mbox lock, but if we'd want exclusive mbox lock
+	   we need to set it here already */
+	if (index->lock_type == MAIL_LOCK_SHARED)
+		(void)mail_index_set_lock(index, MAIL_LOCK_UNLOCK);
+
+	if (!index->set_lock(index, MAIL_LOCK_EXCLUSIVE))
+		return FALSE;
+
+	if (index->mbox_lock_type == MAIL_LOCK_UNLOCK) {
+		lock_type = data_lock_type == MAIL_LOCK_EXCLUSIVE ?
+			MAIL_LOCK_EXCLUSIVE : MAIL_LOCK_SHARED;
+		if (!mbox_lock(index, lock_type))
+			return FALSE;
+	}
+
+	return mbox_sync_full(index);
+}
+
+int mbox_index_sync(MailIndex *index, MailLockType data_lock_type,
+		    int *changes)
 {
 	struct stat st;
 	time_t index_mtime;
@@ -74,55 +99,37 @@
                 mbox_file_close_fd(index);
 	}
 
-	if (lock_type == MAIL_LOCK_EXCLUSIVE) {
-		/* if we know that we want exclusive lock, we might get
-		   it immediately to save extra lock changes */
-		if (!index->set_lock(index, MAIL_LOCK_EXCLUSIVE))
-			return FALSE;
-	}
-
 	if (index_mtime != st.st_mtime || index->mbox_size != filesize) {
 		mbox_file_close_inbuf(index);
 
-		/* problem .. index->mbox_size points to data after the last
-		   message. that should be \n or end of file. modify filesize
-		   accordingly to allow the extra byte. Don't actually bother
-		   to open the file and verify it, it'd just slow things.. */
 		index->mbox_size = get_indexed_mbox_size(index);
-		if (filesize == index->mbox_size+1)
-			index->mbox_size = filesize;
-
 		if (index->file_sync_stamp == 0 &&
-		    index->mbox_size == filesize) {
+		    index->mbox_size == filesize+1) {
 			/* just opened the mailbox, and the file size is same
 			   as we expected. don't bother checking it any
-			   further. */
+			   further. the +1 comes from the extra \n at end. */
 		} else {
 			if (changes != NULL)
 				*changes = TRUE;
 
-			/* file has changed, scan through the whole mbox */
-			if (!mbox_sync_full(index)) {
-				(void)index->set_lock(index, MAIL_LOCK_UNLOCK);
+			if (!mbox_lock_and_sync_full(index, data_lock_type))
 				return FALSE;
-			}
-
-			if (lock_type == MAIL_LOCK_EXCLUSIVE &&
-			    index->mbox_lock_type == MAIL_LOCK_SHARED) {
-				/* mbox_sync_full() left it */
-				if (!mbox_unlock(index))
-					return FALSE;
-			}
 		}
 
 		index->file_sync_stamp = st.st_mtime;
 	}
 
-	if (!index->set_lock(index, lock_type))
-		return FALSE;
+	/* we need some index lock to be able to lock mbox */
+	if (index->lock_type == MAIL_LOCK_UNLOCK) {
+		if (!index->set_lock(index, MAIL_LOCK_SHARED))
+			return FALSE;
+	}
 
-	if (lock_type != MAIL_LOCK_UNLOCK) {
-		if (!mbox_lock(index, lock_type))
+	if (data_lock_type == MAIL_LOCK_UNLOCK) {
+		if (!mbox_unlock(index))
+			return FALSE;
+	} else {
+		if (!mbox_lock(index, data_lock_type))
 			return FALSE;
 	}
 




More information about the dovecot-cvs mailing list