changeset 12267:ff1ac6f22ba1

lib-index: Put back some of the removed locking complexity. This fixes a crash when mmap_disable=no and a index was mmap()ed (which keeps the index locked) and later its read-lock was tried to be changed to write-lock.
author Timo Sirainen <tss@iki.fi>
date Thu, 14 Oct 2010 17:23:11 +0100
parents c38f630dae09
children df6f5d270a46
files src/lib-index/mail-index-lock.c
diffstat 1 files changed, 14 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-index/mail-index-lock.c	Thu Oct 14 17:02:20 2010 +0100
+++ b/src/lib-index/mail-index-lock.c	Thu Oct 14 17:23:11 2010 +0100
@@ -42,25 +42,22 @@
 	int ret;
 
 	i_assert(lock_type == F_RDLCK || lock_type == F_WRLCK);
-	i_assert(timeout_secs == 0 || lock_type == F_RDLCK);
 
-	switch (lock_type) {
-	case F_RDLCK:
-		if (index->lock_type != F_UNLCK) {
-			index->shared_lock_count++;
-			*lock_id_r = index->lock_id_counter;
-			return 1;
-		}
-		break;
-	case F_WRLCK:
-		if (index->lock_type == F_WRLCK) {
-			index->excl_lock_count++;
-			*lock_id_r = index->lock_id_counter + 1;
-			return 1;
-		}
+	if (lock_type == F_RDLCK && index->lock_type != F_UNLCK) {
+		index->shared_lock_count++;
+		*lock_id_r = index->lock_id_counter;
+		ret = 1;
+	} else if (lock_type == F_WRLCK && index->lock_type == F_WRLCK) {
+		index->excl_lock_count++;
+		*lock_id_r = index->lock_id_counter + 1;
+		ret = 1;
+	} else {
+		ret = 0;
+	}
 
-		i_assert(index->lock_type == F_UNLCK);
-		break;
+	if (ret > 0) {
+		/* file is already locked */
+		return 1;
 	}
 
 	if (index->lock_method == FILE_LOCK_METHOD_DOTLOCK &&