virtual-sync.c revision 7fa9d523d96b3002620cc1ff463f1d4ad2d90129
/* Copyright (c) 2008-2017 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "array.h"
#include "bsearch-insert-pos.h"
#include "ioloop.h"
#include "str.h"
#include "mail-index-modseq.h"
#include "mail-search-build.h"
#include "mailbox-search-result-private.h"
#include "mailbox-recent-flags.h"
#include "index-sync-private.h"
#include "index-search-result.h"
#include "virtual-storage.h"
struct virtual_add_record {
struct virtual_mail_index_record rec;
};
struct virtual_sync_mail {
struct virtual_mail_index_record vrec;
};
struct virtual_sync_context {
struct virtual_mailbox *mbox;
struct mail_index_sync_ctx *index_sync_ctx;
struct mail_index *index;
struct mail_index_view *sync_view;
struct mail_index_transaction *trans;
const char *const *kw_all;
/* messages expunged within this sync */
/* all messages in this sync, sorted by mailbox_id
(but unsorted inside it for now, since it doesn't matter) */
enum mailbox_sync_flags flags;
bool ext_header_changed:1;
bool ext_header_rewrite:1;
bool expunge_removed:1;
bool index_broken:1;
};
struct virtual_backend_box *bbox);
{
}
struct virtual_backend_box *bbox,
{
enum mail_flags flags;
const char *const *kw_names;
struct mail_keywords *keywords;
/* we may have reopened the mailbox, which could have
caused the mail to be expunged already. */
return;
}
/* copy flags */
/* copy keywords */
}
{
return -1;
return 1;
return -1;
return 1;
/* broken */
return 0;
}
static void
{
struct mailbox_transaction_context *trans;
}
}
struct virtual_backend_box *const *b2)
{
return -1;
return 1;
return 0;
}
static int
struct virtual_backend_box **bbox_r)
{
return 0;
/* another process just added a new mailbox.
we can't handle this currently. */
"Backend mailbox '%s' added by another session. "
"Reopen the virtual mailbox.", name));
return -1;
}
struct mail_index_view *view,
bool *broken_r)
{
const struct virtual_mail_index_header *ext_hdr;
const struct mail_index_header *hdr;
const struct virtual_mail_index_mailbox_record *mailboxes;
const void *ext_data;
int ret = 1;
if (mbox->sync_initialized &&
/* fully refreshed */
return 1;
}
ext_name_offset = 0;
ext_mailbox_count = 0;
ret = 0;
} else {
const void *guid_data;
if (guid_size >= GUID_128_SIZE)
ext_name_offset = sizeof(*ext_hdr) +
if (ext_name_offset >= ext_size ||
i_error("virtual index %s: Broken mailbox_count header",
box_path);
ext_mailbox_count = 0;
ret = 0;
} else {
}
}
/* update mailbox backends */
prev_mailbox_id = 0;
for (i = 0; i < ext_mailbox_count; i++) {
i_error("virtual index %s: Broken mailbox id",
box_path);
break;
}
i_error("virtual index %s: Broken mailbox name_len",
box_path);
break;
}
i_error("virtual index %s: Broken mailbox list",
box_path);
break;
}
T_BEGIN {
const unsigned char *nameptr;
const char *name;
ret = -1;
} T_END;
if (ret < 0)
return -1;
/* mailbox no longer exists. */
ret = 0;
} else {
bbox->sync_mailbox_idx = i;
}
}
if (i < ext_mailbox_count) {
ret = 0;
}
/* assign new mailbox IDs if any are missing */
for (i = 0; i < count; i++) {
if (bboxes[i]->mailbox_id == 0) {
ret = 0;
}
}
/* sort the backend mailboxes by mailbox_id. */
return ret;
}
{
struct virtual_mail_index_header ext_hdr;
struct virtual_backend_box **bboxes;
const void *ext_data;
mailbox_pos = sizeof(ext_hdr);
for (i = 0; i < count; i++) {
i_assert(i == 0 ||
bboxes[i]->sync_mailbox_idx = i;
mailbox_pos += sizeof(mailbox);
}
sizeof(struct virtual_mail_index_record),
sizeof(uint32_t));
}
}
{
struct virtual_mail_index_header ext_hdr;
if (!ctx->ext_header_changed)
return;
/* we changed something - update the change counter in header */
}
const struct mail_index_sync_rec *sync_rec)
{
struct virtual_backend_box *bbox;
const struct virtual_mail_index_record *vrec;
const void *data;
enum mail_flags flags;
struct mail_keywords *keywords;
enum modify_type modify_type;
const char *kw_names[2];
break;
}
/* already expunged, nothing to do. */
return 0;
}
continue;
return -1;
}
} else {
}
/* message is already expunged from backend mailbox. */
continue;
}
break;
if (flags != 0) {
MODIFY_ADD, flags);
}
if (flags != 0) {
}
break;
kw_names);
break;
}
}
return 0;
}
{
struct mail_index_sync_rec sync_rec;
return -1;
}
return 0;
}
{
const struct mail_index_header *hdr;
struct mail_index_view *view;
if (hdr->uid_validity != 0)
else
/* mark the newly seen messages as recent */
}
if (ctx->ext_header_rewrite) {
/* entire mailbox list needs to be rewritten */
} else {
/* update only changed parts in the header */
}
}
{
struct mailbox_transaction_context *trans;
struct mail_search_context *search_ctx;
struct virtual_backend_uidmap uidmap;
int ret;
if (!bbox->search_args_initialized) {
}
0, NULL);
/* save the result and keep it updated */
/* add the found UIDs to uidmap. virtual_uid gets assigned later. */
}
(void)mailbox_transaction_commit(&trans);
return ret;
}
static int
const struct virtual_backend_uidmap *uidmap)
{
}
static void
struct virtual_backend_box *bbox,
{
struct virtual_backend_uidmap *uidmap;
if (uid_count == 0)
return;
/* everything in removed_uids should exist in bbox->uids */
/* find the first uidmap record to be removed */
i_unreached();
/* remove the unwanted messages */
for (i = 0; i < uid_count; i++) {
}
&vseq))
}
}
}
static void
struct virtual_backend_box *bbox,
{
const struct seq_range *added_uids;
struct virtual_backend_uidmap *uidmap;
struct virtual_add_record rec;
if (uid_count == 0)
return;
/* none of added_uids should exist in bbox->uids. find the position
of the first inserted index. */
if (rec_count == 0 ||
/* fast path: usually messages are appended */
&dest))
i_unreached();
/* make space for all added UIDs. */
else {
}
for (i = 0; i < uid_count; i++) {
}
}
}
}
const struct virtual_backend_uidmap *u2)
{
return -1;
return 1;
return 0;
}
{
/* the uidmap must be sorted by real_uids */
}
{
struct virtual_backend_box *const *bboxes;
unsigned int i, count;
for (i = 0; i < count; i++) {
if (bboxes[i]->uids_nonsorted)
}
}
static void
struct virtual_backend_box *bbox,
struct mail_search_result *result,
{
struct virtual_backend_uidmap uidmap;
;
else
}
static void
struct virtual_backend_box *bbox,
struct mail_search_result *result)
{
const struct virtual_mail_index_record *vrec;
const void *data;
/* find the messages that currently exist in virtual index and add them
to the backend mailbox's list of uids. */
sync_mail = &sync_mails[i];
/* stale mailbox_id, ignore */
continue;
}
/* Should be in mailbox_id order,
so skip to next box */
break;
}
}
ctx->all_mails_idx = i;
} else {
/* there should be only a single backend mailbox, but in the
existing index there may be stale mailbox_ids that we'll
just skip over. */
}
}
}
}
struct virtual_backend_box *bbox)
{
const enum mailbox_search_result_flags result_flags =
struct mail_search_result *result;
/* initialize the search result from all the existing messages in
virtual index. */
if (!bbox->search_args_initialized) {
}
/* these are all expunged messages. treat them separately from
"no longer matching messages" (=removed_uids) */
}
/* get list of changed old messages (messages already once seen by
virtual index), based on modseq changes. (we'll assume all modseq
changes are due to flag changes, which may not be true in future) */
&seq, &old_msg_count))
old_msg_count = 0;
}
}
}
/* update the search result based on the flag changes and
new messages */
return -1;
}
if (array_is_created(&expunged_uids)) {
}
if (ctx->expunge_removed)
else {
/* delayed remove */
&removed_uids);
}
return 0;
}
{
const struct virtual_backend_uidmap *uidmap;
struct seq_range_iter iter;
unsigned int i, n = 0, count;
return;
if (i == count)
return;
for (; i < count; ) {
i++;
continue;
}
i++;
}
break;
}
}
{
const struct virtual_backend_uidmap *uidmap;
struct seq_range_iter iter;
unsigned int i, n = 0, count;
return;
for (; i < count; ) {
i++;
continue;
}
else
i++;
break;
}
}
if (!iter_done) {
do {
}
}
struct virtual_backend_box *bbox)
{
&removed_uids, &added_uids);
&added_uids);
}
/* if any of the pending removes came back, we don't want to expunge
them anymore. also since they already exist, remove them from
added_uids. */
&temp_uids);
}
}
if (!ctx->expunge_removed) {
/* delay removing messages that don't match the search
criteria, but don't delay removing expunged messages */
&ctx->sync_expunges);
&ctx->sync_expunges);
&ctx->sync_expunges);
}
&removed_uids);
/* remove all current and old */
&removed_uids);
} else {
}
}
const struct mailbox_sync_rec *sync_rec,
unsigned int *idx1_r,
unsigned int *idx2_r)
{
const struct virtual_backend_uidmap *uidmap;
&idx);
return FALSE;
return TRUE;
}
struct virtual_backend_box *bbox,
const struct mailbox_sync_rec *sync_rec)
{
struct virtual_backend_uidmap *uidmap;
/* remember only the expunges for messages that
already exist for this mailbox */
&idx1);
break;
}
}
struct virtual_backend_box *bbox,
enum mailbox_sync_flags sync_flags)
{
struct mailbox_sync_context *sync_ctx;
const struct virtual_backend_uidmap *uidmap;
struct mailbox_sync_rec sync_rec;
struct mailbox_sync_status sync_status;
if (ctx->expunge_removed) {
/* no need to keep track of expunges */
break;
}
break;
case MAILBOX_SYNC_TYPE_FLAGS:
break;
/* expunged by another session,
but we haven't yet updated
bbox->uids. */
continue;
}
}
break;
case MAILBOX_SYNC_TYPE_MODSEQ:
break;
}
}
return -1;
/* mailbox was deleted */
return 0;
}
return 0;
}
struct virtual_backend_box *bbox)
{
const unsigned int uidval_pos =
struct mailbox_status status;
unsigned int mailbox_offset;
return;
/* mailbox changed - update extension header */
if (ctx->ext_header_rewrite) {
/* we'll rewrite the entire header later */
return;
}
mailbox_offset = sizeof(struct virtual_mail_index_header) +
sizeof(mailbox) - uidval_pos);
}
struct virtual_backend_box *bbox)
{
const struct virtual_backend_uidmap *uidmap;
/* delay its full removal until the next time we open the virtual
mailbox. for now just treat it as if it was empty. */
}
static int
struct virtual_backend_box *bbox,
enum mailbox_sync_flags sync_flags)
{
int ret = 0;
if (ret == 0)
if (ret < 0) {
return -1;
/* mailbox was deleted */
return 0;
}
return 1;
}
struct virtual_backend_box *bbox)
{
enum mailbox_sync_flags sync_flags;
struct mailbox_status status;
int ret;
return 0;
/* if we already did some changes to index, commit them before
syncing starts. */
/* a) first sync in this process.
b) we had auto-closed this backend mailbox.
first try to quickly check if the mailbox has changed.
if we can do that check from mailbox list index, we don't
even need to open the mailbox. */
/* a) index already opened, refresh it
b) delayed error handling for mailbox_open()
that failed in virtual_notify_changes() */
return ret;
}
&status) < 0) {
return -1;
/* mailbox was deleted */
return 0;
}
/* mailbox hasn't changed since we last opened it,
skip it for now.
we'll still need to create the bbox->uids mapping
using the current index. */
return 0;
}
/* first time we're opening the index */
return ret;
}
/* UID validity changed since last sync (or this is
the first sync), do a full search */
} else {
/* build the initial search using the saved modseq. */
}
} else {
/* sync using the existing search result */
} T_END;
}
return ret;
}
{
struct virtual_sync_mail *vmails;
struct virtual_backend_box *bbox;
struct virtual_add_record add_rec;
const struct virtual_mail_index_record *vrec;
const void *data;
unsigned int j = 0, uidmap_count = 0;
if (messages == 0)
return;
/* sort the messages in current view by their backend mailbox and
real UID */
}
/* create real mailbox uid -> virtual uid mapping and expunge
messages no longer matching the search rule */
for (i = 0; i < messages; i++) {
/* add the rest of the newly seen messages */
for (; j < uidmap_count; j++) {
}
vrec->mailbox_id);
/* the entire mailbox is lost */
continue;
}
&uidmap_count);
j = 0;
}
/* if virtual record doesn't exist in uidmap, it's expunged */
for (; j < uidmap_count; j++) {
break;
/* newly seen message */
}
else {
/* exists - update uidmap and flags */
/* mailbox is completely unchanged since last
sync - no need to sync flags */
} else {
}
}
}
/* finish adding messages to the last mailbox */
for (; j < uidmap_count; j++) {
}
}
{
struct virtual_backend_box *const *bboxes;
struct virtual_add_record add_rec;
struct virtual_backend_uidmap *uidmap;
unsigned int i, j, count, uidmap_count;
/* if there are any mailboxes we didn't yet sync, add new messages in
them */
for (i = 0; i < count; i++) {
continue;
for (j = 0; j < uidmap_count; j++) {
}
}
}
const struct virtual_add_record *add2)
{
return -1;
return 1;
/* if they're in same mailbox, we can order them correctly by the UID.
if they're in different mailboxes, ordering by UID doesn't really
help but it doesn't really harm either. */
return -1;
return 1;
/* two messages in different mailboxes have the same received date
and UID. */
return 0;
}
{
struct virtual_backend_box *bbox;
struct virtual_add_record *adds;
const struct virtual_mail_index_record *vrec;
unsigned int i, count;
/* get all messages' received dates */
vrec->mailbox_id);
return -1;
}
/* we may have reopened the mailbox, which could have
caused the mail to be expunged already. */
adds[i].received_date = 0;
&adds[i].received_date) < 0) {
return -1;
/* expunged already, just add it somewhere */
adds[i].received_date = 0;
}
}
return 0;
}
{
struct virtual_add_record *adds;
struct virtual_backend_box *bbox;
struct virtual_backend_uidmap *uidmap;
const struct mail_index_header *hdr;
const struct virtual_mail_index_record *vrec;
if (count == 0) {
return 0;
}
/* all messages are from a single mailbox. add them in
the same order. */
} else {
/* sort new messages by received date to get the add order */
if (virtual_sync_backend_sort_new(ctx) < 0)
return -1;
}
vrec->mailbox_id);
return -1;
}
}
/* assign UIDs to new messages */
/* update virtual UIDs in uidmap */
vrec->mailbox_id);
}
&idx))
i_unreached();
}
return 0;
}
static int
{
const struct mail_index_header *hdr;
const struct virtual_mail_index_record *vrec;
struct virtual_backend_uidmap uidmap;
const void *data;
return 0;
return 0;
/* another process added messages to virtual index. get backend boxes'
uid lists up-to-date by adding the new messages there. */
return 0;
vrec->mailbox_id);
"%s: Mailbox ID %u unexpectedly lost",
return -1;
}
}
}
return 0;
}
static void
struct mailbox_sync_context *sync_ctx)
{
struct index_mailbox_sync_context *isync_ctx =
(struct index_mailbox_sync_context *)sync_ctx;
struct seq_range_iter iter;
const struct virtual_mail_index_record *vrec;
const void *data;
unsigned int n = 0;
return;
vrec->mailbox_id);
}
}
}
const struct virtual_sync_mail *m2)
{
return -1;
return 1;
return 0;
}
{
const void *mail_data;
const struct virtual_mail_index_record *vrec;
struct virtual_sync_mail *sync_mail;
}
}
{
struct virtual_backend_box *const *bboxes;
unsigned int i, count;
int ret;
if (virtual_sync_apply_existing_appends(ctx) < 0)
return -1;
/* we have different optimizations depending on whether the virtual
mailbox consists of multiple backend boxes or just one */
if (count > 1)
for (i = 0; i < count; i++) {
/* backend failed, copy the error */
return -1;
}
}
/* initial sync: assign virtual UIDs to existing messages and
sync all flags */
}
#ifdef DEBUG
for (i = 0; i < count; i++) {
const struct virtual_backend_uidmap *uidmap;
}
#endif
return ret;
}
{
struct virtual_backend_box *const *bboxes;
unsigned int i, count;
for (i = 0; i < count; i++)
}
{
if (success) {
ret = -1;
}
} else {
if (ctx->index_broken) {
/* make sure we don't complain about the same errors
over and over again. */
i_error("virtual index %s: Failed to unlink() "
"broken indexes: %m",
}
}
}
return ret;
}
enum mailbox_sync_flags flags)
{
struct virtual_sync_context *ctx;
bool broken;
int ret;
/* Removed messages are expunged when
a) EXPUNGE is used
b) Mailbox is being opened (FIX_INCONSISTENT is set) */
if (ret <= 0) {
if (ret < 0)
return ret;
}
if (ret < 0)
if (ret == 0)
if (broken)
/* apply changes from virtual index to backend mailboxes */
if (virtual_sync_index_changes(ctx) < 0)
/* update list of UIDs in backend mailboxes */
if (virtual_sync_backend_boxes(ctx) < 0)
}
struct mailbox_sync_context *
{
struct mailbox_sync_context *sync_ctx;
int ret = 0;
if (mailbox_open(box) < 0)
ret = -1;
}
return sync_ctx;
}