trash-plugin.c revision 1c2116414886236feddb27fc41342478311782ff
/* Copyright (c) 2005-2008 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "array.h"
#include "istream.h"
#include "mail-namespace.h"
#include "mail-search-build.h"
#include "quota-private.h"
#include "quota-plugin.h"
#include "trash-plugin.h"
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#define INIT_TRASH_MAILBOX_COUNT 4
#define MAX_RETRY_COUNT 3
struct trash_mailbox {
const char *name;
int priority; /* lower number = higher priority */
struct mail_storage *storage;
/* temporarily set while cleaning: */
struct mailbox_transaction_context *trans;
struct mail_search_context *search_ctx;
unsigned int mail_set:1;
};
const char *trash_plugin_version = PACKAGE_VERSION;
static int (*trash_next_quota_test_alloc)(struct quota_transaction_context *,
uoff_t, bool *);
static pool_t config_pool;
/* trash_boxes ordered by priority, highest first */
{
struct mail_search_args *search_args;
return 0;
return -1;
search_args, NULL);
}
{
int ret;
else
if (ret <= 0) {
*received_time_r = 0;
return ret;
}
}
return -1;
return 1;
}
struct trash_mailbox *trash)
{
struct mail_storage *const *storages;
unsigned int i, count;
const char *name;
for (i = 0; i < count; i++) {
return;
}
}
}
{
struct trash_mailbox *trashes;
unsigned int i, j, count, oldest_idx;
int ret = 0;
for (i = 0; i < count; ) {
/* expunge oldest mails first in all trash boxes with
same priority */
oldest_idx = count;
for (j = i; j < count; j++) {
break;
&received);
if (ret < 0)
goto err;
if (ret > 0) {
oldest_idx = j;
}
}
}
if (oldest_idx < count) {
&size) < 0) {
/* maybe expunged already? */
continue;
}
size_expunged += size;
if (size_expunged >= size_needed)
break;
} else {
/* find more mails from next priority's mailbox */
i = j;
}
}
err:
for (i = 0; i < count; i++) {
continue;
if (size_expunged >= size_needed)
else {
/* couldn't get enough space, don't expunge anything */
}
}
if (size_expunged < size_needed) {
i_info("trash plugin: Failed to remove enough messages "
"(needed %llu bytes, expunged only %llu bytes)",
(unsigned long long)size_needed,
(unsigned long long)size_expunged);
}
return FALSE;
}
return TRUE;
}
static int
{
int ret, i;
for (i = 0; ; i++) {
if (ret != 0 || *too_large_r) {
i_info("trash plugin: Mail is larger than "
"quota, won't even try to handle");
}
return ret;
}
if (i == MAX_RETRY_COUNT) {
/* trash_try_clean_mails() should have returned 0 if
it couldn't get enough space, but allow retrying
it a couple of times if there was some extra space
that was needed.. */
break;
}
/* not enough space. try deleting some from mailbox. */
if (ret <= 0)
return 0;
}
return 0;
}
{
}
static int read_configuration(const char *path)
{
struct trash_mailbox *trash;
unsigned int count;
int fd;
if (fd == -1) {
return -1;
}
/* <priority> <mailbox name> */
continue;
i_info("trash plugin: Added '%s' with priority %d",
}
}
return 0;
}
void trash_plugin_init(void)
{
const char *env;
i_info("trash plugin: No trash setting, disabled");
return;
}
i_error("trash plugin: quota plugin not initialized");
return;
}
sizeof(trash_boxes) +
(sizeof(struct trash_mailbox) + 32));
if (read_configuration(env) < 0)
return;
}
void trash_plugin_deinit(void)
{
if (config_pool != NULL)
}