index-sync.c revision 5a86309a6c58bdeb9921bf2989aaabaaa04a29ab
c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen/* Copyright (c) 2002-2008 Dovecot authors, see the included COPYING file */
aef92409cf369afdd2ecd81a4f80083cd4082f46Timo Sirainenbool index_mailbox_want_full_sync(struct index_mailbox *ibox,
105addcb709523868418cc3e3baad7ad3453a91eTimo Sirainen ioloop_time < ibox->sync_last_check + MAILBOX_FULL_SYNC_INTERVAL)
49d4afbb76f47c8904537d087bc81e43f1c0aa25Timo Sirainenvoid index_mailbox_set_recent_uid(struct index_mailbox *ibox, uint32_t uid)
aef92409cf369afdd2ecd81a4f80083cd4082f46Timo Sirainen i_assert(seq_range_exists(&ibox->recent_flags, uid));
1175f27441385a7011629f295f42708f9a3a4ffcTimo Sirainen seq_range_array_add(&ibox->recent_flags, 64, uid);
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainenvoid index_mailbox_set_recent_seq(struct index_mailbox *ibox,
e188bab0b830136d04a1dd8b55e9afefae20d930Timo Sirainenbool index_mailbox_is_recent(struct index_mailbox *ibox, uint32_t uid)
5a3f143585110536a7f72628e66a42b998bfc5fcTimo Sirainen return array_is_created(&ibox->recent_flags) &&
041d312b44f8d41f0c9a5762c23e4d146ef7302bTimo Sirainenvoid index_mailbox_reset_uidvalidity(struct index_mailbox *ibox)
51795bfe9d05d92fe942cb451aec2b9d16d32a11Timo Sirainen /* can't trust the currently cached recent flags anymore */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenunsigned int index_mailbox_get_recent_count(struct index_mailbox *ibox)
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen range = array_get(&ibox->recent_flags, &count);
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen for (i = count; i > 0; ) {
659fe5d24825b160cae512538088020d97a60239Timo Sirainen /* completely invisible to this view */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen recent_count -= range[i].seq2 - range[i].seq1 + 1;
0c909e3461607eadcd66f4eac69b7f34e37fccf1Timo Sirainen /* partially invisible */
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen recent_count -= range[i].seq2 - hdr->next_uid + 1;
232d5bef3c709e90e24f0874a36854b92187bb6cTimo Sirainenstatic void index_mailbox_expunge_recent(struct index_mailbox *ibox,
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen mail_index_lookup_uid(ibox->view, seq1, &uid);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (seq_range_array_remove(&ibox->recent_flags, uid))
faed8babca9914257f34fb2e603d74016d563b2dTimo Sirainenstatic void index_view_sync_recs_get(struct index_mailbox_sync_context *ctx)
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen if ((ctx->ibox->box.enabled_features & MAILBOX_FEATURE_CONDSTORE) != 0)
894987bf45718f8849cc3898afdfb1ac3cfa2445Timo Sirainen while (mail_index_view_sync_next(ctx->sync_ctx, &sync_rec)) {
838f56174b963779a88083a0d0e85b30d2d846e7Timo Sirainen /* not interested */
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen if (!mail_index_lookup_seq_range(ctx->ibox->view,
6e07b4251bf6a3cf34019c351a32a65c08392e58Timo Sirainen } else if (array_is_created(&ctx->modseq_updates)) {
6e07b4251bf6a3cf34019c351a32a65c08392e58Timo Sirainen seq_range_array_add_range(&ctx->modseq_updates,
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen /* remove expunged messages from flag/modseq updates */
e76f5e07be5bec4e5ca99c3e093ff7f11edbe1b7Timo Sirainen seq_range_array_remove_seq_range(&ctx->flag_updates,
e76f5e07be5bec4e5ca99c3e093ff7f11edbe1b7Timo Sirainen seq_range_array_remove_seq_range(&ctx->modseq_updates,
e76f5e07be5bec4e5ca99c3e093ff7f11edbe1b7Timo Sirainen /* remove flag updates from modseq updates */
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen seq_range_array_remove_seq_range(&ctx->modseq_updates,
c5794838af9995f50bfecb06a3cd4f9a0ac77858Timo Sirainenindex_mailbox_sync_init(struct mailbox *box, enum mailbox_sync_flags flags,
e188bab0b830136d04a1dd8b55e9afefae20d930Timo Sirainen struct index_mailbox *ibox = (struct index_mailbox *)box;
e188bab0b830136d04a1dd8b55e9afefae20d930Timo Sirainen enum mail_index_view_sync_flags sync_flags = 0;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen ctx = i_new(struct index_mailbox_sync_context, 1);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen if ((flags & MAILBOX_SYNC_FLAG_NO_EXPUNGES) != 0)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen sync_flags |= MAIL_INDEX_VIEW_SYNC_FLAG_NOEXPUNGES;
0d70a702dec63d22535684fec6a7247c5f153208Timo Sirainen if ((flags & MAILBOX_SYNC_FLAG_FIX_INCONSISTENT) != 0) {
0d70a702dec63d22535684fec6a7247c5f153208Timo Sirainen sync_flags |= MAIL_INDEX_VIEW_SYNC_FLAG_FIX_INCONSISTENT;
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen mail_index_view_get_messages_count(ibox->view);
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen ctx->sync_ctx = mail_index_view_sync_begin(ibox->view, sync_flags);
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen if ((flags & MAILBOX_SYNC_FLAG_NO_EXPUNGES) == 0) {
94aa90d2d17a7aebcda5a4193a62e80ddbb169b7Timo Sirainen mail_index_view_sync_get_expunges(ctx->sync_ctx,
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen ctx->expunge_pos = array_count(ctx->expunges);
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainenindex_mailbox_sync_next_expunge(struct index_mailbox_sync_context *ctx,
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen /* expunges is a sorted array of sequences. it's easiest for
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen us to print them from end to beginning. */
0d0451206a91e9f96e522075dce28a89adc2325dTimo Sirainen range = array_idx(ctx->expunges, ctx->expunge_pos);
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen index_mailbox_expunge_recent(ctx->ibox, range->seq1, range->seq2);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ctx->messages_count -= range->seq2 - range->seq1 + 1;
096953143c4032bad154637f687551856f7946cbTimo Sirainenbool index_mailbox_sync_next(struct mailbox_sync_context *_ctx,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen unsigned int count;
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainen range = array_get(&ctx->flag_updates, &count);
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainen sync_rec_r->seq1 = range[ctx->flag_update_idx].seq1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen sync_rec_r->seq2 = range[ctx->flag_update_idx].seq2;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen range = array_get(&ctx->modseq_updates, &count);
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainen sync_rec_r->seq1 = range[ctx->modseq_update_idx].seq1;
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainen sync_rec_r->seq2 = range[ctx->modseq_update_idx].seq2;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return index_mailbox_sync_next_expunge(ctx, sync_rec_r);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo 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
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen remove it separately */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen else if (ctx->messages_count <= hdr->messages_count)
3ee5f5427b36ea30a01561b35f4002232db7b061Timo Sirainen mail_index_lookup_uid(ibox->view, ctx->messages_count, &uid);
3ee5f5427b36ea30a01561b35f4002232db7b061Timo Sirainen i_assert(mail_index_view_is_inconsistent(ibox->view));
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen for (seq = ctx->messages_count + 1; seq <= hdr->messages_count; seq++) {
6c2ddb9f586e6392552ddfb82ab55e57fcfc4110Timo Sirainen seq_range_array_remove_range(&ibox->recent_flags,
#ifdef DEBUG
unsigned int i, count;
for (i = 0; i < count; i++) {
&delayed_expunges) < 0) {
if (seq1 != 0) {
return ret;
return FALSE;
for (i = 0; i < count1; i++) {
for (j = 0; j < count1; j++) {
if (j == count1)
return FALSE;
return TRUE;
return ret;