mail-index-view-sync.c revision d67f54632110cfb6aafe2d7cd1f99b031c0b208a
e59faf65ce864fe95dc00f5d52b8323cdbd0608aTimo Sirainen/* Copyright (C) 2003-2004 Timo Sirainen */
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainenview_sync_get_expunges(struct mail_index_view *view, buffer_t **expunges_r)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen const struct mail_transaction_expunge *exp, *end;
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen /* with mask 0 we don't get anything, we'll just read the expunges
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen while seeking to end */
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen if (mail_transaction_log_view_set(view->log_view,
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (mail_transaction_log_view_next(view->log_view,
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen expunges = mail_transaction_log_view_get_expunges(view->log_view);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen *expunges_r = buffer_create_dynamic(default_pool, size, (size_t)-1);
68f0dfb4b2815ecbc1bd8d8a68adcfd577ec55aeTimo Sirainen buffer_append(*expunges_r, &exp->seq1, sizeof(exp->seq1));
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen buffer_append(*expunges_r, &exp->seq2, sizeof(exp->seq2));
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen mail_transaction_log_view_unset(view->log_view);
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainenint mail_index_view_sync_begin(struct mail_index_view *view,
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen /* We must sync flags as long as view is mmap()ed, as the flags may
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen have already changed under us. */
0db5b158a00c08955bdacc99b1e2cd1ec07f4311Timo Sirainen i_assert((sync_mask & MAIL_INDEX_SYNC_TYPE_FLAGS) != 0);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (mail_index_view_lock_head(view, TRUE) < 0)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if ((sync_mask & MAIL_INDEX_SYNC_TYPE_EXPUNGE) != 0) {
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen /* get list of all expunges first */
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (view_sync_get_expunges(view, &expunges) < 0)
a54fa00087ba926a3d966a8449d8d7579e89911cTimo Sirainen mask = mail_transaction_type_mask_get(sync_mask);
a54fa00087ba926a3d966a8449d8d7579e89911cTimo Sirainen if (mail_transaction_log_view_set(view->log_view,
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen ctx = i_new(struct mail_index_view_sync_ctx, 1);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen ctx->messages_count = mail_index_view_get_message_count(view);
2cfe9983ce7a6280636ee12beccc2e865111967bTimo Sirainenstatic int view_is_transaction_synced(struct mail_index_view *view,
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen data = buffer_get_data(view->log_syncs, &size);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen *((const uint32_t *)(data + sizeof(uoff_t))) == seq)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenstatic int sync_expunge(const struct mail_transaction_expunge *e, void *context)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen mail_index_header_update_counts(&map->hdr_copy,
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen (e->seq1-1) * sizeof(struct mail_index_record),
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen map->records = buffer_get_modifyable_data(map->buffer, NULL);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainenstatic int sync_append(const struct mail_index_record *rec, void *context)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen buffer_append(map->buffer, rec, sizeof(*rec));
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen map->records = buffer_get_modifyable_data(map->buffer, NULL);
a0b89f3b1df99b3a32f44623f13ad1893118825bTimo Sirainen mail_index_header_update_counts(&map->hdr_copy, 0, rec->flags);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen mail_index_header_update_lowwaters(&map->hdr_copy, rec);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenstatic int sync_flag_update(const struct mail_transaction_flag_update *u,
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen unsigned int i, idx;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen rec->flags = (rec->flags & ~u->remove_flags) | u->add_flags;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen for (i = 0; i < INDEX_KEYWORDS_BYTE_COUNT; i++) {
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen mail_index_header_update_counts(&map->hdr_copy, old_flags,
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen mail_index_header_update_lowwaters(&map->hdr_copy, rec);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainenstatic int sync_cache_update(const struct mail_transaction_cache_update *u,
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen map->records[u->seq-1].cache_offset = u->cache_offset;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenstatic int mail_index_view_sync_map(struct mail_index_view_sync_ctx *ctx)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen static struct mail_transaction_map_functions map_funcs = {
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen sync_expunge, sync_append, sync_flag_update, sync_cache_update
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen return mail_transaction_map(ctx->hdr, ctx->data,
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainenstatic int mail_index_view_sync_next_trans(struct mail_index_view_sync_ctx *ctx,
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen struct mail_transaction_log_view *log_view = ctx->view->log_view;
3e28b527dd6048a40684afd29cff0ee008fc0014Timo Sirainen ret = mail_transaction_log_view_next(log_view, &ctx->hdr, &ctx->data,
2cfe9983ce7a6280636ee12beccc2e865111967bTimo Sirainen mail_transaction_log_view_get_prev_pos(log_view, seq_r, offset_r);
c44f402f17f9a58ead24ac0083945cae86fb172bTimo Sirainen /* skip flag changes that we committed ourself or have already synced */
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (view_is_transaction_synced(view, *seq_r, *offset_r))
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (ctx->sync_mask != MAIL_INDEX_SYNC_MASK_ALL) {
e958a3c4573058f17999f0083a34080ca35e34d8Timo Sirainenmail_index_view_sync_get_rec(struct mail_index_view_sync_ctx *ctx,
e958a3c4573058f17999f0083a34080ca35e34d8Timo Sirainen const struct mail_transaction_header *hdr = ctx->hdr;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen switch (hdr->type & MAIL_TRANSACTION_TYPE_MASK) {
a1aaf11831cab8346d6d0dc702e37b3f1d95eb43Timo Sirainen const struct mail_transaction_flag_update *update =
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainenint mail_index_view_sync_next(struct mail_index_view_sync_ctx *ctx,
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen if (ctx->hdr == NULL || ctx->data_offset == ctx->hdr->size) {
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen ret = mail_index_view_sync_next_trans(ctx, &seq,
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen } while (ret == 0);
e958a3c4573058f17999f0083a34080ca35e34d8Timo Sirainen mail_index_view_add_synced_transaction(view, seq,
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainenmail_index_view_sync_get_expunges(struct mail_index_view_sync_ctx *ctx,
68f0dfb4b2815ecbc1bd8d8a68adcfd577ec55aeTimo Sirainenvoid mail_index_view_sync_end(struct mail_index_view_sync_ctx *ctx)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen /* we didn't sync everything */
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen mail_transaction_log_view_unset(view->log_view);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenvoid mail_index_view_add_synced_transaction(struct mail_index_view *view,
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen view->log_syncs = buffer_create_dynamic(default_pool,
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen buffer_append(view->log_syncs, &log_file_offset,