From 6c80172147a5a1cf937dea3f0f02b6fabcea861c Mon Sep 17 00:00:00 2001 From: Timo Sirainen Date: Thu, 14 Oct 2010 17:23:11 +0100 Subject: [PATCH] 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. --- src/lib-index/mail-index-lock.c | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/src/lib-index/mail-index-lock.c b/src/lib-index/mail-index-lock.c index 54d090644c..c964a6b3a2 100644 --- a/src/lib-index/mail-index-lock.c +++ b/src/lib-index/mail-index-lock.c @@ -42,25 +42,22 @@ static int mail_index_lock(struct mail_index *index, int lock_type, 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; - } - i_assert(index->lock_type == F_UNLCK); - break; + 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; + } + + if (ret > 0) { + /* file is already locked */ + return 1; } if (index->lock_method == FILE_LOCK_METHOD_DOTLOCK && -- 2.47.3