From: Timo Sirainen Date: Wed, 21 Jun 2017 23:19:18 +0000 (+0300) Subject: lib-storage: Move .vsize.lock creation to a generic mailbox_lock_file_create() X-Git-Tag: 2.3.0.rc1~1358 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=bd94a2a86e27f4809d087a8269bc4ccdd5f4ede3;p=thirdparty%2Fdovecot%2Fcore.git lib-storage: Move .vsize.lock creation to a generic mailbox_lock_file_create() --- diff --git a/src/lib-storage/index/index-mailbox-size.c b/src/lib-storage/index/index-mailbox-size.c index 948c003c16..8bf00e7390 100644 --- a/src/lib-storage/index/index-mailbox-size.c +++ b/src/lib-storage/index/index-mailbox-size.c @@ -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. */ diff --git a/src/lib-storage/mail-storage-private.h b/src/lib-storage/mail-storage-private.h index 3eea8feec2..c0abd5a564 100644 --- a/src/lib-storage/mail-storage-private.h +++ b/src/lib-storage/mail-storage-private.h @@ -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); diff --git a/src/lib-storage/mail-storage.c b/src/lib-storage/mail-storage.c index b53771875b..1130e1f944 100644 --- a/src/lib-storage/mail-storage.c +++ b/src/lib-storage/mail-storage.c @@ -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; +}