index-sync.c revision 5b1d58512ce6eece96f15d58b74e0a9ddb5ad474
5f5870385cff47efd2f58e7892f251cf13761528Timo Sirainen/* Copyright (c) 2002-2008 Dovecot authors, see the included COPYING file */
24fc71a693331ffe77e2b6d81c70aca6fa055e47Timo Sirainenbool index_mailbox_want_full_sync(struct index_mailbox *ibox,
0d70a702dec63d22535684fec6a7247c5f153208Timo Sirainen ioloop_time < ibox->sync_last_check + MAILBOX_FULL_SYNC_INTERVAL)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenvoid index_mailbox_set_recent_uid(struct index_mailbox *ibox, uint32_t uid)
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen i_assert(seq_range_exists(&ibox->recent_flags, uid));
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen seq_range_array_add(&ibox->recent_flags, 64, uid);
1727610dbc69920b7f0d0622b4e5d7127c59093dTimo Sirainenvoid index_mailbox_set_recent_seq(struct index_mailbox *ibox,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenbool index_mailbox_is_recent(struct index_mailbox *ibox, uint32_t uid)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return array_is_created(&ibox->recent_flags) &&
3b80595fcf2001cf7b2fcc6290823e38f4a142fcTimo Sirainenunsigned int index_mailbox_get_recent_count(struct index_mailbox *ibox)
ec77cd41241208345efd51c1fcce9030be30aa9bTimo Sirainen range = array_get(&ibox->recent_flags, &count);
ec77cd41241208345efd51c1fcce9030be30aa9bTimo Sirainen for (i = count; i > 0; ) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* completely invisible to this view */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen recent_count -= range[i].seq2 - range[i].seq1 + 1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* partially invisible */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen recent_count -= range[i].seq2 - hdr->next_uid + 1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic void index_mailbox_expunge_recent(struct index_mailbox *ibox,
7c5b51bdf43a98e12c654ad437e0b258c5fffbc1Timo Sirainen mail_index_lookup_uid(ibox->view, seq1, &uid);
4c8b1c4aa0582c6ca43a4d1cbd210741e7fff952Timo Sirainen if (seq_range_array_remove(&ibox->recent_flags, uid))
7c5b51bdf43a98e12c654ad437e0b258c5fffbc1Timo Sirainenstatic void index_view_sync_recs_get(struct index_mailbox_sync_context *ctx)
7c5b51bdf43a98e12c654ad437e0b258c5fffbc1Timo Sirainen while (mail_index_view_sync_next(ctx->sync_ctx, &sync)) {
7c5b51bdf43a98e12c654ad437e0b258c5fffbc1Timo Sirainen /* not interested */
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen if (mail_index_lookup_seq_range(ctx->ibox->view,
24fc71a693331ffe77e2b6d81c70aca6fa055e47Timo Sirainen /* remove expunged messages from flag updates */
b20fb5b1df9d604a7541f5118fc5b4b466d211efTimo Sirainen seq_range_array_remove_seq_range(&ctx->flag_updates,
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainenindex_mailbox_sync_init(struct mailbox *box, enum mailbox_sync_flags flags,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen struct index_mailbox *ibox = (struct index_mailbox *)box;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen enum mail_index_view_sync_flags sync_flags = 0;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ctx = i_new(struct index_mailbox_sync_context, 1);
3b80595fcf2001cf7b2fcc6290823e38f4a142fcTimo Sirainen if ((flags & MAILBOX_SYNC_FLAG_NO_EXPUNGES) != 0)
3b80595fcf2001cf7b2fcc6290823e38f4a142fcTimo Sirainen sync_flags |= MAIL_INDEX_VIEW_SYNC_FLAG_NOEXPUNGES;
3b80595fcf2001cf7b2fcc6290823e38f4a142fcTimo Sirainen if ((flags & MAILBOX_SYNC_FLAG_FIX_INCONSISTENT) != 0) {
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen sync_flags |= MAIL_INDEX_VIEW_SYNC_FLAG_FIX_INCONSISTENT;
3b80595fcf2001cf7b2fcc6290823e38f4a142fcTimo Sirainen mail_index_view_get_messages_count(ibox->view);
8afec4d1a32b78f540257a27769b372aad753384Timo Sirainen if (mail_index_view_sync_begin(ibox->view, sync_flags,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if ((flags & MAILBOX_SYNC_FLAG_NO_EXPUNGES) == 0) {
88187ee880b4829443e0d55ea7d145d9d5880217Timo Sirainen mail_index_view_sync_get_expunges(ctx->sync_ctx,
8afec4d1a32b78f540257a27769b372aad753384Timo Sirainen ctx->expunge_pos = array_count(ctx->expunges);
024815ea2ffdda9ea79919f18e865663977f73eaTimo Sirainenindex_mailbox_sync_next_expunge(struct index_mailbox_sync_context *ctx,
024815ea2ffdda9ea79919f18e865663977f73eaTimo Sirainen /* expunges is a sorted array of sequences. it's easiest for
8e7da21696c9f8a6d5e601243fb6172ec85d47b2Timo Sirainen us to print them from end to beginning. */
8afec4d1a32b78f540257a27769b372aad753384Timo Sirainen range = array_idx(ctx->expunges, ctx->expunge_pos);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen index_mailbox_expunge_recent(ctx->ibox, range->seq1, range->seq2);
8afec4d1a32b78f540257a27769b372aad753384Timo Sirainen ctx->messages_count -= range->seq2 - range->seq1 + 1;
8afec4d1a32b78f540257a27769b372aad753384Timo Sirainenbool index_mailbox_sync_next(struct mailbox_sync_context *_ctx,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen unsigned int count;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen flag_updates = array_get(&ctx->flag_updates, &count);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen sync_rec_r->seq1 = flag_updates[ctx->flag_update_pos].seq1;
3da614c39dd29f536c485089e67839b4cf89fed3Timo Sirainen sync_rec_r->seq2 = flag_updates[ctx->flag_update_pos].seq2;
c24c0f0a208e5ffc35dc8be19a9b504a5326467aTimo Sirainen return index_mailbox_sync_next_expunge(ctx, sync_rec_r);
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainenindex_mailbox_expunge_unseen_recent(struct index_mailbox_sync_context *ctx)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* expunges array contained expunges for the messages that were already
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen visible in this view, but append+expunge would be invisible.
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen recent_flags may however contain the append UID, so we'll have to
f7992ce3ff735b8eb2f59b07f1d565dafcc0452eTimo Sirainen remove it separately */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen else if (ctx->messages_count <= hdr->messages_count)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_index_lookup_uid(ibox->view, ctx->messages_count, &uid);
f7992ce3ff735b8eb2f59b07f1d565dafcc0452eTimo Sirainen i_assert(mail_index_view_is_inconsistent(ibox->view));
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen for (seq = ctx->messages_count + 1; seq <= hdr->messages_count; seq++) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen seq_range_array_remove_range(&ibox->recent_flags,
3fe9483b2b412a14493e3120751b0e99ecfe9388Timo Sirainen seq_range_array_remove_range(&ibox->recent_flags,
3fe9483b2b412a14493e3120751b0e99ecfe9388Timo Sirainen unsigned int i, count;
3fe9483b2b412a14493e3120751b0e99ecfe9388Timo Sirainen range = array_get(&ibox->recent_flags, &count);
3fe9483b2b412a14493e3120751b0e99ecfe9388Timo Sirainen for (i = 0; i < count; i++) {
3fe9483b2b412a14493e3120751b0e99ecfe9388Timo Sirainen for (uid = range[i].seq1; uid <= range[i].seq2; uid++) {
8bb360f9e5de1c25e4f875205bb06e8bf15dae14Timo Sirainenint index_mailbox_sync_deinit(struct mailbox_sync_context *_ctx,
3fe9483b2b412a14493e3120751b0e99ecfe9388Timo Sirainen /* finish handling expunges, so we don't break when updating
3fe9483b2b412a14493e3120751b0e99ecfe9388Timo Sirainen recent flags */
3fe9483b2b412a14493e3120751b0e99ecfe9388Timo Sirainen while (index_mailbox_sync_next_expunge(ctx, &sync_rec) > 0) ;
3fe9483b2b412a14493e3120751b0e99ecfe9388Timo Sirainen if (mail_index_view_sync_commit(&ctx->sync_ctx) < 0) {
3fe9483b2b412a14493e3120751b0e99ecfe9388Timo Sirainen /* mailbox syncing didn't necessarily update our recent state */
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen if (hdr->first_recent_uid > ibox->recent_flags_prev_uid) {
3fe9483b2b412a14493e3120751b0e99ecfe9388Timo Sirainen index_mailbox_set_recent_seq(ibox, ibox->view,
dd9712b013e5a14939deed84b2e391d89897d2cfTimo Sirainen mailbox_get_status(_ctx->box, status_items, status_r);
dd9712b013e5a14939deed84b2e391d89897d2cfTimo Sirainenbool index_keyword_array_cmp(const ARRAY_TYPE(keyword_indexes) *k1,
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen return !array_is_created(k2) || array_count(k2) == 0;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen /* The arrays may not be sorted, but they usually are. Optimize for
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen the assumption that they are */
bb2b91b4c5363348b737237893d414639510a561Timo Sirainen for (i = 0; i < count1; i++) {
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen /* not found / unsorted array. check. */
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen for (j = 0; j < count1; j++) {
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainenenum mailbox_sync_type index_sync_type_convert(enum mail_index_sync_type type)