mail-index-view-sync.c revision 7c5b51bdf43a98e12c654ad437e0b258c5fffbc1
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen/* Copyright (C) 2003-2004 Timo Sirainen */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenview_sync_get_expunges(struct mail_index_view *view, buffer_t **expunges_r)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen struct mail_transaction_expunge *src, *src_end, *dest;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen *expunges_r = buffer_create_dynamic(default_pool, 512, (size_t)-1);
7888a9d2008eab9985096c46e1da9ee985c22a2aTimo Sirainen /* with mask 0 we don't get anything, we'll just read the expunges
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen while seeking to end */
a24f6b02ed8d0dde933a715be1c86f01977bf610Timo Sirainen if (mail_transaction_log_view_set(view->log_view,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen while ((ret = mail_transaction_log_view_next(view->log_view,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_transaction_log_sort_expunges(*expunges_r,
93b29720c5141f787bd1861796867e4595c9d084Timo Sirainen /* convert to sequences */
93b29720c5141f787bd1861796867e4595c9d084Timo Sirainen src = dest = buffer_get_modifyable_data(*expunges_r, &size);
93b29720c5141f787bd1861796867e4595c9d084Timo Sirainen ret = mail_index_lookup_uid_range(view, src->uid1,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen (MAIL_TRANSACTION_EXPUNGE | MAIL_TRANSACTION_APPEND | \
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenint mail_index_view_sync_begin(struct mail_index_view *view,
93b29720c5141f787bd1861796867e4595c9d084Timo Sirainen /* We must sync flags as long as view is mmap()ed, as the flags may
93b29720c5141f787bd1861796867e4595c9d084Timo Sirainen have already changed under us. */
93b29720c5141f787bd1861796867e4595c9d084Timo Sirainen i_assert((sync_mask & MAIL_INDEX_SYNC_TYPE_FLAGS) != 0);
0d658231054332c3f4c04aab0422af649de89a8cTimo Sirainen if (mail_index_view_lock_head(view, TRUE) < 0)
0d658231054332c3f4c04aab0422af649de89a8cTimo Sirainen if ((sync_mask & MAIL_INDEX_SYNC_TYPE_EXPUNGE) != 0) {
0d658231054332c3f4c04aab0422af649de89a8cTimo Sirainen /* get list of all expunges first */
0d658231054332c3f4c04aab0422af649de89a8cTimo Sirainen if (view_sync_get_expunges(view, &expunges) < 0)
9df8c9225140d9d1df5ddf4c6c9da61662ae6c44Timo Sirainen /* only flags, appends and expunges can be left to be synced later */
9df8c9225140d9d1df5ddf4c6c9da61662ae6c44Timo Sirainen want_mask = mail_transaction_type_mask_get(sync_mask);
9df8c9225140d9d1df5ddf4c6c9da61662ae6c44Timo Sirainen i_assert((want_mask & ~MAIL_INDEX_VIEW_VISIBLE_SYNC_MASK) == 0);
9df8c9225140d9d1df5ddf4c6c9da61662ae6c44Timo Sirainen if (mail_transaction_log_view_set(view->log_view,
40ef82c46f6652412b068ebcdac7c3e74840a284Timo Sirainen ctx = i_new(struct mail_index_view_sync_ctx, 1);
40ef82c46f6652412b068ebcdac7c3e74840a284Timo Sirainen if ((sync_mask & MAIL_INDEX_SYNC_TYPE_EXPUNGE) != 0) {
f2786c07cbd4a7a0a6a46c3e06dc4545aaf2f278Timo Sirainen /* keep the old mapping without expunges until we're
f2786c07cbd4a7a0a6a46c3e06dc4545aaf2f278Timo Sirainen fully synced */
f2786c07cbd4a7a0a6a46c3e06dc4545aaf2f278Timo Sirainen /* we need a private copy of the map if we don't want to
f2786c07cbd4a7a0a6a46c3e06dc4545aaf2f278Timo Sirainen sync expunges */
1b56f5fdd415270c743a38719d41b4d9497bcacdTimo Sirainenstatic int view_is_transaction_synced(struct mail_index_view *view,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen data = buffer_get_data(view->log_syncs, &size);
fc7b17677ac1a5fa3f7fe13d5ef7dcfea8d9b4a1Timo Sirainen *((const uint32_t *)(data + sizeof(uoff_t))) == seq)
fc7b17677ac1a5fa3f7fe13d5ef7dcfea8d9b4a1Timo Sirainenstatic int mail_index_view_sync_next_trans(struct mail_index_view_sync_ctx *ctx,
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen struct mail_transaction_log_view *log_view = ctx->view->log_view;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen ret = mail_transaction_log_view_next(log_view, &ctx->hdr, &ctx->data,
1b56f5fdd415270c743a38719d41b4d9497bcacdTimo Sirainen mail_transaction_log_view_get_prev_pos(log_view, seq_r, offset_r);
1b56f5fdd415270c743a38719d41b4d9497bcacdTimo Sirainen /* skip flag changes that we committed ourself or have already synced */
1b56f5fdd415270c743a38719d41b4d9497bcacdTimo Sirainen if (view_is_transaction_synced(view, *seq_r, *offset_r))
1b56f5fdd415270c743a38719d41b4d9497bcacdTimo Sirainen /* expunges have to be synced afterwards so that caller can still get
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen information of the messages. otherwise caller most likely wants to
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen see only updated information. */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen (ctx->hdr->type & MAIL_TRANSACTION_EXPUNGE) == 0) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen memset(&sync_map_ctx, 0, sizeof(sync_map_ctx));
a24f6b02ed8d0dde933a715be1c86f01977bf610Timo Sirainen if (mail_transaction_map(view->index, ctx->hdr, ctx->data,
9df8c9225140d9d1df5ddf4c6c9da61662ae6c44Timo Sirainen if ((ctx->hdr->type & ctx->trans_sync_mask) == 0)
a24f6b02ed8d0dde933a715be1c86f01977bf610Timo Sirainen ~(MAIL_INDEX_MAIL_FLAG_DIRTY | MAIL_RECENT)) == 0 && \
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen memcmp((u)->add_keywords, empty, INDEX_KEYWORDS_BYTE_COUNT) == 0 && \
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen memcmp((u)->add_keywords, empty, INDEX_KEYWORDS_BYTE_COUNT) == 0)
a24f6b02ed8d0dde933a715be1c86f01977bf610Timo Sirainenmail_index_view_sync_get_rec(struct mail_index_view_sync_ctx *ctx,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen static keywords_mask_t empty_keywords = { 0, };
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const struct mail_transaction_header *hdr = ctx->hdr;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen switch (hdr->type & MAIL_TRANSACTION_TYPE_MASK) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const struct mail_transaction_flag_update *update =
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (!FLAG_UPDATE_IS_INTERNAL(update, empty_keywords))
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenint mail_index_view_sync_next(struct mail_index_view_sync_ctx *ctx,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (ctx->hdr == NULL || ctx->data_offset == ctx->hdr->size) {
44ff75ca53188056ff5a3e50428e3f2078800b3cTimo Sirainen ret = mail_index_view_sync_next_trans(ctx, &seq,
fadd878cd6098f5b873c21c121209a922679dae4Timo Sirainen } while (ret == 0);
fadd878cd6098f5b873c21c121209a922679dae4Timo Sirainen } while (!mail_index_view_sync_get_rec(ctx, sync_rec));
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenmail_index_view_sync_get_expunges(struct mail_index_view_sync_ctx *ctx,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenvoid mail_index_view_sync_end(struct mail_index_view_sync_ctx *ctx)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (view->log_syncs != NULL && !ctx->skipped_some)
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen /* we didn't sync everything */
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen if ((ctx->trans_sync_mask & MAIL_TRANSACTION_APPEND) != 0)
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen view->messages_count = view->map->records_count;
1225a5a7ce39f1d5545d5ed3b84ecd4f72438d36Timo Sirainen (void)mail_transaction_log_view_set(view->log_view,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenvoid mail_index_view_add_synced_transaction(struct mail_index_view *view,
1225a5a7ce39f1d5545d5ed3b84ecd4f72438d36Timo Sirainen view->log_syncs = buffer_create_dynamic(default_pool,
6eb30032b4a50c383dea4c9c74342d906de6ad36Timo Sirainen buffer_append(view->log_syncs, &log_file_offset,