mail-autoexpunge.c revision 6c51e3c3dc8dc5dc6fef2280b7c2c9ebef3de8f0
2454dfa32c93c20a8522c6ed42fe057baaac9f9aStephan Bosch/* Copyright (c) 2015-2017 Dovecot authors, see the included COPYING file */
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen#define AUTOEXPUNGE_LOCK_FNAME "dovecot.autoexpunge.lock"
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainenstatic bool mailbox_autoexpunge_lock(struct mail_user *user,
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen /* Try to lock the autoexpunging. If the lock already exists, another
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen process is already busy with expunging, so we don't have to do it.
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen The easiest place where to store the lock file to is the home
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen directory, but allow autoexpunging to work even if we can't get
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen it. The lock isn't really required; it 1) improves performance
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen so that multiple processes won't do the same work unnecessarily,
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen and 2) it helps to avoid duplicate mails being added with
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen lazy_expunge. */
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen if ((ret = mail_user_get_home(user, &home)) > 0) {
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen const struct mail_storage_settings *mail_set =
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen lock_set.lock_method = mail_set->parsed_lock_method,
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen lock->path = t_strdup_printf("%s/"AUTOEXPUNGE_LOCK_FNAME, home);
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen lock->fd = file_create_locked(lock->path, &lock_set,
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen i_error("autoexpunge: Couldn't lock %s: %s", lock->path, error);
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen } else if (ret == 0) {
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen i_warning("autoexpunge: User has no home directory, can't lock");
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainenmailbox_autoexpunge(struct mailbox *box, unsigned int interval_time,
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen unsigned int max_mails, unsigned int *expunged_count)
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen time_t timestamp, expire_time, last_rename_stamp = 0;
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen unsigned int count = 0;
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen if ((unsigned int)ioloop_time < interval_time)
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen /* first try to check quickly from mailbox list index if we should
2974dca6be5120e49279f06c8aa952e5fac56048Timo Sirainen bother opening this mailbox. */
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen if (mailbox_get_status(box, STATUS_MESSAGES, &status) < 0) {
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen if (mailbox_get_last_mail_error(box) == MAIL_ERROR_NOTFOUND) {
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen /* autocreated mailbox doesn't exist yet */
&metadata) < 0)
count++;
} else if (interval_time == 0) {
count++;
if (mailbox_transaction_commit(&t) < 0)
return ret;
unsigned int autoexpunge,
unsigned int autoexpunge_max_mails,
unsigned int *expunged_count)
expunged_count) < 0) {
unsigned int *expunged_count)
const char *iter_name;
} T_END;
unsigned int *expunged_count)
const char *vname;
return TRUE;
return FALSE;
return TRUE;
unsigned int expunged_count = 0;
return expunged_count;