cydir-sync.c revision 1d2b188f0eedc3cab6e27ceac5425a037f38042e
2454dfa32c93c20a8522c6ed42fe057baaac9f9aStephan Bosch/* Copyright (c) 2007-2009 Dovecot authors, see the included COPYING file */
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen#include "lib.h"
bdd36cfdba3ff66d25570a9ff568d69e1eb543cfTimo Sirainen#include "ioloop.h"
c53af2efae7a3484a23412e9be2c8e81523ab65eTimo Sirainen#include "str.h"
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen#include "cydir-storage.h"
6303191abcb37164f435ccdc56e9dbddf1288851Timo Sirainen#include "cydir-sync.h"
e303353998b1959d1fa914a6be2e85cb9a2615c4Timo Sirainen
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainenstatic void cydir_sync_set_uidvalidity(struct cydir_sync_context *ctx)
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen{
6303191abcb37164f435ccdc56e9dbddf1288851Timo Sirainen uint32_t uid_validity = ioloop_time;
6303191abcb37164f435ccdc56e9dbddf1288851Timo Sirainen
6303191abcb37164f435ccdc56e9dbddf1288851Timo Sirainen mail_index_update_header(ctx->trans,
c53af2efae7a3484a23412e9be2c8e81523ab65eTimo Sirainen offsetof(struct mail_index_header, uid_validity),
c53af2efae7a3484a23412e9be2c8e81523ab65eTimo Sirainen &uid_validity, sizeof(uid_validity), TRUE);
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen ctx->uid_validity = uid_validity;
678d0463849ba777106eb7875f27db07a5d8e3dfTimo Sirainen}
678d0463849ba777106eb7875f27db07a5d8e3dfTimo Sirainen
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainenstatic string_t *cydir_get_path_prefix(struct cydir_mailbox *mbox)
6303191abcb37164f435ccdc56e9dbddf1288851Timo Sirainen{
6303191abcb37164f435ccdc56e9dbddf1288851Timo Sirainen string_t *path = str_new(default_pool, 256);
6303191abcb37164f435ccdc56e9dbddf1288851Timo Sirainen const char *dir;
2129efcd74a72d051d182171b7e07916308e386dTimo Sirainen
c53af2efae7a3484a23412e9be2c8e81523ab65eTimo Sirainen dir = mailbox_list_get_path(mbox->ibox.box.list, mbox->ibox.box.name,
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen MAILBOX_LIST_PATH_TYPE_MAILBOX);
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen str_append(path, dir);
c53af2efae7a3484a23412e9be2c8e81523ab65eTimo Sirainen str_append_c(path, '/');
c53af2efae7a3484a23412e9be2c8e81523ab65eTimo Sirainen return path;
678d0463849ba777106eb7875f27db07a5d8e3dfTimo Sirainen}
678d0463849ba777106eb7875f27db07a5d8e3dfTimo Sirainen
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainenstatic void
7dc4bf28c0c4c673a198070edd1ed54e14ae39aeTimo Sirainencydir_sync_expunge(struct cydir_sync_context *ctx, uint32_t seq1, uint32_t seq2)
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen{
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen struct mailbox *box = &ctx->mbox->ibox.box;
678d0463849ba777106eb7875f27db07a5d8e3dfTimo Sirainen uint32_t uid;
678d0463849ba777106eb7875f27db07a5d8e3dfTimo Sirainen
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen if (ctx->path == NULL) {
7dc4bf28c0c4c673a198070edd1ed54e14ae39aeTimo Sirainen ctx->path = cydir_get_path_prefix(ctx->mbox);
7dc4bf28c0c4c673a198070edd1ed54e14ae39aeTimo Sirainen ctx->path_dir_prefix_len = str_len(ctx->path);
7dc4bf28c0c4c673a198070edd1ed54e14ae39aeTimo Sirainen }
7dc4bf28c0c4c673a198070edd1ed54e14ae39aeTimo Sirainen
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen for (; seq1 <= seq2; seq1++) {
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen mail_index_lookup_uid(ctx->sync_view, seq1, &uid);
6303191abcb37164f435ccdc56e9dbddf1288851Timo Sirainen
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen str_truncate(ctx->path, ctx->path_dir_prefix_len);
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen str_printfa(ctx->path, "%u.", uid);
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen if (unlink(str_c(ctx->path)) == 0) {
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen if (box->v.sync_notify != NULL) {
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen box->v.sync_notify(box, uid,
678d0463849ba777106eb7875f27db07a5d8e3dfTimo Sirainen MAILBOX_SYNC_TYPE_EXPUNGE);
678d0463849ba777106eb7875f27db07a5d8e3dfTimo Sirainen }
2129efcd74a72d051d182171b7e07916308e386dTimo Sirainen mail_index_expunge(ctx->trans, seq1);
2129efcd74a72d051d182171b7e07916308e386dTimo Sirainen } else if (errno != ENOENT) {
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen mail_storage_set_critical(&ctx->mbox->storage->storage,
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen "unlink(%s) failed: %m", str_c(ctx->path));
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen /* continue anyway */
c53af2efae7a3484a23412e9be2c8e81523ab65eTimo Sirainen }
c53af2efae7a3484a23412e9be2c8e81523ab65eTimo Sirainen }
c53af2efae7a3484a23412e9be2c8e81523ab65eTimo Sirainen}
c53af2efae7a3484a23412e9be2c8e81523ab65eTimo Sirainen
c53af2efae7a3484a23412e9be2c8e81523ab65eTimo Sirainenstatic void cydir_sync_index(struct cydir_sync_context *ctx)
c53af2efae7a3484a23412e9be2c8e81523ab65eTimo Sirainen{
c53af2efae7a3484a23412e9be2c8e81523ab65eTimo Sirainen struct mailbox *box = &ctx->mbox->ibox.box;
c53af2efae7a3484a23412e9be2c8e81523ab65eTimo Sirainen const struct mail_index_header *hdr;
c53af2efae7a3484a23412e9be2c8e81523ab65eTimo Sirainen struct mail_index_sync_rec sync_rec;
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen uint32_t seq1, seq2;
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen hdr = mail_index_get_header(ctx->sync_view);
623eaaf530b2bd9cddbaeb88a211873ec9dee436Timo Sirainen if (hdr->uid_validity != 0)
623eaaf530b2bd9cddbaeb88a211873ec9dee436Timo Sirainen ctx->uid_validity = hdr->uid_validity;
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen else
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen cydir_sync_set_uidvalidity(ctx);
6303191abcb37164f435ccdc56e9dbddf1288851Timo Sirainen
623eaaf530b2bd9cddbaeb88a211873ec9dee436Timo Sirainen /* mark the newly seen messages as recent */
623eaaf530b2bd9cddbaeb88a211873ec9dee436Timo Sirainen if (mail_index_lookup_seq_range(ctx->sync_view, hdr->first_recent_uid,
623eaaf530b2bd9cddbaeb88a211873ec9dee436Timo Sirainen hdr->next_uid, &seq1, &seq2)) {
623eaaf530b2bd9cddbaeb88a211873ec9dee436Timo Sirainen index_mailbox_set_recent_seq(&ctx->mbox->ibox, ctx->sync_view,
623eaaf530b2bd9cddbaeb88a211873ec9dee436Timo Sirainen seq1, seq2);
623eaaf530b2bd9cddbaeb88a211873ec9dee436Timo Sirainen }
0d1b8b6bec79746c5d89d57dd8c1688946bd9237Josef 'Jeff' Sipek
c53af2efae7a3484a23412e9be2c8e81523ab65eTimo Sirainen while (mail_index_sync_next(ctx->index_sync_ctx, &sync_rec)) {
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen if (!mail_index_lookup_seq_range(ctx->sync_view,
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen sync_rec.uid1, sync_rec.uid2,
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen &seq1, &seq2)) {
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen /* already expunged, nothing to do. */
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen continue;
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen }
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen
009217abb57a24a4076092e8e4e165545747839eStephan Bosch switch (sync_rec.type) {
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen case MAIL_INDEX_SYNC_TYPE_APPEND:
7dc4bf28c0c4c673a198070edd1ed54e14ae39aeTimo Sirainen /* don't care */
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen break;
efe78d3ba24fc866af1c79b9223dc0809ba26cadStephan Bosch case MAIL_INDEX_SYNC_TYPE_EXPUNGE:
7dc4bf28c0c4c673a198070edd1ed54e14ae39aeTimo Sirainen cydir_sync_expunge(ctx, seq1, seq2);
7dc4bf28c0c4c673a198070edd1ed54e14ae39aeTimo Sirainen break;
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen case MAIL_INDEX_SYNC_TYPE_FLAGS:
7dc4bf28c0c4c673a198070edd1ed54e14ae39aeTimo Sirainen case MAIL_INDEX_SYNC_TYPE_KEYWORD_ADD:
7dc4bf28c0c4c673a198070edd1ed54e14ae39aeTimo Sirainen case MAIL_INDEX_SYNC_TYPE_KEYWORD_REMOVE:
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen case MAIL_INDEX_SYNC_TYPE_KEYWORD_RESET:
7dc4bf28c0c4c673a198070edd1ed54e14ae39aeTimo Sirainen /* FIXME: should be bother calling sync_notify()? */
7dc4bf28c0c4c673a198070edd1ed54e14ae39aeTimo Sirainen break;
7dc4bf28c0c4c673a198070edd1ed54e14ae39aeTimo Sirainen }
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen }
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen if (box->v.sync_notify != NULL)
6303191abcb37164f435ccdc56e9dbddf1288851Timo Sirainen box->v.sync_notify(box, 0, 0);
c53af2efae7a3484a23412e9be2c8e81523ab65eTimo Sirainen}
c53af2efae7a3484a23412e9be2c8e81523ab65eTimo Sirainen
c53af2efae7a3484a23412e9be2c8e81523ab65eTimo Sirainenint cydir_sync_begin(struct cydir_mailbox *mbox,
c53af2efae7a3484a23412e9be2c8e81523ab65eTimo Sirainen struct cydir_sync_context **ctx_r, bool force)
c53af2efae7a3484a23412e9be2c8e81523ab65eTimo Sirainen{
c53af2efae7a3484a23412e9be2c8e81523ab65eTimo Sirainen struct cydir_sync_context *ctx;
2129efcd74a72d051d182171b7e07916308e386dTimo Sirainen enum mail_index_sync_flags sync_flags;
2129efcd74a72d051d182171b7e07916308e386dTimo Sirainen int ret;
c53af2efae7a3484a23412e9be2c8e81523ab65eTimo Sirainen
c53af2efae7a3484a23412e9be2c8e81523ab65eTimo Sirainen ctx = i_new(struct cydir_sync_context, 1);
2129efcd74a72d051d182171b7e07916308e386dTimo Sirainen ctx->mbox = mbox;
c53af2efae7a3484a23412e9be2c8e81523ab65eTimo Sirainen
2129efcd74a72d051d182171b7e07916308e386dTimo Sirainen sync_flags = MAIL_INDEX_SYNC_FLAG_FLUSH_DIRTY;
2129efcd74a72d051d182171b7e07916308e386dTimo Sirainen if (!mbox->ibox.keep_recent)
2129efcd74a72d051d182171b7e07916308e386dTimo Sirainen sync_flags |= MAIL_INDEX_SYNC_FLAG_DROP_RECENT;
c53af2efae7a3484a23412e9be2c8e81523ab65eTimo Sirainen if (!force)
c53af2efae7a3484a23412e9be2c8e81523ab65eTimo Sirainen sync_flags |= MAIL_INDEX_SYNC_FLAG_REQUIRE_CHANGES;
c53af2efae7a3484a23412e9be2c8e81523ab65eTimo Sirainen
2129efcd74a72d051d182171b7e07916308e386dTimo Sirainen ret = mail_index_sync_begin(mbox->ibox.index, &ctx->index_sync_ctx,
2129efcd74a72d051d182171b7e07916308e386dTimo Sirainen &ctx->sync_view, &ctx->trans,
e303353998b1959d1fa914a6be2e85cb9a2615c4Timo Sirainen sync_flags);
2129efcd74a72d051d182171b7e07916308e386dTimo Sirainen if (ret <= 0) {
2129efcd74a72d051d182171b7e07916308e386dTimo Sirainen if (ret < 0)
2129efcd74a72d051d182171b7e07916308e386dTimo Sirainen mail_storage_set_index_error(&mbox->ibox);
c53af2efae7a3484a23412e9be2c8e81523ab65eTimo Sirainen i_free(ctx);
c53af2efae7a3484a23412e9be2c8e81523ab65eTimo Sirainen *ctx_r = NULL;
6303191abcb37164f435ccdc56e9dbddf1288851Timo Sirainen return ret;
2ac5f36aa7c2e7a07ba8815d43a6d7483f62e74cTimo Sirainen }
6303191abcb37164f435ccdc56e9dbddf1288851Timo Sirainen
6303191abcb37164f435ccdc56e9dbddf1288851Timo Sirainen cydir_sync_index(ctx);
2129efcd74a72d051d182171b7e07916308e386dTimo Sirainen *ctx_r = ctx;
2129efcd74a72d051d182171b7e07916308e386dTimo Sirainen return 0;
c53af2efae7a3484a23412e9be2c8e81523ab65eTimo Sirainen}
abb404575a238f27ea03b6049880f30f1656ddc4Timo Sirainen
2129efcd74a72d051d182171b7e07916308e386dTimo Sirainenint cydir_sync_finish(struct cydir_sync_context **_ctx, bool success)
6303191abcb37164f435ccdc56e9dbddf1288851Timo Sirainen{
6303191abcb37164f435ccdc56e9dbddf1288851Timo Sirainen struct cydir_sync_context *ctx = *_ctx;
6303191abcb37164f435ccdc56e9dbddf1288851Timo Sirainen int ret = success ? 0 : -1;
6303191abcb37164f435ccdc56e9dbddf1288851Timo Sirainen
6303191abcb37164f435ccdc56e9dbddf1288851Timo Sirainen *_ctx = NULL;
6303191abcb37164f435ccdc56e9dbddf1288851Timo Sirainen if (success) {
6303191abcb37164f435ccdc56e9dbddf1288851Timo Sirainen if (mail_index_sync_commit(&ctx->index_sync_ctx) < 0) {
6303191abcb37164f435ccdc56e9dbddf1288851Timo Sirainen mail_storage_set_index_error(&ctx->mbox->ibox);
6303191abcb37164f435ccdc56e9dbddf1288851Timo Sirainen ret = -1;
dc1bc1685e4a0d58ae7bacaecc282d0ebde2d7daTimo Sirainen }
6303191abcb37164f435ccdc56e9dbddf1288851Timo Sirainen } else {
6303191abcb37164f435ccdc56e9dbddf1288851Timo Sirainen mail_index_sync_rollback(&ctx->index_sync_ctx);
6303191abcb37164f435ccdc56e9dbddf1288851Timo Sirainen }
6303191abcb37164f435ccdc56e9dbddf1288851Timo Sirainen if (ctx->path != NULL)
6303191abcb37164f435ccdc56e9dbddf1288851Timo Sirainen str_free(&ctx->path);
6303191abcb37164f435ccdc56e9dbddf1288851Timo Sirainen i_free(ctx);
c53af2efae7a3484a23412e9be2c8e81523ab65eTimo Sirainen return 0;
c53af2efae7a3484a23412e9be2c8e81523ab65eTimo Sirainen}
c53af2efae7a3484a23412e9be2c8e81523ab65eTimo Sirainen
6303191abcb37164f435ccdc56e9dbddf1288851Timo Sirainenstatic int cydir_sync(struct cydir_mailbox *mbox)
c53af2efae7a3484a23412e9be2c8e81523ab65eTimo Sirainen{
c53af2efae7a3484a23412e9be2c8e81523ab65eTimo Sirainen struct cydir_sync_context *sync_ctx;
c53af2efae7a3484a23412e9be2c8e81523ab65eTimo Sirainen
c53af2efae7a3484a23412e9be2c8e81523ab65eTimo Sirainen if (cydir_sync_begin(mbox, &sync_ctx, FALSE) < 0)
c53af2efae7a3484a23412e9be2c8e81523ab65eTimo Sirainen return -1;
c53af2efae7a3484a23412e9be2c8e81523ab65eTimo Sirainen
c53af2efae7a3484a23412e9be2c8e81523ab65eTimo Sirainen return sync_ctx == NULL ? 0 :
e34d170f8f0e084bd94bfbc1a7085ece67e508dfTimo Sirainen cydir_sync_finish(&sync_ctx, TRUE);
6303191abcb37164f435ccdc56e9dbddf1288851Timo Sirainen}
struct mailbox_sync_context *
cydir_storage_sync_init(struct mailbox *box, enum mailbox_sync_flags flags)
{
struct cydir_mailbox *mbox = (struct cydir_mailbox *)box;
int ret = 0;
if (!box->opened) {
if (mailbox_open(box) < 0)
ret = -1;
}
if (index_mailbox_want_full_sync(&mbox->ibox, flags) && ret == 0)
ret = cydir_sync(mbox);
return index_mailbox_sync_init(box, flags, ret < 0);
}