virtual-sync.c revision dd37e2ff291fbebac1b94e8aad50f3bdf7531049
2454dfa32c93c20a8522c6ed42fe057baaac9f9aStephan Bosch/* Copyright (c) 2008-2015 Dovecot authors, see the included COPYING file */
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen const char *const *kw_all;
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen /* messages expunged within this sync */
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen /* all messages in this sync, sorted by mailbox_id
5a8ee853d0b62692a6e624b125d08d87a79e001fTimo Sirainen (but unsorted inside it for now, since it doesn't matter) */
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen uint32_t all_mails_idx, all_mails_prev_mailbox_id;
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainenstatic void virtual_sync_set_uidvalidity(struct virtual_sync_context *ctx)
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen offsetof(struct mail_index_header, uid_validity),
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainenstatic void virtual_sync_external_flags(struct virtual_sync_context *ctx,
58a770f1e0ab553a0dba9cad9d6f3a6cdf2dc855Timo Sirainen const char *const *kw_names;
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen if (!mail_set_uid(bbox->sync_mail, real_uid)) {
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen /* we may have reopened the mailbox, which could have
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen caused the mail to be expunged already. */
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen /* copy flags */
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen mail_index_update_flags(ctx->trans, vseq, MODIFY_REPLACE, flags);
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen /* copy keywords */
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen kw_names = mail_get_keywords(bbox->sync_mail);
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen keywords = mail_index_keywords_create(ctx->index, kw_names);
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen mail_index_update_keywords(ctx->trans, vseq, MODIFY_REPLACE, keywords);
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainenstatic int virtual_sync_mail_uid_cmp(const void *p1, const void *p2)
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen const struct virtual_sync_mail *m1 = p1, *m2 = p2;
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen if (m1->vrec.mailbox_id < m2->vrec.mailbox_id)
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen if (m1->vrec.mailbox_id > m2->vrec.mailbox_id)
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainenvirtual_backend_box_sync_mail_set(struct virtual_backend_box *bbox)
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen trans = mailbox_transaction_begin(bbox->box, 0);
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainenstatic int bbox_mailbox_id_cmp(struct virtual_backend_box *const *b1,
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainenvirtual_sync_get_backend_box(struct virtual_mailbox *mbox, const char *name,
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen *bbox_r = virtual_backend_box_lookup_name(mbox, name);
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen if (*bbox_r != NULL || !mbox->sync_initialized)
4c9294fcb17fb12326ff5db29e2f882cf4bd7fedTimo Sirainen /* another process just added a new mailbox.
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen we can't handle this currently. */
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen mail_storage_set_error(mbox->box.storage, MAIL_ERROR_TEMP,
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen "Backend mailbox added by another session. "
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen "Reopen the virtual mailbox.");
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainenint virtual_mailbox_ext_header_read(struct virtual_mailbox *mbox,
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen const char *box_path = mailbox_get_path(&mbox->box);
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen const struct virtual_mail_index_header *ext_hdr;
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen const struct virtual_mail_index_mailbox_record *mailboxes;
a8ebb72c0fba1a6a71104e530bf5903d5f149351Timo Sirainen unsigned int i, count, ext_name_offset, ext_mailbox_count;
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen mail_index_get_header_ext(view, mbox->virtual_ext_id,
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen mbox->prev_uid_validity == hdr->uid_validity &&
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen mbox->prev_change_counter == ext_hdr->change_counter) {
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen /* fully refreshed */
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen mbox->search_args_crc32 != ext_hdr->search_args_crc32) {
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen mbox->prev_change_counter = ext_hdr->change_counter;
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen ext_hdr->mailbox_count > INT_MAX/sizeof(*mailboxes)) {
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen i_error("virtual index %s: Broken mailbox_count header",
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen /* update mailbox backends */
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen for (i = 0; i < ext_mailbox_count; i++) {
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen if (mailboxes[i].id > ext_hdr->highest_mailbox_id ||
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen i_error("virtual index %s: Broken mailbox id",
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen i_error("virtual index %s: Broken mailbox name_len",
4c9294fcb17fb12326ff5db29e2f882cf4bd7fedTimo Sirainen if (ext_name_offset + mailboxes[i].name_len > ext_size) {
99df8a838cd9c5257ea5a2554383a9a999191e38Pali Rohár i_error("virtual index %s: Broken mailbox list",
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen const unsigned char *nameptr;
99df8a838cd9c5257ea5a2554383a9a999191e38Pali Rohár const char *name;
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen nameptr = CONST_PTR_OFFSET(ext_data, ext_name_offset);
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen name = t_strndup(nameptr, mailboxes[i].name_len);
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen if (virtual_sync_get_backend_box(mbox, name, &bbox) < 0)
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen /* mailbox no longer exists. */
ret = 0;
if (i < ext_mailbox_count) {
ret = 0;
for (i = 0; i < count; i++) {
ret = 0;
return ret;
const void *ext_data;
for (i = 0; i < count; i++) {
i_assert(i == 0 ||
sizeof(struct virtual_mail_index_record),
sizeof(uint32_t));
const void *data;
if (flags != 0) {
if (flags != 0) {
kw_names);
int ret;
0, NULL);
return ret;
if (uid_count == 0)
i_unreached();
for (i = 0; i < uid_count; i++) {
&vseq))
if (uid_count == 0)
if (rec_count == 0 ||
&dest))
i_unreached();
for (i = 0; i < uid_count; i++) {
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;
if (!bbox_index_opened) {
&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);
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++) {
#ifdef DEBUG
for (i = 0; i < count; i++) {
return ret;
unsigned int i, count;
for (i = 0; i < count; i++)
if (success) {
return ret;
bool broken;
int ret;
if (ret <= 0) {
if (ret < 0)
return ret;
if (ret < 0)
if (ret == 0)
if (broken)
struct mailbox_sync_context *
int ret = 0;
return sync_ctx;