cydir-sync.c revision b7ad29db8cf906da01ee4626730868881087950b
e59faf65ce864fe95dc00f5d52b8323cdbd0608aTimo Sirainen/* Copyright (c) 2007-2008 Dovecot authors, see the included COPYING file */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "lib.h"
16f816d3f3c32ae3351834253f52ddd0212bcbf3Timo Sirainen#include "ioloop.h"
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen#include "str.h"
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen#include "cydir-storage.h"
58be9d6bcc3800f5b3d76a064ee767fbe31a5a8aTimo Sirainen#include "cydir-sync.h"
1098fc409a45e7603701dc94635927a673bee0c1Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic void cydir_sync_set_uidvalidity(struct cydir_sync_context *ctx)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen uint32_t uid_validity = ioloop_time;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_index_update_header(ctx->trans,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen offsetof(struct mail_index_header, uid_validity),
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen &uid_validity, sizeof(uid_validity), TRUE);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ctx->uid_validity = uid_validity;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic string_t *cydir_get_path_prefix(struct cydir_mailbox *mbox)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen string_t *path = str_new(default_pool, 256);
66ecc94150cbce23aad3240135e0782e0a74d479Timo Sirainen const char *dir;
66ecc94150cbce23aad3240135e0782e0a74d479Timo Sirainen
ef5fb27361cc5e15766e85e28355750ff04b13c9Timo Sirainen dir = mailbox_list_get_path(mbox->storage->storage.list,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mbox->ibox.box.name,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen MAILBOX_LIST_PATH_TYPE_MAILBOX);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen str_append(path, dir);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen str_append_c(path, '/');
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return path;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
6bc0f424bcdb9119d8159874cf98adfa53eefd9aTimo Sirainenstatic void
6bc0f424bcdb9119d8159874cf98adfa53eefd9aTimo Sirainencydir_sync_expunge(struct cydir_sync_context *ctx, uint32_t seq1, uint32_t seq2)
6bc0f424bcdb9119d8159874cf98adfa53eefd9aTimo Sirainen{
6bc0f424bcdb9119d8159874cf98adfa53eefd9aTimo Sirainen struct mailbox *box = &ctx->mbox->ibox.box;
6bc0f424bcdb9119d8159874cf98adfa53eefd9aTimo Sirainen uint32_t uid;
6bc0f424bcdb9119d8159874cf98adfa53eefd9aTimo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (ctx->path == NULL) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ctx->path = cydir_get_path_prefix(ctx->mbox);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ctx->path_dir_prefix_len = str_len(ctx->path);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
6bc0f424bcdb9119d8159874cf98adfa53eefd9aTimo Sirainen
ad850190d946d34966a56838cfdb216e021b5b5fTimo Sirainen for (; seq1 <= seq2; seq1++) {
ad850190d946d34966a56838cfdb216e021b5b5fTimo Sirainen mail_index_lookup_uid(ctx->sync_view, seq1, &uid);
ad850190d946d34966a56838cfdb216e021b5b5fTimo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen str_truncate(ctx->path, ctx->path_dir_prefix_len);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen str_printfa(ctx->path, "%u.", uid);
19e8adccba16ff419f5675b1575358c2956dce83Timo Sirainen if (unlink(str_c(ctx->path)) == 0) {
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen if (box->v.sync_notify != NULL) {
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen box->v.sync_notify(box, uid,
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen MAILBOX_SYNC_TYPE_EXPUNGE);
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen }
19e8adccba16ff419f5675b1575358c2956dce83Timo Sirainen mail_index_expunge(ctx->trans, seq1);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen } else if (errno != ENOENT) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_storage_set_critical(&ctx->mbox->storage->storage,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen "unlink(%s) failed: %m", str_c(ctx->path));
8aacc9e7c84f8376822823ec98c2f551d4919b2eTimo Sirainen /* continue anyway */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic void cydir_sync_index(struct cydir_sync_context *ctx)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
1098fc409a45e7603701dc94635927a673bee0c1Timo Sirainen struct mailbox *box = &ctx->mbox->ibox.box;
1098fc409a45e7603701dc94635927a673bee0c1Timo Sirainen const struct mail_index_header *hdr;
1098fc409a45e7603701dc94635927a673bee0c1Timo Sirainen struct mail_index_sync_rec sync_rec;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen uint32_t seq1, seq2;
1098fc409a45e7603701dc94635927a673bee0c1Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen hdr = mail_index_get_header(ctx->sync_view);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (hdr->uid_validity != 0)
6e235046e1d8e9d89fc948f5c623676c20421a28Timo Sirainen ctx->uid_validity = hdr->uid_validity;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen else
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainen cydir_sync_set_uidvalidity(ctx);
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainen
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainen /* mark the newly seen messages as recent */
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainen if (mail_index_lookup_seq_range(ctx->sync_view, hdr->first_recent_uid,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen hdr->next_uid, &seq1, &seq2)) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen index_mailbox_set_recent_seq(&ctx->mbox->ibox, ctx->sync_view,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen seq1, seq2);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen while (mail_index_sync_next(ctx->index_sync_ctx, &sync_rec)) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (!mail_index_lookup_seq_range(ctx->sync_view,
abe7afb8f1766fbcef1b9df513109e43d7d16e49Timo Sirainen sync_rec.uid1, sync_rec.uid2,
abe7afb8f1766fbcef1b9df513109e43d7d16e49Timo Sirainen &seq1, &seq2)) {
abe7afb8f1766fbcef1b9df513109e43d7d16e49Timo Sirainen /* already expunged, nothing to do. */
abe7afb8f1766fbcef1b9df513109e43d7d16e49Timo Sirainen continue;
abe7afb8f1766fbcef1b9df513109e43d7d16e49Timo Sirainen }
abe7afb8f1766fbcef1b9df513109e43d7d16e49Timo Sirainen
abe7afb8f1766fbcef1b9df513109e43d7d16e49Timo Sirainen switch (sync_rec.type) {
abe7afb8f1766fbcef1b9df513109e43d7d16e49Timo Sirainen case MAIL_INDEX_SYNC_TYPE_APPEND:
abe7afb8f1766fbcef1b9df513109e43d7d16e49Timo Sirainen /* don't care */
2131ef7a3390f15ea6a958256ea54908f1096350Timo Sirainen break;
abe7afb8f1766fbcef1b9df513109e43d7d16e49Timo Sirainen case MAIL_INDEX_SYNC_TYPE_EXPUNGE:
2131ef7a3390f15ea6a958256ea54908f1096350Timo Sirainen cydir_sync_expunge(ctx, seq1, seq2);
2131ef7a3390f15ea6a958256ea54908f1096350Timo Sirainen break;
2131ef7a3390f15ea6a958256ea54908f1096350Timo Sirainen case MAIL_INDEX_SYNC_TYPE_FLAGS:
2131ef7a3390f15ea6a958256ea54908f1096350Timo Sirainen case MAIL_INDEX_SYNC_TYPE_KEYWORD_ADD:
2131ef7a3390f15ea6a958256ea54908f1096350Timo Sirainen case MAIL_INDEX_SYNC_TYPE_KEYWORD_REMOVE:
abe7afb8f1766fbcef1b9df513109e43d7d16e49Timo Sirainen case MAIL_INDEX_SYNC_TYPE_KEYWORD_RESET:
abe7afb8f1766fbcef1b9df513109e43d7d16e49Timo Sirainen /* FIXME: should be bother calling sync_notify()? */
69e03a846f6980144aa75bff0590c04852bffbbcTimo Sirainen break;
69e03a846f6980144aa75bff0590c04852bffbbcTimo Sirainen }
2131ef7a3390f15ea6a958256ea54908f1096350Timo Sirainen }
69e03a846f6980144aa75bff0590c04852bffbbcTimo Sirainen
69e03a846f6980144aa75bff0590c04852bffbbcTimo Sirainen if (box->v.sync_notify != NULL)
69e03a846f6980144aa75bff0590c04852bffbbcTimo Sirainen box->v.sync_notify(box, 0, 0);
69e03a846f6980144aa75bff0590c04852bffbbcTimo Sirainen}
69e03a846f6980144aa75bff0590c04852bffbbcTimo Sirainen
69e03a846f6980144aa75bff0590c04852bffbbcTimo Sirainenint cydir_sync_begin(struct cydir_mailbox *mbox,
69e03a846f6980144aa75bff0590c04852bffbbcTimo Sirainen struct cydir_sync_context **ctx_r, bool force)
69e03a846f6980144aa75bff0590c04852bffbbcTimo Sirainen{
69e03a846f6980144aa75bff0590c04852bffbbcTimo Sirainen struct cydir_sync_context *ctx;
69e03a846f6980144aa75bff0590c04852bffbbcTimo Sirainen enum mail_index_sync_flags sync_flags;
69e03a846f6980144aa75bff0590c04852bffbbcTimo Sirainen int ret;
69e03a846f6980144aa75bff0590c04852bffbbcTimo Sirainen
69e03a846f6980144aa75bff0590c04852bffbbcTimo Sirainen ctx = i_new(struct cydir_sync_context, 1);
69e03a846f6980144aa75bff0590c04852bffbbcTimo Sirainen ctx->mbox = mbox;
69e03a846f6980144aa75bff0590c04852bffbbcTimo Sirainen
69e03a846f6980144aa75bff0590c04852bffbbcTimo Sirainen sync_flags = MAIL_INDEX_SYNC_FLAG_FLUSH_DIRTY;
66ecc94150cbce23aad3240135e0782e0a74d479Timo Sirainen if (!mbox->ibox.keep_recent)
bc564f1d3d953cf724828322b11ae89e0f59ffc9Timo Sirainen sync_flags |= MAIL_INDEX_SYNC_FLAG_DROP_RECENT;
04b8a90af181cc4c7959266855e8ed50a22ed413Timo Sirainen if (!force)
bc564f1d3d953cf724828322b11ae89e0f59ffc9Timo Sirainen sync_flags |= MAIL_INDEX_SYNC_FLAG_REQUIRE_CHANGES;
2131ef7a3390f15ea6a958256ea54908f1096350Timo Sirainen
69e03a846f6980144aa75bff0590c04852bffbbcTimo Sirainen ret = mail_index_sync_begin(mbox->ibox.index, &ctx->index_sync_ctx,
69e03a846f6980144aa75bff0590c04852bffbbcTimo Sirainen &ctx->sync_view, &ctx->trans,
69e03a846f6980144aa75bff0590c04852bffbbcTimo Sirainen sync_flags);
bc564f1d3d953cf724828322b11ae89e0f59ffc9Timo Sirainen if (ret <= 0) {
bc564f1d3d953cf724828322b11ae89e0f59ffc9Timo Sirainen if (ret < 0)
bc564f1d3d953cf724828322b11ae89e0f59ffc9Timo Sirainen mail_storage_set_index_error(&mbox->ibox);
bc564f1d3d953cf724828322b11ae89e0f59ffc9Timo Sirainen i_free(ctx);
bc564f1d3d953cf724828322b11ae89e0f59ffc9Timo Sirainen *ctx_r = NULL;
bc564f1d3d953cf724828322b11ae89e0f59ffc9Timo Sirainen return ret;
bc564f1d3d953cf724828322b11ae89e0f59ffc9Timo Sirainen }
66ecc94150cbce23aad3240135e0782e0a74d479Timo Sirainen
2131ef7a3390f15ea6a958256ea54908f1096350Timo Sirainen cydir_sync_index(ctx);
bc564f1d3d953cf724828322b11ae89e0f59ffc9Timo Sirainen *ctx_r = ctx;
bc564f1d3d953cf724828322b11ae89e0f59ffc9Timo Sirainen return 0;
bc564f1d3d953cf724828322b11ae89e0f59ffc9Timo Sirainen}
bc564f1d3d953cf724828322b11ae89e0f59ffc9Timo Sirainen
bc564f1d3d953cf724828322b11ae89e0f59ffc9Timo Sirainenint cydir_sync_finish(struct cydir_sync_context **_ctx, bool success)
bc564f1d3d953cf724828322b11ae89e0f59ffc9Timo Sirainen{
bc564f1d3d953cf724828322b11ae89e0f59ffc9Timo Sirainen struct cydir_sync_context *ctx = *_ctx;
bc564f1d3d953cf724828322b11ae89e0f59ffc9Timo Sirainen int ret = success ? 0 : -1;
2131ef7a3390f15ea6a958256ea54908f1096350Timo Sirainen
bc564f1d3d953cf724828322b11ae89e0f59ffc9Timo Sirainen *_ctx = NULL;
69e03a846f6980144aa75bff0590c04852bffbbcTimo Sirainen if (success) {
69e03a846f6980144aa75bff0590c04852bffbbcTimo Sirainen if (mail_index_sync_commit(&ctx->index_sync_ctx) < 0) {
fd2f5fbc1f07aa93e2214a28cdf02437fb7d06c8Timo Sirainen mail_storage_set_index_error(&ctx->mbox->ibox);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ret = -1;
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen }
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen } else {
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen mail_index_sync_rollback(&ctx->index_sync_ctx);
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainen }
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainen if (ctx->path != NULL)
69e03a846f6980144aa75bff0590c04852bffbbcTimo Sirainen str_free(&ctx->path);
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen i_free(ctx);
fd2f5fbc1f07aa93e2214a28cdf02437fb7d06c8Timo Sirainen return 0;
fd2f5fbc1f07aa93e2214a28cdf02437fb7d06c8Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
fd2f5fbc1f07aa93e2214a28cdf02437fb7d06c8Timo Sirainenstatic int cydir_sync(struct cydir_mailbox *mbox)
fd2f5fbc1f07aa93e2214a28cdf02437fb7d06c8Timo Sirainen{
c53e8ee216904ffe6de4f6518d9f9f5107b7610eTimo Sirainen struct cydir_sync_context *sync_ctx;
c53e8ee216904ffe6de4f6518d9f9f5107b7610eTimo Sirainen
fd2f5fbc1f07aa93e2214a28cdf02437fb7d06c8Timo Sirainen if (cydir_sync_begin(mbox, &sync_ctx, FALSE) < 0)
fd2f5fbc1f07aa93e2214a28cdf02437fb7d06c8Timo Sirainen return -1;
df6478c4cf605bd81b3891c148b84c14eb6c4035Timo Sirainen
fd2f5fbc1f07aa93e2214a28cdf02437fb7d06c8Timo Sirainen return sync_ctx == NULL ? 0 :
fd2f5fbc1f07aa93e2214a28cdf02437fb7d06c8Timo Sirainen cydir_sync_finish(&sync_ctx, TRUE);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstruct mailbox_sync_context *
abe7afb8f1766fbcef1b9df513109e43d7d16e49Timo Sirainencydir_storage_sync_init(struct mailbox *box, enum mailbox_sync_flags flags)
1098fc409a45e7603701dc94635927a673bee0c1Timo Sirainen{
fd2f5fbc1f07aa93e2214a28cdf02437fb7d06c8Timo Sirainen struct cydir_mailbox *mbox = (struct cydir_mailbox *)box;
fd2f5fbc1f07aa93e2214a28cdf02437fb7d06c8Timo Sirainen int ret = 0;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen if (!box->opened)
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen index_storage_mailbox_open(&mbox->ibox);
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen if (index_mailbox_want_full_sync(&mbox->ibox, flags))
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen ret = cydir_sync(mbox);
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen
fd2f5fbc1f07aa93e2214a28cdf02437fb7d06c8Timo Sirainen return index_mailbox_sync_init(box, flags, ret < 0);
fd2f5fbc1f07aa93e2214a28cdf02437fb7d06c8Timo Sirainen}
fd2f5fbc1f07aa93e2214a28cdf02437fb7d06c8Timo Sirainen