virtual-sync.c revision ce930f99c6a78f2c74b00df1ad2337095978a9db
bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2008-2014 Dovecot authors, see the included COPYING file */
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch const char *const *kw_all;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch /* messages expunged within this sync */
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch /* all messages in this sync, sorted by mailbox_id
66134fbce11778241ca9f8458ee2a0488a05bde0Stephan Bosch (but unsorted inside it for now, since it doesn't matter) */
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch uint32_t all_mails_idx, all_mails_prev_mailbox_id;
66134fbce11778241ca9f8458ee2a0488a05bde0Stephan Boschstatic void virtual_sync_set_uidvalidity(struct virtual_sync_context *ctx)
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch offsetof(struct mail_index_header, uid_validity),
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Boschstatic void virtual_sync_external_flags(struct virtual_sync_context *ctx,
f883bf3eff62f5d27df5ee9ee664edc38a77937fStephan Bosch const char *const *kw_names;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch if (!mail_set_uid(bbox->sync_mail, real_uid)) {
66134fbce11778241ca9f8458ee2a0488a05bde0Stephan Bosch /* we may have reopened the mailbox, which could have
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch caused the mail to be expunged already. */
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch /* copy flags */
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch mail_index_update_flags(ctx->trans, vseq, MODIFY_REPLACE, flags);
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch /* copy keywords */
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch kw_names = mail_get_keywords(bbox->sync_mail);
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch keywords = mail_index_keywords_create(ctx->index, kw_names);
66134fbce11778241ca9f8458ee2a0488a05bde0Stephan Bosch mail_index_update_keywords(ctx->trans, vseq, MODIFY_REPLACE, keywords);
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Boschstatic int virtual_sync_mail_uid_cmp(const void *p1, const void *p2)
66134fbce11778241ca9f8458ee2a0488a05bde0Stephan Bosch const struct virtual_sync_mail *m1 = p1, *m2 = p2;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch if (m1->vrec.mailbox_id < m2->vrec.mailbox_id)
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch if (m1->vrec.mailbox_id > m2->vrec.mailbox_id)
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Boschvirtual_backend_box_sync_mail_set(struct virtual_backend_box *bbox)
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch trans = mailbox_transaction_begin(bbox->box, 0);
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Boschstatic int bbox_mailbox_id_cmp(struct virtual_backend_box *const *b1,
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Boschvirtual_sync_get_backend_box(struct virtual_sync_context *ctx, const char *name,
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch *bbox_r = virtual_backend_box_lookup_name(ctx->mbox, name);
66134fbce11778241ca9f8458ee2a0488a05bde0Stephan Bosch if (*bbox_r != NULL || !ctx->mbox->sync_initialized)
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch /* another process just added a new mailbox.
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch we can't handle this currently. */
66134fbce11778241ca9f8458ee2a0488a05bde0Stephan Bosch mail_storage_set_error(ctx->mbox->box.storage, MAIL_ERROR_TEMP,
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch "Backend mailbox added by another session. "
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch "Reopen the virtual mailbox.");
66134fbce11778241ca9f8458ee2a0488a05bde0Stephan Boschstatic int virtual_sync_ext_header_read(struct virtual_sync_context *ctx)
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch const char *box_path = mailbox_get_path(&ctx->mbox->box);
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch const struct virtual_mail_index_header *ext_hdr;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch const struct virtual_mail_index_mailbox_record *mailboxes;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch unsigned int i, count, ext_name_offset, ext_mailbox_count;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch mail_index_get_header_ext(ctx->sync_view, ctx->mbox->virtual_ext_id,
66134fbce11778241ca9f8458ee2a0488a05bde0Stephan Bosch ctx->mbox->prev_uid_validity == hdr->uid_validity &&
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch ctx->mbox->prev_change_counter == ext_hdr->change_counter) {
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch /* fully refreshed */
66134fbce11778241ca9f8458ee2a0488a05bde0Stephan Bosch ctx->mbox->prev_uid_validity = hdr->uid_validity;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch ctx->mbox->search_args_crc32 != ext_hdr->search_args_crc32) {
66134fbce11778241ca9f8458ee2a0488a05bde0Stephan Bosch ctx->mbox->prev_change_counter = ext_hdr->change_counter;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch ext_hdr->mailbox_count > INT_MAX/sizeof(*mailboxes)) {
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch i_error("virtual index %s: Broken mailbox_count header",
66134fbce11778241ca9f8458ee2a0488a05bde0Stephan Bosch /* update mailbox backends */
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch for (i = 0; i < ext_mailbox_count; i++) {
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch if (mailboxes[i].id > ext_hdr->highest_mailbox_id ||
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch i_error("virtual index %s: Broken mailbox id",
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch i_error("virtual index %s: Broken mailbox name_len",
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch if (ext_name_offset + mailboxes[i].name_len > ext_size) {
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch i_error("virtual index %s: Broken mailbox list",
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch const unsigned char *nameptr;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch nameptr = CONST_PTR_OFFSET(ext_data, ext_name_offset);
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch name = t_strndup(nameptr, mailboxes[i].name_len);
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch if (virtual_sync_get_backend_box(ctx, name, &bbox) < 0)
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch /* mailbox no longer exists. */
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch bbox->sync_uid_validity = mailboxes[i].uid_validity;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch ctx->mbox->highest_mailbox_id = ext_hdr == NULL ? 0 :
66134fbce11778241ca9f8458ee2a0488a05bde0Stephan Bosch /* assign new mailbox IDs if any are missing */
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch bboxes = array_get_modifiable(&ctx->mbox->backend_boxes, &count);
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch for (i = 0; i < count; i++) {
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch bboxes[i]->mailbox_id = ++ctx->mbox->highest_mailbox_id;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch /* sort the backend mailboxes by mailbox_id. */
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch array_sort(&ctx->mbox->backend_boxes, bbox_mailbox_id_cmp);
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Boschstatic void virtual_sync_ext_header_rewrite(struct virtual_sync_context *ctx)
66134fbce11778241ca9f8458ee2a0488a05bde0Stephan Bosch struct virtual_mail_index_mailbox_record mailbox;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch bboxes = array_get_modifiable(&ctx->mbox->backend_boxes, &count);
66134fbce11778241ca9f8458ee2a0488a05bde0Stephan Bosch name_pos = mailbox_pos + sizeof(mailbox) * count;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch ext_hdr.change_counter = ++ctx->mbox->prev_change_counter;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch ext_hdr.highest_mailbox_id = ctx->mbox->highest_mailbox_id;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch ext_hdr.search_args_crc32 = ctx->mbox->search_args_crc32;
66134fbce11778241ca9f8458ee2a0488a05bde0Stephan Bosch buf = buffer_create_dynamic(pool_datastack_create(), name_pos + 256);
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch buffer_append(buf, &ext_hdr, sizeof(ext_hdr));
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch for (i = 0; i < count; i++) {
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch bboxes[i]->mailbox_id > bboxes[i-1]->mailbox_id);
66134fbce11778241ca9f8458ee2a0488a05bde0Stephan Bosch mailbox.uid_validity = bboxes[i]->sync_uid_validity;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch mailbox.highest_modseq = bboxes[i]->ondisk_highest_modseq;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch buffer_write(buf, mailbox_pos, &mailbox, sizeof(mailbox));
66134fbce11778241ca9f8458ee2a0488a05bde0Stephan Bosch buffer_write(buf, name_pos, bboxes[i]->name, mailbox.name_len);
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch mail_index_get_header_ext(ctx->sync_view, ctx->mbox->virtual_ext_id,
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch mail_index_ext_resize(ctx->trans, ctx->mbox->virtual_ext_id,
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch mail_index_update_header_ext(ctx->trans, ctx->mbox->virtual_ext_id,
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Boschstatic void virtual_sync_ext_header_update(struct virtual_sync_context *ctx)
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch /* we changed something - update the change counter in header */
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch ext_hdr.change_counter = ++ctx->mbox->prev_change_counter;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch mail_index_update_header_ext(ctx->trans, ctx->mbox->virtual_ext_id,
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch offsetof(struct virtual_mail_index_header, change_counter),
66134fbce11778241ca9f8458ee2a0488a05bde0Stephan Bosch &ext_hdr.change_counter, sizeof(ext_hdr.change_counter));
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Boschstatic int virtual_sync_index_rec(struct virtual_sync_context *ctx,
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch uint32_t virtual_ext_id = ctx->mbox->virtual_ext_id;
62aa68310d6f42467ca26880f678173bf1d26a83Stephan Bosch if (!mail_index_lookup_seq_range(ctx->sync_view,
66134fbce11778241ca9f8458ee2a0488a05bde0Stephan Bosch /* already expunged, nothing to do. */
66134fbce11778241ca9f8458ee2a0488a05bde0Stephan Bosch mail_index_lookup_ext(ctx->sync_view, vseq, virtual_ext_id,
62aa68310d6f42467ca26880f678173bf1d26a83Stephan Bosch bbox = virtual_backend_box_lookup(ctx->mbox, vrec->mailbox_id);
62aa68310d6f42467ca26880f678173bf1d26a83Stephan Bosch if (virtual_backend_box_open(ctx->mbox, bbox) < 0) {
62aa68310d6f42467ca26880f678173bf1d26a83Stephan Bosch virtual_backend_box_accessed(ctx->mbox, bbox);
62aa68310d6f42467ca26880f678173bf1d26a83Stephan Bosch if (!mail_set_uid(bbox->sync_mail, vrec->real_uid)) {
62aa68310d6f42467ca26880f678173bf1d26a83Stephan Bosch /* message is already expunged from backend mailbox. */
62aa68310d6f42467ca26880f678173bf1d26a83Stephan Bosch flags = sync_rec->add_flags & MAIL_FLAGS_NONRECENT;
62aa68310d6f42467ca26880f678173bf1d26a83Stephan Bosch flags = sync_rec->remove_flags & MAIL_FLAGS_NONRECENT;
62aa68310d6f42467ca26880f678173bf1d26a83Stephan Bosch kw_names[0] = ctx->kw_all[sync_rec->keyword_idx];
62aa68310d6f42467ca26880f678173bf1d26a83Stephan Bosch keywords = mailbox_keywords_create_valid(bbox->box,
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Boschstatic int virtual_sync_index_changes(struct virtual_sync_context *ctx)
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch keywords = mail_index_get_keywords(ctx->index);
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch ctx->kw_all = array_count(keywords) == 0 ? NULL :
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch while (mail_index_sync_next(ctx->index_sync_ctx, &sync_rec)) {
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch if (virtual_sync_index_rec(ctx, &sync_rec) < 0)
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Boschstatic void virtual_sync_index_finish(struct virtual_sync_context *ctx)
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch /* mark the newly seen messages as recent */
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch if (mail_index_lookup_seq_range(ctx->sync_view, hdr->first_recent_uid,
66134fbce11778241ca9f8458ee2a0488a05bde0Stephan Bosch index_mailbox_set_recent_seq(&ctx->mbox->box, ctx->sync_view,
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch /* entire mailbox list needs to be rewritten */
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch /* update only changed parts in the header */
66134fbce11778241ca9f8458ee2a0488a05bde0Stephan Boschstatic int virtual_sync_backend_box_init(struct virtual_backend_box *bbox)
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch enum mailbox_search_result_flags result_flags;
66134fbce11778241ca9f8458ee2a0488a05bde0Stephan Bosch trans = mailbox_transaction_begin(bbox->box, 0);
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch mail_search_args_init(bbox->search_args, bbox->box, FALSE, NULL);
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch search_ctx = mailbox_search_init(trans, bbox->search_args, NULL,
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch /* save the result and keep it updated */
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch result_flags = MAILBOX_SEARCH_RESULT_FLAG_UPDATE |
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch mailbox_search_result_save(search_ctx, result_flags);
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch /* add the found UIDs to uidmap. virtual_uid gets assigned later. */
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch while (mailbox_search_next(search_ctx, &mail)) {
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Boschvirtual_backend_uidmap_bsearch_cmp(const uint32_t *uidp,
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Boschvirtual_sync_mailbox_box_remove(struct virtual_sync_context *ctx,
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch unsigned int i, src, dest, uid_count, rec_count;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch /* everything in removed_uids should exist in bbox->uids */
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch uidmap = array_get_modifiable(&bbox->uids, &rec_count);
66134fbce11778241ca9f8458ee2a0488a05bde0Stephan Bosch /* find the first uidmap record to be removed */
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch if (!array_bsearch_insert_pos(&bbox->uids, &uids[0].seq1,
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch /* remove the unwanted messages */
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch for (i = 0; i < uid_count; i++) {
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Boschvirtual_sync_mailbox_box_add(struct virtual_sync_context *ctx,
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch unsigned int i, src, dest, uid_count, add_count, rec_count;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch added_uids = array_get(added_uids_arr, &uid_count);
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch /* none of added_uids should exist in bbox->uids. find the position
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch of the first inserted index. */
66134fbce11778241ca9f8458ee2a0488a05bde0Stephan Bosch uidmap = array_get_modifiable(&bbox->uids, &rec_count);
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch added_uids[0].seq1 > uidmap[rec_count-1].real_uid) {
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch /* fast path: usually messages are appended */
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch } else if (array_bsearch_insert_pos(&bbox->uids, &added_uids[0].seq1,
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch /* make space for all added UIDs. */
66134fbce11778241ca9f8458ee2a0488a05bde0Stephan Bosch array_idx_clear(&bbox->uids, dest + add_count-1);
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch uidmap = array_get_modifiable(&bbox->uids, &rec_count);
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch /* add/move the UIDs to their correct positions */
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch for (i = 0; i < uid_count; i++) {
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch while (src < rec_count && uidmap[src].real_uid < add_uid)
66134fbce11778241ca9f8458ee2a0488a05bde0Stephan Bosch for (; add_uid <= added_uids[i].seq2; add_uid++, dest++) {
e9c433c5b8ef9a1b4246ebe10beb90fa01e05cafPhil Carmodystatic int virtual_backend_uidmap_cmp(const struct virtual_backend_uidmap *u1,
e9c433c5b8ef9a1b4246ebe10beb90fa01e05cafPhil Carmodystatic void virtual_sync_bbox_uids_sort(struct virtual_backend_box *bbox)
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch /* the uidmap must be sorted by real_uids */
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch array_sort(&bbox->uids, virtual_backend_uidmap_cmp);
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainenstatic void virtual_sync_backend_boxes_sort_uids(struct virtual_mailbox *mbox)
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch unsigned int i, count;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch bboxes = array_get(&mbox->backend_boxes, &count);
66134fbce11778241ca9f8458ee2a0488a05bde0Stephan Bosch for (i = 0; i < count; i++) {
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Boschvirtual_sync_backend_add_vmsgs_results(struct virtual_sync_context *ctx,
289ce1246b1c7511f8ceec02344316064ffbe546Phil Carmody mail_index_lookup_uid(ctx->sync_view, vseq, &vuid);
e87b25495e78a6f61f236b188bfd68070eff3295Phil Carmody else if (mail_index_lookup_seq(bbox->box->view, real_uid, &seq))
66134fbce11778241ca9f8458ee2a0488a05bde0Stephan Bosch seq_range_array_add(&result->removed_uids, real_uid);
66134fbce11778241ca9f8458ee2a0488a05bde0Stephan Boschvirtual_sync_backend_handle_old_vmsgs(struct virtual_sync_context *ctx,
289ce1246b1c7511f8ceec02344316064ffbe546Phil Carmody const struct virtual_sync_mail *sync_mail, *sync_mails;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch /* find the messages that currently exist in virtual index and add them
e87b25495e78a6f61f236b188bfd68070eff3295Phil Carmody to the backend mailbox's list of uids. */
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch i_assert(ctx->all_mails_prev_mailbox_id < bbox->mailbox_id);
e87b25495e78a6f61f236b188bfd68070eff3295Phil Carmody sync_mails = array_get(&ctx->all_mails, &messages);
289ce1246b1c7511f8ceec02344316064ffbe546Phil Carmody for (i = ctx->all_mails_idx; i < messages; i++) {
e87b25495e78a6f61f236b188bfd68070eff3295Phil Carmody if (sync_mail->vrec.mailbox_id != bbox->mailbox_id) {
289ce1246b1c7511f8ceec02344316064ffbe546Phil Carmody if (sync_mail->vrec.mailbox_id < bbox->mailbox_id) {
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch /* stale mailbox_id, ignore */
289ce1246b1c7511f8ceec02344316064ffbe546Phil Carmody /* Should be in mailbox_id order,
289ce1246b1c7511f8ceec02344316064ffbe546Phil Carmody so skip to next box */
289ce1246b1c7511f8ceec02344316064ffbe546Phil Carmody virtual_sync_backend_add_vmsgs_results(ctx, bbox,
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch sync_mail->vrec.real_uid, result, sync_mail->vseq);
289ce1246b1c7511f8ceec02344316064ffbe546Phil Carmody ctx->all_mails_prev_mailbox_id = bbox->mailbox_id;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch /* there should be only a single backend mailbox, but in the
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch existing index there may be stale mailbox_ids that we'll
e87b25495e78a6f61f236b188bfd68070eff3295Phil Carmody just skip over. */
289ce1246b1c7511f8ceec02344316064ffbe546Phil Carmody messages = mail_index_view_get_messages_count(ctx->sync_view);
289ce1246b1c7511f8ceec02344316064ffbe546Phil Carmodystatic int virtual_sync_backend_box_continue(struct virtual_sync_context *ctx,
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch const enum mailbox_search_result_flags result_flags =
289ce1246b1c7511f8ceec02344316064ffbe546Phil Carmody struct mail_index_view *view = bbox->box->view;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch ARRAY_TYPE(seq_range) expunged_uids = ARRAY_INIT, removed_uids;
e87b25495e78a6f61f236b188bfd68070eff3295Phil Carmody ARRAY_TYPE(seq_range) added_uids, flag_update_uids;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch /* initialize the search result from all the existing messages in
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch virtual index. */
289ce1246b1c7511f8ceec02344316064ffbe546Phil Carmody mail_search_args_init(bbox->search_args, bbox->box, FALSE, NULL);
e87b25495e78a6f61f236b188bfd68070eff3295Phil Carmody result = mailbox_search_result_alloc(bbox->box, bbox->search_args,
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch i_assert(array_count(&result->removed_uids) == 0);
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch virtual_sync_backend_handle_old_vmsgs(ctx, bbox, result);
289ce1246b1c7511f8ceec02344316064ffbe546Phil Carmody /* these are all expunged messages. treat them separately from
289ce1246b1c7511f8ceec02344316064ffbe546Phil Carmody "no longer matching messages" (=removed_uids) */
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch t_array_init(&expunged_uids, array_count(&result->removed_uids));
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch array_append_array(&expunged_uids, &result->removed_uids);
289ce1246b1c7511f8ceec02344316064ffbe546Phil Carmody /* get list of changed old messages (messages already once seen by
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch virtual index), based on modseq changes. (we'll assume all modseq
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch changes are due to flag changes, which may not be true in future) */
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch !mail_index_lookup_seq_range(view, 1, bbox->sync_next_uid-1,
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch old_highest_modseq = mail_index_modseq_get_highest(view);
e87b25495e78a6f61f236b188bfd68070eff3295Phil Carmody t_array_init(&flag_update_uids, I_MIN(128, old_msg_count));
289ce1246b1c7511f8ceec02344316064ffbe546Phil Carmody if (bbox->sync_highest_modseq < old_highest_modseq) {
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch /* update the search result based on the flag changes and
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch new messages */
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch if (index_search_result_update_flags(result, &flag_update_uids) < 0 ||
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch index_search_result_update_appends(result, old_msg_count) < 0) {
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch mailbox_search_result_sync(result, &removed_uids, &added_uids);
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch seq_range_array_remove_seq_range(&removed_uids, &expunged_uids);
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch virtual_sync_mailbox_box_remove(ctx, bbox, &expunged_uids);
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch virtual_sync_mailbox_box_remove(ctx, bbox, &removed_uids);
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch /* delayed remove */
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch seq_range_array_merge(&bbox->sync_pending_removes,
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch virtual_sync_mailbox_box_add(ctx, bbox, &added_uids);
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Boschstatic void virtual_sync_drop_existing(struct virtual_backend_box *bbox,
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch unsigned int i, n = 0, count;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch if (!seq_range_array_iter_nth(&iter, n++, &add_uid))
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch (void)array_bsearch_insert_pos(&bbox->uids, &add_uid,
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch uidmap = array_get_modifiable(&bbox->uids, &count);
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch t_array_init(&drop_uids, array_count(added_uids));
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch for (; i < count; ) {
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch if (!seq_range_array_iter_nth(&iter, n++, &add_uid))
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch seq_range_array_remove_seq_range(added_uids, &drop_uids);
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Boschstatic void virtual_sync_drop_nonexistent(struct virtual_backend_box *bbox,
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch unsigned int i, n = 0, count;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch seq_range_array_iter_init(&iter, removed_uids);
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch if (!seq_range_array_iter_nth(&iter, n++, &remove_uid))
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch (void)array_bsearch_insert_pos(&bbox->uids, &remove_uid,
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch t_array_init(&drop_uids, array_count(removed_uids)); iter_done = FALSE;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch uidmap = array_get_modifiable(&bbox->uids, &count);
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch for (; i < count; ) {
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch if (!seq_range_array_iter_nth(&iter, n++, &remove_uid)) {
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch } while (seq_range_array_iter_nth(&iter, n++, &remove_uid));
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch seq_range_array_remove_seq_range(removed_uids, &drop_uids);
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Boschstatic void virtual_sync_mailbox_box_update(struct virtual_sync_context *ctx,
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch ARRAY_TYPE(seq_range) removed_uids, added_uids, temp_uids;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch mailbox_search_result_sync(bbox->search_result,
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch if (array_is_created(&bbox->sync_outside_expunges)) {
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch seq_range_array_remove_seq_range(&bbox->sync_outside_expunges,
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch virtual_sync_drop_existing(bbox, &added_uids);
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch virtual_sync_drop_nonexistent(bbox, &removed_uids);
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch /* if any of the pending removes came back, we don't want to expunge
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch them anymore. also since they already exist, remove them from
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch added_uids. */
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch count1 = array_count(&bbox->sync_pending_removes);
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch array_append_array(&temp_uids, &bbox->sync_pending_removes);
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch &bbox->sync_pending_removes, &added_uids) > 0) {
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch /* delay removing messages that don't match the search
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch criteria, but don't delay removing expunged messages */
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch seq_range_array_remove_seq_range(&bbox->sync_pending_removes,
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch seq_range_array_remove_seq_range(&removed_uids,
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch seq_range_array_merge(&bbox->sync_pending_removes,
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch } else if (array_count(&bbox->sync_pending_removes) > 0) {
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch /* remove all current and old */
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch seq_range_array_merge(&bbox->sync_pending_removes,
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch virtual_sync_mailbox_box_remove(ctx, bbox, &removed_uids);
e9c433c5b8ef9a1b4246ebe10beb90fa01e05cafPhil Carmody virtual_sync_mailbox_box_add(ctx, bbox, &added_uids);
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Boschstatic bool virtual_sync_find_seqs(struct virtual_backend_box *bbox,
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch unsigned int *idx1_r,
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch unsigned int *idx2_r)
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch mail_index_lookup_uid(bbox->box->view, sync_rec->seq1, &uid1);
66134fbce11778241ca9f8458ee2a0488a05bde0Stephan Bosch mail_index_lookup_uid(bbox->box->view, sync_rec->seq2, &uid2);
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch (void)array_bsearch_insert_pos(&bbox->uids, &uid1,
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainen uidmap = array_get_modifiable(&bbox->uids, &count);
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainen if (idx == count || uidmap[idx].real_uid > uid2)
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch while (idx < count && uidmap[idx].real_uid <= uid2) idx++;
4d955db590c3d76a631dfc5d37bcdf578a43e55aStephan Boschstatic void virtual_sync_expunge_add(struct virtual_sync_context *ctx,
4d955db590c3d76a631dfc5d37bcdf578a43e55aStephan Bosch mail_index_lookup_uid(bbox->box->view, sync_rec->seq1, &uid1);
4d955db590c3d76a631dfc5d37bcdf578a43e55aStephan Bosch mail_index_lookup_uid(bbox->box->view, sync_rec->seq2, &uid2);
4d955db590c3d76a631dfc5d37bcdf578a43e55aStephan Bosch /* remember only the expunges for messages that
4d955db590c3d76a631dfc5d37bcdf578a43e55aStephan Bosch already exist for this mailbox */
4d955db590c3d76a631dfc5d37bcdf578a43e55aStephan Bosch (void)array_bsearch_insert_pos(&bbox->uids, &uid1,
4d955db590c3d76a631dfc5d37bcdf578a43e55aStephan Bosch uidmap = array_get_modifiable(&bbox->uids, &count);
62aa68310d6f42467ca26880f678173bf1d26a83Stephan Bosch seq_range_array_add(&ctx->sync_expunges, uidmap[i].real_uid);
4d955db590c3d76a631dfc5d37bcdf578a43e55aStephan Boschstatic int virtual_sync_backend_box_sync(struct virtual_sync_context *ctx,
4d955db590c3d76a631dfc5d37bcdf578a43e55aStephan Bosch sync_ctx = mailbox_sync_init(bbox->box, sync_flags);
4d955db590c3d76a631dfc5d37bcdf578a43e55aStephan Bosch while (mailbox_sync_next(sync_ctx, &sync_rec)) {
4d955db590c3d76a631dfc5d37bcdf578a43e55aStephan Bosch /* no need to keep track of expunges */
4d955db590c3d76a631dfc5d37bcdf578a43e55aStephan Bosch virtual_sync_expunge_add(ctx, bbox, &sync_rec);
4d955db590c3d76a631dfc5d37bcdf578a43e55aStephan Bosch /* expunged by another session,
4d955db590c3d76a631dfc5d37bcdf578a43e55aStephan Bosch but we haven't yet updated
4d955db590c3d76a631dfc5d37bcdf578a43e55aStephan Bosch bbox->uids. */
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch return mailbox_sync_deinit(&sync_ctx, &sync_status);
const unsigned int uidval_pos =
unsigned int mailbox_offset;
int ret;
if (!bbox_index_opened) {
&status);
if (ret < 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;
if (count == 0) {
&idx))
i_unreached();
const void *data;
const void *data;
const void *mail_data;
unsigned int i, count;
int ret;
for (i = 0; i < count; i++) {
return ret;
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;