trash-plugin.c revision b76aa19f9f0bd097a500e95b238f15fb3398488d
/* 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
#define TRASH_USER_CONTEXT(obj) \
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;
};
struct trash_user {
/* ordered by priority, highest first */
};
const char *trash_plugin_version = PACKAGE_VERSION;
static void (*trash_next_hook_mail_namespaces_created)
(struct mail_namespace *namespaces);
static int (*trash_next_quota_test_alloc)(struct quota_transaction_context *,
uoff_t, bool *);
{
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 *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;
}
struct trash_mailbox *trash)
{
struct mail_namespace *ns;
const char *name;
return TRUE;
}
}
return FALSE;
}
{
}
{
struct trash_mailbox *trash;
unsigned int count;
if (fd == -1) {
return -1;
}
/* <priority> <mailbox name> */
continue;
i_error("trash: Namespace not found for mailbox '%s'",
ret = -1;
}
i_info("trash plugin: Added '%s' with priority %d",
}
}
return ret;
}
static void
{
struct trash_user *tuser;
const char *env;
i_info("trash plugin: No trash setting, disabled");
return;
}
i_error("trash plugin: quota plugin not initialized");
return;
}
}
}
void trash_plugin_init(void)
{
}
void trash_plugin_deinit(void)
{
}