Mercurial > dovecot > original-hg > dovecot-1.2
changeset 9506:09bed694c6ca HEAD
lazy-expunge: Support a single-namespace configuration.
If a mailbox is deleted, its contents are merged with the existing expunged
messages.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Mon, 14 Dec 2009 15:12:18 -0500 |
parents | 5e2206e3c750 |
children | 9af9343084d4 |
files | src/plugins/lazy-expunge/lazy-expunge-plugin.c |
diffstat | 1 files changed, 116 insertions(+), 17 deletions(-) [+] |
line wrap: on
line diff
--- a/src/plugins/lazy-expunge/lazy-expunge-plugin.c Thu Dec 10 13:28:02 2009 -0500 +++ b/src/plugins/lazy-expunge/lazy-expunge-plugin.c Mon Dec 14 15:12:18 2009 -0500 @@ -8,6 +8,7 @@ #include "mkdir-parents.h" #include "maildir-storage.h" #include "mail-namespace.h" +#include "mail-search-build.h" #include "lazy-expunge-plugin.h" #include <stdio.h> @@ -41,6 +42,7 @@ union mailbox_list_module_context module_ctx; struct mail_storage *storage; + bool deleting; }; struct lazy_expunge_mail_storage { @@ -465,6 +467,86 @@ } static int +mailbox_move_all_mails(struct mailbox_list *list, + const char *src_name, const char *dest_name) +{ + struct mail_storage *storage = list->ns->storage; + struct mailbox *src_box, *dest_box; + struct mail_search_args *search_args; + struct mailbox_transaction_context *src_trans, *dest_trans; + struct mail_search_context *search_ctx; + struct mail_save_context *save_ctx; + struct mail *mail; + struct mail_keywords *keywords; + const char *const *keywords_list; + const char *errstr; + enum mail_error error; + int ret; + + dest_box = mailbox_open(&storage, dest_name, NULL, 0); + if (dest_box == NULL) { + errstr = mail_storage_get_last_error(storage, &error); + i_error("lazy_expunge: Couldn't open DELETE dest mailbox " + "%s: %s", dest_name, errstr); + return -1; + } + + src_box = mailbox_open(&storage, src_name, NULL, + MAILBOX_OPEN_KEEP_LOCKED); + if (src_box == NULL) { + errstr = mail_storage_get_last_error(storage, &error); + if (error == MAIL_ERROR_NOTFOUND) + return 0; + i_error("lazy_expunge: Couldn't open DELETE source mailbox " + "%s: %s", src_name, errstr); + return -1; + } + + src_trans = mailbox_transaction_begin(src_box, 0); + dest_trans = mailbox_transaction_begin(dest_box, + MAILBOX_TRANSACTION_FLAG_EXTERNAL); + + search_args = mail_search_build_init(); + mail_search_build_add_all(search_args); + search_ctx = mailbox_search_init(src_trans, search_args, NULL); + mail_search_args_unref(&search_args); + + mail = mail_alloc(src_trans, 0, NULL); + while ((ret = mailbox_search_next(search_ctx, mail)) > 0) { + keywords_list = mail_get_keywords(mail); + keywords = str_array_length(keywords_list) == 0 ? NULL : + mailbox_keywords_create_valid(dest_box, keywords_list); + + save_ctx = mailbox_save_alloc(dest_trans); + mailbox_save_set_flags(save_ctx, + mail_get_flags(mail) & ~MAIL_DELETED, + keywords); + ret = mailbox_copy(&save_ctx, mail); + mailbox_keywords_free(dest_box, &keywords); + + if (ret < 0 && !mail->expunged) + break; + } + mail_free(&mail); + + if (mailbox_search_deinit(&search_ctx) < 0) + ret = -1; + + (void)mailbox_transaction_commit(&src_trans); + if (ret == 0) + ret = mailbox_transaction_commit(&dest_trans); + else + mailbox_transaction_rollback(&dest_trans); + + mailbox_close(&src_box); + mailbox_close(&dest_box); + + if (ret == 0) + ret = mailbox_list_delete_mailbox(list, src_name); + return ret; +} + +static int lazy_expunge_mailbox_list_delete(struct mailbox_list *list, const char *name) { struct lazy_expunge_mail_user *luser = @@ -472,14 +554,14 @@ struct lazy_expunge_mailbox_list *llist = LAZY_EXPUNGE_LIST_CONTEXT(list); struct lazy_expunge_mail_storage *lstorage; - struct mailbox_list *dest_list; + struct mailbox_list *dest_list, *expunge_list; enum mailbox_name_status status; const char *destname; struct tm *tm; char timestamp[256]; int ret; - if (llist->storage == NULL) { + if (llist->storage == NULL || llist->deleting) { /* not a maildir storage */ return llist->module_ctx.super.delete_mailbox(list, name); } @@ -503,14 +585,25 @@ return -1; } - /* destination mailbox name needs to contain a timestamp */ - tm = localtime(&ioloop_time); - if (strftime(timestamp, sizeof(timestamp), "%Y%m%d-%H%M%S", tm) == 0) - i_strocpy(timestamp, dec2str(ioloop_time), sizeof(timestamp)); - destname = t_strconcat(name, "-", timestamp, NULL); + expunge_list = luser->lazy_ns[LAZY_NAMESPACE_EXPUNGE]->storage->list; + dest_list = luser->lazy_ns[LAZY_NAMESPACE_DELETE]->storage->list; + + if (expunge_list == dest_list) { + /* if there are no expunged messages in this mailbox, + we can simply rename the mailbox to the destination name */ + destname = name; + } else { + /* destination mailbox name needs to contain a timestamp */ + tm = localtime(&ioloop_time); + if (strftime(timestamp, sizeof(timestamp), + "%Y%m%d-%H%M%S", tm) == 0) { + i_strocpy(timestamp, dec2str(ioloop_time), + sizeof(timestamp)); + } + destname = t_strconcat(name, "-", timestamp, NULL); + } /* first move the actual mailbox */ - dest_list = luser->lazy_ns[LAZY_NAMESPACE_DELETE]->storage->list; if ((ret = mailbox_move(list, name, dest_list, &destname)) < 0) return -1; if (ret == 0) { @@ -519,11 +612,16 @@ return -1; } + if (expunge_list == dest_list && strcmp(destname, name) != 0) { + llist->deleting = TRUE; + (void)mailbox_move_all_mails(dest_list, destname, name); + llist->deleting = FALSE; + } + /* next move the expunged messages mailbox, if it exists */ - list = luser->lazy_ns[LAZY_NAMESPACE_EXPUNGE]->storage->list; - dest_list = - luser->lazy_ns[LAZY_NAMESPACE_DELETE_EXPUNGE]->storage->list; - (void)mailbox_move(list, name, dest_list, &destname); + dest_list = luser->lazy_ns[LAZY_NAMESPACE_DELETE_EXPUNGE]->storage->list; + if (expunge_list != dest_list) + (void)mailbox_move(expunge_list, name, dest_list, &destname); return 0; } @@ -538,7 +636,7 @@ /* if this is one of our internal storages, mark it as such before quota plugin sees it */ p = t_strsplit_spaces(getenv("LAZY_EXPUNGE"), " "); - for (i = 0; i < LAZY_NAMESPACE_COUNT; i++, p++) { + for (i = 0; i < LAZY_NAMESPACE_COUNT && *p != NULL; i++, p++) { if (strcmp(storage->ns->prefix, *p) == 0) { storage->ns->flags |= NAMESPACE_FLAG_NOQUOTA; break; @@ -588,12 +686,9 @@ int i; p = t_strsplit_spaces(getenv("LAZY_EXPUNGE"), " "); - for (i = 0; i < LAZY_NAMESPACE_COUNT; i++, p++) { + for (i = 0; i < LAZY_NAMESPACE_COUNT && *p != NULL; i++, p++) { const char *name = *p; - if (name == NULL) - i_fatal("lazy_expunge: Missing namespace #%d", i + 1); - luser->lazy_ns[i] = mail_namespace_find_prefix(namespaces, name); if (luser->lazy_ns[i] == NULL) @@ -608,6 +703,10 @@ lstorage = LAZY_EXPUNGE_CONTEXT(luser->lazy_ns[i]->storage); lstorage->internal_namespace = TRUE; } + if (i == 0) + i_fatal("lazy_expunge: No namespaces defined"); + for (; i < LAZY_NAMESPACE_COUNT; i++) + luser->lazy_ns[i] = luser->lazy_ns[i-1]; if (lazy_expunge_next_hook_mail_namespaces_created != NULL) lazy_expunge_next_hook_mail_namespaces_created(namespaces);