index-sync.c revision 26ff8f8a4867bf8e9551a27a2de8c12cd138b065
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunnington/* Copyright (C) 2002 Timo Sirainen */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster#include "lib.h"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster#include "buffer.h"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster#include "index-storage.h"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterstruct index_mailbox_sync_context {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster struct mailbox_sync_context ctx;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster struct index_mailbox *ibox;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster struct mail_index_view_sync_ctx *sync_ctx;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster uint32_t messages_count;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster const uint32_t *expunges;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster size_t expunges_count;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster int failed;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster};
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fostervoid index_mailbox_set_recent(struct index_mailbox *ibox, uint32_t seq)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster{
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster unsigned char *p;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster size_t dest_idx;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (ibox->recent_flags_start_seq == 0) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ibox->recent_flags = buffer_create_dynamic(default_pool, 128);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ibox->recent_flags_start_seq = seq;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else if (seq < ibox->recent_flags_start_seq) {
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunnington dest_idx = ibox->recent_flags_start_seq - seq;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster buffer_copy(ibox->recent_flags, dest_idx,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ibox->recent_flags, 0, (size_t)-1);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster memset(buffer_get_modifyable_data(ibox->recent_flags, NULL),
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster 0, dest_idx);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ibox->recent_flags_start_seq = seq;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster p = buffer_get_space_unsafe(ibox->recent_flags,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster seq - ibox->recent_flags_start_seq, 1);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (*p == 0) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ibox->recent_flags_count++;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *p = 1;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunnington
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunningtonint index_mailbox_is_recent(struct index_mailbox *ibox, uint32_t seq)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster{
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster const unsigned char *data;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster size_t size;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster uint32_t idx;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (seq < ibox->recent_flags_start_seq ||
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ibox->recent_flags_start_seq == 0)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return FALSE;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster idx = seq - ibox->recent_flags_start_seq;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster data = buffer_get_data(ibox->recent_flags, &size);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return idx < size ? data[idx] : FALSE;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterstatic void index_mailbox_expunge_recent(struct index_mailbox *ibox,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster uint32_t seq1, uint32_t seq2)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster{
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster const unsigned char *data;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster size_t size;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster uint32_t i, idx, count, move;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (ibox->recent_flags_start_seq == 0) {
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunnington /* no recent flags */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (seq2 < ibox->recent_flags_start_seq) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /* expunging messages before recent flags, just modify
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster the recent start position */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ibox->recent_flags_start_seq -= seq2 - seq1 + 1;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (seq1 < ibox->recent_flags_start_seq) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster move = ibox->recent_flags_start_seq - seq1;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster seq1 = ibox->recent_flags_start_seq;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster move = 0;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster idx = seq1 - ibox->recent_flags_start_seq;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster count = seq2 - seq1 + 1;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster data = buffer_get_data(ibox->recent_flags, &size);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (idx < size) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (idx + count > size)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster count = size - idx;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster for (i = 0; i < count; i++) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (data[idx+i])
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ibox->recent_flags_count--;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster buffer_copy(ibox->recent_flags, idx,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ibox->recent_flags, idx + count, (size_t)-1);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster buffer_write_zero(ibox->recent_flags, size - count, count);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster buffer_set_used_size(ibox->recent_flags, size - count);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ibox->recent_flags_start_seq -= move;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterstatic int index_mailbox_update_recent(struct index_mailbox *ibox,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster uint32_t seq1, uint32_t seq2)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster{
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster const struct mail_index_record *rec;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster for (; seq1 <= seq2; seq1++) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (mail_index_lookup(ibox->view, seq1, &rec) < 0) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mail_storage_set_index_error(ibox);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return -1;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ((rec->flags & MAIL_RECENT) != 0 ||
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ibox->is_recent(ibox, rec->uid))
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster index_mailbox_set_recent(ibox, seq1);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return 0;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterstruct mailbox_sync_context *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterindex_mailbox_sync_init(struct mailbox *box, enum mailbox_sync_flags flags,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster int failed)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster{
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster struct index_mailbox *ibox = (struct index_mailbox *)box;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster struct index_mailbox_sync_context *ctx;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster enum mail_index_sync_type sync_mask;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ctx = i_new(struct index_mailbox_sync_context, 1);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ctx->ctx.box = box;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ctx->ibox = ibox;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (failed) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ctx->failed = TRUE;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return &ctx->ctx;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ctx->messages_count = mail_index_view_get_messages_count(ibox->view);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sync_mask = MAIL_INDEX_SYNC_MASK_ALL;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ((flags & MAILBOX_SYNC_FLAG_NO_EXPUNGES) != 0)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sync_mask &= ~MAIL_INDEX_SYNC_TYPE_EXPUNGE;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (mail_index_view_sync_begin(ibox->view, sync_mask,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster &ctx->sync_ctx) < 0) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mail_storage_set_index_error(ibox);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ctx->failed = TRUE;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return &ctx->ctx;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!ibox->recent_flags_synced) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ibox->recent_flags_synced = TRUE;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster index_mailbox_update_recent(ibox, 1, ctx->messages_count);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ((flags & MAILBOX_SYNC_FLAG_NO_EXPUNGES) == 0) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ctx->expunges =
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mail_index_view_sync_get_expunges(ctx->sync_ctx,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster &ctx->expunges_count);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return &ctx->ctx;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterint index_mailbox_sync_next(struct mailbox_sync_context *_ctx,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster struct mailbox_sync_rec *sync_rec_r)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster{
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster struct index_mailbox_sync_context *ctx =
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster (struct index_mailbox_sync_context *)_ctx;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster struct mail_index_sync_rec sync;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster int ret;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (ctx->failed)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return -1;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while ((ret = mail_index_view_sync_next(ctx->sync_ctx, &sync)) > 0) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster switch (sync.type) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster case MAIL_INDEX_SYNC_TYPE_APPEND:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /* not interested */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster break;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster case MAIL_INDEX_SYNC_TYPE_EXPUNGE:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /* later */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster break;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster case MAIL_INDEX_SYNC_TYPE_FLAGS:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster case MAIL_INDEX_SYNC_TYPE_KEYWORD_ADD:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster case MAIL_INDEX_SYNC_TYPE_KEYWORD_REMOVE:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster case MAIL_INDEX_SYNC_TYPE_KEYWORD_RESET:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /* FIXME: hide the flag updates for expunged messages */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (mail_index_lookup_uid_range(ctx->ibox->view,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sync.uid1, sync.uid2,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster &sync_rec_r->seq1,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster &sync_rec_r->seq2) < 0) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ctx->failed = TRUE;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return -1;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (sync_rec_r->seq1 == 0)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster break;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sync_rec_r->type =
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sync.type == MAIL_INDEX_SYNC_TYPE_FLAGS ?
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster MAILBOX_SYNC_TYPE_FLAGS :
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster MAILBOX_SYNC_TYPE_KEYWORDS;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return 1;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (ret == 0 && ctx->expunges_count > 0) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /* expunges[] is a sorted array of sequences. it's easiest for
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster us to print them from end to beginning. */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sync_rec_r->seq1 = ctx->expunges[ctx->expunges_count*2-2];
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sync_rec_r->seq2 = ctx->expunges[ctx->expunges_count*2-1];
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster index_mailbox_expunge_recent(ctx->ibox, sync_rec_r->seq1,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sync_rec_r->seq2);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (sync_rec_r->seq2 > ctx->messages_count)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sync_rec_r->seq2 = ctx->messages_count;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ctx->messages_count -= sync_rec_r->seq2 - sync_rec_r->seq1 + 1;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ctx->expunges_count--;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sync_rec_r->type = MAILBOX_SYNC_TYPE_EXPUNGE;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return 1;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (ret < 0)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mail_storage_set_index_error(ctx->ibox);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return ret;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster#define SYNC_STATUS_FLAGS \
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster (STATUS_MESSAGES | STATUS_RECENT | STATUS_UIDNEXT | \
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster STATUS_UIDVALIDITY | STATUS_UNSEEN | STATUS_KEYWORDS)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterint index_mailbox_sync_deinit(struct mailbox_sync_context *_ctx,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster struct mailbox_status *status_r)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster{
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster struct index_mailbox_sync_context *ctx =
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster (struct index_mailbox_sync_context *)_ctx;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster struct index_mailbox *ibox = ctx->ibox;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster uint32_t messages_count;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster int ret = ctx->failed ? -1 : 0;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (ctx->sync_ctx != NULL)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mail_index_view_sync_end(ctx->sync_ctx);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (ret == 0) {
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunnington messages_count = mail_index_view_get_messages_count(ibox->view);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (messages_count != ctx->messages_count) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster index_mailbox_update_recent(ibox,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ctx->messages_count+1,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster messages_count);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ibox->synced_recent_count = ibox->recent_flags_count;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ret = index_storage_get_status_locked(ctx->ibox,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster SYNC_STATUS_FLAGS,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster status_r);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mail_index_view_unlock(ctx->ibox->view);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster i_free(ctx);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return ret;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster