Skip to content

Commit

Permalink
lib-storage: Move .vsize.lock creation to a generic mailbox_lock_file…
Browse files Browse the repository at this point in the history
…_create()
  • Loading branch information
sirainen authored and GitLab committed Jun 28, 2017
1 parent 2864ce8 commit bd94a2a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 33 deletions.
35 changes: 2 additions & 33 deletions src/lib-storage/index/index-mailbox-size.c
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
{
Expand All @@ -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. */
Expand Down
7 changes: 7 additions & 0 deletions src/lib-storage/mail-storage-private.h
Expand Up @@ -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);
Expand Down
30 changes: 30 additions & 0 deletions src/lib-storage/mail-storage.c
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
}

0 comments on commit bd94a2a

Please sign in to comment.