dovecot-2.0: dbox: Use flock() for file locking if possible. If ...

dovecot at dovecot.org dovecot at dovecot.org
Tue Apr 13 12:33:20 EEST 2010


details:   http://hg.dovecot.org/dovecot-2.0/rev/3d585e69aa95
changeset: 11132:3d585e69aa95
user:      Timo Sirainen <tss at iki.fi>
date:      Tue Apr 13 12:33:17 2010 +0300
description:
dbox: Use flock() for file locking if possible. If not, require client_limit=1.
Maybe some day dbox code can be redesigned in a way that allows it to work
with fcntl() locking too.

diffstat:

 src/lib-storage/index/dbox-common/dbox-file.c     |  10 +++++++++-
 src/lib-storage/index/dbox-multi/Makefile.am      |   1 +
 src/lib-storage/index/dbox-multi/mdbox-storage.c  |   9 +++++++++
 src/lib-storage/index/dbox-single/Makefile.am     |   1 +
 src/lib-storage/index/dbox-single/sdbox-storage.c |  18 +++++++++++++++++-
 5 files changed, 37 insertions(+), 2 deletions(-)

diffs (117 lines):

diff -r 6b7b2987c41f -r 3d585e69aa95 src/lib-storage/index/dbox-common/dbox-file.c
--- a/src/lib-storage/index/dbox-common/dbox-file.c	Mon Apr 12 19:14:55 2010 +0300
+++ b/src/lib-storage/index/dbox-common/dbox-file.c	Tue Apr 13 12:33:17 2010 +0300
@@ -24,6 +24,14 @@
 
 #define DBOX_READ_BLOCK_SIZE 4096
 
+/* prefer flock(). fcntl() locking currently breaks if trying to access the
+   same file from multiple mail_storages within same process. */
+#ifdef HAVE_FLOCK
+#  define DBOX_FILE_LOCK_METHOD FILE_LOCK_METHOD_FLOCK
+#else
+#  define DBOX_FILE_LOCK_METHOD FILE_LOCK_METHOD_FCNTL
+#endif
+
 const char *dbox_generate_tmp_filename(void)
 {
 	static unsigned int create_count = 0;
@@ -291,7 +299,7 @@
 	i_assert(file->fd != -1);
 
 	ret = file_try_lock(file->fd, file->cur_path, F_WRLCK,
-			    FILE_LOCK_METHOD_FCNTL, &file->lock);
+			    DBOX_FILE_LOCK_METHOD, &file->lock);
 	if (ret < 0) {
 		mail_storage_set_critical(&file->storage->storage,
 			"file_try_lock(%s) failed: %m", file->cur_path);
diff -r 6b7b2987c41f -r 3d585e69aa95 src/lib-storage/index/dbox-multi/Makefile.am
--- a/src/lib-storage/index/dbox-multi/Makefile.am	Mon Apr 12 19:14:55 2010 +0300
+++ b/src/lib-storage/index/dbox-multi/Makefile.am	Tue Apr 13 12:33:17 2010 +0300
@@ -3,6 +3,7 @@
 AM_CPPFLAGS = \
 	-I$(top_srcdir)/src/lib \
 	-I$(top_srcdir)/src/lib-settings \
+	-I$(top_srcdir)/src/lib-master \
 	-I$(top_srcdir)/src/lib-mail \
 	-I$(top_srcdir)/src/lib-imap \
 	-I$(top_srcdir)/src/lib-index \
diff -r 6b7b2987c41f -r 3d585e69aa95 src/lib-storage/index/dbox-multi/mdbox-storage.c
--- a/src/lib-storage/index/dbox-multi/mdbox-storage.c	Mon Apr 12 19:14:55 2010 +0300
+++ b/src/lib-storage/index/dbox-multi/mdbox-storage.c	Tue Apr 13 12:33:17 2010 +0300
@@ -4,6 +4,7 @@
 #include "array.h"
 #include "ioloop.h"
 #include "mkdir-parents.h"
+#include "master-service.h"
 #include "mail-index-modseq.h"
 #include "mail-index-alloc-cache.h"
 #include "mailbox-log.h"
@@ -46,6 +47,14 @@
 		return -1;
 	}
 
+#ifndef HAVE_FLOCK
+	if (master_service_get_client_limit(master_service) > 1) {
+		*error_r = "mdbox requires client_limit=1 for service "
+			"since your OS doesn't support flock()";
+		return -1;
+	}
+#endif
+
 	_storage->unique_root_dir =
 		p_strdup(_storage->pool, ns->list->set.root_dir);
 
diff -r 6b7b2987c41f -r 3d585e69aa95 src/lib-storage/index/dbox-single/Makefile.am
--- a/src/lib-storage/index/dbox-single/Makefile.am	Mon Apr 12 19:14:55 2010 +0300
+++ b/src/lib-storage/index/dbox-single/Makefile.am	Tue Apr 13 12:33:17 2010 +0300
@@ -3,6 +3,7 @@
 AM_CPPFLAGS = \
 	-I$(top_srcdir)/src/lib \
 	-I$(top_srcdir)/src/lib-settings \
+	-I$(top_srcdir)/src/lib-master \
 	-I$(top_srcdir)/src/lib-mail \
 	-I$(top_srcdir)/src/lib-imap \
 	-I$(top_srcdir)/src/lib-index \
diff -r 6b7b2987c41f -r 3d585e69aa95 src/lib-storage/index/dbox-single/sdbox-storage.c
--- a/src/lib-storage/index/dbox-single/sdbox-storage.c	Mon Apr 12 19:14:55 2010 +0300
+++ b/src/lib-storage/index/dbox-single/sdbox-storage.c	Tue Apr 13 12:33:17 2010 +0300
@@ -1,6 +1,7 @@
 /* Copyright (c) 2007-2010 Dovecot authors, see the included COPYING file */
 
 #include "lib.h"
+#include "master-service.h"
 #include "mail-index-modseq.h"
 #include "dbox-mail.h"
 #include "dbox-save.h"
@@ -25,6 +26,21 @@
 	return &storage->storage.storage;
 }
 
+static int
+sdbox_storage_create(struct mail_storage *storage ATTR_UNUSED,
+		     struct mail_namespace *ns ATTR_UNUSED,
+		     const char **error_r ATTR_UNUSED)
+{
+#ifndef HAVE_FLOCK
+	if (master_service_get_client_limit(master_service) > 1) {
+		*error_r = "dbox requires client_limit=1 for service "
+			"since your OS doesn't support flock()";
+		return -1;
+	}
+#endif
+	return 0;
+}
+
 static struct mailbox *
 sdbox_mailbox_alloc(struct mail_storage *storage, struct mailbox_list *list,
 		    const char *name, enum mailbox_flags flags)
@@ -211,7 +227,7 @@
 	.v = {
                 NULL,
 		sdbox_storage_alloc,
-		NULL,
+		sdbox_storage_create,
 		NULL,
 		NULL,
 		dbox_storage_get_list_settings,


More information about the dovecot-cvs mailing list