]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-storage: Move .vsize.lock creation to a generic mailbox_lock_file_create()
authorTimo Sirainen <timo.sirainen@dovecot.fi>
Wed, 21 Jun 2017 23:19:18 +0000 (02:19 +0300)
committerGitLab <gitlab@git.dovecot.net>
Wed, 28 Jun 2017 18:59:03 +0000 (21:59 +0300)
src/lib-storage/index/index-mailbox-size.c
src/lib-storage/mail-storage-private.h
src/lib-storage/mail-storage.c

index 948c003c168fd4493d384520f4bdf886fea4f18a..8bf00e7390b1e6b6cd61328e78418c8dc5ce3906 100644 (file)
@@ -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 @@ index_mailbox_vsize_update_init(struct mailbox *box)
        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 @@ static bool vsize_update_lock_full(struct mailbox_vsize_update *update,
        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. */
index 3eea8feec2f9c1c84835ae8b39c30e4a588694d0..c0abd5a564164e579ce17f095c0171978f89587c 100644 (file)
@@ -780,6 +780,13 @@ bool mailbox_is_autocreated(struct mailbox *box);
 /* 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);
index b53771875ba0ff9740c423b04c1cac46a420cf1f..1130e1f944291a721b36004b2efd8ed5aefcf854 100644 (file)
@@ -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"
@@ -2767,3 +2768,32 @@ void mail_set_mail_cache_corrupted(struct mail *mail, const char *fmt, ...)
 
        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;
+}