sdbox-sync.c revision ae1636c09e096904119db3410ba911625246f892
5f5870385cff47efd2f58e7892f251cf13761528Timo Sirainen/* Copyright (c) 2007-2011 Dovecot authors, see the included COPYING file */
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainendbox_sync_file_move_if_needed(struct dbox_file *file,
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen bool move_to_alt = type == SDBOX_SYNC_ENTRY_TYPE_MOVE_TO_ALT;
3c3002ee03e2c20034f73cfec80c6647320bd27cTimo Sirainen if (move_to_alt != dbox_file_is_in_alt(file)) {
3c3002ee03e2c20034f73cfec80c6647320bd27cTimo Sirainen /* move the file. if it fails, nothing broke so
3c3002ee03e2c20034f73cfec80c6647320bd27cTimo Sirainen don't worry about it. */
3c3002ee03e2c20034f73cfec80c6647320bd27cTimo Sirainen if (dbox_file_open(file, &deleted) > 0 && !deleted)
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainenstatic void sdbox_sync_file(struct sdbox_sync_context *ctx,
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen if (!mail_index_transaction_is_expunged(ctx->trans, seq)) {
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainenstatic void sdbox_sync_add(struct sdbox_sync_context *ctx,
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen if (sync_rec->type == MAIL_INDEX_SYNC_TYPE_EXPUNGE) {
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen /* we're interested */
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen } else if (sync_rec->type == MAIL_INDEX_SYNC_TYPE_FLAGS) {
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen /* we care only about alt flag changes */
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen if ((sync_rec->add_flags & DBOX_INDEX_FLAG_ALT) != 0)
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen else if ((sync_rec->remove_flags & DBOX_INDEX_FLAG_ALT) != 0)
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen /* not interested */
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen if (!mail_index_lookup_seq_range(ctx->sync_view,
3c3002ee03e2c20034f73cfec80c6647320bd27cTimo Sirainen /* already expunged everything. nothing to do. */
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen mail_index_lookup_uid(ctx->sync_view, seq, &uid);
3c3002ee03e2c20034f73cfec80c6647320bd27cTimo Sirainenstatic int sdbox_sync_index(struct sdbox_sync_context *ctx)
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen /* newly created index file */
678d0463849ba777106eb7875f27db07a5d8e3dfTimo Sirainen "sdbox %s: Broken index: missing UIDVALIDITY",
3c3002ee03e2c20034f73cfec80c6647320bd27cTimo Sirainen /* mark the newly seen messages as recent */
3c3002ee03e2c20034f73cfec80c6647320bd27cTimo Sirainen if (mail_index_lookup_seq_range(ctx->sync_view, hdr->first_recent_uid,
3c3002ee03e2c20034f73cfec80c6647320bd27cTimo Sirainen index_mailbox_set_recent_seq(box, ctx->sync_view, seq1, seq2);
3c3002ee03e2c20034f73cfec80c6647320bd27cTimo Sirainen while (mail_index_sync_next(ctx->index_sync_ctx, &sync_rec))
88339cce70862ea6c6fac5615975e1e94a52a72eTimo Sirainenstatic void dbox_sync_file_expunge(struct sdbox_sync_context *ctx,
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen ret = sdbox_file_unlink_with_attachments(sfile);
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen /* do sync_notify only when the file was unlinked by us */
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen box->v.sync_notify(box, uid, MAILBOX_SYNC_TYPE_EXPUNGE);
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainenstatic void dbox_sync_expunge_files(struct sdbox_sync_context *ctx)
88339cce70862ea6c6fac5615975e1e94a52a72eTimo Sirainen /* NOTE: Index is no longer locked. Multiple processes may be unlinking
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen the files at the same time. */
88339cce70862ea6c6fac5615975e1e94a52a72eTimo Sirainen ctx->mbox->box.v.sync_notify(&ctx->mbox->box, 0, 0);
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainensdbox_refresh_header(struct sdbox_mailbox *mbox, bool retry, bool log_error)
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen ret = sdbox_read_header(mbox, &hdr, log_error);
3c3002ee03e2c20034f73cfec80c6647320bd27cTimo Sirainen return sdbox_refresh_header(mbox, FALSE, log_error);
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainenint sdbox_sync_begin(struct sdbox_mailbox *mbox, enum sdbox_sync_flags flags,
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen struct mail_storage *storage = mbox->box.storage;
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen unsigned int i;
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen force_rebuild = (flags & SDBOX_SYNC_FLAG_FORCE_REBUILD) != 0;
e34d170f8f0e084bd94bfbc1a7085ece67e508dfTimo Sirainen sync_flags = index_storage_get_sync_flags(&mbox->box);
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen if (!rebuild && (flags & SDBOX_SYNC_FLAG_FORCE) == 0)
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen sync_flags |= MAIL_INDEX_SYNC_FLAG_REQUIRE_CHANGES;
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen /* don't write unnecessary dirty flag updates */
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen sync_flags |= MAIL_INDEX_SYNC_FLAG_AVOID_FLAG_UPDATES;
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen for (i = 0;; i++) {
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen /* failure. keep the index locked while we're doing a
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen "sdbox %s: Index keeps breaking",
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen /* do a full resync and try again. */
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen mail_index_sync_rollback(&ctx->index_sync_ctx);
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainenint sdbox_sync_finish(struct sdbox_sync_context **_ctx, bool success)
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen if (mail_index_sync_commit(&ctx->index_sync_ctx) < 0) {
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen mail_storage_set_index_error(&ctx->mbox->box);
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen mail_index_sync_rollback(&ctx->index_sync_ctx);
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainenint sdbox_sync(struct sdbox_mailbox *mbox, enum sdbox_sync_flags flags)
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen if (sdbox_sync_begin(mbox, flags, &sync_ctx) < 0)
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainensdbox_storage_sync_init(struct mailbox *box, enum mailbox_sync_flags flags)
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen struct sdbox_mailbox *mbox = (struct sdbox_mailbox *)box;
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen if (ret == 0 && index_mailbox_want_full_sync(&mbox->box, flags)) {
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen if ((flags & MAILBOX_SYNC_FLAG_FORCE_RESYNC) != 0)
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen sdbox_sync_flags |= SDBOX_SYNC_FLAG_FORCE_REBUILD;