virtual-sync.c revision 905457e0982fc15930d90e174f271dc69f9afcf9
2e37d45867d081db150ab78dad303b9077aea24fTimo Sirainen/* Copyright (c) 2008-2009 Dovecot authors, see the included COPYING file */
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen const char *const *kw_all;
5a0ac2e5ef482016e00575a7dce83f52c1704732Timo Sirainen /* messages expunged within this sync */
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen ARRAY_DEFINE(all_adds, struct virtual_add_record);
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainenstatic void virtual_sync_set_uidvalidity(struct virtual_sync_context *ctx)
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen offsetof(struct mail_index_header, uid_validity),
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainenstatic void virtual_sync_external_flags(struct virtual_sync_context *ctx,
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen const char *const *kw_names;
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen if (!mail_set_uid(bbox->sync_mail, real_uid)) {
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen /* copy flags */
64055bc6d2ed9e25b3b1db3b5b90d0bdb77cd715Timo Sirainen mail_index_update_flags(ctx->trans, vseq, MODIFY_REPLACE, flags);
64055bc6d2ed9e25b3b1db3b5b90d0bdb77cd715Timo Sirainen /* copy keywords */
64055bc6d2ed9e25b3b1db3b5b90d0bdb77cd715Timo Sirainen kw_names = mail_get_keywords(bbox->sync_mail);
64055bc6d2ed9e25b3b1db3b5b90d0bdb77cd715Timo Sirainen keywords = mail_index_keywords_create(ctx->index, kw_names);
64055bc6d2ed9e25b3b1db3b5b90d0bdb77cd715Timo Sirainen mail_index_update_keywords(ctx->trans, vseq, MODIFY_REPLACE, keywords);
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainenstatic int virtual_sync_mail_cmp(const void *p1, const void *p2)
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen const struct virtual_sync_mail *m1 = p1, *m2 = p2;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen if (m1->vrec.mailbox_id < m2->vrec.mailbox_id)
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen if (m1->vrec.mailbox_id > m2->vrec.mailbox_id)
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainenvirtual_backend_box_sync_mail_set(struct virtual_backend_box *bbox)
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen trans = mailbox_transaction_begin(bbox->box, 0);
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainenvirtual_backend_box_sync_mail_unset(struct virtual_backend_box *bbox)
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainenstatic int bbox_mailbox_id_cmp(const void *p1, const void *p2)
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen const struct virtual_backend_box *const *b1 = p1, *const *b2 = p2;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainenstatic bool virtual_sync_ext_header_read(struct virtual_sync_context *ctx)
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen const struct virtual_mail_index_header *ext_hdr;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen const struct virtual_mail_index_mailbox_record *mailboxes;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen unsigned int i, count, ext_name_offset, ext_mailbox_count;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen mail_index_get_header_ext(ctx->sync_view, ctx->mbox->virtual_ext_id,
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen ctx->mbox->prev_uid_validity == hdr->uid_validity &&
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen ctx->mbox->prev_change_counter == ext_hdr->change_counter) {
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen /* fully refreshed */
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen ctx->mbox->prev_uid_validity = hdr->uid_validity;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen ctx->mbox->search_args_crc32 != ext_hdr->search_args_crc32) {
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen ctx->mbox->prev_change_counter = ext_hdr->change_counter;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen ext_hdr->mailbox_count > INT_MAX/sizeof(*mailboxes)) {
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen i_error("virtual index %s: Broken mailbox_count header",
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen /* update mailbox backends */
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen for (i = 0; i < ext_mailbox_count; i++) {
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen if (mailboxes[i].id > ext_hdr->highest_mailbox_id ||
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen i_error("virtual index %s: Broken mailbox id",
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen i_error("virtual index %s: Broken mailbox name_len",
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen if (ext_name_offset + mailboxes[i].name_len > ext_size) {
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen i_error("virtual index %s: Broken mailbox list",
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen const unsigned char *nameptr;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen nameptr = CONST_PTR_OFFSET(ext_data, ext_name_offset);
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen name = t_strndup(nameptr, mailboxes[i].name_len);
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen bbox = virtual_backend_box_lookup_name(ctx->mbox, name);
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen /* mailbox no longer exists */
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen bbox->sync_uid_validity = mailboxes[i].uid_validity;
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen bbox->sync_highest_modseq = mailboxes[i].highest_modseq;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen ctx->mbox->highest_mailbox_id = ext_hdr == NULL ? 0 :
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen /* assign new mailbox IDs if any are missing */
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen bboxes = array_get_modifiable(&ctx->mbox->backend_boxes, &count);
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen for (i = 0; i < count; i++) {
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen bboxes[i]->mailbox_id = ++ctx->mbox->highest_mailbox_id;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen /* sort the backend mailboxes by mailbox_id. */
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen qsort(bboxes, count, sizeof(*bboxes), bbox_mailbox_id_cmp);
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainenstatic void virtual_sync_ext_header_rewrite(struct virtual_sync_context *ctx)
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen struct virtual_mail_index_mailbox_record mailbox;
ab7b5b9286104974c2a572a499ccf8b56c5d2955Timo Sirainen bboxes = array_get_modifiable(&ctx->mbox->backend_boxes, &count);
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen name_pos = mailbox_pos + sizeof(mailbox) * count;
ab7b5b9286104974c2a572a499ccf8b56c5d2955Timo Sirainen ext_hdr.change_counter = ++ctx->mbox->prev_change_counter;
ab7b5b9286104974c2a572a499ccf8b56c5d2955Timo Sirainen ext_hdr.highest_mailbox_id = ctx->mbox->highest_mailbox_id;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen ext_hdr.search_args_crc32 = ctx->mbox->search_args_crc32;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen buf = buffer_create_dynamic(pool_datastack_create(), name_pos + 256);
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen buffer_append(buf, &ext_hdr, sizeof(ext_hdr));
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen for (i = 0; i < count; i++) {
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen bboxes[i]->mailbox_id > bboxes[i-1]->mailbox_id);
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen mailbox.uid_validity = bboxes[i]->sync_uid_validity;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen mailbox.highest_modseq = bboxes[i]->sync_highest_modseq;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen buffer_write(buf, mailbox_pos, &mailbox, sizeof(mailbox));
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen buffer_write(buf, name_pos, bboxes[i]->name, mailbox.name_len);
ab7b5b9286104974c2a572a499ccf8b56c5d2955Timo Sirainen mail_index_get_header_ext(ctx->sync_view, ctx->mbox->virtual_ext_id,
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen mail_index_ext_resize(ctx->trans, ctx->mbox->virtual_ext_id,
sizeof(struct virtual_mail_index_record),
sizeof(uint32_t));
const void *data;
bool expunged;
if (flags != 0) {
if (flags != 0) {
kw_names);
kw_names);
keywords);
i_unreached();
int ret;
return ret;
if (uid_count == 0)
sizeof(*uidmap),
i_unreached();
for (i = 0; i < uid_count; i++) {
&vseq))
if (uid_count == 0)
if (rec_count == 0 ||
sizeof(*uidmap),
&dest))
i_unreached();
for (i = 0; i < uid_count; i++) {
unsigned int uid_count;
unsigned int i, count;
for (i = 0; i < count; i++) {
const void *data;
bool expunged;
&seq)) {
old_msg_count = 0;
unsigned int i, n = 0, count;
if (i == count)
for (; i < count; ) {
unsigned int i, n = 0, count;
if (i == count)
for (; i < count; ) {
&added_uids);
&temp_uids);
&removed_uids);
&removed_uids);
unsigned int *idx1_r,
unsigned int *idx2_r)
return FALSE;
return TRUE;
case MAILBOX_SYNC_TYPE_FLAGS:
case MAILBOX_SYNC_TYPE_MODSEQ:
const unsigned int uidval_pos =
unsigned int mailbox_offset;
int ret;
&status) < 0)
} T_END;
return ret;
const void *data;
bool expunged;
unsigned int j = 0, uidmap_count = 0;
for (i = 0; i < messages; i++) {
for (; j < uidmap_count; j++) {
&uidmap_count);
for (; j < uidmap_count; j++) {
for (; j < uidmap_count; j++) {
for (i = 0; i < count; i++) {
for (j = 0; j < uidmap_count; j++) {
unsigned int i, count;
i_unreached();
if (count == 0) {
sizeof(*uidmap),
&idx))
i_unreached();
const void *data;
bool expunged;
const void *data;
bool expunged;
unsigned int i, count;
for (i = 0; i < count; i++) {
unsigned int i, count;
for (i = 0; i < count; i++)
if (success) {
return ret;
int ret;
if (ret <= 0) {
if (ret < 0)
return ret;
struct mailbox_sync_context *
int ret = 0;
return sync_ctx;