changeset 22276:5fac15015445

lib-storage: Move .vsize.lock creation to a generic mailbox_lock_file_create()
author Timo Sirainen <timo.sirainen@dovecot.fi>
date Thu, 22 Jun 2017 02:19:18 +0300
parents c4061e9cc721
children 55bf504485e8
files src/lib-storage/index/index-mailbox-size.c src/lib-storage/mail-storage-private.h src/lib-storage/mail-storage.c
diffstat 3 files changed, 39 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-storage/index/index-mailbox-size.c	Thu Jun 22 02:07:16 2017 +0300
+++ b/src/lib-storage/index/index-mailbox-size.c	Thu Jun 22 02:19:18 2017 +0300
@@ -5,7 +5,6 @@
 #include "strescape.h"
 #include "net.h"
 #include "write-full.h"
-#include "file-create-locked.h"
 #include "mail-search-build.h"
 #include "index-storage.h"
 #include "index-mailbox-size.h"
@@ -120,36 +119,6 @@
 	return update;
 }
 
-static int
-vsize_lock_create(struct mailbox *box, const char *lock_fname,
-		  unsigned int lock_secs, struct file_lock **lock_r,
-		  const char **error_r)
-{
-	const struct mailbox_permissions *perm;
-	struct file_create_settings set;
-	const char *lock_path;
-	bool created;
-
-	perm = mailbox_get_permissions(box);
-	i_zero(&set);
-	set.lock_timeout_secs =
-		mail_storage_get_lock_timeout(box->storage, lock_secs);
-	set.lock_method = box->storage->set->parsed_lock_method;
-	set.mode = perm->file_create_mode;
-	set.gid = perm->file_create_gid;
-	set.gid_origin = perm->file_create_gid_origin;
-
-	lock_path = t_strdup_printf("%s/%s", box->index->dir, lock_fname);
-	if (file_create_locked(lock_path, &set, lock_r, &created, error_r) == -1) {
-		*error_r = t_strdup_printf("file_create_locked(%s) failed: %s",
-					   lock_path, *error_r);
-		return errno == EAGAIN ? 0 : -1;
-	}
-	file_lock_set_close_on_free(*lock_r, TRUE);
-	file_lock_set_unlink_on_free(*lock_r, TRUE);
-	return 1;
-}
-
 static bool vsize_update_lock_full(struct mailbox_vsize_update *update,
 				   unsigned int lock_secs)
 {
@@ -164,8 +133,8 @@
 	if (MAIL_INDEX_IS_IN_MEMORY(box->index))
 		return FALSE;
 
-	ret = vsize_lock_create(box, VSIZE_LOCK_SUFFIX, lock_secs,
-				&update->lock, &error);
+	ret = mailbox_lock_file_create(box, VSIZE_LOCK_SUFFIX, lock_secs,
+				       &update->lock, &error);
 	if (ret <= 0) {
 		/* don't log lock timeouts, because we're somewhat expecting
 		   them. Especially when lock_secs is 0. */
--- a/src/lib-storage/mail-storage-private.h	Thu Jun 22 02:07:16 2017 +0300
+++ b/src/lib-storage/mail-storage-private.h	Thu Jun 22 02:19:18 2017 +0300
@@ -786,6 +786,13 @@
 /* Returns -1 if error, 0 if failed with EEXIST, 1 if ok */
 int mailbox_create_fd(struct mailbox *box, const char *path, int flags,
 		      int *fd_r);
+/* Create a lock file to the mailbox with the given filename. If it succeeds,
+   returns 1 and lock_r, which needs to be freed once finished with the lock.
+   If lock_secs is reached, returns 0 and error_r. Returns -1 and sets error_r
+   on other errors. */
+int mailbox_lock_file_create(struct mailbox *box, const char *lock_fname,
+			     unsigned int lock_secs, struct file_lock **lock_r,
+			     const char **error_r);
 unsigned int mail_storage_get_lock_timeout(struct mail_storage *storage,
 					   unsigned int secs);
 void mail_storage_free_binary_cache(struct mail_storage *storage);
--- a/src/lib-storage/mail-storage.c	Thu Jun 22 02:07:16 2017 +0300
+++ b/src/lib-storage/mail-storage.c	Thu Jun 22 02:19:18 2017 +0300
@@ -7,6 +7,7 @@
 #include "str.h"
 #include "str-sanitize.h"
 #include "unichar.h"
+#include "file-create-locked.h"
 #include "istream.h"
 #include "eacces-error.h"
 #include "mkdir-parents.h"
@@ -2762,3 +2763,32 @@
 
 	va_end(va);
 }
+
+int mailbox_lock_file_create(struct mailbox *box, const char *lock_fname,
+			     unsigned int lock_secs, struct file_lock **lock_r,
+			     const char **error_r)
+{
+	const struct mailbox_permissions *perm;
+	struct file_create_settings set;
+	const char *lock_path;
+	bool created;
+
+	perm = mailbox_get_permissions(box);
+	i_zero(&set);
+	set.lock_timeout_secs =
+		mail_storage_get_lock_timeout(box->storage, lock_secs);
+	set.lock_method = box->storage->set->parsed_lock_method;
+	set.mode = perm->file_create_mode;
+	set.gid = perm->file_create_gid;
+	set.gid_origin = perm->file_create_gid_origin;
+
+	lock_path = t_strdup_printf("%s/%s", box->index->dir, lock_fname);
+	if (file_create_locked(lock_path, &set, lock_r, &created, error_r) == -1) {
+		*error_r = t_strdup_printf("file_create_locked(%s) failed: %s",
+					   lock_path, *error_r);
+		return errno == EAGAIN ? 0 : -1;
+	}
+	file_lock_set_close_on_free(*lock_r, TRUE);
+	file_lock_set_unlink_on_free(*lock_r, TRUE);
+	return 1;
+}