trash-plugin.c revision 326fb016a23480e4ff8dcc03dc80e76812859bd6
f76545cf39fa3d24a185f56216eb7c1edb9e1e9eSteven Haehn/* Copyright (c) 2005-2017 Dovecot authors, see the included COPYING file */
f76545cf39fa3d24a185f56216eb7c1edb9e1e9eSteven Haehn const char *name;
f76545cf39fa3d24a185f56216eb7c1edb9e1e9eSteven Haehn int priority; /* lower number = higher priority */
f76545cf39fa3d24a185f56216eb7c1edb9e1e9eSteven Haehn /* temporarily set while cleaning: */
6336b638e9afd018de5f6c516eac4775d140fdaeJHKST /* ordered by priority, highest first */
f76545cf39fa3d24a185f56216eb7c1edb9e1e9eSteven Haehnconst char *trash_plugin_version = DOVECOT_ABI_VERSION;
f76545cf39fa3d24a185f56216eb7c1edb9e1e9eSteven Haehnstatic MODULE_CONTEXT_DEFINE_INIT(trash_user_module,
f76545cf39fa3d24a185f56216eb7c1edb9e1e9eSteven Haehnstatic int (*trash_next_quota_test_alloc)(struct quota_transaction_context *,
6336b638e9afd018de5f6c516eac4775d140fdaeJHKSTstatic int trash_clean_mailbox_open(struct trash_mailbox *trash)
f76545cf39fa3d24a185f56216eb7c1edb9e1e9eSteven Haehn trash->box = mailbox_alloc(trash->ns->list, trash->name, 0);
f76545cf39fa3d24a185f56216eb7c1edb9e1e9eSteven Haehn if (mailbox_sync(trash->box, MAILBOX_SYNC_FLAG_FULL_READ) < 0)
5fa2dfe6c5b3512e4b4181ec7a11d400a4837ed0Trond Norbye trash->trans = mailbox_transaction_begin(trash->box, 0);
f76545cf39fa3d24a185f56216eb7c1edb9e1e9eSteven Haehn trash->search_ctx = mailbox_search_init(trash->trans,
3ba66fbb56ef22f183da783a1b2718280c357a4eStanislav Kozina return mailbox_search_next(trash->search_ctx, &trash->mail) ? 1 : 0;
5fa2dfe6c5b3512e4b4181ec7a11d400a4837ed0Trond Norbyestatic int trash_clean_mailbox_get_next(struct trash_mailbox *trash,
3ba66fbb56ef22f183da783a1b2718280c357a4eStanislav Kozina if (mail_get_received_date(trash->mail, received_time_r) < 0)
f76545cf39fa3d24a185f56216eb7c1edb9e1e9eSteven Haehnstatic int trash_try_clean_mails(struct quota_transaction_context *ctx,
5fa2dfe6c5b3512e4b4181ec7a11d400a4837ed0Trond Norbye struct trash_user *tuser = TRASH_USER_CONTEXT(ctx->quota->user);
f76545cf39fa3d24a185f56216eb7c1edb9e1e9eSteven Haehn unsigned int expunged_count = 0;
5fa2dfe6c5b3512e4b4181ec7a11d400a4837ed0Trond Norbye trashes = array_get_modifiable(&tuser->trash_boxes, &count);
5fa2dfe6c5b3512e4b4181ec7a11d400a4837ed0Trond Norbye for (i = 0; i < count; ) {
f76545cf39fa3d24a185f56216eb7c1edb9e1e9eSteven Haehn /* expunge oldest mails first in all trash boxes with
f76545cf39fa3d24a185f56216eb7c1edb9e1e9eSteven Haehn same priority */
f76545cf39fa3d24a185f56216eb7c1edb9e1e9eSteven Haehn for (j = i; j < count; j++) {
f76545cf39fa3d24a185f56216eb7c1edb9e1e9eSteven Haehn if (trashes[j].priority != trashes[i].priority)
5fa2dfe6c5b3512e4b4181ec7a11d400a4837ed0Trond Norbye ret = trash_clean_mailbox_get_next(&trashes[j],
5fa2dfe6c5b3512e4b4181ec7a11d400a4837ed0Trond Norbye if (oldest == (time_t)-1 || received < oldest) {
5fa2dfe6c5b3512e4b4181ec7a11d400a4837ed0Trond Norbye if (mail_get_physical_size(trashes[oldest_idx].mail,
5fa2dfe6c5b3512e4b4181ec7a11d400a4837ed0Trond Norbye /* maybe expunged already? */
5fa2dfe6c5b3512e4b4181ec7a11d400a4837ed0Trond Norbye /* find more mails from next priority's mailbox */
5fa2dfe6c5b3512e4b4181ec7a11d400a4837ed0Trond Norbye for (i = 0; i < count; i++) {
5fa2dfe6c5b3512e4b4181ec7a11d400a4837ed0Trond Norbye (void)mailbox_transaction_commit(&trash->trans);
5fa2dfe6c5b3512e4b4181ec7a11d400a4837ed0Trond Norbye /* couldn't get enough space, don't expunge anything */
5fa2dfe6c5b3512e4b4181ec7a11d400a4837ed0Trond Norbye i_debug("trash plugin: Failed to remove enough messages "
f76545cf39fa3d24a185f56216eb7c1edb9e1e9eSteven Haehn "(needed %llu bytes, expunged only %llu bytes)",
f76545cf39fa3d24a185f56216eb7c1edb9e1e9eSteven Haehn (unsigned long long)size_needed,
(unsigned long long)size_expunged);
int ret, i;
unsigned int count_needed = 0;
return ret;
if (i == MAX_RETRY_COUNT) {
if (ret <= 0)
return FALSE;
return TRUE;
return ret;
const char *env;
void trash_plugin_deinit(void)