cydir-sync.c revision ccec5f82349eae44087900c0e64ed1fd5a1a6fca
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen/* Copyright (C) 2007 Timo Sirainen */
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
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainenstatic int 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 return 0;
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 const char *dir;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
ccc895c0358108d2304239063e940b7d75f364abTimo Sirainen dir = mailbox_list_get_path(mbox->storage->storage.list,
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen mbox->ibox.box.name,
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen MAILBOX_LIST_PATH_TYPE_MAILBOX);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen str_append(path, dir);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen str_append_c(path, '/');
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen return path;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen}
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainenstatic int
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainencydir_sync_expunge(struct cydir_sync_context *ctx, uint32_t seq1, uint32_t seq2)
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen{
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen struct mailbox *box = &ctx->mbox->ibox.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++) {
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen if (mail_index_lookup_uid(ctx->sync_view, seq1, &uid) < 0) {
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen mail_storage_set_index_error(&ctx->mbox->ibox);
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen return -1;
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen }
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 return 0;
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen}
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainenstatic int cydir_sync_index(struct cydir_sync_context *ctx)
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen{
4b41116563110d00330896a568eff1078c382827Timo Sirainen struct mailbox *box = &ctx->mbox->ibox.box;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen const struct mail_index_header *hdr;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen struct mail_index_sync_rec sync_rec;
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen uint32_t seq1, seq2;
4b41116563110d00330896a568eff1078c382827Timo Sirainen int ret = 1;
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;
ccec5f82349eae44087900c0e64ed1fd5a1a6fcaTimo Sirainen else {
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen if (cydir_sync_set_uidvalidity(ctx) < 0)
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen return -1;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen }
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen /* mark the newly seen messages as recent */
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen if (mail_index_lookup_uid_range(ctx->sync_view, hdr->first_recent_uid,
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen hdr->next_uid, &seq1, &seq2) < 0) {
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen mail_storage_set_index_error(&ctx->mbox->ibox);
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen return -1;
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen }
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen if (seq1 != 0) {
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen index_mailbox_set_recent_seq(&ctx->mbox->ibox, ctx->sync_view,
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen seq1, seq2);
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen }
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen
4b41116563110d00330896a568eff1078c382827Timo Sirainen while (ret > 0 &&
4b41116563110d00330896a568eff1078c382827Timo Sirainen (ret = mail_index_sync_next(ctx->index_sync_ctx,
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen &sync_rec)) > 0) {
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen if (mail_index_lookup_uid_range(ctx->sync_view,
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen sync_rec.uid1, sync_rec.uid2,
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen &seq1, &seq2) < 0) {
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen mail_storage_set_index_error(&ctx->mbox->ibox);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen return -1;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen }
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen if (seq1 == 0) {
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_APPEND:
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen /* don't care */
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen break;
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen case MAIL_INDEX_SYNC_TYPE_EXPUNGE:
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen if (cydir_sync_expunge(ctx, seq1, seq2) < 0)
4b41116563110d00330896a568eff1078c382827Timo Sirainen ret = -1;
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 case MAIL_INDEX_SYNC_TYPE_KEYWORD_RESET:
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);
4b41116563110d00330896a568eff1078c382827Timo Sirainen return ret;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen}
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainenint cydir_sync_begin(struct cydir_mailbox *mbox,
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen struct cydir_sync_context **ctx_r)
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen{
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen struct cydir_sync_context *ctx;
2d39dc1a453546892109b35c0d9770369011a13dTimo Sirainen enum mail_index_sync_flags sync_flags;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen int ret;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen ctx = i_new(struct cydir_sync_context, 1);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen ctx->mbox = mbox;
2d39dc1a453546892109b35c0d9770369011a13dTimo Sirainen
2d39dc1a453546892109b35c0d9770369011a13dTimo Sirainen sync_flags = MAIL_INDEX_SYNC_FLAG_FLUSH_DIRTY;
2d39dc1a453546892109b35c0d9770369011a13dTimo Sirainen if (!mbox->ibox.keep_recent)
2d39dc1a453546892109b35c0d9770369011a13dTimo Sirainen sync_flags |= MAIL_INDEX_SYNC_FLAG_DROP_RECENT;
2d39dc1a453546892109b35c0d9770369011a13dTimo Sirainen
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen ret = mail_index_sync_begin(mbox->ibox.index, &ctx->index_sync_ctx,
aa0647f2debf0d48d504a321186f66c85596aaf4Timo Sirainen &ctx->sync_view, &ctx->trans,
2d39dc1a453546892109b35c0d9770369011a13dTimo Sirainen (uint32_t)-1, (uoff_t)-1, sync_flags);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen if (ret <= 0) {
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen if (ret < 0)
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen mail_storage_set_index_error(&mbox->ibox);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen i_free(ctx);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen return ret;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen }
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen if (cydir_sync_index(ctx) < 0) {
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen mail_index_sync_rollback(&ctx->index_sync_ctx);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen i_free(ctx);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen return -1;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen }
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
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) {
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen mail_storage_set_index_error(&ctx->mbox->ibox);
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);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen return 0;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen}
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainenint cydir_sync(struct cydir_mailbox *mbox)
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen{
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen struct cydir_sync_context *sync_ctx;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen if (cydir_sync_begin(mbox, &sync_ctx) < 0)
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen return -1;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen return 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
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen if (!box->opened)
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen index_storage_mailbox_open(&mbox->ibox);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen if ((flags & MAILBOX_SYNC_FLAG_FAST) == 0 ||
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen mbox->ibox.sync_last_check + MAILBOX_FULL_SYNC_INTERVAL <=
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen ioloop_time)
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen ret = cydir_sync(mbox);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen return index_mailbox_sync_init(box, flags, ret < 0);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen}