# HG changeset patch # User Timo Sirainen # Date 1032781126 -10800 # Node ID 5509d87fe68b48756eb96ec7bf7dad088a4c69a6 # Parent ef6ae9e1b585bee6a018803fdd612c9bcd51aaaa mbox locking changes. support read-locking now. there's still problems though.. diff -r ef6ae9e1b585 -r 5509d87fe68b src/lib-index/mbox/mbox-fsck.c --- a/src/lib-index/mbox/mbox-fsck.c Mon Sep 23 14:11:58 2002 +0300 +++ b/src/lib-index/mbox/mbox-fsck.c Mon Sep 23 14:38:46 2002 +0300 @@ -291,9 +291,7 @@ inbuf = io_buffer_create_mmap(fd, default_pool, MAIL_MMAP_BLOCK_SIZE, 0); - /* lock the mailbox so we can be sure no-one interrupts us. - we are trying to repair our index after all. */ - if (!mbox_lock(index, index->mbox_path, fd)) + if (!mbox_lock(index, index->mbox_path, fd, FALSE)) failed = TRUE; else { failed = !mbox_index_fsck_buf(index, inbuf); diff -r ef6ae9e1b585 -r 5509d87fe68b src/lib-index/mbox/mbox-lock.c --- a/src/lib-index/mbox/mbox-lock.c Mon Sep 23 14:11:58 2002 +0300 +++ b/src/lib-index/mbox/mbox-lock.c Mon Sep 23 14:38:46 2002 +0300 @@ -29,24 +29,30 @@ #define STALE_LOCK_TIMEOUT (60*10) #ifdef USE_FLOCK - -static int mbox_lock_flock(MailIndex *index, const char *path, int fd, int set) +static int mbox_lock_flock(MailIndex *index, const char *path, int fd, + int lock_type) { - if (flock(fd, set ? LOCK_EX : LOCK_UN) == -1) { - index_file_set_syscall_error(index, path, "flock()"); - return FALSE; - } + if (lock_type == F_WRLCK) + lock_type = LOCK_EX; + else if (lock_type == F_RDLCK) + lock_type = LOCK_SH; + else + lock_type = LOCK_UN; + + if (flock(fd, lock_type) < 0) + return index_file_set_syscall_error(index, path, "flock()"); return TRUE; } #else -static int mbox_lock_fcntl(MailIndex *index, const char *path, int fd, int set) +static int mbox_lock_fcntl(MailIndex *index, const char *path, int fd, + int lock_type) { struct flock fl; - fl.l_type = set ? F_WRLCK : F_UNLCK; + fl.l_type = lock_type; fl.l_whence = SEEK_SET; fl.l_start = 0; fl.l_len = 0; @@ -117,22 +123,27 @@ return FALSE; } -int mbox_lock(MailIndex *index, const char *path, int fd) +int mbox_lock(MailIndex *index, const char *path, int fd, int exclusive) { + int lock_type; + i_assert(fd >= 0); if (++index->mbox_locks > 1) return TRUE; + lock_type = exclusive ? F_WRLCK : F_RDLCK; #ifdef USE_FLOCK - if (!mbox_lock_flock(index, path, fd, TRUE)) + if (!mbox_lock_flock(index, path, fd, lock_type)) return FALSE; #else - if (!mbox_lock_fcntl(index, path, fd, TRUE)) + if (!mbox_lock_fcntl(index, path, fd, lock_type)) return FALSE; #endif - if (!mbox_lock_dotlock(index, path, TRUE)) - return FALSE; + if (exclusive) { + if (!mbox_lock_dotlock(index, path, TRUE)) + return FALSE; + } return TRUE; } @@ -140,15 +151,16 @@ int mbox_unlock(MailIndex *index, const char *path, int fd) { i_assert(fd >= 0); + i_assert(index->mbox_locks > 0); if (--index->mbox_locks > 0) return TRUE; #ifdef USE_FLOCK - if (!mbox_lock_flock(index, path, fd, FALSE)) + if (!mbox_lock_flock(index, path, fd, F_UNLCK)) return FALSE; #else - if (!mbox_lock_fcntl(index, path, fd, FALSE)) + if (!mbox_lock_fcntl(index, path, fd, F_UNLCK)) return FALSE; #endif if (!mbox_lock_dotlock(index, path, FALSE)) diff -r ef6ae9e1b585 -r 5509d87fe68b src/lib-index/mbox/mbox-lock.h --- a/src/lib-index/mbox/mbox-lock.h Mon Sep 23 14:11:58 2002 +0300 +++ b/src/lib-index/mbox/mbox-lock.h Mon Sep 23 14:38:46 2002 +0300 @@ -1,7 +1,7 @@ #ifndef __MBOX_LOCK_H #define __MBOX_LOCK_H -int mbox_lock(MailIndex *index, const char *path, int fd); +int mbox_lock(MailIndex *index, const char *path, int fd, int exclusive); int mbox_unlock(MailIndex *index, const char *path, int fd); #endif diff -r ef6ae9e1b585 -r 5509d87fe68b src/lib-index/mbox/mbox-rebuild.c --- a/src/lib-index/mbox/mbox-rebuild.c Mon Sep 23 14:11:58 2002 +0300 +++ b/src/lib-index/mbox/mbox-rebuild.c Mon Sep 23 14:38:46 2002 +0300 @@ -50,7 +50,7 @@ return mbox_set_syscall_error(index, "open()"); /* lock the mailbox so we can be sure no-one interrupts us. */ - if (!mbox_lock(index, index->mbox_path, fd)) { + if (!mbox_lock(index, index->mbox_path, fd, FALSE)) { if (close(fd) < 0) mbox_set_syscall_error(index, "close()"); return FALSE; diff -r ef6ae9e1b585 -r 5509d87fe68b src/lib-index/mbox/mbox-rewrite.c --- a/src/lib-index/mbox/mbox-rewrite.c Mon Sep 23 14:11:58 2002 +0300 +++ b/src/lib-index/mbox/mbox-rewrite.c Mon Sep 23 14:38:46 2002 +0300 @@ -5,6 +5,7 @@ #include "temp-string.h" #include "write-full.h" #include "mbox-index.h" +#include "mbox-lock.h" #include "mail-index-util.h" #include "mail-custom-flags.h" @@ -288,16 +289,23 @@ if (in_fd == -1) return mbox_set_syscall_error(index, "open()"); - inbuf = io_buffer_create_mmap(in_fd, default_pool, - MAIL_MMAP_BLOCK_SIZE, 0); - out_fd = mail_index_create_temp_file(index, &path); if (out_fd == -1) { if (close(in_fd) < 0) mbox_set_syscall_error(index, "close()"); - io_buffer_destroy(inbuf); return FALSE; } + + if (!mbox_lock(index, index->mbox_path, in_fd, FALSE)) { + if (close(in_fd) < 0) + mbox_set_syscall_error(index, "close()"); + if (close(out_fd) < 0) + index_file_set_syscall_error(index, path, "close()"); + return FALSE; + } + + inbuf = io_buffer_create_mmap(in_fd, default_pool, + MAIL_MMAP_BLOCK_SIZE, 0); outbuf = io_buffer_create_file(out_fd, default_pool, 8192); failed = FALSE; seq = 1; @@ -363,12 +371,13 @@ } } + (void)mbox_unlock(index, index->mbox_path, in_fd); (void)unlink(path); + if (close(in_fd) < 0) + mbox_set_syscall_error(index, "close()"); if (close(out_fd) < 0) index_file_set_syscall_error(index, path, "close()"); - if (close(in_fd) < 0) - mbox_set_syscall_error(index, "close()"); io_buffer_destroy(outbuf); io_buffer_destroy(inbuf); return failed; diff -r ef6ae9e1b585 -r 5509d87fe68b src/lib-storage/index/mbox/mbox-expunge.c --- a/src/lib-storage/index/mbox/mbox-expunge.c Mon Sep 23 14:11:58 2002 +0300 +++ b/src/lib-storage/index/mbox/mbox-expunge.c Mon Sep 23 14:38:46 2002 +0300 @@ -125,7 +125,7 @@ return FALSE; } - if (!mbox_lock(ibox->index, ibox->index->mbox_path, fd)) { + if (!mbox_lock(ibox->index, ibox->index->mbox_path, fd, TRUE)) { (void)close(fd); return FALSE; } diff -r ef6ae9e1b585 -r 5509d87fe68b src/lib-storage/index/mbox/mbox-save.c --- a/src/lib-storage/index/mbox/mbox-save.c Mon Sep 23 14:11:58 2002 +0300 +++ b/src/lib-storage/index/mbox/mbox-save.c Mon Sep 23 14:38:46 2002 +0300 @@ -121,7 +121,7 @@ return FALSE; } - if (!mbox_lock(ibox->index, ibox->index->mbox_path, fd)) { + if (!mbox_lock(ibox->index, ibox->index->mbox_path, fd, TRUE)) { (void)close(fd); return mail_storage_set_index_error(ibox); }