bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2007-2018 Dovecot authors, see the included COPYING file */
4146f38412c650f4d0e100cf22d7b32e7d24b2b6Timo Sirainen
4146f38412c650f4d0e100cf22d7b32e7d24b2b6Timo Sirainen#include "lib.h"
4146f38412c650f4d0e100cf22d7b32e7d24b2b6Timo Sirainen#include "ioloop.h"
4146f38412c650f4d0e100cf22d7b32e7d24b2b6Timo Sirainen#include "raw-storage.h"
4146f38412c650f4d0e100cf22d7b32e7d24b2b6Timo Sirainen#include "raw-sync.h"
436adac819e7cbeef04af08dcc6a4f3ecd4a1d9eMartti Rannanjärvi#include "mailbox-recent-flags.h"
4146f38412c650f4d0e100cf22d7b32e7d24b2b6Timo Sirainen
4146f38412c650f4d0e100cf22d7b32e7d24b2b6Timo Sirainenstatic int raw_sync(struct raw_mailbox *mbox)
4146f38412c650f4d0e100cf22d7b32e7d24b2b6Timo Sirainen{
4146f38412c650f4d0e100cf22d7b32e7d24b2b6Timo Sirainen struct mail_index_sync_ctx *index_sync_ctx;
4146f38412c650f4d0e100cf22d7b32e7d24b2b6Timo Sirainen struct mail_index_view *sync_view;
323c4e6d0c66310ea127184c9ecc60ad5e1f92b6Timo Sirainen struct mail_index_sync_rec sync_rec;
4146f38412c650f4d0e100cf22d7b32e7d24b2b6Timo Sirainen struct mail_index_transaction *trans;
4146f38412c650f4d0e100cf22d7b32e7d24b2b6Timo Sirainen uint32_t seq, uid_validity = ioloop_time;
4146f38412c650f4d0e100cf22d7b32e7d24b2b6Timo Sirainen enum mail_index_sync_flags sync_flags;
d6a1fa1d65c6d1996937802c2482c0f14dd821a7Timo Sirainen int ret;
4146f38412c650f4d0e100cf22d7b32e7d24b2b6Timo Sirainen
4146f38412c650f4d0e100cf22d7b32e7d24b2b6Timo Sirainen i_assert(!mbox->synced);
4146f38412c650f4d0e100cf22d7b32e7d24b2b6Timo Sirainen
3f91e60401495a4046c73992fabaa5e77200a451Timo Sirainen sync_flags = index_storage_get_sync_flags(&mbox->box) |
3f91e60401495a4046c73992fabaa5e77200a451Timo Sirainen MAIL_INDEX_SYNC_FLAG_FLUSH_DIRTY |
d6a1fa1d65c6d1996937802c2482c0f14dd821a7Timo Sirainen MAIL_INDEX_SYNC_FLAG_REQUIRE_CHANGES;
4146f38412c650f4d0e100cf22d7b32e7d24b2b6Timo Sirainen
22ed97ce474ef40616a873d7356fd54faa8fec29Timo Sirainen if (mail_index_view_get_messages_count(mbox->box.view) > 0) {
22ed97ce474ef40616a873d7356fd54faa8fec29Timo Sirainen /* already-synced index was opened via
22ed97ce474ef40616a873d7356fd54faa8fec29Timo Sirainen mail-index-alloc-cache. */
22ed97ce474ef40616a873d7356fd54faa8fec29Timo Sirainen return 0;
22ed97ce474ef40616a873d7356fd54faa8fec29Timo Sirainen }
22ed97ce474ef40616a873d7356fd54faa8fec29Timo Sirainen
d22301419109ed4a38351715e6760011421dadecTimo Sirainen ret = mail_index_sync_begin(mbox->box.index, &index_sync_ctx,
d6a1fa1d65c6d1996937802c2482c0f14dd821a7Timo Sirainen &sync_view, &trans, sync_flags);
d6a1fa1d65c6d1996937802c2482c0f14dd821a7Timo Sirainen if (ret <= 0) {
d6a1fa1d65c6d1996937802c2482c0f14dd821a7Timo Sirainen if (ret < 0)
1f1ee8db68d9ae1604350801cd8dc33ebe29fe8aTimo Sirainen mailbox_set_index_error(&mbox->box);
d6a1fa1d65c6d1996937802c2482c0f14dd821a7Timo Sirainen return ret;
4146f38412c650f4d0e100cf22d7b32e7d24b2b6Timo Sirainen }
4146f38412c650f4d0e100cf22d7b32e7d24b2b6Timo Sirainen
4146f38412c650f4d0e100cf22d7b32e7d24b2b6Timo Sirainen /* set our uidvalidity */
4146f38412c650f4d0e100cf22d7b32e7d24b2b6Timo Sirainen mail_index_update_header(trans,
4146f38412c650f4d0e100cf22d7b32e7d24b2b6Timo Sirainen offsetof(struct mail_index_header, uid_validity),
4146f38412c650f4d0e100cf22d7b32e7d24b2b6Timo Sirainen &uid_validity, sizeof(uid_validity), TRUE);
4146f38412c650f4d0e100cf22d7b32e7d24b2b6Timo Sirainen
4146f38412c650f4d0e100cf22d7b32e7d24b2b6Timo Sirainen /* add our one and only message */
4146f38412c650f4d0e100cf22d7b32e7d24b2b6Timo Sirainen mail_index_append(trans, 1, &seq);
817d027593510c3ba70ad542ce0011f5f6916d1eTimo Sirainen mailbox_recent_flags_set_uid(&mbox->box, 1);
4146f38412c650f4d0e100cf22d7b32e7d24b2b6Timo Sirainen
323c4e6d0c66310ea127184c9ecc60ad5e1f92b6Timo Sirainen while (mail_index_sync_next(index_sync_ctx, &sync_rec)) ;
4146f38412c650f4d0e100cf22d7b32e7d24b2b6Timo Sirainen if (mail_index_sync_commit(&index_sync_ctx) < 0) {
1f1ee8db68d9ae1604350801cd8dc33ebe29fe8aTimo Sirainen mailbox_set_index_error(&mbox->box);
4146f38412c650f4d0e100cf22d7b32e7d24b2b6Timo Sirainen return -1;
4146f38412c650f4d0e100cf22d7b32e7d24b2b6Timo Sirainen }
4146f38412c650f4d0e100cf22d7b32e7d24b2b6Timo Sirainen mbox->synced = TRUE;
4146f38412c650f4d0e100cf22d7b32e7d24b2b6Timo Sirainen return 0;
4146f38412c650f4d0e100cf22d7b32e7d24b2b6Timo Sirainen}
4146f38412c650f4d0e100cf22d7b32e7d24b2b6Timo Sirainen
4146f38412c650f4d0e100cf22d7b32e7d24b2b6Timo Sirainenstruct mailbox_sync_context *
4146f38412c650f4d0e100cf22d7b32e7d24b2b6Timo Sirainenraw_storage_sync_init(struct mailbox *box, enum mailbox_sync_flags flags)
4146f38412c650f4d0e100cf22d7b32e7d24b2b6Timo Sirainen{
8af1a286fb27446eeb0e7a8764cd3ed8d35ff2acJosef 'Jeff' Sipek struct raw_mailbox *mbox = RAW_MAILBOX(box);
4146f38412c650f4d0e100cf22d7b32e7d24b2b6Timo Sirainen int ret = 0;
4146f38412c650f4d0e100cf22d7b32e7d24b2b6Timo Sirainen
e098ac1cd4ff0afb33b22cc7fd288014f4fdb0bcTimo Sirainen if (!mbox->synced)
4146f38412c650f4d0e100cf22d7b32e7d24b2b6Timo Sirainen ret = raw_sync(mbox);
4146f38412c650f4d0e100cf22d7b32e7d24b2b6Timo Sirainen
4146f38412c650f4d0e100cf22d7b32e7d24b2b6Timo Sirainen return index_mailbox_sync_init(box, flags, ret < 0);
4146f38412c650f4d0e100cf22d7b32e7d24b2b6Timo Sirainen}