cydir-sync.c revision feaa6a3d82ea61496ced1f83a726ff33047c7da2
5f5870385cff47efd2f58e7892f251cf13761528Timo Sirainen/* Copyright (c) 2007-2012 Dovecot authors, see the included COPYING file */
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen#include "lib.h"
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen#include "ioloop.h"
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen#include "str.h"
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen#include "cydir-storage.h"
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen#include "cydir-sync.h"
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
88187ee880b4829443e0d55ea7d145d9d5880217Timo Sirainenstatic void cydir_sync_set_uidvalidity(struct cydir_sync_context *ctx)
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen{
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen uint32_t uid_validity = ioloop_time;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
aa0647f2debf0d48d504a321186f66c85596aaf4Timo Sirainen mail_index_update_header(ctx->trans,
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen offsetof(struct mail_index_header, uid_validity),
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen &uid_validity, sizeof(uid_validity), TRUE);
ccec5f82349eae44087900c0e64ed1fd5a1a6fcaTimo Sirainen ctx->uid_validity = uid_validity;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen}
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainenstatic string_t *cydir_get_path_prefix(struct cydir_mailbox *mbox)
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen{
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen string_t *path = str_new(default_pool, 256);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
feaa6a3d82ea61496ced1f83a726ff33047c7da2Timo Sirainen str_append(path, mailbox_get_path(&mbox->box));
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen str_append_c(path, '/');
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen return path;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen}
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
88187ee880b4829443e0d55ea7d145d9d5880217Timo Sirainenstatic void
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainencydir_sync_expunge(struct cydir_sync_context *ctx, uint32_t seq1, uint32_t seq2)
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen{
d22301419109ed4a38351715e6760011421dadecTimo Sirainen struct mailbox *box = &ctx->mbox->box;
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen uint32_t uid;
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen if (ctx->path == NULL) {
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen ctx->path = cydir_get_path_prefix(ctx->mbox);
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen ctx->path_dir_prefix_len = str_len(ctx->path);
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen }
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen for (; seq1 <= seq2; seq1++) {
88187ee880b4829443e0d55ea7d145d9d5880217Timo Sirainen mail_index_lookup_uid(ctx->sync_view, seq1, &uid);
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen str_truncate(ctx->path, ctx->path_dir_prefix_len);
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen str_printfa(ctx->path, "%u.", uid);
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen if (unlink(str_c(ctx->path)) == 0) {
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen if (box->v.sync_notify != NULL) {
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen box->v.sync_notify(box, uid,
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen MAILBOX_SYNC_TYPE_EXPUNGE);
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen }
e063aca6bc2f08bec516d4b631052ea9191f011dTimo Sirainen mail_index_expunge(ctx->trans, seq1);
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen } else if (errno != ENOENT) {
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen mail_storage_set_critical(&ctx->mbox->storage->storage,
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen "unlink(%s) failed: %m", str_c(ctx->path));
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen /* continue anyway */
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen }
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen }
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen}
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen
b08b33d1f5ce3721dc2d83586c9cb0ca141331fdTimo Sirainenstatic void cydir_sync_index(struct cydir_sync_context *ctx)
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen{
d22301419109ed4a38351715e6760011421dadecTimo Sirainen struct mailbox *box = &ctx->mbox->box;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen const struct mail_index_header *hdr;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen struct mail_index_sync_rec sync_rec;
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen uint32_t seq1, seq2;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen hdr = mail_index_get_header(ctx->sync_view);
ccec5f82349eae44087900c0e64ed1fd5a1a6fcaTimo Sirainen if (hdr->uid_validity != 0)
ccec5f82349eae44087900c0e64ed1fd5a1a6fcaTimo Sirainen ctx->uid_validity = hdr->uid_validity;
88187ee880b4829443e0d55ea7d145d9d5880217Timo Sirainen else
88187ee880b4829443e0d55ea7d145d9d5880217Timo Sirainen cydir_sync_set_uidvalidity(ctx);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen /* mark the newly seen messages as recent */
3da614c39dd29f536c485089e67839b4cf89fed3Timo Sirainen if (mail_index_lookup_seq_range(ctx->sync_view, hdr->first_recent_uid,
3da614c39dd29f536c485089e67839b4cf89fed3Timo Sirainen hdr->next_uid, &seq1, &seq2)) {
d22301419109ed4a38351715e6760011421dadecTimo Sirainen index_mailbox_set_recent_seq(&ctx->mbox->box, ctx->sync_view,
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen seq1, seq2);
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen }
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen
b08b33d1f5ce3721dc2d83586c9cb0ca141331fdTimo Sirainen while (mail_index_sync_next(ctx->index_sync_ctx, &sync_rec)) {
3da614c39dd29f536c485089e67839b4cf89fed3Timo Sirainen if (!mail_index_lookup_seq_range(ctx->sync_view,
3da614c39dd29f536c485089e67839b4cf89fed3Timo Sirainen sync_rec.uid1, sync_rec.uid2,
3da614c39dd29f536c485089e67839b4cf89fed3Timo Sirainen &seq1, &seq2)) {
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen /* already expunged, nothing to do. */
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen continue;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen }
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen switch (sync_rec.type) {
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen case MAIL_INDEX_SYNC_TYPE_EXPUNGE:
88187ee880b4829443e0d55ea7d145d9d5880217Timo Sirainen cydir_sync_expunge(ctx, seq1, seq2);
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen break;
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen case MAIL_INDEX_SYNC_TYPE_FLAGS:
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen case MAIL_INDEX_SYNC_TYPE_KEYWORD_ADD:
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen case MAIL_INDEX_SYNC_TYPE_KEYWORD_REMOVE:
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen /* FIXME: should be bother calling sync_notify()? */
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen break;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen }
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen }
4b41116563110d00330896a568eff1078c382827Timo Sirainen
4b41116563110d00330896a568eff1078c382827Timo Sirainen if (box->v.sync_notify != NULL)
4b41116563110d00330896a568eff1078c382827Timo Sirainen box->v.sync_notify(box, 0, 0);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen}
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainenint cydir_sync_begin(struct cydir_mailbox *mbox,
d6a1fa1d65c6d1996937802c2482c0f14dd821a7Timo Sirainen struct cydir_sync_context **ctx_r, bool force)
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen{
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen struct cydir_sync_context *ctx;
2d39dc1a453546892109b35c0d9770369011a13dTimo Sirainen enum mail_index_sync_flags sync_flags;
d6a1fa1d65c6d1996937802c2482c0f14dd821a7Timo Sirainen int ret;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen ctx = i_new(struct cydir_sync_context, 1);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen ctx->mbox = mbox;
2d39dc1a453546892109b35c0d9770369011a13dTimo Sirainen
3f91e60401495a4046c73992fabaa5e77200a451Timo Sirainen sync_flags = index_storage_get_sync_flags(&mbox->box) |
3f91e60401495a4046c73992fabaa5e77200a451Timo Sirainen MAIL_INDEX_SYNC_FLAG_FLUSH_DIRTY;
d6a1fa1d65c6d1996937802c2482c0f14dd821a7Timo Sirainen if (!force)
d6a1fa1d65c6d1996937802c2482c0f14dd821a7Timo Sirainen sync_flags |= MAIL_INDEX_SYNC_FLAG_REQUIRE_CHANGES;
d6a1fa1d65c6d1996937802c2482c0f14dd821a7Timo Sirainen
d22301419109ed4a38351715e6760011421dadecTimo Sirainen ret = mail_index_sync_begin(mbox->box.index, &ctx->index_sync_ctx,
d6a1fa1d65c6d1996937802c2482c0f14dd821a7Timo Sirainen &ctx->sync_view, &ctx->trans,
d6a1fa1d65c6d1996937802c2482c0f14dd821a7Timo Sirainen sync_flags);
d6a1fa1d65c6d1996937802c2482c0f14dd821a7Timo Sirainen if (ret <= 0) {
d6a1fa1d65c6d1996937802c2482c0f14dd821a7Timo Sirainen if (ret < 0)
d22301419109ed4a38351715e6760011421dadecTimo Sirainen mail_storage_set_index_error(&mbox->box);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen i_free(ctx);
d6a1fa1d65c6d1996937802c2482c0f14dd821a7Timo Sirainen *ctx_r = NULL;
d6a1fa1d65c6d1996937802c2482c0f14dd821a7Timo Sirainen return ret;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen }
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
b08b33d1f5ce3721dc2d83586c9cb0ca141331fdTimo Sirainen cydir_sync_index(ctx);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen *ctx_r = ctx;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen return 0;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen}
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainenint cydir_sync_finish(struct cydir_sync_context **_ctx, bool success)
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen{
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen struct cydir_sync_context *ctx = *_ctx;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen int ret = success ? 0 : -1;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen *_ctx = NULL;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen if (success) {
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen if (mail_index_sync_commit(&ctx->index_sync_ctx) < 0) {
d22301419109ed4a38351715e6760011421dadecTimo Sirainen mail_storage_set_index_error(&ctx->mbox->box);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen ret = -1;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen }
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen } else {
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen mail_index_sync_rollback(&ctx->index_sync_ctx);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen }
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen if (ctx->path != NULL)
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen str_free(&ctx->path);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen i_free(ctx);
af4244c8d400d25e7fb49311c75c9b4cd5fe8275Timo Sirainen return ret;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen}
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
26af8fb913367d40a821d8713980cb1731ad6fcbTimo Sirainenstatic int cydir_sync(struct cydir_mailbox *mbox)
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen{
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen struct cydir_sync_context *sync_ctx;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
d6a1fa1d65c6d1996937802c2482c0f14dd821a7Timo Sirainen if (cydir_sync_begin(mbox, &sync_ctx, FALSE) < 0)
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen return -1;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
d6a1fa1d65c6d1996937802c2482c0f14dd821a7Timo Sirainen return sync_ctx == NULL ? 0 :
d6a1fa1d65c6d1996937802c2482c0f14dd821a7Timo Sirainen cydir_sync_finish(&sync_ctx, TRUE);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen}
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainenstruct mailbox_sync_context *
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainencydir_storage_sync_init(struct mailbox *box, enum mailbox_sync_flags flags)
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen{
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen struct cydir_mailbox *mbox = (struct cydir_mailbox *)box;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen int ret = 0;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen if (!box->opened) {
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen if (mailbox_open(box) < 0)
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen ret = -1;
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen }
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
d22301419109ed4a38351715e6760011421dadecTimo Sirainen if (index_mailbox_want_full_sync(&mbox->box, flags) && ret == 0)
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen ret = cydir_sync(mbox);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen return index_mailbox_sync_init(box, flags, ret < 0);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen}