virtual-sync.c revision 7ab62198bfb9ac210b11ea7a147234e0f72f6448
2e37d45867d081db150ab78dad303b9077aea24fTimo Sirainen/* Copyright (c) 2008-2014 Dovecot authors, see the included COPYING file */
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen const char *const *kw_all;
ad004e44be109684521494b5af2ad1da39b8bb27Timo Sirainen /* messages expunged within this sync */
f059a046515f4b2b15a6c2a10a6f12f6166e39a5Timo Sirainenstatic void virtual_sync_set_uidvalidity(struct virtual_sync_context *ctx)
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen offsetof(struct mail_index_header, uid_validity),
5a250816ffc4cc5db203f9410ea99b6601c7b91aTimo Sirainenstatic void virtual_sync_external_flags(struct virtual_sync_context *ctx,
4c6ddf2491104f917d00e6900e833e80ea02c7b6Timo Sirainen const char *const *kw_names;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen if (!mail_set_uid(bbox->sync_mail, real_uid)) {
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen /* copy flags */
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen mail_index_update_flags(ctx->trans, vseq, MODIFY_REPLACE, flags);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen /* copy keywords */
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen kw_names = mail_get_keywords(bbox->sync_mail);
6fdfa4d4cf14d1d7764d7faa8258f112e39c8dbeTimo Sirainen keywords = mail_index_keywords_create(ctx->index, kw_names);
6fdfa4d4cf14d1d7764d7faa8258f112e39c8dbeTimo Sirainen mail_index_update_keywords(ctx->trans, vseq, MODIFY_REPLACE, keywords);
5a9e240ebf8d0daaf029973973b52e415148070bTimo Sirainenstatic int virtual_sync_mail_cmp(const void *p1, const void *p2)
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen const struct virtual_sync_mail *m1 = p1, *m2 = p2;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen if (m1->vrec.mailbox_id < m2->vrec.mailbox_id)
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen if (m1->vrec.mailbox_id > m2->vrec.mailbox_id)
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenvirtual_backend_box_sync_mail_set(struct virtual_backend_box *bbox)
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen trans = mailbox_transaction_begin(bbox->box, 0);
54533aa265f5c87730022cc7576090bc51370f97Timo Sirainenvirtual_backend_box_sync_mail_unset(struct virtual_backend_box *bbox)
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenstatic int bbox_mailbox_id_cmp(struct virtual_backend_box *const *b1,
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenvirtual_sync_get_backend_box(struct virtual_sync_context *ctx, const char *name,
5a9e240ebf8d0daaf029973973b52e415148070bTimo Sirainen *bbox_r = virtual_backend_box_lookup_name(ctx->mbox, name);
5a9e240ebf8d0daaf029973973b52e415148070bTimo Sirainen if (*bbox_r != NULL || !ctx->mbox->sync_initialized)
a75907609d7c410c9e17beedfafbf28b4439fa8aTimo Sirainen /* another process just added a new mailbox.
a75907609d7c410c9e17beedfafbf28b4439fa8aTimo Sirainen we can't handle this currently. */
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen mail_storage_set_error(ctx->mbox->box.storage, MAIL_ERROR_TEMP,
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen "Backend mailbox added by another session. "
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen "Reopen the virtual mailbox.");
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenstatic int virtual_sync_ext_header_read(struct virtual_sync_context *ctx)
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen const char *box_path = mailbox_get_path(&ctx->mbox->box);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen const struct virtual_mail_index_header *ext_hdr;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen const struct virtual_mail_index_mailbox_record *mailboxes;
05e55893a799de645fc8cd2203d6013f0e0f1b79Timo Sirainen unsigned int i, count, ext_name_offset, ext_mailbox_count;
2aac7ca853f63b62ea79ef8eae9ded83ed6063a5Timo Sirainen mail_index_get_header_ext(ctx->sync_view, ctx->mbox->virtual_ext_id,
5d2e7ec2ea725c8a6a63f56b771e746f93e782ecTimo Sirainen ctx->mbox->prev_uid_validity == hdr->uid_validity &&
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen ctx->mbox->prev_change_counter == ext_hdr->change_counter) {
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen /* fully refreshed */
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen ctx->mbox->prev_uid_validity = hdr->uid_validity;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen ctx->mbox->search_args_crc32 != ext_hdr->search_args_crc32) {
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen ctx->mbox->prev_change_counter = ext_hdr->change_counter;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen ext_hdr->mailbox_count > INT_MAX/sizeof(*mailboxes)) {
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen i_error("virtual index %s: Broken mailbox_count header",
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen /* update mailbox backends */
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen for (i = 0; i < ext_mailbox_count; i++) {
ec23e16ed879e289d12c6e1a5f9745dd3979004aTimo Sirainen if (mailboxes[i].id > ext_hdr->highest_mailbox_id ||
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen i_error("virtual index %s: Broken mailbox id",
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen i_error("virtual index %s: Broken mailbox name_len",
6303191abcb37164f435ccdc56e9dbddf1288851Timo Sirainen if (ext_name_offset + mailboxes[i].name_len > ext_size) {
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen i_error("virtual index %s: Broken mailbox list",
54533aa265f5c87730022cc7576090bc51370f97Timo Sirainen const unsigned char *nameptr;
54533aa265f5c87730022cc7576090bc51370f97Timo Sirainen nameptr = CONST_PTR_OFFSET(ext_data, ext_name_offset);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen name = t_strndup(nameptr, mailboxes[i].name_len);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen if (virtual_sync_get_backend_box(ctx, name, &bbox) < 0)
6fdfa4d4cf14d1d7764d7faa8258f112e39c8dbeTimo Sirainen /* mailbox no longer exists. */
6fdfa4d4cf14d1d7764d7faa8258f112e39c8dbeTimo Sirainen bbox->sync_uid_validity = mailboxes[i].uid_validity;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen ctx->mbox->highest_mailbox_id = ext_hdr == NULL ? 0 :
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen /* assign new mailbox IDs if any are missing */
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen bboxes = array_get_modifiable(&ctx->mbox->backend_boxes, &count);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen for (i = 0; i < count; i++) {
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen bboxes[i]->mailbox_id = ++ctx->mbox->highest_mailbox_id;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen /* sort the backend mailboxes by mailbox_id. */
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen array_sort(&ctx->mbox->backend_boxes, bbox_mailbox_id_cmp);
cec3230c9b2a96bac1ea42c69475e8aea4b91eabTimo Sirainenstatic void virtual_sync_ext_header_rewrite(struct virtual_sync_context *ctx)
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen struct virtual_mail_index_mailbox_record mailbox;
cec3230c9b2a96bac1ea42c69475e8aea4b91eabTimo Sirainen bboxes = array_get_modifiable(&ctx->mbox->backend_boxes, &count);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen name_pos = mailbox_pos + sizeof(mailbox) * count;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen ext_hdr.change_counter = ++ctx->mbox->prev_change_counter;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen ext_hdr.highest_mailbox_id = ctx->mbox->highest_mailbox_id;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen ext_hdr.search_args_crc32 = ctx->mbox->search_args_crc32;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen buf = buffer_create_dynamic(pool_datastack_create(), name_pos + 256);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen buffer_append(buf, &ext_hdr, sizeof(ext_hdr));
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen for (i = 0; i < count; i++) {
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen bboxes[i]->mailbox_id > bboxes[i-1]->mailbox_id);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen mailbox.uid_validity = bboxes[i]->sync_uid_validity;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen mailbox.highest_modseq = bboxes[i]->ondisk_highest_modseq;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen buffer_write(buf, mailbox_pos, &mailbox, sizeof(mailbox));
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen buffer_write(buf, name_pos, bboxes[i]->name, mailbox.name_len);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen mail_index_get_header_ext(ctx->sync_view, ctx->mbox->virtual_ext_id,
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen mail_index_ext_resize(ctx->trans, ctx->mbox->virtual_ext_id,
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen mail_index_update_header_ext(ctx->trans, ctx->mbox->virtual_ext_id,
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenstatic void virtual_sync_ext_header_update(struct virtual_sync_context *ctx)
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen /* we changed something - update the change counter in header */
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen ext_hdr.change_counter = ++ctx->mbox->prev_change_counter;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen mail_index_update_header_ext(ctx->trans, ctx->mbox->virtual_ext_id,
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen offsetof(struct virtual_mail_index_header, change_counter),
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen &ext_hdr.change_counter, sizeof(ext_hdr.change_counter));
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenstatic int virtual_sync_index_rec(struct virtual_sync_context *ctx,
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen uint32_t virtual_ext_id = ctx->mbox->virtual_ext_id;
f77ffa31038d46ca9c6d24d93e3d76c9aa8d4d0cTimo Sirainen if (!mail_index_lookup_seq_range(ctx->sync_view,
f77ffa31038d46ca9c6d24d93e3d76c9aa8d4d0cTimo Sirainen /* already expunged, nothing to do. */
f77ffa31038d46ca9c6d24d93e3d76c9aa8d4d0cTimo Sirainen mail_index_lookup_ext(ctx->sync_view, vseq, virtual_ext_id,
9439bed2f07d6475febd8a247cd2f0990fb32a13Timo Sirainen bbox = virtual_backend_box_lookup(ctx->mbox, vrec->mailbox_id);
45155bb1250cf5a120278f349465aded513a100fTimo Sirainen if (!mail_set_uid(bbox->sync_mail, vrec->real_uid)) {
45155bb1250cf5a120278f349465aded513a100fTimo Sirainen /* message is already expunged from backend mailbox. */
9439bed2f07d6475febd8a247cd2f0990fb32a13Timo Sirainen flags = sync_rec->add_flags & MAIL_FLAGS_NONRECENT;
4e35bae013cee5a06d281776a347b534b958aaa4Timo Sirainen flags = sync_rec->remove_flags & MAIL_FLAGS_NONRECENT;
bb8d0ec26bdd548624d7a7424071cca693b72f55Timo Sirainen kw_names[0] = ctx->kw_all[sync_rec->keyword_idx];
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen keywords = mailbox_keywords_create_valid(bbox->box,
9439bed2f07d6475febd8a247cd2f0990fb32a13Timo Sirainenstatic int virtual_sync_index_changes(struct virtual_sync_context *ctx)
88c92ce2caa8f9fa34708471c6ed4e974d5a7953Timo Sirainen keywords = mail_index_get_keywords(ctx->index);
88c92ce2caa8f9fa34708471c6ed4e974d5a7953Timo Sirainen ctx->kw_all = array_count(keywords) == 0 ? NULL :
9439bed2f07d6475febd8a247cd2f0990fb32a13Timo Sirainen while (mail_index_sync_next(ctx->index_sync_ctx, &sync_rec)) {
9439bed2f07d6475febd8a247cd2f0990fb32a13Timo Sirainen if (virtual_sync_index_rec(ctx, &sync_rec) < 0)
9439bed2f07d6475febd8a247cd2f0990fb32a13Timo Sirainenstatic void virtual_sync_index_finish(struct virtual_sync_context *ctx)
9439bed2f07d6475febd8a247cd2f0990fb32a13Timo Sirainen /* mark the newly seen messages as recent */
9439bed2f07d6475febd8a247cd2f0990fb32a13Timo Sirainen if (mail_index_lookup_seq_range(ctx->sync_view, hdr->first_recent_uid,
9439bed2f07d6475febd8a247cd2f0990fb32a13Timo Sirainen index_mailbox_set_recent_seq(&ctx->mbox->box, ctx->sync_view,
9439bed2f07d6475febd8a247cd2f0990fb32a13Timo Sirainen /* entire mailbox list needs to be rewritten */
9439bed2f07d6475febd8a247cd2f0990fb32a13Timo Sirainen /* update only changed parts in the header */
9439bed2f07d6475febd8a247cd2f0990fb32a13Timo Sirainenstatic int virtual_sync_backend_box_init(struct virtual_backend_box *bbox)
97db4761382024093f441e4bc78ba8b6a056504dTimo Sirainen enum mailbox_search_result_flags result_flags;
9439bed2f07d6475febd8a247cd2f0990fb32a13Timo Sirainen trans = mailbox_transaction_begin(bbox->box, 0);
19e8adccba16ff419f5675b1575358c2956dce83Timo Sirainen mail_search_args_init(bbox->search_args, bbox->box, FALSE, NULL);
8372fc7efb6d64dff2e5f55fb4a3822c56869cfeTimo Sirainen search_ctx = mailbox_search_init(trans, bbox->search_args, NULL,
8372fc7efb6d64dff2e5f55fb4a3822c56869cfeTimo Sirainen /* save the result and keep it updated */
8372fc7efb6d64dff2e5f55fb4a3822c56869cfeTimo Sirainen result_flags = MAILBOX_SEARCH_RESULT_FLAG_UPDATE |
1d4f710106fb498750456724628da6063e012e6dTimo Sirainen mailbox_search_result_save(search_ctx, result_flags);
b9c76fe9d9ca194816606342da1ddbd9be6bc8abTimo Sirainen /* add the found UIDs to uidmap. virtual_uid gets assigned later. */
b9c76fe9d9ca194816606342da1ddbd9be6bc8abTimo Sirainen while (mailbox_search_next(search_ctx, &mail)) {
89795c6bbbc52bb382e88bc8617d22092223e9a5Timo Sirainenvirtual_backend_uidmap_bsearch_cmp(const uint32_t *uidp,
89795c6bbbc52bb382e88bc8617d22092223e9a5Timo Sirainenvirtual_sync_mailbox_box_remove(struct virtual_sync_context *ctx,
89795c6bbbc52bb382e88bc8617d22092223e9a5Timo Sirainen unsigned int i, src, dest, uid_count, rec_count;
434abef12f61881a5cfa28d27193d0854a9639a0Timo Sirainen /* everything in removed_uids should exist in bbox->uids */
434abef12f61881a5cfa28d27193d0854a9639a0Timo Sirainen uidmap = array_get_modifiable(&bbox->uids, &rec_count);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen /* find the first uidmap record to be removed */
38318f5e82662615cd88e99e398efe4a630ce020Timo Sirainen if (!array_bsearch_insert_pos(&bbox->uids, &uids[0].seq1,
b9c76fe9d9ca194816606342da1ddbd9be6bc8abTimo Sirainen /* remove the unwanted messages */
b9c76fe9d9ca194816606342da1ddbd9be6bc8abTimo Sirainen for (i = 0; i < uid_count; i++) {
28dae6a0064e79f86da091625b0f2b92336a2a91Timo Sirainenvirtual_sync_mailbox_box_add(struct virtual_sync_context *ctx,
3ffb7fd86484c474b42f3f1e981ab0f7168b5df9Timo Sirainen unsigned int i, src, dest, uid_count, add_count, rec_count;
7c849dbc7be089175c1a83a84ee7249ed695810dTimo Sirainen added_uids = array_get(added_uids_arr, &uid_count);
843640f0ca224bb9999acb290bca5f76037ab984Timo Sirainen /* none of added_uids should exist in bbox->uids. find the position
28dae6a0064e79f86da091625b0f2b92336a2a91Timo Sirainen of the first inserted index. */
28dae6a0064e79f86da091625b0f2b92336a2a91Timo Sirainen uidmap = array_get_modifiable(&bbox->uids, &rec_count);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen added_uids[0].seq1 > uidmap[rec_count-1].real_uid) {
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen /* fast path: usually messages are appended */
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen } else if (array_bsearch_insert_pos(&bbox->uids, &added_uids[0].seq1,
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen /* make space for all added UIDs. */
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen array_idx_clear(&bbox->uids, dest + add_count-1);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen uidmap = array_get_modifiable(&bbox->uids, &rec_count);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen /* add/move the UIDs to their correct positions */
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen for (i = 0; i < uid_count; i++) {
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen while (src < rec_count && uidmap[src].real_uid < add_uid)
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen for (; add_uid <= added_uids[i].seq2; add_uid++, dest++) {
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenstatic int virtual_backend_uidmap_cmp(const struct virtual_backend_uidmap *u1,
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenstatic void virtual_sync_bbox_uids_sort(struct virtual_backend_box *bbox)
unsigned int i, count;
for (i = 0; i < count; i++) {
const void *data;
old_msg_count = 0;
&removed_uids);
unsigned int i, n = 0, count;
if (i == count)
for (; i < count; ) {
unsigned int i, n = 0, count;
for (; i < count; ) {
if (!iter_done) {
&added_uids);
&temp_uids);
&removed_uids);
&removed_uids);
unsigned int *idx1_r,
unsigned int *idx2_r)
&idx);
return FALSE;
return TRUE;
&idx1);
case MAILBOX_SYNC_TYPE_FLAGS:
case MAILBOX_SYNC_TYPE_MODSEQ:
const unsigned int uidval_pos =
unsigned int mailbox_offset;
int ret;
&status) < 0)
if (!bbox_index_opened) {
} T_END;
return ret;
const void *data;
unsigned int j = 0, uidmap_count = 0;
if (messages == 0)
for (i = 0; i < messages; i++) {
for (; j < uidmap_count; 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) {
&idx))
i_unreached();
const void *data;
const void *data;
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;
if (ret < 0)
if (ret == 0)
struct mailbox_sync_context *
int ret = 0;
return sync_ctx;