bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen#include "lib.h"
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen#include "array.h"
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen#include "ioloop.h"
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen#include "mkdir-parents.h"
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen#include "master-service.h"
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen#include "mail-index-modseq.h"
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen#include "mail-index-alloc-cache.h"
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen#include "mailbox-log.h"
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen#include "mailbox-list-private.h"
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen#include "mail-copy.h"
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen#include "dbox-mail.h"
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen#include "dbox-save.h"
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen#include "mdbox-map.h"
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen#include "mdbox-file.h"
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen#include "mdbox-sync.h"
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen#include "mdbox-storage-rebuild.h"
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen#include "mdbox-storage.h"
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainenextern struct mail_storage mdbox_deleted_storage;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainenextern struct mailbox mdbox_deleted_mailbox;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainenextern struct dbox_storage_vfuncs mdbox_deleted_dbox_storage_vfuncs;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainenstatic struct mail_storage *mdbox_deleted_storage_alloc(void)
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen{
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen struct mdbox_storage *storage;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen pool_t pool;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen pool = pool_alloconly_create("mdbox deleted storage", 2048);
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen storage = p_new(pool, struct mdbox_storage, 1);
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen storage->storage.v = mdbox_dbox_storage_vfuncs;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen storage->storage.storage = mdbox_deleted_storage;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen storage->storage.storage.pool = pool;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen return &storage->storage.storage;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen}
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainenstatic struct mailbox *
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainenmdbox_deleted_mailbox_alloc(struct mail_storage *storage,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen struct mailbox_list *list,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen const char *vname, enum mailbox_flags flags)
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen{
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen struct mdbox_mailbox *mbox;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen pool_t pool;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen flags |= MAILBOX_FLAG_READONLY | MAILBOX_FLAG_NO_INDEX_FILES;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen pool = pool_alloconly_create("mdbox deleted mailbox", 1024*3);
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen mbox = p_new(pool, struct mdbox_mailbox, 1);
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen mbox->box = mdbox_deleted_mailbox;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen mbox->box.pool = pool;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen mbox->box.storage = storage;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen mbox->box.list = list;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen mbox->box.mail_vfuncs = &mdbox_mail_vfuncs;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen index_storage_mailbox_alloc(&mbox->box, vname, flags, MAIL_INDEX_PREFIX);
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen
b67974c4b89ab6950c2694cce8dfb1b6561cc084Josef 'Jeff' Sipek mbox->storage = MDBOX_STORAGE(storage);
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen return &mbox->box;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen}
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainenstatic int
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainenmdbox_deleted_mailbox_create_indexes(struct mailbox *box,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen const struct mailbox_update *update,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen struct mail_index_transaction *trans)
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen{
b67974c4b89ab6950c2694cce8dfb1b6561cc084Josef 'Jeff' Sipek struct mdbox_mailbox *mbox = MDBOX_MAILBOX(box);
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen struct mail_index_transaction *new_trans = NULL;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen uint32_t uid_validity = ioloop_time;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen uint32_t uid_next = 1;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen if (update != NULL && update->uid_validity != 0)
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen uid_validity = update->uid_validity;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen if (trans == NULL) {
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen new_trans = mail_index_transaction_begin(box->view, 0);
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen trans = new_trans;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen }
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen mail_index_update_header(trans,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen offsetof(struct mail_index_header, uid_validity),
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen &uid_validity, sizeof(uid_validity), TRUE);
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen mail_index_update_header(trans,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen offsetof(struct mail_index_header, next_uid),
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen &uid_next, sizeof(uid_next), TRUE);
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen mbox->creating = TRUE;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen mdbox_update_header(mbox, trans, update);
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen mbox->creating = FALSE;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen if (new_trans != NULL) {
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen if (mail_index_transaction_commit(&new_trans) < 0) {
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen mailbox_set_index_error(box);
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen return -1;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen }
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen }
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen return 0;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen}
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainenstatic const char *
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainenmdbox_get_attachment_path_suffix(struct dbox_file *file ATTR_UNUSED)
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen{
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen return "";
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen}
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainenstatic int
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainenmdbox_deleted_mailbox_get_metadata(struct mailbox *box,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen enum mailbox_metadata_items items,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen struct mailbox_metadata *metadata_r)
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen{
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen if (index_mailbox_get_metadata(box, items, metadata_r) < 0)
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen return -1;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen if ((items & MAILBOX_METADATA_GUID) != 0)
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen guid_128_generate(metadata_r->guid);
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen return 0;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen}
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainenstatic struct mail_save_context *
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainenmdbox_deleted_save_alloc(struct mailbox_transaction_context *t)
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen{
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen struct mail_save_context *ctx;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen ctx = i_new(struct mail_save_context, 1);
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen ctx->transaction = t;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen return ctx;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen}
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainenstatic int
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainenmdbox_deleted_save_begin(struct mail_save_context *ctx,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen struct istream *input ATTR_UNUSED)
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen{
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen mail_storage_set_error(ctx->transaction->box->storage,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen MAIL_ERROR_NOTPOSSIBLE, "mdbox_deleted doesn't support saving mails");
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen return -1;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen}
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainenstatic int
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainenmdbox_deleted_save_continue(struct mail_save_context *ctx ATTR_UNUSED)
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen{
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen return -1;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen}
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainenstatic int mdbox_deleted_save_finish(struct mail_save_context *ctx)
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen{
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen index_save_context_free(ctx);
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen return -1;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen}
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainenstatic void
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainenmdbox_deleted_save_cancel(struct mail_save_context *ctx)
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen{
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen index_save_context_free(ctx);
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen}
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainenstatic int mdbox_deleted_sync(struct mdbox_mailbox *mbox,
0e94016c18197cb42d00be0085e6da4223a1c84eTimo Sirainen enum mdbox_sync_flags flags ATTR_UNUSED)
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen{
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen struct mail_index_sync_ctx *index_sync_ctx;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen struct mail_index_view *sync_view;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen struct mail_index_transaction *trans;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen struct mdbox_mail_index_record rec;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen struct mdbox_map_mail_index_record map_rec;
0e94016c18197cb42d00be0085e6da4223a1c84eTimo Sirainen enum mail_index_sync_flags sync_flags;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen uint16_t refcount;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen uint32_t map_seq, map_count, seq, uid = 0;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen int ret = 0;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen if (mbox->mdbox_deleted_synced) {
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen /* don't bother supporting incremental syncs */
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen return 0;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen }
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen if (!mbox->box.inbox_user && mbox->box.name[0] != '\0') {
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen /* since mailbox list currently shows all the existing
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen mailboxes, we don't want all of them to list the deleted
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen messages. only show messages in user's INBOX or the
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen namespace prefix. */
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen return 0;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen }
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen if (mdbox_map_open(mbox->storage->map) < 0)
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen return -1;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen if (mdbox_deleted_mailbox_create_indexes(&mbox->box, NULL, NULL) < 0)
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen return -1;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen
efe78d3ba24fc866af1c79b9223dc0809ba26cadStephan Bosch i_zero(&rec);
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen rec.save_date = ioloop_time;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen
0e94016c18197cb42d00be0085e6da4223a1c84eTimo Sirainen sync_flags = index_storage_get_sync_flags(&mbox->box);
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen if (mail_index_sync_begin(mbox->box.index, &index_sync_ctx,
0e94016c18197cb42d00be0085e6da4223a1c84eTimo Sirainen &sync_view, &trans, sync_flags) < 0) {
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen mailbox_set_index_error(&mbox->box);
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen return -1;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen }
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen map_count = mdbox_map_get_messages_count(mbox->storage->map);
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen for (map_seq = 1; map_seq <= map_count; map_seq++) {
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen if (mdbox_map_lookup_seq_full(mbox->storage->map, map_seq,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen &map_rec, &refcount) < 0) {
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen ret = -1;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen break;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen }
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen if (refcount == 0) {
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen rec.map_uid = mdbox_map_lookup_uid(mbox->storage->map,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen map_seq);
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen mail_index_append(trans, ++uid, &seq);
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen mail_index_update_ext(trans, seq,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen mbox->ext_id, &rec, NULL);
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen }
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen }
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen if (ret < 0)
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen mail_index_sync_rollback(&index_sync_ctx);
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen else {
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen if (mail_index_sync_commit(&index_sync_ctx) < 0) {
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen mailbox_set_index_error(&mbox->box);
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen ret = -1;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen } else {
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen mbox->mdbox_deleted_synced = TRUE;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen }
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen }
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen return ret;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen}
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainenstatic struct mailbox_sync_context *
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainenmdbox_deleted_storage_sync_init(struct mailbox *box,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen enum mailbox_sync_flags flags)
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen{
b67974c4b89ab6950c2694cce8dfb1b6561cc084Josef 'Jeff' Sipek struct mdbox_mailbox *mbox = MDBOX_MAILBOX(box);
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen enum mdbox_sync_flags mdbox_sync_flags = 0;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen int ret = 0;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen
e098ac1cd4ff0afb33b22cc7fd288014f4fdb0bcTimo Sirainen if (index_mailbox_want_full_sync(&mbox->box, flags) ||
e098ac1cd4ff0afb33b22cc7fd288014f4fdb0bcTimo Sirainen mbox->storage->corrupted)
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen ret = mdbox_deleted_sync(mbox, mdbox_sync_flags);
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen return index_mailbox_sync_init(box, flags, ret < 0);
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen}
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainenstruct mail_storage mdbox_deleted_storage = {
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen .name = MDBOX_DELETED_STORAGE_NAME,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen .class_flags = MAIL_STORAGE_CLASS_FLAG_UNIQUE_ROOT |
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen MAIL_STORAGE_CLASS_FLAG_HAVE_MAIL_GUIDS |
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen MAIL_STORAGE_CLASS_FLAG_HAVE_MAIL_SAVE_GUIDS |
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen MAIL_STORAGE_CLASS_FLAG_BINARY_DATA,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen .v = {
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen mdbox_get_setting_parser_info,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen mdbox_deleted_storage_alloc,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen mdbox_storage_create,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen mdbox_storage_destroy,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen NULL,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen dbox_storage_get_list_settings,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen NULL,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen mdbox_deleted_mailbox_alloc,
857c471c13ca215f4be9dd4b336b742b8d434e31Timo Sirainen NULL,
857c471c13ca215f4be9dd4b336b742b8d434e31Timo Sirainen NULL,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen }
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen};
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainenstruct mailbox mdbox_deleted_mailbox = {
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen .v = {
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen index_storage_is_readonly,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen index_storage_mailbox_enable,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen index_storage_mailbox_exists,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen mdbox_mailbox_open,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen index_storage_mailbox_close,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen index_storage_mailbox_free,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen dbox_mailbox_create,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen index_storage_mailbox_update,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen index_storage_mailbox_delete,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen index_storage_mailbox_rename,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen index_storage_get_status,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen mdbox_deleted_mailbox_get_metadata,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen index_storage_set_subscribed,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen index_storage_attribute_set,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen index_storage_attribute_get,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen index_storage_attribute_iter_init,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen index_storage_attribute_iter_next,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen index_storage_attribute_iter_deinit,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen index_storage_list_index_has_changed,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen index_storage_list_index_update_sync,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen mdbox_deleted_storage_sync_init,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen index_mailbox_sync_next,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen index_mailbox_sync_deinit,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen NULL,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen dbox_notify_changes,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen index_transaction_begin,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen index_transaction_commit,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen index_transaction_rollback,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen NULL,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen dbox_mail_alloc,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen index_storage_search_init,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen index_storage_search_deinit,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen index_storage_search_next_nonblock,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen index_storage_search_next_update_seq,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen mdbox_deleted_save_alloc,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen mdbox_deleted_save_begin,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen mdbox_deleted_save_continue,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen mdbox_deleted_save_finish,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen mdbox_deleted_save_cancel,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen mail_storage_copy,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen NULL,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen NULL,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen NULL,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen index_storage_is_inconsistent
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen }
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen};
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainenstruct dbox_storage_vfuncs mdbox_deleted_dbox_storage_vfuncs = {
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen mdbox_file_unrefed,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen mdbox_file_create_fd,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen mdbox_mail_open,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen mdbox_deleted_mailbox_create_indexes,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen mdbox_get_attachment_path_suffix,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen mdbox_set_mailbox_corrupted,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen mdbox_set_file_corrupted
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen};