virtual-sync.c revision 1d2b188f0eedc3cab6e27ceac5425a037f38042e
45312f52ff3a3d4c137447be4c7556500c2f8bf2Timo Sirainen/* Copyright (c) 2008-2009 Dovecot authors, see the included COPYING file */
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include "lib.h"
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include "array.h"
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen#include "bsearch-insert-pos.h"
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include "ioloop.h"
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include "str.h"
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen#include "mail-index-modseq.h"
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include "mail-search-build.h"
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen#include "mailbox-search-result-private.h"
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen#include "index-sync-private.h"
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen#include "index-search-result.h"
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include "virtual-storage.h"
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include <stdlib.h>
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainenstruct virtual_add_record {
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen struct virtual_mail_index_record rec;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen time_t received_date;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen};
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstruct virtual_sync_mail {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen uint32_t vseq;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct virtual_mail_index_record vrec;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen};
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstruct virtual_sync_context {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct virtual_mailbox *mbox;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct mail_index_sync_ctx *index_sync_ctx;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct mail_index *index;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct mail_index_view *sync_view;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct mail_index_transaction *trans;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen const char *const *kw_all;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen /* messages expunged within this sync */
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen ARRAY_TYPE(seq_range) sync_expunges;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen ARRAY_DEFINE(all_adds, struct virtual_add_record);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen enum mailbox_sync_flags flags;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen uint32_t uid_validity;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen unsigned int ext_header_changed:1;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen unsigned int ext_header_rewrite:1;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen unsigned int expunge_removed:1;
939a0d82523538b2de38a02bc9f790a67b7ebf47Timo Sirainen unsigned int index_broken:1;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen};
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstatic void virtual_sync_set_uidvalidity(struct virtual_sync_context *ctx)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen{
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen uint32_t uid_validity = ioloop_time;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen mail_index_update_header(ctx->trans,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen offsetof(struct mail_index_header, uid_validity),
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen &uid_validity, sizeof(uid_validity), TRUE);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ctx->uid_validity = uid_validity;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen}
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstatic void virtual_sync_external_flags(struct virtual_sync_context *ctx,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct virtual_backend_box *bbox,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen uint32_t vseq, uint32_t real_uid)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen{
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen enum mail_flags flags;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen const char *const *kw_names;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct mail_keywords *keywords;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen if (!mail_set_uid(bbox->sync_mail, real_uid)) {
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen i_panic("UID %u lost unexpectedly from %s",
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen real_uid, bbox->box->name);
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen }
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen /* copy flags */
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen flags = mail_get_flags(bbox->sync_mail);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen mail_index_update_flags(ctx->trans, vseq, MODIFY_REPLACE, flags);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen /* copy keywords */
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen kw_names = mail_get_keywords(bbox->sync_mail);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen keywords = mail_index_keywords_create(ctx->index, kw_names);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen mail_index_update_keywords(ctx->trans, vseq, MODIFY_REPLACE, keywords);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen mail_index_keywords_free(&keywords);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen}
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstatic int virtual_sync_mail_cmp(const void *p1, const void *p2)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen{
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen const struct virtual_sync_mail *m1 = p1, *m2 = p2;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (m1->vrec.mailbox_id < m2->vrec.mailbox_id)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen return -1;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (m1->vrec.mailbox_id > m2->vrec.mailbox_id)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen return 1;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (m1->vrec.real_uid < m2->vrec.real_uid)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen return -1;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (m1->vrec.real_uid > m2->vrec.real_uid)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen return 1;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen /* broken */
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen return 0;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen}
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainenstatic void
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainenvirtual_backend_box_sync_mail_set(struct virtual_backend_box *bbox)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen{
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen struct mailbox_transaction_context *trans;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen if (bbox->sync_mail == NULL) {
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen trans = mailbox_transaction_begin(bbox->box, 0);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen bbox->sync_mail = mail_alloc(trans, 0, NULL);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen }
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen}
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainenstatic void
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainenvirtual_backend_box_sync_mail_unset(struct virtual_backend_box *bbox)
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen{
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen struct mailbox_transaction_context *trans;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen if (bbox->sync_mail != NULL) {
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen trans = bbox->sync_mail->transaction;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen mail_free(&bbox->sync_mail);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen (void)mailbox_transaction_commit(&trans);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen }
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen}
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainenstatic int bbox_mailbox_id_cmp(const void *p1, const void *p2)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen{
7a24bdc1a5e2d5368c2569b4852192f2bdb5a31fTimo Sirainen const struct virtual_backend_box *const *b1 = p1, *const *b2 = p2;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
7a24bdc1a5e2d5368c2569b4852192f2bdb5a31fTimo Sirainen if ((*b1)->mailbox_id < (*b2)->mailbox_id)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen return -1;
7a24bdc1a5e2d5368c2569b4852192f2bdb5a31fTimo Sirainen if ((*b1)->mailbox_id > (*b2)->mailbox_id)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen return 1;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen return 0;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen}
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainenstatic bool virtual_sync_ext_header_read(struct virtual_sync_context *ctx)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen{
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen const struct virtual_mail_index_header *ext_hdr;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen const struct mail_index_header *hdr;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen const struct virtual_mail_index_mailbox_record *mailboxes;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen struct virtual_backend_box *bbox, **bboxes;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen const void *ext_data;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen size_t ext_size;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen unsigned int i, count, ext_name_offset, ext_mailbox_count;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen uint32_t prev_mailbox_id;
939a0d82523538b2de38a02bc9f790a67b7ebf47Timo Sirainen bool ret = TRUE;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen hdr = mail_index_get_header(ctx->sync_view);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen mail_index_get_header_ext(ctx->sync_view, ctx->mbox->virtual_ext_id,
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen &ext_data, &ext_size);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ext_hdr = ext_data;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (ctx->mbox->sync_initialized &&
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ctx->mbox->prev_uid_validity == hdr->uid_validity &&
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ext_size >= sizeof(*ext_hdr) &&
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ctx->mbox->prev_change_counter == ext_hdr->change_counter) {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen /* fully refreshed */
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen return TRUE;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen }
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ctx->mbox->sync_initialized = TRUE;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ctx->mbox->prev_uid_validity = hdr->uid_validity;
7e209b78ca757294dbbc15604c88673b3a6b0c39Timo Sirainen if (ext_hdr == NULL ||
7e209b78ca757294dbbc15604c88673b3a6b0c39Timo Sirainen ctx->mbox->search_args_crc32 != ext_hdr->search_args_crc32) {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen mailboxes = NULL;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ext_name_offset = 0;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ext_mailbox_count = 0;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen } else {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ctx->mbox->prev_change_counter = ext_hdr->change_counter;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen mailboxes = (const void *)(ext_hdr + 1);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ext_name_offset = sizeof(*ext_hdr) +
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ext_hdr->mailbox_count * sizeof(*mailboxes);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (ext_name_offset >= ext_size ||
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ext_hdr->mailbox_count > INT_MAX/sizeof(*mailboxes)) {
939a0d82523538b2de38a02bc9f790a67b7ebf47Timo Sirainen i_error("virtual index %s: Broken mailbox_count header",
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen ctx->mbox->ibox.box.path);
939a0d82523538b2de38a02bc9f790a67b7ebf47Timo Sirainen ctx->index_broken = TRUE;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ext_mailbox_count = 0;
939a0d82523538b2de38a02bc9f790a67b7ebf47Timo Sirainen ret = FALSE;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen } else {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ext_mailbox_count = ext_hdr->mailbox_count;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen }
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen }
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen /* update mailbox backends */
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen prev_mailbox_id = 0;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen for (i = 0; i < ext_mailbox_count; i++) {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (mailboxes[i].id > ext_hdr->highest_mailbox_id ||
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen mailboxes[i].id <= prev_mailbox_id) {
939a0d82523538b2de38a02bc9f790a67b7ebf47Timo Sirainen i_error("virtual index %s: Broken mailbox id",
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen ctx->mbox->ibox.box.path);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen break;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen }
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (mailboxes[i].name_len == 0 ||
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen mailboxes[i].name_len > ext_size) {
939a0d82523538b2de38a02bc9f790a67b7ebf47Timo Sirainen i_error("virtual index %s: Broken mailbox name_len",
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen ctx->mbox->ibox.box.path);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen break;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen }
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (ext_name_offset + mailboxes[i].name_len > ext_size) {
939a0d82523538b2de38a02bc9f790a67b7ebf47Timo Sirainen i_error("virtual index %s: Broken mailbox list",
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen ctx->mbox->ibox.box.path);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen break;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen }
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen T_BEGIN {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen const unsigned char *nameptr;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen const char *name;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen nameptr = CONST_PTR_OFFSET(ext_data, ext_name_offset);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen name = t_strndup(nameptr, mailboxes[i].name_len);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen bbox = virtual_backend_box_lookup_name(ctx->mbox, name);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen } T_END;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (bbox == NULL) {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen /* mailbox no longer exists */
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ret = FALSE;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen } else {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen bbox->mailbox_id = mailboxes[i].id;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen bbox->sync_uid_validity = mailboxes[i].uid_validity;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen bbox->sync_highest_modseq = mailboxes[i].highest_modseq;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen bbox->sync_next_uid = mailboxes[i].next_uid;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen bbox->sync_mailbox_idx = i;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen }
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ext_name_offset += mailboxes[i].name_len;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen prev_mailbox_id = mailboxes[i].id;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen }
939a0d82523538b2de38a02bc9f790a67b7ebf47Timo Sirainen if (i < ext_mailbox_count) {
939a0d82523538b2de38a02bc9f790a67b7ebf47Timo Sirainen ctx->index_broken = TRUE;
939a0d82523538b2de38a02bc9f790a67b7ebf47Timo Sirainen ret = FALSE;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen }
939a0d82523538b2de38a02bc9f790a67b7ebf47Timo Sirainen ctx->mbox->highest_mailbox_id = ext_hdr == NULL ? 0 :
939a0d82523538b2de38a02bc9f790a67b7ebf47Timo Sirainen ext_hdr->highest_mailbox_id;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen /* assign new mailbox IDs if any are missing */
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen bboxes = array_get_modifiable(&ctx->mbox->backend_boxes, &count);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen for (i = 0; i < count; i++) {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (bboxes[i]->mailbox_id == 0) {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen bboxes[i]->mailbox_id = ++ctx->mbox->highest_mailbox_id;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ret = FALSE;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen }
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen }
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen /* sort the backend mailboxes by mailbox_id. */
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen qsort(bboxes, count, sizeof(*bboxes), bbox_mailbox_id_cmp);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen return ret;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen}
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainenstatic void virtual_sync_ext_header_rewrite(struct virtual_sync_context *ctx)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen{
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen struct virtual_mail_index_header ext_hdr;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen struct virtual_mail_index_mailbox_record mailbox;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen struct virtual_backend_box **bboxes;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen buffer_t *buf;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen const void *ext_data;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen size_t ext_size;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen unsigned int i, mailbox_pos, name_pos, count;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen bboxes = array_get_modifiable(&ctx->mbox->backend_boxes, &count);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen mailbox_pos = sizeof(ext_hdr);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen name_pos = mailbox_pos + sizeof(mailbox) * count;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen memset(&ext_hdr, 0, sizeof(ext_hdr));
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen memset(&mailbox, 0, sizeof(mailbox));
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ext_hdr.change_counter = ++ctx->mbox->prev_change_counter;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ext_hdr.mailbox_count = count;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ext_hdr.highest_mailbox_id = ctx->mbox->highest_mailbox_id;
7e209b78ca757294dbbc15604c88673b3a6b0c39Timo Sirainen ext_hdr.search_args_crc32 = ctx->mbox->search_args_crc32;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen buf = buffer_create_dynamic(pool_datastack_create(), name_pos + 256);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen buffer_append(buf, &ext_hdr, sizeof(ext_hdr));
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen for (i = 0; i < count; i++) {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen i_assert(i == 0 ||
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen bboxes[i]->mailbox_id > bboxes[i-1]->mailbox_id);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen bboxes[i]->sync_mailbox_idx = i;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen mailbox.id = bboxes[i]->mailbox_id;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen mailbox.name_len = strlen(bboxes[i]->name);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen mailbox.uid_validity = bboxes[i]->sync_uid_validity;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen mailbox.highest_modseq = bboxes[i]->sync_highest_modseq;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen mailbox.next_uid = bboxes[i]->sync_next_uid;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen buffer_write(buf, mailbox_pos, &mailbox, sizeof(mailbox));
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen buffer_write(buf, name_pos, bboxes[i]->name, mailbox.name_len);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen mailbox_pos += sizeof(mailbox);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen name_pos += mailbox.name_len;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen }
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen i_assert(buf->used == name_pos);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen mail_index_get_header_ext(ctx->sync_view, ctx->mbox->virtual_ext_id,
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen &ext_data, &ext_size);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (ext_size < name_pos) {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen mail_index_ext_resize(ctx->trans, ctx->mbox->virtual_ext_id,
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen name_pos,
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen sizeof(struct virtual_mail_index_record),
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen sizeof(uint32_t));
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen }
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen mail_index_update_header_ext(ctx->trans, ctx->mbox->virtual_ext_id,
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen 0, buf->data, name_pos);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen}
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainenstatic void virtual_sync_ext_header_update(struct virtual_sync_context *ctx)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen{
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen struct virtual_mail_index_header ext_hdr;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (!ctx->ext_header_changed)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen return;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen /* we changed something - update the change counter in header */
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ext_hdr.change_counter = ++ctx->mbox->prev_change_counter;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen mail_index_update_header_ext(ctx->trans, ctx->mbox->virtual_ext_id,
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen offsetof(struct virtual_mail_index_header, change_counter),
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen &ext_hdr.change_counter, sizeof(ext_hdr.change_counter));
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen}
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstatic void virtual_sync_index_rec(struct virtual_sync_context *ctx,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen const struct mail_index_sync_rec *sync_rec)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen{
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen uint32_t virtual_ext_id = ctx->mbox->virtual_ext_id;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct virtual_backend_box *bbox;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen const struct virtual_mail_index_record *vrec;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen const void *data;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen enum mail_flags flags;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct mail_keywords *keywords;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen enum modify_type modify_type;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen const char *kw_names[2];
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen uint32_t vseq, seq1, seq2;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen bool expunged;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen switch (sync_rec->type) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen case MAIL_INDEX_SYNC_TYPE_APPEND:
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen /* don't care */
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen return;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen case MAIL_INDEX_SYNC_TYPE_EXPUNGE:
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen case MAIL_INDEX_SYNC_TYPE_FLAGS:
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen case MAIL_INDEX_SYNC_TYPE_KEYWORD_ADD:
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen case MAIL_INDEX_SYNC_TYPE_KEYWORD_REMOVE:
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen case MAIL_INDEX_SYNC_TYPE_KEYWORD_RESET:
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen break;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen }
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (!mail_index_lookup_seq_range(ctx->sync_view,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen sync_rec->uid1, sync_rec->uid2,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen &seq1, &seq2)) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen /* already expunged, nothing to do. */
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen return;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen }
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen for (vseq = seq1; vseq <= seq2; vseq++) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen mail_index_lookup_ext(ctx->sync_view, vseq, virtual_ext_id,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen &data, &expunged);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen vrec = data;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen bbox = virtual_backend_box_lookup(ctx->mbox, vrec->mailbox_id);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (bbox == NULL)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen continue;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen virtual_backend_box_sync_mail_set(bbox);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (!mail_set_uid(bbox->sync_mail, vrec->real_uid))
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen i_panic("UID lost unexpectedly");
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen switch (sync_rec->type) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen case MAIL_INDEX_SYNC_TYPE_EXPUNGE:
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen mail_expunge(bbox->sync_mail);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen break;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen case MAIL_INDEX_SYNC_TYPE_FLAGS:
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen flags = sync_rec->add_flags & MAIL_FLAGS_NONRECENT;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (flags != 0) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen mail_update_flags(bbox->sync_mail,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen MODIFY_ADD, flags);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen }
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen flags = sync_rec->remove_flags & MAIL_FLAGS_NONRECENT;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (flags != 0) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen mail_update_flags(bbox->sync_mail,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen MODIFY_REMOVE, flags);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen }
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen break;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen case MAIL_INDEX_SYNC_TYPE_KEYWORD_ADD:
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen case MAIL_INDEX_SYNC_TYPE_KEYWORD_REMOVE:
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen kw_names[0] = ctx->kw_all[sync_rec->keyword_idx];
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen kw_names[1] = NULL;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen keywords = mailbox_keywords_create_valid(bbox->box,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen kw_names);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen modify_type = sync_rec->type ==
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen MAIL_INDEX_SYNC_TYPE_KEYWORD_ADD ?
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen MODIFY_ADD : MODIFY_REMOVE;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen mail_update_keywords(bbox->sync_mail,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen modify_type, keywords);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen mailbox_keywords_free(bbox->box, &keywords);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen break;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen case MAIL_INDEX_SYNC_TYPE_KEYWORD_RESET:
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen kw_names[0] = NULL;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen keywords = mailbox_keywords_create_valid(bbox->box,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen kw_names);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen mail_update_keywords(bbox->sync_mail, MODIFY_REPLACE,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen keywords);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen mailbox_keywords_free(bbox->box, &keywords);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen break;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen case MAIL_INDEX_SYNC_TYPE_APPEND:
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen i_unreached();
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen }
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen }
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen}
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainenstatic void virtual_sync_index_changes(struct virtual_sync_context *ctx)
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen{
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen const ARRAY_TYPE(keywords) *keywords;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen struct mail_index_sync_rec sync_rec;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen keywords = mail_index_get_keywords(ctx->index);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen ctx->kw_all = array_count(keywords) == 0 ? NULL :
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen array_idx(keywords, 0);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen while (mail_index_sync_next(ctx->index_sync_ctx, &sync_rec))
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen virtual_sync_index_rec(ctx, &sync_rec);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen}
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainenstatic void virtual_sync_index_finish(struct virtual_sync_context *ctx)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen{
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct mailbox *box = &ctx->mbox->ibox.box;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen const struct mail_index_header *hdr;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen uint32_t seq1, seq2;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen hdr = mail_index_get_header(ctx->sync_view);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (hdr->uid_validity != 0)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ctx->uid_validity = hdr->uid_validity;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen else
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen virtual_sync_set_uidvalidity(ctx);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen /* mark the newly seen messages as recent */
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (mail_index_lookup_seq_range(ctx->sync_view, hdr->first_recent_uid,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen hdr->next_uid, &seq1, &seq2)) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen index_mailbox_set_recent_seq(&ctx->mbox->ibox, ctx->sync_view,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen seq1, seq2);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen }
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (ctx->ext_header_rewrite) {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen /* entire mailbox list needs to be rewritten */
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen virtual_sync_ext_header_rewrite(ctx);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen } else {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen /* update only changed parts in the header */
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen virtual_sync_ext_header_update(ctx);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen }
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (box->v.sync_notify != NULL)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen box->v.sync_notify(box, 0, 0);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen}
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainenstatic int virtual_sync_backend_box_init(struct virtual_backend_box *bbox)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen{
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct mailbox_transaction_context *trans;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct mail_search_context *search_ctx;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct mail *mail;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen struct virtual_backend_uidmap uidmap;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen enum mailbox_search_result_flags result_flags;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen int ret;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen trans = mailbox_transaction_begin(bbox->box, 0);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen mail = mail_alloc(trans, 0, NULL);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
db0735f9b388c5bcfb781b1b25015e898d63d953Timo Sirainen search_ctx = mailbox_search_init(trans, bbox->search_args, NULL);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen /* save the result and keep it updated */
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen result_flags = MAILBOX_SEARCH_RESULT_FLAG_UPDATE |
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen MAILBOX_SEARCH_RESULT_FLAG_QUEUE_SYNC;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen bbox->search_result =
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen mailbox_search_result_save(search_ctx, result_flags);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen /* add the found UIDs to uidmap. virtual_uid gets assigned later. */
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen memset(&uidmap, 0, sizeof(uidmap));
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen array_clear(&bbox->uids);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen while (mailbox_search_next(search_ctx, mail) > 0) {
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen uidmap.real_uid = mail->uid;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen array_append(&bbox->uids, &uidmap, 1);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen }
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ret = mailbox_search_deinit(&search_ctx);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen mail_free(&mail);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen (void)mailbox_transaction_commit(&trans);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen return ret;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen}
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainenstatic int virtual_backend_uidmap_bsearch_cmp(const void *key, const void *data)
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen{
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen const uint32_t *uidp = key;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen const struct virtual_backend_uidmap *uidmap = data;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen return *uidp < uidmap->real_uid ? -1 :
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen (*uidp > uidmap->real_uid ? 1 : 0);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen}
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainenstatic void
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainenvirtual_sync_mailbox_box_remove(struct virtual_sync_context *ctx,
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen struct virtual_backend_box *bbox,
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen const ARRAY_TYPE(seq_range) *removed_uids)
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen{
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen const struct seq_range *uids;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen struct virtual_backend_uidmap *uidmap;
e3736b5d480878031c386ac55d201fcf08e68766Timo Sirainen unsigned int i, src, dest, uid_count, rec_count;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen uint32_t uid, vseq;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen uids = array_get(removed_uids, &uid_count);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen if (uid_count == 0)
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen return;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen /* everything in removed_uids should exist in bbox->uids */
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen uidmap = array_get_modifiable(&bbox->uids, &rec_count);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen i_assert(rec_count >= uid_count);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen /* find the first uidmap record to be removed */
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen if (!bsearch_insert_pos(&uids[0].seq1, uidmap, rec_count,
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen sizeof(*uidmap),
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen virtual_backend_uidmap_bsearch_cmp, &src))
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen i_unreached();
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen /* remove the unwanted messages */
e3736b5d480878031c386ac55d201fcf08e68766Timo Sirainen dest = src;
e3736b5d480878031c386ac55d201fcf08e68766Timo Sirainen for (i = 0; i < uid_count; i++) {
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen uid = uids[i].seq1;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen while (uidmap[src].real_uid != uid) {
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen uidmap[dest++] = uidmap[src++];
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen i_assert(src < rec_count);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen }
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen for (; uid <= uids[i].seq2; uid++, src++) {
e3736b5d480878031c386ac55d201fcf08e68766Timo Sirainen i_assert(src < rec_count);
e3736b5d480878031c386ac55d201fcf08e68766Timo Sirainen i_assert(uidmap[src].real_uid == uid);
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen if (mail_index_lookup_seq(ctx->sync_view,
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen uidmap[src].virtual_uid,
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen &vseq))
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen mail_index_expunge(ctx->trans, vseq);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen }
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen }
fb7dd075cf883e5e7defbc0c8fb8326e30bdccdeTimo Sirainen array_delete(&bbox->uids, dest, src - dest);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen}
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainenstatic void
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainenvirtual_sync_mailbox_box_add(struct virtual_sync_context *ctx,
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen struct virtual_backend_box *bbox,
a21823d90cee6a18aeab0378637472c7e3fbbab2Timo Sirainen const ARRAY_TYPE(seq_range) *added_uids_arr)
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen{
a21823d90cee6a18aeab0378637472c7e3fbbab2Timo Sirainen const struct seq_range *added_uids;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen struct virtual_backend_uidmap *uidmap;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen struct virtual_add_record rec;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen unsigned int i, src, dest, uid_count, add_count, rec_count;
a21823d90cee6a18aeab0378637472c7e3fbbab2Timo Sirainen uint32_t add_uid;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
a21823d90cee6a18aeab0378637472c7e3fbbab2Timo Sirainen added_uids = array_get(added_uids_arr, &uid_count);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen if (uid_count == 0)
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen return;
a21823d90cee6a18aeab0378637472c7e3fbbab2Timo Sirainen add_count = seq_range_count(added_uids_arr);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen /* none of added_uids should exist in bbox->uids. find the position
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen of the first inserted index. */
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen uidmap = array_get_modifiable(&bbox->uids, &rec_count);
a21823d90cee6a18aeab0378637472c7e3fbbab2Timo Sirainen if (rec_count == 0 ||
a21823d90cee6a18aeab0378637472c7e3fbbab2Timo Sirainen added_uids[0].seq1 > uidmap[rec_count-1].real_uid) {
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen /* fast path: usually messages are appended */
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen dest = rec_count;
a21823d90cee6a18aeab0378637472c7e3fbbab2Timo Sirainen } else if (bsearch_insert_pos(&added_uids[0].seq1, uidmap, rec_count,
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen sizeof(*uidmap),
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen virtual_backend_uidmap_bsearch_cmp,
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen &dest))
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen i_unreached();
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen /* make space for all added UIDs. */
a21823d90cee6a18aeab0378637472c7e3fbbab2Timo Sirainen if (rec_count == dest)
a21823d90cee6a18aeab0378637472c7e3fbbab2Timo Sirainen array_idx_clear(&bbox->uids, dest + add_count-1);
a21823d90cee6a18aeab0378637472c7e3fbbab2Timo Sirainen else {
a21823d90cee6a18aeab0378637472c7e3fbbab2Timo Sirainen array_copy(&bbox->uids.arr, dest + add_count,
a21823d90cee6a18aeab0378637472c7e3fbbab2Timo Sirainen &bbox->uids.arr, dest, rec_count - dest);
a21823d90cee6a18aeab0378637472c7e3fbbab2Timo Sirainen }
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen uidmap = array_get_modifiable(&bbox->uids, &rec_count);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen src = dest + add_count;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen /* add/move the UIDs to their correct positions */
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen memset(&rec, 0, sizeof(rec));
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen rec.rec.mailbox_id = bbox->mailbox_id;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen for (i = 0; i < uid_count; i++) {
a21823d90cee6a18aeab0378637472c7e3fbbab2Timo Sirainen add_uid = added_uids[i].seq1;
41955c400476941fa274f18b106a5922866fd780Timo Sirainen while (src < rec_count && uidmap[src].real_uid < add_uid)
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen uidmap[dest++] = uidmap[src++];
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
a21823d90cee6a18aeab0378637472c7e3fbbab2Timo Sirainen for (; add_uid <= added_uids[i].seq2; add_uid++, dest++) {
e3736b5d480878031c386ac55d201fcf08e68766Timo Sirainen i_assert(dest < rec_count);
e3736b5d480878031c386ac55d201fcf08e68766Timo Sirainen
a21823d90cee6a18aeab0378637472c7e3fbbab2Timo Sirainen uidmap[dest].real_uid = add_uid;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen uidmap[dest].virtual_uid = 0;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
d516e6848ecfbc7381abe9414fd8011fdf9d8c95Timo Sirainen if (ctx->mbox->uids_mapped) {
a21823d90cee6a18aeab0378637472c7e3fbbab2Timo Sirainen rec.rec.real_uid = add_uid;
d516e6848ecfbc7381abe9414fd8011fdf9d8c95Timo Sirainen array_append(&ctx->all_adds, &rec, 1);
d516e6848ecfbc7381abe9414fd8011fdf9d8c95Timo Sirainen }
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen }
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen }
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen}
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainenstatic int virtual_backend_uidmap_cmp(const void *p1, const void *p2)
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen{
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen const struct virtual_backend_uidmap *u1 = p1, *u2 = p2;
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen if (u1->real_uid < u2->real_uid)
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen return -1;
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen if (u1->real_uid > u2->real_uid)
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen return 1;
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen return 0;
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen}
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainenstatic void virtual_sync_bbox_uids_sort(struct virtual_backend_box *bbox)
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen{
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen struct virtual_backend_uidmap *uids;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen unsigned int uid_count;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen /* the uidmap must be sorted by real_uids */
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen uids = array_get_modifiable(&bbox->uids, &uid_count);
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen qsort(uids, uid_count, sizeof(*uids), virtual_backend_uidmap_cmp);
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen bbox->uids_nonsorted = FALSE;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen}
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainenstatic void virtual_sync_backend_boxes_sort_uids(struct virtual_mailbox *mbox)
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen{
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen struct virtual_backend_box *const *bboxes;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen unsigned int i, count;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen bboxes = array_get(&mbox->backend_boxes, &count);
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen for (i = 0; i < count; i++) {
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen if (bboxes[i]->uids_nonsorted)
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen virtual_sync_bbox_uids_sort(bboxes[i]);
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen }
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen}
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainenstatic void
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainenvirtual_sync_backend_handle_old_vmsgs(struct virtual_sync_context *ctx,
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen struct virtual_backend_box *bbox,
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen struct mail_search_result *result)
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen{
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen struct index_mailbox *ibox = (struct index_mailbox *)bbox->box;
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen const struct virtual_mail_index_record *vrec;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen struct virtual_backend_uidmap uidmap;
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen const void *data;
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen uint32_t seq, vseq, vuid, messages;
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen bool expunged;
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen /* add the currently existing UIDs to uidmap. remember the messages
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen that were already expunged */
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen memset(&uidmap, 0, sizeof(uidmap));
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen array_clear(&bbox->uids);
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen messages = mail_index_view_get_messages_count(ctx->sync_view);
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen for (vseq = 1; vseq <= messages; vseq++) {
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen mail_index_lookup_uid(ctx->sync_view, vseq, &vuid);
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen mail_index_lookup_ext(ctx->sync_view, vseq,
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen ctx->mbox->virtual_ext_id,
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen &data, &expunged);
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen vrec = data;
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen if (vrec->mailbox_id == bbox->mailbox_id) {
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen uidmap.real_uid = vrec->real_uid;
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen uidmap.virtual_uid = vuid;
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen array_append(&bbox->uids, &uidmap, 1);
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen if (mail_index_lookup_seq(ibox->view, vrec->real_uid,
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen &seq)) {
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen seq_range_array_add(&result->uids, 0,
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen vrec->real_uid);
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen } else {
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen seq_range_array_add(&result->removed_uids, 0,
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen vrec->real_uid);
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen }
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen }
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen }
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen virtual_sync_bbox_uids_sort(bbox);
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen}
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainenstatic int virtual_sync_backend_box_continue(struct virtual_sync_context *ctx,
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen struct virtual_backend_box *bbox)
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen{
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen const enum mailbox_search_result_flags result_flags =
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen MAILBOX_SEARCH_RESULT_FLAG_UPDATE |
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen MAILBOX_SEARCH_RESULT_FLAG_QUEUE_SYNC;
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen struct index_mailbox *ibox = (struct index_mailbox *)bbox->box;
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen struct mail_search_result *result;
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen ARRAY_TYPE(seq_range) removed_uids, added_uids, flag_updates;
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen uint64_t modseq;
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen uint32_t seq, old_msg_count;
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen /* initialize the search result from all the existing messages in
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen virtual index. */
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen result = mailbox_search_result_alloc(bbox->box, bbox->search_args,
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen result_flags);
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen mailbox_search_result_initial_done(result);
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen virtual_sync_backend_handle_old_vmsgs(ctx, bbox, result);
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen /* get list of changed old messages, based on modseq changes.
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen (we'll assume all modseq changes are due to flag changes, which
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen may not be true in future.) */
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen if (bbox->sync_next_uid <= 1 ||
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen !mail_index_lookup_seq_range(ibox->view, 1, bbox->sync_next_uid-1,
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen &seq, &old_msg_count))
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen old_msg_count = 0;
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen t_array_init(&flag_updates, I_MIN(128, old_msg_count));
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen for (seq = 1; seq <= old_msg_count; seq++) {
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen modseq = mail_index_modseq_lookup(ibox->view, seq);
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen if (modseq > bbox->sync_highest_modseq)
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen seq_range_array_add(&flag_updates, 0, seq);
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen }
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen /* update the search result based on the flag changes and
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen new messages */
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen if (index_search_result_update_flags(result, &flag_updates) < 0 ||
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen index_search_result_update_appends(result, old_msg_count) < 0) {
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen mailbox_search_result_free(&result);
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen return -1;
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen }
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen t_array_init(&removed_uids, 128);
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen t_array_init(&added_uids, 128);
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen mailbox_search_result_sync(result, &removed_uids, &added_uids);
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen virtual_sync_mailbox_box_remove(ctx, bbox, &removed_uids);
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen virtual_sync_mailbox_box_add(ctx, bbox, &added_uids);
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen bbox->search_result = result;
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen return 0;
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen}
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainenstatic void virtual_sync_drop_existing(struct virtual_backend_box *bbox,
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen ARRAY_TYPE(seq_range) *added_uids)
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen{
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen ARRAY_TYPE(seq_range) drop_uids;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen const struct virtual_backend_uidmap *uidmap;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen struct seq_range_iter iter;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen unsigned int i, n = 0, count;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen uint32_t add_uid;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen seq_range_array_iter_init(&iter, added_uids);
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen if (!seq_range_array_iter_nth(&iter, n++, &add_uid))
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen return;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen uidmap = array_get_modifiable(&bbox->uids, &count);
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen (void)bsearch_insert_pos(&add_uid, uidmap, count, sizeof(*uidmap),
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen virtual_backend_uidmap_bsearch_cmp, &i);
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen if (i == count)
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen return;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen t_array_init(&drop_uids, array_count(added_uids));
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen for (; i < count; ) {
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen if (uidmap[i].real_uid < add_uid) {
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen i++;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen continue;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen }
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen if (uidmap[i].real_uid == add_uid) {
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen seq_range_array_add(&drop_uids, 0, add_uid);
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen i++;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen }
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen if (!seq_range_array_iter_nth(&iter, n++, &add_uid))
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen break;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen }
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen seq_range_array_remove_seq_range(added_uids, &drop_uids);
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen}
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainenstatic void virtual_sync_drop_nonexisting(struct virtual_backend_box *bbox,
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen ARRAY_TYPE(seq_range) *removed_uids)
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen{
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen ARRAY_TYPE(seq_range) drop_uids;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen const struct virtual_backend_uidmap *uidmap;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen struct seq_range_iter iter;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen unsigned int i, n = 0, count;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen uint32_t remove_uid;
c20d64512d687f0abf7c1a0aa4fa0174da0a18e3Timo Sirainen bool iter_done = FALSE;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen seq_range_array_iter_init(&iter, removed_uids);
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen if (!seq_range_array_iter_nth(&iter, n++, &remove_uid))
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen return;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen uidmap = array_get_modifiable(&bbox->uids, &count);
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen (void)bsearch_insert_pos(&remove_uid, uidmap, count, sizeof(*uidmap),
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen virtual_backend_uidmap_bsearch_cmp, &i);
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen
c20d64512d687f0abf7c1a0aa4fa0174da0a18e3Timo Sirainen t_array_init(&drop_uids, array_count(removed_uids)); iter_done = FALSE;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen for (; i < count; ) {
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen if (uidmap[i].real_uid < remove_uid) {
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen i++;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen continue;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen }
c20d64512d687f0abf7c1a0aa4fa0174da0a18e3Timo Sirainen if (uidmap[i].real_uid != remove_uid)
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen seq_range_array_add(&drop_uids, 0, remove_uid);
c20d64512d687f0abf7c1a0aa4fa0174da0a18e3Timo Sirainen else
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen i++;
c20d64512d687f0abf7c1a0aa4fa0174da0a18e3Timo Sirainen if (!seq_range_array_iter_nth(&iter, n++, &remove_uid)) {
c20d64512d687f0abf7c1a0aa4fa0174da0a18e3Timo Sirainen iter_done = TRUE;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen break;
c20d64512d687f0abf7c1a0aa4fa0174da0a18e3Timo Sirainen }
c20d64512d687f0abf7c1a0aa4fa0174da0a18e3Timo Sirainen }
c20d64512d687f0abf7c1a0aa4fa0174da0a18e3Timo Sirainen if (!iter_done) {
c20d64512d687f0abf7c1a0aa4fa0174da0a18e3Timo Sirainen do {
c20d64512d687f0abf7c1a0aa4fa0174da0a18e3Timo Sirainen seq_range_array_add(&drop_uids, 0, remove_uid);
c20d64512d687f0abf7c1a0aa4fa0174da0a18e3Timo Sirainen } while (!seq_range_array_iter_nth(&iter, n++, &remove_uid));
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen }
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen seq_range_array_remove_seq_range(removed_uids, &drop_uids);
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen}
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainenstatic void virtual_sync_mailbox_box_update(struct virtual_sync_context *ctx,
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen struct virtual_backend_box *bbox)
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen{
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen ARRAY_TYPE(seq_range) removed_uids, added_uids, temp_uids;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen unsigned int count1, count2;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen t_array_init(&removed_uids, 128);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen t_array_init(&added_uids, 128);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen mailbox_search_result_sync(bbox->search_result,
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen &removed_uids, &added_uids);
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen if (array_is_created(&bbox->sync_outside_expunges)) {
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen seq_range_array_remove_seq_range(&bbox->sync_outside_expunges,
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen &added_uids);
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen seq_range_array_merge(&removed_uids,
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen &bbox->sync_outside_expunges);
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen array_clear(&bbox->sync_outside_expunges);
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen }
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen virtual_sync_drop_existing(bbox, &added_uids);
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen virtual_sync_drop_nonexisting(bbox, &removed_uids);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen /* if any of the pending removes came back, we don't want to expunge
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen them anymore. also since they already exist, remove them from
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen added_uids. */
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen count1 = array_count(&bbox->sync_pending_removes);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen count2 = array_count(&added_uids);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen if (count1 > 0 && count2 > 0) {
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen t_array_init(&temp_uids, count1);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen array_append_array(&temp_uids, &bbox->sync_pending_removes);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen if (seq_range_array_remove_seq_range(
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen &bbox->sync_pending_removes, &added_uids) > 0) {
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen seq_range_array_remove_seq_range(&added_uids,
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen &temp_uids);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen }
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen }
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen if (!ctx->expunge_removed) {
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen /* delay removing messages that don't match the search
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen criteria, but don't delay removing expunged messages */
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen if (array_count(&ctx->sync_expunges) > 0) {
41955c400476941fa274f18b106a5922866fd780Timo Sirainen seq_range_array_remove_seq_range(&bbox->sync_pending_removes,
41955c400476941fa274f18b106a5922866fd780Timo Sirainen &ctx->sync_expunges);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen seq_range_array_remove_seq_range(&removed_uids,
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen &ctx->sync_expunges);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen virtual_sync_mailbox_box_remove(ctx, bbox,
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen &ctx->sync_expunges);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen }
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen seq_range_array_merge(&bbox->sync_pending_removes,
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen &removed_uids);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen } else if (array_count(&bbox->sync_pending_removes) > 0) {
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen /* remove all current and old */
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen seq_range_array_merge(&bbox->sync_pending_removes,
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen &removed_uids);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen virtual_sync_mailbox_box_remove(ctx, bbox,
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen &bbox->sync_pending_removes);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen array_clear(&bbox->sync_pending_removes);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen } else {
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen virtual_sync_mailbox_box_remove(ctx, bbox, &removed_uids);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen }
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen virtual_sync_mailbox_box_add(ctx, bbox, &added_uids);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen}
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainenstatic bool virtual_sync_find_seqs(struct virtual_backend_box *bbox,
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen const struct mailbox_sync_rec *sync_rec,
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen unsigned int *idx1_r,
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen unsigned int *idx2_r)
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen{
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen struct index_mailbox *ibox = (struct index_mailbox *)bbox->box;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen const struct virtual_backend_uidmap *uidmap;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen unsigned int idx, count;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen uint32_t uid1, uid2;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen mail_index_lookup_uid(ibox->view, sync_rec->seq1, &uid1);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen mail_index_lookup_uid(ibox->view, sync_rec->seq2, &uid2);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen uidmap = array_get_modifiable(&bbox->uids, &count);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen (void)bsearch_insert_pos(&uid1, uidmap, count, sizeof(*uidmap),
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen virtual_backend_uidmap_bsearch_cmp, &idx);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen if (idx == count || uidmap[idx].real_uid > uid2)
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen return FALSE;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen *idx1_r = idx;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen while (idx < count && uidmap[idx].real_uid <= uid2) idx++;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen *idx2_r = idx - 1;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen return TRUE;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen}
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
41955c400476941fa274f18b106a5922866fd780Timo Sirainenstatic void virtual_sync_expunge_add(struct virtual_sync_context *ctx,
41955c400476941fa274f18b106a5922866fd780Timo Sirainen struct virtual_backend_box *bbox,
41955c400476941fa274f18b106a5922866fd780Timo Sirainen const struct mailbox_sync_rec *sync_rec)
41955c400476941fa274f18b106a5922866fd780Timo Sirainen{
41955c400476941fa274f18b106a5922866fd780Timo Sirainen struct index_mailbox *ibox = (struct index_mailbox *)bbox->box;
41955c400476941fa274f18b106a5922866fd780Timo Sirainen struct virtual_backend_uidmap *uidmap;
41955c400476941fa274f18b106a5922866fd780Timo Sirainen uint32_t uid1, uid2;
41955c400476941fa274f18b106a5922866fd780Timo Sirainen unsigned int i, idx1, count;
41955c400476941fa274f18b106a5922866fd780Timo Sirainen
41955c400476941fa274f18b106a5922866fd780Timo Sirainen mail_index_lookup_uid(ibox->view, sync_rec->seq1, &uid1);
41955c400476941fa274f18b106a5922866fd780Timo Sirainen mail_index_lookup_uid(ibox->view, sync_rec->seq2, &uid2);
41955c400476941fa274f18b106a5922866fd780Timo Sirainen
41955c400476941fa274f18b106a5922866fd780Timo Sirainen /* remember only the expunges for messages that
41955c400476941fa274f18b106a5922866fd780Timo Sirainen already exist for this mailbox */
41955c400476941fa274f18b106a5922866fd780Timo Sirainen uidmap = array_get_modifiable(&bbox->uids, &count);
41955c400476941fa274f18b106a5922866fd780Timo Sirainen (void)bsearch_insert_pos(&uid1, uidmap, count, sizeof(*uidmap),
41955c400476941fa274f18b106a5922866fd780Timo Sirainen virtual_backend_uidmap_bsearch_cmp, &idx1);
41955c400476941fa274f18b106a5922866fd780Timo Sirainen for (i = idx1; i < count; i++) {
41955c400476941fa274f18b106a5922866fd780Timo Sirainen if (uidmap[i].real_uid > uid2)
41955c400476941fa274f18b106a5922866fd780Timo Sirainen break;
41955c400476941fa274f18b106a5922866fd780Timo Sirainen seq_range_array_add(&ctx->sync_expunges, 0, uidmap[i].real_uid);
41955c400476941fa274f18b106a5922866fd780Timo Sirainen }
41955c400476941fa274f18b106a5922866fd780Timo Sirainen}
41955c400476941fa274f18b106a5922866fd780Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainenstatic int virtual_sync_backend_box_sync(struct virtual_sync_context *ctx,
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen struct virtual_backend_box *bbox,
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen enum mailbox_sync_flags sync_flags)
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen{
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen struct mailbox_sync_context *sync_ctx;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen const struct virtual_backend_uidmap *uidmap;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen struct mailbox_sync_rec sync_rec;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen unsigned int idx1, idx2;
41955c400476941fa274f18b106a5922866fd780Timo Sirainen uint32_t vseq, vuid;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen sync_ctx = mailbox_sync_init(bbox->box, sync_flags);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen virtual_backend_box_sync_mail_set(bbox);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen while (mailbox_sync_next(sync_ctx, &sync_rec)) {
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen switch (sync_rec.type) {
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen case MAILBOX_SYNC_TYPE_EXPUNGE:
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen if (ctx->expunge_removed) {
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen /* no need to keep track of expunges */
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen break;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen }
41955c400476941fa274f18b106a5922866fd780Timo Sirainen virtual_sync_expunge_add(ctx, bbox, &sync_rec);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen break;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen case MAILBOX_SYNC_TYPE_FLAGS:
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen if (!virtual_sync_find_seqs(bbox, &sync_rec,
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen &idx1, &idx2))
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen break;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen uidmap = array_idx(&bbox->uids, 0);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen for (; idx1 <= idx2; idx1++) {
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen vuid = uidmap[idx1].virtual_uid;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen if (!mail_index_lookup_seq(ctx->sync_view,
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen vuid, &vseq)) {
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen /* expunged by another session,
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen but we haven't yet updated
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen bbox->uids. */
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen continue;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen }
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen virtual_sync_external_flags(ctx, bbox, vseq,
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen uidmap[idx1].real_uid);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen }
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen break;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen case MAILBOX_SYNC_TYPE_MODSEQ:
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen break;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen }
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen }
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen return mailbox_sync_deinit(&sync_ctx, 0, NULL);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen}
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainenstatic void virtual_sync_backend_ext_header(struct virtual_sync_context *ctx,
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen struct virtual_backend_box *bbox)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen{
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen const unsigned int uidval_pos =
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen offsetof(struct virtual_mail_index_mailbox_record,
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen uid_validity);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen struct mailbox_status status;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen struct virtual_mail_index_mailbox_record mailbox;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen unsigned int mailbox_offset;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen mailbox_get_status(bbox->box, STATUS_UIDVALIDITY |
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen STATUS_HIGHESTMODSEQ, &status);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (bbox->sync_uid_validity == status.uidvalidity &&
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen bbox->sync_next_uid == status.uidnext &&
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen bbox->sync_highest_modseq == status.highest_modseq)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen return;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen /* mailbox changed - update extension header */
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen bbox->sync_uid_validity = status.uidvalidity;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen bbox->sync_highest_modseq = status.highest_modseq;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen bbox->sync_next_uid = status.uidnext;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
5b486fdbf2077a994337dc8bd4477ec51d5daf4eTimo Sirainen if (ctx->ext_header_rewrite) {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen /* we'll rewrite the entire header later */
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen return;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen }
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen memset(&mailbox, 0, sizeof(mailbox));
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen mailbox.uid_validity = bbox->sync_uid_validity;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen mailbox.highest_modseq = bbox->sync_highest_modseq;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen mailbox.next_uid = bbox->sync_next_uid;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen mailbox_offset = sizeof(struct virtual_mail_index_header) +
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen bbox->sync_mailbox_idx * sizeof(mailbox);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen mail_index_update_header_ext(ctx->trans, ctx->mbox->virtual_ext_id,
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen mailbox_offset + uidval_pos,
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen CONST_PTR_OFFSET(&mailbox, uidval_pos),
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen sizeof(mailbox) - uidval_pos);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ctx->ext_header_changed = TRUE;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen}
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainenstatic int virtual_sync_backend_box(struct virtual_sync_context *ctx,
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen struct virtual_backend_box *bbox)
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen{
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen struct index_mailbox *ibox = (struct index_mailbox *)bbox->box;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen enum mailbox_sync_flags sync_flags;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen struct mailbox_status status;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen int ret;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen if (!bbox->box->opened) {
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen if (mailbox_open(bbox->box) < 0)
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen return -1;
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen }
3c097377e865689723c8737537886b01a5ebd3d9Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen /* if we already did some changes to index, commit them before
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen syncing starts. */
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen virtual_backend_box_sync_mail_unset(bbox);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen /* we use modseqs for speeding up initial search result build.
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen make sure the backend has them enabled. */
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen mail_index_modseq_enable(ibox->index);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen sync_flags = ctx->flags & (MAILBOX_SYNC_FLAG_FULL_READ |
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen MAILBOX_SYNC_FLAG_FULL_WRITE |
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen MAILBOX_SYNC_FLAG_FAST);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen if (bbox->search_result == NULL) {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen /* first sync in this process */
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen i_assert(ctx->expunge_removed);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (mailbox_sync(bbox->box, sync_flags, STATUS_UIDVALIDITY,
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen &status) < 0)
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen return -1;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen virtual_backend_box_sync_mail_set(bbox);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (status.uidvalidity != bbox->sync_uid_validity) {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen /* UID validity changed since last sync (or this is
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen the first sync), do a full search */
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ret = virtual_sync_backend_box_init(bbox);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen } else {
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen /* build the initial search using the saved modseq. */
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ret = virtual_sync_backend_box_continue(ctx, bbox);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen }
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen } else {
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen /* sync using the existing search result */
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen i_array_init(&ctx->sync_expunges, 32);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen ret = virtual_sync_backend_box_sync(ctx, bbox, sync_flags);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen if (ret == 0) T_BEGIN {
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen virtual_sync_mailbox_box_update(ctx, bbox);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen } T_END;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen array_free(&ctx->sync_expunges);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen }
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen virtual_sync_backend_ext_header(ctx, bbox);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen return ret;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen}
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainenstatic void virtual_sync_backend_map_uids(struct virtual_sync_context *ctx)
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen{
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen uint32_t virtual_ext_id = ctx->mbox->virtual_ext_id;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen struct virtual_sync_mail *vmails;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen struct virtual_backend_box *bbox, *const *bboxes;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen struct virtual_backend_uidmap *uidmap = NULL;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen struct virtual_add_record add_rec;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen const struct virtual_mail_index_record *vrec;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen const void *data;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen bool expunged;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen uint32_t i, vseq, vuid, messages, count;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen unsigned int j = 0, uidmap_count = 0;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen messages = mail_index_view_get_messages_count(ctx->sync_view);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen /* sort the messages in current view by their backend mailbox and
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen real UID */
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen vmails = messages == 0 ? NULL :
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen i_new(struct virtual_sync_mail, messages);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen for (vseq = 1; vseq <= messages; vseq++) {
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen mail_index_lookup_ext(ctx->sync_view, vseq, virtual_ext_id,
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen &data, &expunged);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen vrec = data;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen vmails[vseq-1].vseq = vseq;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen vmails[vseq-1].vrec = *vrec;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen }
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen qsort(vmails, messages, sizeof(*vmails), virtual_sync_mail_cmp);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen /* create real mailbox uid -> virtual uid mapping and expunge
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen messages no longer matching the search rule */
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen memset(&add_rec, 0, sizeof(add_rec));
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen bbox = NULL;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen for (i = 0; i < messages; i++) {
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen vseq = vmails[i].vseq;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen vrec = &vmails[i].vrec;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen if (bbox == NULL || bbox->mailbox_id != vrec->mailbox_id) {
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen /* add the rest of the newly seen messages */
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen for (; j < uidmap_count; j++) {
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen add_rec.rec.real_uid = uidmap[j].real_uid;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen array_append(&ctx->all_adds, &add_rec, 1);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen }
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen bbox = virtual_backend_box_lookup(ctx->mbox,
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen vrec->mailbox_id);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen if (bbox == NULL) {
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen /* the entire mailbox is lost */
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen mail_index_expunge(ctx->trans, vseq);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen continue;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen }
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen uidmap = array_get_modifiable(&bbox->uids,
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen &uidmap_count);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen j = 0;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen add_rec.rec.mailbox_id = bbox->mailbox_id;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen bbox->sync_seen = TRUE;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen }
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen mail_index_lookup_uid(ctx->sync_view, vseq, &vuid);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen /* if virtual record doesn't exist in uidmap, it's expunged */
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen for (; j < uidmap_count; j++) {
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen if (uidmap[j].real_uid >= vrec->real_uid)
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen break;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen /* newly seen message */
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen add_rec.rec.real_uid = uidmap[j].real_uid;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen array_append(&ctx->all_adds, &add_rec, 1);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen }
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen if (j == uidmap_count || uidmap[j].real_uid != vrec->real_uid)
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen mail_index_expunge(ctx->trans, vseq);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen else {
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen /* exists - update uidmap and flags */
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen uidmap[j++].virtual_uid = vuid;
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen virtual_sync_external_flags(ctx, bbox, vseq,
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen vrec->real_uid);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen }
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen }
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen i_free(vmails);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
2b9fd042e701cfe7d79c4294a5ab401d6ec9ce18Timo Sirainen /* finish adding messages to the last mailbox */
2b9fd042e701cfe7d79c4294a5ab401d6ec9ce18Timo Sirainen for (; j < uidmap_count; j++) {
2b9fd042e701cfe7d79c4294a5ab401d6ec9ce18Timo Sirainen add_rec.rec.real_uid = uidmap[j].real_uid;
2b9fd042e701cfe7d79c4294a5ab401d6ec9ce18Timo Sirainen array_append(&ctx->all_adds, &add_rec, 1);
2b9fd042e701cfe7d79c4294a5ab401d6ec9ce18Timo Sirainen }
2b9fd042e701cfe7d79c4294a5ab401d6ec9ce18Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen /* if there are any mailboxes we didn't yet sync, add new messages in
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen them */
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen bboxes = array_get(&ctx->mbox->backend_boxes, &count);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen for (i = 0; i < count; i++) {
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen if (bboxes[i]->sync_seen)
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen continue;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen add_rec.rec.mailbox_id = bboxes[i]->mailbox_id;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen uidmap = array_get_modifiable(&bboxes[i]->uids, &uidmap_count);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen for (j = 0; j < uidmap_count; j++) {
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen add_rec.rec.real_uid = uidmap[j].real_uid;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen array_append(&ctx->all_adds, &add_rec, 1);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen }
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen }
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen}
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainenstatic int virtual_add_record_cmp(const void *p1, const void *p2)
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen{
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen const struct virtual_add_record *add1 = p1, *add2 = p2;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen if (add1->received_date < add2->received_date)
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen return -1;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen if (add1->received_date > add2->received_date)
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen return 1;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen /* if they're in same mailbox, we can order them correctly by the UID.
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen if they're in different mailboxes, ordering by UID doesn't really
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen help but it doesn't really harm either. */
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen if (add1->rec.real_uid < add2->rec.real_uid)
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen return -1;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen if (add1->rec.real_uid > add2->rec.real_uid)
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen return 1;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen /* two messages in different mailboxes have the same received date
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen and UID. */
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen return 0;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen}
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainenstatic void virtual_sync_backend_sort_new(struct virtual_sync_context *ctx)
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen{
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen struct virtual_backend_box *bbox;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen struct virtual_add_record *adds;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen const struct virtual_mail_index_record *vrec;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen unsigned int i, count;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen /* get all messages' received dates */
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen adds = array_get_modifiable(&ctx->all_adds, &count);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen for (bbox = NULL, i = 0; i < count; i++) {
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen vrec = &adds[i].rec;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen if (bbox == NULL || bbox->mailbox_id != vrec->mailbox_id) {
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen bbox = virtual_backend_box_lookup(ctx->mbox,
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen vrec->mailbox_id);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen }
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen if (!mail_set_uid(bbox->sync_mail, vrec->real_uid))
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen i_unreached();
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen if (mail_get_received_date(bbox->sync_mail,
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen &adds[i].received_date) < 0) {
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen /* probably expunged already, just add it somewhere */
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen adds[i].received_date = 0;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen }
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen }
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen qsort(adds, count, sizeof(*adds), virtual_add_record_cmp);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen}
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainenstatic void virtual_sync_backend_add_new(struct virtual_sync_context *ctx)
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen{
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen uint32_t virtual_ext_id = ctx->mbox->virtual_ext_id;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen struct virtual_add_record *adds;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen struct virtual_backend_box *bbox;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen struct virtual_backend_uidmap *uidmap;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen const struct mail_index_header *hdr;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen const struct virtual_mail_index_record *vrec;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen unsigned int i, count, idx, uid_count;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen uint32_t vseq, first_uid, next_uid;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen hdr = mail_index_get_header(ctx->sync_view);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen adds = array_get_modifiable(&ctx->all_adds, &count);
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen if (count == 0) {
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen ctx->mbox->sync_virtual_next_uid = hdr->next_uid;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen return;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen }
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen if (adds[0].rec.mailbox_id == adds[count-1].rec.mailbox_id) {
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen /* all messages are from a single mailbox. add them in
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen the same order. */
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen } else {
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen /* sort new messages by received date to get the add order */
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen virtual_sync_backend_sort_new(ctx);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen }
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen for (bbox = NULL, i = 0; i < count; i++) {
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen vrec = &adds[i].rec;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen if (bbox == NULL || bbox->mailbox_id != vrec->mailbox_id) {
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen bbox = virtual_backend_box_lookup(ctx->mbox,
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen vrec->mailbox_id);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen }
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen mail_index_append(ctx->trans, 0, &vseq);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen mail_index_update_ext(ctx->trans, vseq, virtual_ext_id,
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen vrec, NULL);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen virtual_sync_external_flags(ctx, bbox, vseq, vrec->real_uid);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen }
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen /* assign UIDs to new messages */
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen first_uid = hdr->next_uid;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen mail_index_append_assign_uids(ctx->trans, first_uid, &next_uid);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen /* update virtual UIDs in uidmap */
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen for (bbox = NULL, i = 0; i < count; i++) {
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen vrec = &adds[i].rec;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen if (bbox == NULL || bbox->mailbox_id != vrec->mailbox_id) {
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen bbox = virtual_backend_box_lookup(ctx->mbox,
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen vrec->mailbox_id);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen }
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen uidmap = array_get_modifiable(&bbox->uids, &uid_count);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen if (!bsearch_insert_pos(&vrec->real_uid, uidmap, uid_count,
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen sizeof(*uidmap),
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen virtual_backend_uidmap_bsearch_cmp,
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen &idx))
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen i_unreached();
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen i_assert(uidmap[idx].virtual_uid == 0);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen uidmap[idx].virtual_uid = first_uid + i;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen }
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen ctx->mbox->sync_virtual_next_uid = first_uid + i;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen}
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainenstatic void
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainenvirtual_sync_apply_existing_appends(struct virtual_sync_context *ctx)
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen{
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen uint32_t virtual_ext_id = ctx->mbox->virtual_ext_id;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen struct virtual_backend_box *bbox = NULL;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen const struct mail_index_header *hdr;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen const struct virtual_mail_index_record *vrec;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen struct virtual_backend_uidmap uidmap;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen const void *data;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen bool expunged;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen uint32_t seq, seq2;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen if (!ctx->mbox->uids_mapped)
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen return;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen hdr = mail_index_get_header(ctx->sync_view);
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen if (ctx->mbox->sync_virtual_next_uid >= hdr->next_uid)
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen return;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen /* another process added messages to virtual index. get backend boxes'
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen uid lists up-to-date by adding the new messages there. */
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen if (!mail_index_lookup_seq_range(ctx->sync_view,
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen ctx->mbox->sync_virtual_next_uid,
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen (uint32_t)-1, &seq, &seq2))
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen return;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen memset(&uidmap, 0, sizeof(uidmap));
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen for (; seq <= seq2; seq++) {
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen mail_index_lookup_ext(ctx->sync_view, seq, virtual_ext_id,
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen &data, &expunged);
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen vrec = data;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen uidmap.real_uid = vrec->real_uid;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen mail_index_lookup_uid(ctx->sync_view, seq, &uidmap.virtual_uid);
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen if (bbox == NULL || bbox->mailbox_id != vrec->mailbox_id) {
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen bbox = virtual_backend_box_lookup(ctx->mbox,
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen vrec->mailbox_id);
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen }
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen array_append(&bbox->uids, &uidmap, 1);
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen bbox->uids_nonsorted = TRUE;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen }
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen virtual_sync_backend_boxes_sort_uids(ctx->mbox);
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen}
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainenstatic void
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainenvirtual_sync_apply_existing_expunges(struct virtual_mailbox *mbox,
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen struct mailbox_sync_context *sync_ctx)
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen{
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen struct index_mailbox_sync_context *isync_ctx =
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen (struct index_mailbox_sync_context *)sync_ctx;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen struct virtual_backend_box *bbox = NULL;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen struct seq_range_iter iter;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen const struct virtual_mail_index_record *vrec;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen const void *data;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen bool expunged;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen unsigned int n = 0;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen uint32_t seq;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen if (isync_ctx->expunges == NULL)
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen return;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen seq_range_array_iter_init(&iter, isync_ctx->expunges);
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen while (seq_range_array_iter_nth(&iter, n++, &seq)) {
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen mail_index_lookup_ext(mbox->ibox.view, seq,
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen mbox->virtual_ext_id, &data, &expunged);
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen vrec = data;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen if (bbox == NULL || bbox->mailbox_id != vrec->mailbox_id) {
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen bbox = virtual_backend_box_lookup(mbox,
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen vrec->mailbox_id);
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen if (!array_is_created(&bbox->sync_outside_expunges))
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen i_array_init(&bbox->sync_outside_expunges, 32);
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen }
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen seq_range_array_add(&bbox->sync_outside_expunges, 0,
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen vrec->real_uid);
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen }
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen}
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstatic int virtual_sync_backend_boxes(struct virtual_sync_context *ctx)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen{
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct virtual_backend_box *const *bboxes;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen unsigned int i, count;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen virtual_sync_apply_existing_appends(ctx);
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen i_array_init(&ctx->all_adds, 128);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen bboxes = array_get(&ctx->mbox->backend_boxes, &count);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen for (i = 0; i < count; i++) {
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen if (virtual_sync_backend_box(ctx, bboxes[i]) < 0) {
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen /* backend failed, copy the error */
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen virtual_box_copy_error(&ctx->mbox->ibox.box,
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen bboxes[i]->box);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen return -1;
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen }
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen }
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen if (!ctx->mbox->uids_mapped) {
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen /* initial sync: assign virtual UIDs to existing messages and
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen sync all flags */
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen ctx->mbox->uids_mapped = TRUE;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen virtual_sync_backend_map_uids(ctx);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen }
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen virtual_sync_backend_add_new(ctx);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen array_free(&ctx->all_adds);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen return 0;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen}
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstatic void virtual_sync_backend_boxes_finish(struct virtual_sync_context *ctx)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen{
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct virtual_backend_box *const *bboxes;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen unsigned int i, count;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen bboxes = array_get(&ctx->mbox->backend_boxes, &count);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen for (i = 0; i < count; i++)
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen virtual_backend_box_sync_mail_unset(bboxes[i]);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen}
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstatic int virtual_sync_finish(struct virtual_sync_context *ctx, bool success)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen{
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen int ret = success ? 0 : -1;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen virtual_sync_backend_boxes_finish(ctx);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (success) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (mail_index_sync_commit(&ctx->index_sync_ctx) < 0) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen mail_storage_set_index_error(&ctx->mbox->ibox);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ret = -1;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen }
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen } else {
939a0d82523538b2de38a02bc9f790a67b7ebf47Timo Sirainen if (ctx->index_broken) {
939a0d82523538b2de38a02bc9f790a67b7ebf47Timo Sirainen /* make sure we don't complain about the same errors
939a0d82523538b2de38a02bc9f790a67b7ebf47Timo Sirainen over and over again. */
939a0d82523538b2de38a02bc9f790a67b7ebf47Timo Sirainen if (mail_index_unlink(ctx->index) < 0) {
939a0d82523538b2de38a02bc9f790a67b7ebf47Timo Sirainen i_error("virtual index %s: Failed to unlink() "
939a0d82523538b2de38a02bc9f790a67b7ebf47Timo Sirainen "broken indexes: %m",
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen ctx->mbox->ibox.box.path);
939a0d82523538b2de38a02bc9f790a67b7ebf47Timo Sirainen }
939a0d82523538b2de38a02bc9f790a67b7ebf47Timo Sirainen }
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen mail_index_sync_rollback(&ctx->index_sync_ctx);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen }
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen i_free(ctx);
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen return ret;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen}
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstatic int virtual_sync(struct virtual_mailbox *mbox,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen enum mailbox_sync_flags flags)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen{
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct virtual_sync_context *ctx;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen enum mail_index_sync_flags index_sync_flags;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen int ret;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ctx = i_new(struct virtual_sync_context, 1);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ctx->mbox = mbox;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ctx->flags = flags;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ctx->index = mbox->ibox.index;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen /* Removed messages are expunged when
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen a) EXPUNGE is used
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen b) Mailbox is being opened (FIX_INCONSISTENT is set) */
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ctx->expunge_removed =
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen (ctx->flags & (MAILBOX_SYNC_FLAG_EXPUNGE |
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen MAILBOX_SYNC_FLAG_FIX_INCONSISTENT)) != 0;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen index_sync_flags = MAIL_INDEX_SYNC_FLAG_FLUSH_DIRTY |
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen MAIL_INDEX_SYNC_FLAG_AVOID_FLAG_UPDATES;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (!mbox->ibox.keep_recent)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen index_sync_flags |= MAIL_INDEX_SYNC_FLAG_DROP_RECENT;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ret = mail_index_sync_begin(ctx->index, &ctx->index_sync_ctx,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen &ctx->sync_view, &ctx->trans,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen index_sync_flags);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (ret <= 0) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (ret < 0)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen mail_storage_set_index_error(&mbox->ibox);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen i_free(ctx);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen return ret;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen }
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (!virtual_sync_ext_header_read(ctx))
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ctx->ext_header_rewrite = TRUE;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen /* apply changes from virtual index to backend mailboxes */
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen virtual_sync_index_changes(ctx);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen /* update list of UIDs in backend mailboxes */
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen if (virtual_sync_backend_boxes(ctx) < 0)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen return virtual_sync_finish(ctx, FALSE);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen virtual_sync_index_finish(ctx);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen return virtual_sync_finish(ctx, TRUE);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen}
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstruct mailbox_sync_context *
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenvirtual_storage_sync_init(struct mailbox *box, enum mailbox_sync_flags flags)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen{
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct virtual_mailbox *mbox = (struct virtual_mailbox *)box;
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen struct mailbox_sync_context *sync_ctx;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen int ret = 0;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen if (!box->opened) {
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen if (mailbox_open(box) < 0)
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen ret = -1;
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen }
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen if (index_mailbox_want_full_sync(&mbox->ibox, flags) && ret == 0)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ret = virtual_sync(mbox, flags);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen sync_ctx = index_mailbox_sync_init(box, flags, ret < 0);
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen virtual_sync_apply_existing_expunges(mbox, sync_ctx);
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen return sync_ctx;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen}