static int mdbox_sync_try_begin(struct mdbox_sync_context *ctx, enum mail_index_sync_flags sync_flags) { struct mdbox_mailbox *mbox = ctx->mbox; int ret; ret = mail_index_sync_begin(mbox->box.index, &ctx->index_sync_ctx, &ctx->sync_view, &ctx->trans, sync_flags); if (mail_index_reset_fscked(mbox->box.index)) mdbox_storage_set_corrupted(mbox->storage); if (ret < 0) { mailbox_set_index_error(&mbox->box); return -1; } if (ret == 0) { /* nothing to do */ return 0; } if (!mdbox_map_atomic_is_locked(ctx->atomic) && mail_index_sync_has_expunges(ctx->index_sync_ctx)) { /* we have expunges, so we need to write to map. it needs to be locked before mailbox index. */ mail_index_sync_rollback(&ctx->index_sync_ctx); if (mdbox_map_atomic_lock(ctx->atomic) < 0) return -1; return mdbox_sync_try_begin(ctx, sync_flags); } return 1; }
struct mailbox_sync_context * mdbox_storage_sync_init(struct mailbox *box, enum mailbox_sync_flags flags) { struct mdbox_mailbox *mbox = (struct mdbox_mailbox *)box; enum mdbox_sync_flags mdbox_sync_flags = 0; int ret = 0; if (!box->opened) { if (mailbox_open(box) < 0) ret = -1; } if (box->opened) { if (mail_index_reset_fscked(box->index)) mdbox_storage_set_corrupted(mbox->storage); } if (ret == 0 && (index_mailbox_want_full_sync(&mbox->box, flags) || mbox->storage->corrupted)) { if ((flags & MAILBOX_SYNC_FLAG_FORCE_RESYNC) != 0) mdbox_sync_flags |= MDBOX_SYNC_FLAG_FORCE_REBUILD; ret = mdbox_sync(mbox, mdbox_sync_flags); } return index_mailbox_sync_init(box, flags, ret < 0); }
int mdbox_map_refresh(struct mdbox_map *map) { struct mail_index_view_sync_ctx *ctx; bool delayed_expunges, fscked; int ret = 0; /* some open files may have read partially written mails. now that map syncing makes the new mails visible, we need to make sure the partial data is flushed out of memory */ mdbox_files_sync_input(map->storage); if (mail_index_refresh(map->view->index) < 0) { mail_storage_set_internal_error(MAP_STORAGE(map)); mail_index_reset_error(map->index); return -1; } if (mail_index_view_get_transaction_count(map->view) > 0) { /* can't sync when there are transactions */ return 0; } ctx = mail_index_view_sync_begin(map->view, MAIL_INDEX_VIEW_SYNC_FLAG_FIX_INCONSISTENT); fscked = mail_index_reset_fscked(map->view->index); if (mail_index_view_sync_commit(&ctx, &delayed_expunges) < 0) { mail_storage_set_internal_error(MAP_STORAGE(map)); mail_index_reset_error(map->index); ret = -1; } if (fscked) mdbox_storage_set_corrupted(map->storage); return ret; }