cydir-sync.c revision ccc895c0358108d2304239063e940b7d75f364ab
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 struct mail_index_transaction *trans;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen uint32_t uid_validity = ioloop_time;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen uint32_t seq;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen uoff_t offset;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen trans = mail_index_transaction_begin(ctx->sync_view, FALSE, TRUE);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen mail_index_update_header(trans,
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen offsetof(struct mail_index_header, uid_validity),
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen &uid_validity, sizeof(uid_validity), TRUE);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen if (mail_index_transaction_commit(&trans, &seq, &offset) < 0) {
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen mail_storage_set_index_error(&ctx->mbox->ibox);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen return -1;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen }
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen return 0;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen}
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainenstatic string_t *cydir_get_path_prefix(struct cydir_mailbox *mbox)
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen{
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen string_t *path = t_str_new(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
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainenstatic int cydir_sync_index(struct cydir_sync_context *ctx)
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen{
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen const struct mail_index_header *hdr;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen struct mail_index_sync_rec sync_rec;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen string_t *path = NULL;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen unsigned int prefix_len = 0;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen uint32_t seq1, seq2, uid;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen int ret;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen hdr = mail_index_get_header(ctx->sync_view);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen if (hdr->uid_validity == 0) {
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen if (cydir_sync_set_uidvalidity(ctx) < 0)
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen return -1;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen }
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen /* unlink expunged messages */
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen while ((ret = mail_index_sync_next(ctx->index_sync_ctx,
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen &sync_rec)) > 0) {
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen if (sync_rec.type != MAIL_INDEX_SYNC_TYPE_EXPUNGE)
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen continue;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
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) {
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen /* already expunged everything. nothing to do. */
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen continue;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen }
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen if (path == NULL) {
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen path = cydir_get_path_prefix(ctx->mbox);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen prefix_len = str_len(path);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen }
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen for (; seq1 <= seq2; seq1++) {
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen if (mail_index_lookup_uid(ctx->sync_view, seq1,
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen &uid) < 0) {
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen mail_storage_set_index_error(&ctx->mbox->ibox);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen return -1;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen }
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen str_truncate(path, prefix_len);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen str_printfa(path, "%u.", uid);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen if (unlink(str_c(path)) < 0 && errno != ENOENT) {
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen mail_storage_set_critical(
ccc895c0358108d2304239063e940b7d75f364abTimo Sirainen &ctx->mbox->storage->storage,
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen "unlink(%s) failed: %m", str_c(path));
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen /* continue anyway */
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen }
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen }
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen }
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen return 0;
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;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen int ret;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen ctx = i_new(struct cydir_sync_context, 1);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen ctx->mbox = mbox;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen ret = mail_index_sync_begin(mbox->ibox.index, &ctx->index_sync_ctx,
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen &ctx->sync_view, (uint32_t)-1, (uoff_t)-1,
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen !mbox->ibox.keep_recent, TRUE);
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 }
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}