mbox-sync.c revision f12aabfeba81f0d741971d2b7e4a5008eb4383ca
76b43e4417bab52e913da39b5f5bc2a130d3f149Timo Sirainen/* Copyright (C) 2004 Timo Sirainen */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen/*
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen Modifying mbox can be slow, so we try to do it all at once minimizing the
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen required disk I/O. We may need to:
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen - Update message flags in Status, X-Status and X-Keywords headers
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen - Write missing X-UID and X-IMAPbase headers
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen - Write missing or broken Content-Length header if there's space
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen - Expunge specified messages
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen Here's how we do it:
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen - Start reading the mails from the beginning
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen - X-Keywords, X-UID and X-IMAPbase headers may contain padding at the end
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen of them, remember how much each message has and offset to beginning of the
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen padding
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen - If header needs to be rewritten and there's enough space, do it
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen - If we didn't have enough space, remember how much was missing
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen - Continue reading and counting the padding in each message. If available
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen padding is enough to rewrite all the previous messages needing it, do it
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen - When we encounter expunged message, treat all of it as padding and
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen rewrite previous messages if needed (and there's enough space).
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen Afterwards keep moving messages backwards to fill the expunged space.
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen Moving is done by rewriting each message's headers, with possibly adding
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen missing Content-Length header and padding. Message bodies are moved
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen without modifications.
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen - If we encounter end of file, grow the file and rewrite needed messages
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen - Rewriting is done by moving message body forward, rewriting message's
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen header and doing the same for previous message, until all of them are
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen rewritten.
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen*/
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "lib.h"
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen#include "ioloop.h"
16f816d3f3c32ae3351834253f52ddd0212bcbf3Timo Sirainen#include "array.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "buffer.h"
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen#include "hostpid.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "istream.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "file-set-size.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "str.h"
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen#include "read-full.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "write-full.h"
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen#include "message-date.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "istream-raw-mbox.h"
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen#include "mbox-storage.h"
022412398e56a8f31ef111cfd7271498d64af9a9Timo Sirainen#include "mbox-from.h"
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen#include "mbox-file.h"
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen#include "mbox-lock.h"
9aa52288a4b53186d81b0ec9afa7d9e0a8ee8753Timo Sirainen#include "mbox-sync-private.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include <stddef.h>
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen#include <stdlib.h>
7a6b45405fb1544ac476e6eb1402a70cc1ddcdcfTimo Sirainen#include <utime.h>
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainen#include <sys/stat.h>
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen/* The text below was taken exactly as c-client wrote it to my mailbox,
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen so it's probably copyrighted by University of Washington. */
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen#define PSEUDO_MESSAGE_BODY \
f7d43647acc6dc80064c8c4cacf5bf86f754c530Timo Sirainen"This text is part of the internal format of your mail folder, and is not\n" \
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen"a real message. It is created automatically by the mail system software.\n" \
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen"If deleted, important folder data will be lost, and it will be re-created\n" \
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen"with the data reset to initial values.\n"
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainenvoid mbox_sync_set_critical(struct mbox_sync_context *sync_ctx,
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen const char *fmt, ...)
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen{
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen va_list va;
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen if (sync_ctx->ext_modified) {
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen mail_storage_set_critical(&sync_ctx->mbox->storage->storage,
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen "mbox file %s was modified while we were syncing, "
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen "check your locking settings", sync_ctx->mbox->path);
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen }
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen va_start(va, fmt);
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen mail_storage_set_critical(&sync_ctx->mbox->storage->storage,
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen "%s", t_strdup_vprintf(fmt, va));
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen va_end(va);
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen}
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainenint mbox_sync_seek(struct mbox_sync_context *sync_ctx, uoff_t from_offset)
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen{
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen if (istream_raw_mbox_seek(sync_ctx->input, from_offset) < 0) {
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen mbox_sync_set_critical(sync_ctx,
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen "Unexpectedly lost From-line at offset %"PRIuUOFF_T
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen " from mbox file %s", from_offset,
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen sync_ctx->mbox->path);
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen return -1;
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen }
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen return 0;
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen}
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainenvoid mbox_sync_file_update_ext_modified(struct mbox_sync_context *sync_ctx)
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen{
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen struct stat st;
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen /* Do this even if ext_modified is already set. Expunging code relies
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen on last_stat being updated. */
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen if (fstat(sync_ctx->write_fd, &st) < 0) {
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen mbox_set_syscall_error(sync_ctx->mbox, "fstat()");
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen return;
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen }
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen if (st.st_size != sync_ctx->last_stat.st_size ||
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen (sync_ctx->last_stat.st_mtime != 0 &&
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen (st.st_mtime != sync_ctx->last_stat.st_mtime
6288d3611eda14a017dae9927b73f46afb646c96Timo Sirainen#ifdef HAVE_STAT_TV_NSEC
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen /* nanoseconds give better precision to this check if they're
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen supported by the OS */
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen || st.st_mtim.tv_nsec != sync_ctx->last_stat.st_mtim.tv_nsec
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen#endif
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen )))
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen sync_ctx->ext_modified = TRUE;
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen sync_ctx->last_stat = st;
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen}
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainenvoid mbox_sync_file_updated(struct mbox_sync_context *sync_ctx, bool dirty)
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen{
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen if (dirty) {
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen /* just mark the stat as dirty. */
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen sync_ctx->last_stat.st_mtime = 0;
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen return;
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen }
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen if (fstat(sync_ctx->write_fd, &sync_ctx->last_stat) < 0)
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen mbox_set_syscall_error(sync_ctx->mbox, "fstat()");
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen i_stream_sync(sync_ctx->input);
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen}
9e59a1f3f095b3099478562cf3f3970a24736970Timo Sirainen
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainenstatic void mbox_sync_array_delete_to(ARRAY_TYPE(sync_recs) *syncs_arr,
178511b57faa7c3f8203dd8b7e4059d00cbfc23aTimo Sirainen uint32_t last_uid)
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen{
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen struct mail_index_sync_rec *syncs;
2e937ed8585299b2e879a28314902a5f644813d2Timo Sirainen unsigned int src, dest, count;
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen syncs = array_get_modifiable(syncs_arr, &count);
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen for (src = dest = 0; src < count; src++) {
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen i_assert(last_uid >= syncs[src].uid1);
4d25408732be27e91f0430f71e87242760c2517cTimo Sirainen if (last_uid <= syncs[src].uid2) {
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen /* keep it */
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen if (src != dest)
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen syncs[dest] = syncs[src];
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen dest++;
e5c08648676d1989f6e70b95e5990c26b3e8b96bTimo Sirainen }
4d25408732be27e91f0430f71e87242760c2517cTimo Sirainen }
c3412ddeb9abc13f99d3caf50faf76cd99f7e9d2Timo Sirainen
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen array_delete(syncs_arr, dest, count - dest);
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen}
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainenstatic int
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainenmbox_sync_read_next_mail(struct mbox_sync_context *sync_ctx,
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen struct mbox_sync_mail_context *mail_ctx)
1e76a5b92f9d82d557f81f080f3dfad1c9d8f200Timo Sirainen{
1e76a5b92f9d82d557f81f080f3dfad1c9d8f200Timo Sirainen /* get EOF */
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen (void)istream_raw_mbox_get_header_offset(sync_ctx->input);
9a06cabdfdf4d5e2f19a07e506c3c7d08a7e7038Timo Sirainen if (istream_raw_mbox_is_eof(sync_ctx->input))
9a06cabdfdf4d5e2f19a07e506c3c7d08a7e7038Timo Sirainen return 0;
9a06cabdfdf4d5e2f19a07e506c3c7d08a7e7038Timo Sirainen
e8a35266a5ceacdfafeeffd6bddae77931ff97ebTimo Sirainen p_clear(sync_ctx->mail_keyword_pool);
9aa52288a4b53186d81b0ec9afa7d9e0a8ee8753Timo Sirainen memset(mail_ctx, 0, sizeof(*mail_ctx));
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen mail_ctx->sync_ctx = sync_ctx;
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen mail_ctx->seq = ++sync_ctx->seq;
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen mail_ctx->header = sync_ctx->header;
b08b33d1f5ce3721dc2d83586c9cb0ca141331fdTimo Sirainen
b08b33d1f5ce3721dc2d83586c9cb0ca141331fdTimo Sirainen mail_ctx->mail.from_offset =
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen istream_raw_mbox_get_start_offset(sync_ctx->input);
f6699a08521aacc4c2bb5b6175691dad5f715f8cTimo Sirainen mail_ctx->mail.offset =
6843896c40bee4f9b6680ca7ced598c446e9f999Timo Sirainen istream_raw_mbox_get_header_offset(sync_ctx->input);
6843896c40bee4f9b6680ca7ced598c446e9f999Timo Sirainen
6843896c40bee4f9b6680ca7ced598c446e9f999Timo Sirainen mbox_sync_parse_next_mail(sync_ctx->input, mail_ctx);
6843896c40bee4f9b6680ca7ced598c446e9f999Timo Sirainen i_assert(sync_ctx->input->v_offset != mail_ctx->mail.from_offset ||
b08b33d1f5ce3721dc2d83586c9cb0ca141331fdTimo Sirainen sync_ctx->input->eof);
022412398e56a8f31ef111cfd7271498d64af9a9Timo Sirainen
022412398e56a8f31ef111cfd7271498d64af9a9Timo Sirainen mail_ctx->mail.body_size =
022412398e56a8f31ef111cfd7271498d64af9a9Timo Sirainen istream_raw_mbox_get_body_size(sync_ctx->input,
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen mail_ctx->content_length);
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen i_assert(mail_ctx->mail.body_size < OFF_T_MAX);
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen if ((mail_ctx->mail.flags & MAIL_RECENT) != 0 &&
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen !mail_ctx->mail.pseudo) {
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen if (!sync_ctx->mbox->ibox.keep_recent) {
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen /* need to add 'O' flag to Status-header */
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen mail_ctx->need_rewrite = TRUE;
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen }
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen mail_ctx->recent = TRUE;
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen }
f6699a08521aacc4c2bb5b6175691dad5f715f8cTimo Sirainen return 1;
f6699a08521aacc4c2bb5b6175691dad5f715f8cTimo Sirainen}
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen
f6699a08521aacc4c2bb5b6175691dad5f715f8cTimo Sirainenstatic bool mbox_sync_buf_have_expunges(ARRAY_TYPE(sync_recs) *syncs_arr)
f6699a08521aacc4c2bb5b6175691dad5f715f8cTimo Sirainen{
df4018ae2f0a95be602f724ca70df7e0e3bd6a7dTimo Sirainen const struct mail_index_sync_rec *syncs;
df4018ae2f0a95be602f724ca70df7e0e3bd6a7dTimo Sirainen unsigned int i, count;
a393d9d6dabdc46cf724f8cb004a652b4036d53dTimo Sirainen
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen syncs = array_get(syncs_arr, &count);
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen for (i = 0; i < count; i++) {
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen if (syncs[i].type == MAIL_INDEX_SYNC_TYPE_EXPUNGE)
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen return TRUE;
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen }
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen return FALSE;
a393d9d6dabdc46cf724f8cb004a652b4036d53dTimo Sirainen}
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainenstatic int mbox_sync_read_index_syncs(struct mbox_sync_context *sync_ctx,
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen uint32_t uid, bool *sync_expunge_r)
2bf7bb14894faf721518e2122a14a2389ef94078Timo Sirainen{
9a06cabdfdf4d5e2f19a07e506c3c7d08a7e7038Timo Sirainen struct mail_index_sync_rec *sync_rec = &sync_ctx->sync_rec;
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen int ret;
9a06cabdfdf4d5e2f19a07e506c3c7d08a7e7038Timo Sirainen
2bf7bb14894faf721518e2122a14a2389ef94078Timo Sirainen *sync_expunge_r = FALSE;
2bf7bb14894faf721518e2122a14a2389ef94078Timo Sirainen
2bf7bb14894faf721518e2122a14a2389ef94078Timo Sirainen if (sync_ctx->index_sync_ctx == NULL)
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen return 0;
9a06cabdfdf4d5e2f19a07e506c3c7d08a7e7038Timo Sirainen
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen if (uid == 0) {
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen /* nothing for this or the future ones */
7f773564b94e6054a40d3785cb63c29f1e4d4deeTimo Sirainen uid = (uint32_t)-1;
2bf7bb14894faf721518e2122a14a2389ef94078Timo Sirainen }
2bf7bb14894faf721518e2122a14a2389ef94078Timo Sirainen
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen mbox_sync_array_delete_to(&sync_ctx->syncs, uid);
9a06cabdfdf4d5e2f19a07e506c3c7d08a7e7038Timo Sirainen while (uid >= sync_rec->uid1) {
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen if (uid <= sync_rec->uid2 &&
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen sync_rec->type != MAIL_INDEX_SYNC_TYPE_APPEND &&
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen (sync_rec->type != MAIL_INDEX_SYNC_TYPE_EXPUNGE ||
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen !sync_ctx->mbox->mbox_readonly)) {
6843896c40bee4f9b6680ca7ced598c446e9f999Timo Sirainen array_append(&sync_ctx->syncs, sync_rec, 1);
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen if (sync_rec->type == MAIL_INDEX_SYNC_TYPE_EXPUNGE)
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen *sync_expunge_r = TRUE;
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen }
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen
7f3be7d885c75cdd77f536929a45bc9764595960Timo Sirainen ret = mail_index_sync_next(sync_ctx->index_sync_ctx, sync_rec);
7f3be7d885c75cdd77f536929a45bc9764595960Timo Sirainen if (ret < 0) {
7f3be7d885c75cdd77f536929a45bc9764595960Timo Sirainen mail_storage_set_index_error(&sync_ctx->mbox->ibox);
7f3be7d885c75cdd77f536929a45bc9764595960Timo Sirainen return -1;
7f3be7d885c75cdd77f536929a45bc9764595960Timo Sirainen }
f6699a08521aacc4c2bb5b6175691dad5f715f8cTimo Sirainen
f6699a08521aacc4c2bb5b6175691dad5f715f8cTimo Sirainen if (ret == 0) {
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen memset(sync_rec, 0, sizeof(*sync_rec));
f6699a08521aacc4c2bb5b6175691dad5f715f8cTimo Sirainen break;
f6699a08521aacc4c2bb5b6175691dad5f715f8cTimo Sirainen }
df4018ae2f0a95be602f724ca70df7e0e3bd6a7dTimo Sirainen
df4018ae2f0a95be602f724ca70df7e0e3bd6a7dTimo Sirainen switch (sync_rec->type) {
7f3be7d885c75cdd77f536929a45bc9764595960Timo Sirainen case MAIL_INDEX_SYNC_TYPE_APPEND:
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen if (sync_rec->uid2 >= sync_ctx->next_uid)
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen sync_ctx->next_uid = sync_rec->uid2 + 1;
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen memset(sync_rec, 0, sizeof(*sync_rec));
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen break;
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen case MAIL_INDEX_SYNC_TYPE_EXPUNGE:
f0f9c8e94abac18f8acd91b9e724c4c32863723aTimo Sirainen break;
7f3be7d885c75cdd77f536929a45bc9764595960Timo Sirainen case MAIL_INDEX_SYNC_TYPE_FLAGS:
7f3be7d885c75cdd77f536929a45bc9764595960Timo Sirainen case MAIL_INDEX_SYNC_TYPE_KEYWORD_ADD:
7f3be7d885c75cdd77f536929a45bc9764595960Timo Sirainen case MAIL_INDEX_SYNC_TYPE_KEYWORD_REMOVE:
7f3be7d885c75cdd77f536929a45bc9764595960Timo Sirainen case MAIL_INDEX_SYNC_TYPE_KEYWORD_RESET:
7f3be7d885c75cdd77f536929a45bc9764595960Timo Sirainen if (sync_ctx->delay_writes) {
7f3be7d885c75cdd77f536929a45bc9764595960Timo Sirainen /* we're not going to write these yet */
7f3be7d885c75cdd77f536929a45bc9764595960Timo Sirainen uint32_t seq1, seq2;
7f3be7d885c75cdd77f536929a45bc9764595960Timo Sirainen
7f3be7d885c75cdd77f536929a45bc9764595960Timo Sirainen if (mail_index_lookup_uid_range(
7f3be7d885c75cdd77f536929a45bc9764595960Timo Sirainen sync_ctx->sync_view,
7f3be7d885c75cdd77f536929a45bc9764595960Timo Sirainen sync_rec->uid1, sync_rec->uid2,
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen &seq1, &seq2) < 0)
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen return -1;
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen memset(sync_rec, 0, sizeof(*sync_rec));
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen if (seq1 == 0)
4d25408732be27e91f0430f71e87242760c2517cTimo Sirainen break;
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen mail_index_update_flags_range(sync_ctx->t,
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen seq1, seq2, MODIFY_ADD,
4d25408732be27e91f0430f71e87242760c2517cTimo Sirainen (enum mail_flags)
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen MAIL_INDEX_MAIL_FLAG_DIRTY);
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen }
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen break;
4d25408732be27e91f0430f71e87242760c2517cTimo Sirainen }
4d25408732be27e91f0430f71e87242760c2517cTimo Sirainen }
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen if (!*sync_expunge_r)
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen *sync_expunge_r = mbox_sync_buf_have_expunges(&sync_ctx->syncs);
8af07808ba203f8709e2ff9eaf2291e1c4a4d53dTimo Sirainen
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen return 0;
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen}
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainenvoid mbox_sync_apply_index_syncs(struct mbox_sync_context *sync_ctx,
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen struct mbox_sync_mail *mail,
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen enum mailbox_sync_type *sync_type_r)
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen{
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen const struct mail_index_sync_rec *syncs;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen unsigned int i, count;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen enum mailbox_sync_type sync_type = 0;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
61f5256ef248d35459b53534ae428bf6d016e1c5Timo Sirainen syncs = array_get(&sync_ctx->syncs, &count);
61f5256ef248d35459b53534ae428bf6d016e1c5Timo Sirainen for (i = 0; i < count; i++) {
61f5256ef248d35459b53534ae428bf6d016e1c5Timo Sirainen switch (syncs[i].type) {
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen case MAIL_INDEX_SYNC_TYPE_FLAGS:
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen mail_index_sync_flags_apply(&syncs[i], &mail->flags);
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen sync_type |= MAILBOX_SYNC_TYPE_FLAGS;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen break;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen case MAIL_INDEX_SYNC_TYPE_KEYWORD_ADD:
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen case MAIL_INDEX_SYNC_TYPE_KEYWORD_REMOVE:
a0b0d629931773c17a236f6214adbe0e13b9b3fdTimo Sirainen case MAIL_INDEX_SYNC_TYPE_KEYWORD_RESET:
a0b0d629931773c17a236f6214adbe0e13b9b3fdTimo Sirainen if (!array_is_created(&mail->keywords)) {
a0b0d629931773c17a236f6214adbe0e13b9b3fdTimo Sirainen /* no existing keywords */
a0b0d629931773c17a236f6214adbe0e13b9b3fdTimo Sirainen if (syncs[i].type !=
a0b0d629931773c17a236f6214adbe0e13b9b3fdTimo Sirainen MAIL_INDEX_SYNC_TYPE_KEYWORD_ADD)
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen break;
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen /* adding, create the array */
a0b0d629931773c17a236f6214adbe0e13b9b3fdTimo Sirainen p_array_init(&mail->keywords,
a0b0d629931773c17a236f6214adbe0e13b9b3fdTimo Sirainen sync_ctx->mail_keyword_pool,
a0b0d629931773c17a236f6214adbe0e13b9b3fdTimo Sirainen I_MIN(10, count - i));
a0b0d629931773c17a236f6214adbe0e13b9b3fdTimo Sirainen }
a0b0d629931773c17a236f6214adbe0e13b9b3fdTimo Sirainen if (mail_index_sync_keywords_apply(&syncs[i],
a0b0d629931773c17a236f6214adbe0e13b9b3fdTimo Sirainen &mail->keywords))
a0b0d629931773c17a236f6214adbe0e13b9b3fdTimo Sirainen sync_type |= MAILBOX_SYNC_TYPE_KEYWORDS;
a0b0d629931773c17a236f6214adbe0e13b9b3fdTimo Sirainen break;
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen default:
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen break;
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen }
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen }
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen *sync_type_r = sync_type;
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen}
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainenstatic int
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainenmbox_sync_read_index_rec(struct mbox_sync_context *sync_ctx,
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen uint32_t uid, const struct mail_index_record **rec_r)
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen{
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen const struct mail_index_record *rec = NULL;
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen uint32_t messages_count;
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen int ret = 0;
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen messages_count =
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen mail_index_view_get_messages_count(sync_ctx->sync_view);
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen while (sync_ctx->idx_seq <= messages_count) {
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen ret = mail_index_lookup(sync_ctx->sync_view,
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen sync_ctx->idx_seq, &rec);
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen if (ret < 0) {
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen mail_storage_set_index_error(&sync_ctx->mbox->ibox);
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen return -1;
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen }
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen i_assert(ret != 0); /* we should be looking at head index */
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen if (uid <= rec->uid)
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen break;
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen /* externally expunged message, remove from index */
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen mail_index_expunge(sync_ctx->t, sync_ctx->idx_seq);
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen sync_ctx->idx_seq++;
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen rec = NULL;
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen }
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen if (rec == NULL && uid < sync_ctx->idx_next_uid) {
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen /* this UID was already in index and it was expunged */
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen mbox_sync_set_critical(sync_ctx,
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen "mbox sync: Expunged message reappeared in mailbox %s "
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen "(UID %u < %u, seq=%u, idx_msgs=%u)",
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen sync_ctx->mbox->path, uid, sync_ctx->idx_next_uid,
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen sync_ctx->seq, messages_count);
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen ret = 0; rec = NULL;
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen } else if (rec != NULL && rec->uid != uid) {
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen /* new UID in the middle of the mailbox - shouldn't happen */
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen mbox_sync_set_critical(sync_ctx,
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen "mbox sync: UID inserted in the middle of mailbox %s "
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen "(%u > %u, seq=%u, idx_msgs=%u)", sync_ctx->mbox->path,
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen rec->uid, uid, sync_ctx->seq, messages_count);
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen ret = 0; rec = NULL;
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen } else {
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen ret = 1;
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen }
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen *rec_r = rec;
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen return ret;
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen}
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainenstatic int mbox_sync_find_index_md5(struct mbox_sync_context *sync_ctx,
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen unsigned char hdr_md5_sum[],
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen const struct mail_index_record **rec_r)
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen{
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen const struct mail_index_record *rec = NULL;
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen uint32_t messages_count;
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen const void *data;
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen int ret;
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen messages_count =
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen mail_index_view_get_messages_count(sync_ctx->sync_view);
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen while (sync_ctx->idx_seq <= messages_count) {
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen ret = mail_index_lookup(sync_ctx->sync_view,
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen sync_ctx->idx_seq, &rec);
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen if (ret < 0) {
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen mail_storage_set_index_error(&sync_ctx->mbox->ibox);
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen return -1;
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen }
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen if (mail_index_lookup_ext(sync_ctx->sync_view,
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen sync_ctx->idx_seq,
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen sync_ctx->mbox->ibox.md5hdr_ext_idx,
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen &data) < 0) {
33d63688ed8b26dc333e3c2edbfb2fe6e412604dTimo Sirainen mail_storage_set_index_error(&sync_ctx->mbox->ibox);
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen return -1;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen }
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen if (data != NULL && memcmp(data, hdr_md5_sum, 16) == 0)
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen break;
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen
d0d7fcf3ce44f26fdf34c1542a25cec644c5c4c7Timo Sirainen /* externally expunged message, remove from index */
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen mail_index_expunge(sync_ctx->t, sync_ctx->idx_seq);
d0d7fcf3ce44f26fdf34c1542a25cec644c5c4c7Timo Sirainen sync_ctx->idx_seq++;
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen rec = NULL;
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen }
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen *rec_r = rec;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen return 0;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen}
7f3be7d885c75cdd77f536929a45bc9764595960Timo Sirainen
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainenstatic int
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainenmbox_sync_update_from_offset(struct mbox_sync_context *sync_ctx,
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen struct mbox_sync_mail *mail,
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen bool nocheck)
7f3be7d885c75cdd77f536929a45bc9764595960Timo Sirainen{
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen const void *data;
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen uint64_t offset;
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen
3f26c5aced2e71efc783f26bb8a7ac53f7504622Timo Sirainen if (!nocheck) {
3f26c5aced2e71efc783f26bb8a7ac53f7504622Timo Sirainen /* see if from_offset needs updating */
3f26c5aced2e71efc783f26bb8a7ac53f7504622Timo Sirainen if (mail_index_lookup_ext(sync_ctx->sync_view,
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen sync_ctx->idx_seq,
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen sync_ctx->mbox->mbox_ext_idx,
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen &data) < 0) {
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen mail_storage_set_index_error(&sync_ctx->mbox->ibox);
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen return -1;
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen }
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen if (data != NULL &&
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen *((const uint64_t *)data) == mail->from_offset)
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen return 0;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen }
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen offset = mail->from_offset;
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen mail_index_update_ext(sync_ctx->t, sync_ctx->idx_seq,
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen sync_ctx->mbox->mbox_ext_idx, &offset, NULL);
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen return 0;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen}
a0b0d629931773c17a236f6214adbe0e13b9b3fdTimo Sirainen
a0b0d629931773c17a236f6214adbe0e13b9b3fdTimo Sirainenstatic void
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainenmbox_sync_update_index_keywords(struct mbox_sync_mail_context *mail_ctx)
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen{
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen struct mbox_sync_context *sync_ctx = mail_ctx->sync_ctx;
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen struct mail_keywords *keywords;
c84985f66c5c83db642d136aefa8864cb45158cfTimo Sirainen
c84985f66c5c83db642d136aefa8864cb45158cfTimo Sirainen keywords = !array_is_created(&mail_ctx->mail.keywords) ?
c84985f66c5c83db642d136aefa8864cb45158cfTimo Sirainen mail_index_keywords_create(sync_ctx->t, NULL) :
c84985f66c5c83db642d136aefa8864cb45158cfTimo Sirainen mail_index_keywords_create_from_indexes(sync_ctx->t,
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen &mail_ctx->mail.keywords);
a8b8cce8569753ed47c94782283a24fb11c5ab52Timo Sirainen mail_index_update_keywords(sync_ctx->t, sync_ctx->idx_seq,
a8b8cce8569753ed47c94782283a24fb11c5ab52Timo Sirainen MODIFY_REPLACE, keywords);
e8a35266a5ceacdfafeeffd6bddae77931ff97ebTimo Sirainen mail_index_keywords_free(&keywords);
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen}
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainenstatic int
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainenmbox_sync_update_md5_if_changed(struct mbox_sync_mail_context *mail_ctx)
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen{
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen struct mbox_sync_context *sync_ctx = mail_ctx->sync_ctx;
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen const void *ext_data;
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen if (mail_index_lookup_ext(sync_ctx->sync_view, sync_ctx->idx_seq,
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen sync_ctx->mbox->ibox.md5hdr_ext_idx,
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen &ext_data) < 0) {
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen mail_storage_set_index_error(&sync_ctx->mbox->ibox);
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen return -1;
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen }
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen
4d25408732be27e91f0430f71e87242760c2517cTimo Sirainen if (ext_data == NULL ||
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen memcmp(mail_ctx->hdr_md5_sum, ext_data, 16) != 0) {
4d25408732be27e91f0430f71e87242760c2517cTimo Sirainen mail_index_update_ext(sync_ctx->t, sync_ctx->idx_seq,
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen sync_ctx->mbox->ibox.md5hdr_ext_idx,
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen mail_ctx->hdr_md5_sum, NULL);
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen }
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen return 0;
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen}
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainenstatic int mbox_sync_update_index(struct mbox_sync_mail_context *mail_ctx,
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen const struct mail_index_record *rec)
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen{
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen struct mbox_sync_context *sync_ctx = mail_ctx->sync_ctx;
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen struct mbox_sync_mail *mail = &mail_ctx->mail;
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen struct mailbox *box = &sync_ctx->mbox->ibox.box;
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen uint8_t mbox_flags;
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen mbox_flags = mail->flags & MAIL_FLAGS_MASK;
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen if (mail_ctx->dirty)
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen mbox_flags |= MAIL_INDEX_MAIL_FLAG_DIRTY;
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen else if (!sync_ctx->delay_writes)
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen mbox_flags &= ~MAIL_INDEX_MAIL_FLAG_DIRTY;
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen if (rec == NULL) {
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen /* new message */
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen mail_index_append(sync_ctx->t, mail->uid, &sync_ctx->idx_seq);
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen mail_index_update_flags(sync_ctx->t, sync_ctx->idx_seq,
16aba431c576c1dbd99cbaae4f9d65eea9ad73c2Timo Sirainen MODIFY_REPLACE, mbox_flags);
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen mbox_sync_update_index_keywords(mail_ctx);
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen if (sync_ctx->mbox->mbox_save_md5 != 0) {
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen mail_index_update_ext(sync_ctx->t, sync_ctx->idx_seq,
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen sync_ctx->mbox->ibox.md5hdr_ext_idx,
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen mail_ctx->hdr_md5_sum, NULL);
16aba431c576c1dbd99cbaae4f9d65eea9ad73c2Timo Sirainen }
16aba431c576c1dbd99cbaae4f9d65eea9ad73c2Timo Sirainen } else {
16aba431c576c1dbd99cbaae4f9d65eea9ad73c2Timo Sirainen /* see if we need to update flags in index file. the flags in
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen sync records are automatically applied to rec->flags at the
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen end of index syncing, so calculate those new flags first */
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen struct mbox_sync_mail idx_mail;
16aba431c576c1dbd99cbaae4f9d65eea9ad73c2Timo Sirainen enum mailbox_sync_type sync_type;
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen
16aba431c576c1dbd99cbaae4f9d65eea9ad73c2Timo Sirainen memset(&idx_mail, 0, sizeof(idx_mail));
16aba431c576c1dbd99cbaae4f9d65eea9ad73c2Timo Sirainen idx_mail.flags = rec->flags;
16aba431c576c1dbd99cbaae4f9d65eea9ad73c2Timo Sirainen
16aba431c576c1dbd99cbaae4f9d65eea9ad73c2Timo Sirainen /* get old keywords */
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen t_push();
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen t_array_init(&idx_mail.keywords, 32);
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen if (mail_index_lookup_keywords(sync_ctx->sync_view,
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen sync_ctx->idx_seq,
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen &idx_mail.keywords) < 0) {
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen mail_storage_set_index_error(&sync_ctx->mbox->ibox);
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen t_pop();
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen return -1;
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen }
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen mbox_sync_apply_index_syncs(sync_ctx, &idx_mail,
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen &sync_type);
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen if (sync_type != 0 && box->v.sync_notify != NULL)
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen box->v.sync_notify(box, rec->uid, sync_type);
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen#define SYNC_FLAGS (MAIL_RECENT | MAIL_INDEX_MAIL_FLAG_DIRTY)
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen if ((idx_mail.flags & MAIL_INDEX_MAIL_FLAG_DIRTY) != 0) {
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen /* flags are dirty. ignore whatever was in the mbox,
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen but update recent/dirty flag states if needed. */
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen mbox_flags &= SYNC_FLAGS;
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen mbox_flags |= idx_mail.flags & ~SYNC_FLAGS;
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen if (sync_ctx->delay_writes)
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen mbox_flags |= MAIL_INDEX_MAIL_FLAG_DIRTY;
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen } else {
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen /* keep index's internal flags */
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen mbox_flags &= MAIL_FLAGS_MASK | SYNC_FLAGS;
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen mbox_flags |= idx_mail.flags &
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen ~(MAIL_FLAGS_MASK | SYNC_FLAGS);
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen }
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen if ((idx_mail.flags & ~SYNC_FLAGS) !=
064bfeee2f9156683b191cc0f3f7b242720942f7Timo Sirainen (mbox_flags & ~SYNC_FLAGS)) {
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen /* flags other than recent/dirty have changed */
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen mail_index_update_flags(sync_ctx->t, sync_ctx->idx_seq,
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen MODIFY_REPLACE, mbox_flags);
b225c3c65f360d7b833f09f9b2fb3035ed5ea600Timo Sirainen } else {
064bfeee2f9156683b191cc0f3f7b242720942f7Timo Sirainen if (((idx_mail.flags ^ mbox_flags) &
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen MAIL_RECENT) != 0) {
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen /* drop recent flag (it can only be dropped) */
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen mail_index_update_flags(sync_ctx->t,
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen sync_ctx->idx_seq,
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen MODIFY_REMOVE, MAIL_RECENT);
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen }
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen if (((idx_mail.flags ^ mbox_flags) &
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen MAIL_INDEX_MAIL_FLAG_DIRTY) != 0) {
8907d617ce7c4f390c0f42f6f694db2fecdd5775Timo Sirainen /* dirty flag state changed */
01cbf4ac5d44137ab434791be7f838d98d0fcf3bTimo Sirainen bool dirty = (mbox_flags &
01cbf4ac5d44137ab434791be7f838d98d0fcf3bTimo Sirainen MAIL_INDEX_MAIL_FLAG_DIRTY) != 0;
39775ad03c459efe64cce924658da5094ba417e1Timo Sirainen mail_index_update_flags(sync_ctx->t,
39775ad03c459efe64cce924658da5094ba417e1Timo Sirainen sync_ctx->idx_seq,
ab3c1eab9ca13916358a9e8b12df8212fefb7dbfTimo Sirainen dirty ? MODIFY_ADD : MODIFY_REMOVE,
01cbf4ac5d44137ab434791be7f838d98d0fcf3bTimo Sirainen (enum mail_flags)
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen MAIL_INDEX_MAIL_FLAG_DIRTY);
3021a062b16ff0138408be6107d6bcd0ced280b9Timo Sirainen }
39775ad03c459efe64cce924658da5094ba417e1Timo Sirainen }
39775ad03c459efe64cce924658da5094ba417e1Timo Sirainen
1e76a5b92f9d82d557f81f080f3dfad1c9d8f200Timo Sirainen if ((idx_mail.flags & MAIL_INDEX_MAIL_FLAG_DIRTY) == 0 &&
e5c08648676d1989f6e70b95e5990c26b3e8b96bTimo Sirainen !index_keyword_array_cmp(&idx_mail.keywords,
e5c08648676d1989f6e70b95e5990c26b3e8b96bTimo Sirainen &mail_ctx->mail.keywords))
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainen mbox_sync_update_index_keywords(mail_ctx);
39775ad03c459efe64cce924658da5094ba417e1Timo Sirainen t_pop();
39775ad03c459efe64cce924658da5094ba417e1Timo Sirainen
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen /* see if we need to update md5 sum. */
01cbf4ac5d44137ab434791be7f838d98d0fcf3bTimo Sirainen if (sync_ctx->mbox->mbox_save_md5 != 0) {
01cbf4ac5d44137ab434791be7f838d98d0fcf3bTimo Sirainen if (mbox_sync_update_md5_if_changed(mail_ctx) < 0)
01cbf4ac5d44137ab434791be7f838d98d0fcf3bTimo Sirainen return -1;
8907d617ce7c4f390c0f42f6f694db2fecdd5775Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
c35d0c3eb4ba89432896e1f9770ab31cc63c232bTimo Sirainen
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen if (mail_ctx->recent &&
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen (rec == NULL || (rec->flags & MAIL_INDEX_MAIL_FLAG_DIRTY) == 0 ||
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen (rec->flags & MAIL_RECENT) != 0)) {
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen index_mailbox_set_recent(&sync_ctx->mbox->ibox,
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen sync_ctx->idx_seq);
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen }
e063aca6bc2f08bec516d4b631052ea9191f011dTimo Sirainen
c35d0c3eb4ba89432896e1f9770ab31cc63c232bTimo Sirainen /* update from_offsets, but not if we're going to rewrite this message.
1e76a5b92f9d82d557f81f080f3dfad1c9d8f200Timo Sirainen rewriting would just move it anyway. */
4d25408732be27e91f0430f71e87242760c2517cTimo Sirainen if (sync_ctx->need_space_seq == 0) {
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen bool nocheck = rec == NULL || sync_ctx->expunged_space > 0;
4d25408732be27e91f0430f71e87242760c2517cTimo Sirainen if (mbox_sync_update_from_offset(sync_ctx, mail, nocheck) < 0)
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen return -1;
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen }
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen return 0;
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen}
c35d0c3eb4ba89432896e1f9770ab31cc63c232bTimo Sirainen
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainenstatic int mbox_read_from_line(struct mbox_sync_mail_context *ctx)
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen{
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen struct istream *input = ctx->sync_ctx->file_input;
c35d0c3eb4ba89432896e1f9770ab31cc63c232bTimo Sirainen const unsigned char *data;
c35d0c3eb4ba89432896e1f9770ab31cc63c232bTimo Sirainen size_t size, from_line_size;
c35d0c3eb4ba89432896e1f9770ab31cc63c232bTimo Sirainen
c35d0c3eb4ba89432896e1f9770ab31cc63c232bTimo Sirainen buffer_set_used_size(ctx->sync_ctx->from_line, 0);
2be66b9eddad3841a1195fe9aeb1eaf0f28f1116Timo Sirainen from_line_size = ctx->hdr_offset - ctx->mail.from_offset;
2be66b9eddad3841a1195fe9aeb1eaf0f28f1116Timo Sirainen
c35d0c3eb4ba89432896e1f9770ab31cc63c232bTimo Sirainen i_stream_seek(input, ctx->mail.from_offset);
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen for (;;) {
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen data = i_stream_get_data(input, &size);
c35d0c3eb4ba89432896e1f9770ab31cc63c232bTimo Sirainen if (size >= from_line_size)
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen size = from_line_size;
b2ecd50bb98c44816cb07c17aa17fae2b425f941Timo Sirainen
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen buffer_append(ctx->sync_ctx->from_line, data, size);
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen i_stream_skip(input, size);
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen from_line_size -= size;
c35d0c3eb4ba89432896e1f9770ab31cc63c232bTimo Sirainen
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen if (from_line_size == 0)
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen break;
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen if (i_stream_read(input) < 0)
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen return -1;
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen }
b2ecd50bb98c44816cb07c17aa17fae2b425f941Timo Sirainen
c35d0c3eb4ba89432896e1f9770ab31cc63c232bTimo Sirainen return 0;
7fd72a47d7ddfbd38c8697e228b6951f495dfb61Timo Sirainen}
7fd72a47d7ddfbd38c8697e228b6951f495dfb61Timo Sirainen
7fd72a47d7ddfbd38c8697e228b6951f495dfb61Timo Sirainenstatic int mbox_rewrite_base_uid_last(struct mbox_sync_context *sync_ctx)
7fd72a47d7ddfbd38c8697e228b6951f495dfb61Timo Sirainen{
7fd72a47d7ddfbd38c8697e228b6951f495dfb61Timo Sirainen unsigned char buf[10];
c35d0c3eb4ba89432896e1f9770ab31cc63c232bTimo Sirainen const char *str;
c35d0c3eb4ba89432896e1f9770ab31cc63c232bTimo Sirainen uint32_t uid_last;
7fd72a47d7ddfbd38c8697e228b6951f495dfb61Timo Sirainen unsigned int i;
7fd72a47d7ddfbd38c8697e228b6951f495dfb61Timo Sirainen int ret;
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen i_assert(sync_ctx->base_uid_last_offset != 0);
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen /* first check that the 10 bytes are there and they're exactly as
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen expected. just an extra safety check to make sure we never write
6a1e4eb2c6a267bec1e8704ce9137bebb7792702Timo Sirainen to wrong location in the mbox file. */
60d3fa9883237e896a8704275b6116fa46f7ffdaTimo Sirainen ret = pread_full(sync_ctx->write_fd, buf, sizeof(buf),
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen sync_ctx->base_uid_last_offset);
659fe5d24825b160cae512538088020d97a60239Timo Sirainen if (ret < 0) {
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen mbox_set_syscall_error(sync_ctx->mbox, "pread_full()");
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen return -1;
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen }
c35d0c3eb4ba89432896e1f9770ab31cc63c232bTimo Sirainen if (ret == 0) {
c35d0c3eb4ba89432896e1f9770ab31cc63c232bTimo Sirainen mbox_sync_set_critical(sync_ctx,
c35d0c3eb4ba89432896e1f9770ab31cc63c232bTimo Sirainen "X-IMAPbase uid-last unexpectedly points outside "
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen "mbox file %s", sync_ctx->mbox->path);
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen return -1;
064bfeee2f9156683b191cc0f3f7b242720942f7Timo Sirainen }
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen
7fd72a47d7ddfbd38c8697e228b6951f495dfb61Timo Sirainen for (i = 0, uid_last = 0; i < sizeof(buf); i++) {
7fd72a47d7ddfbd38c8697e228b6951f495dfb61Timo Sirainen if (buf[i] < '0' || buf[i] > '9') {
7fd72a47d7ddfbd38c8697e228b6951f495dfb61Timo Sirainen uid_last = (uint32_t)-1;
7fd72a47d7ddfbd38c8697e228b6951f495dfb61Timo Sirainen break;
c35d0c3eb4ba89432896e1f9770ab31cc63c232bTimo Sirainen }
7fd72a47d7ddfbd38c8697e228b6951f495dfb61Timo Sirainen uid_last = uid_last * 10 + (buf[i] - '0');
b2ecd50bb98c44816cb07c17aa17fae2b425f941Timo Sirainen }
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (uid_last != sync_ctx->base_uid_last) {
7a6b45405fb1544ac476e6eb1402a70cc1ddcdcfTimo Sirainen mbox_sync_set_critical(sync_ctx,
7a6b45405fb1544ac476e6eb1402a70cc1ddcdcfTimo Sirainen "X-IMAPbase uid-last unexpectedly lost in mbox file %s",
7a6b45405fb1544ac476e6eb1402a70cc1ddcdcfTimo Sirainen sync_ctx->mbox->path);
7a6b45405fb1544ac476e6eb1402a70cc1ddcdcfTimo Sirainen return -1;
7a6b45405fb1544ac476e6eb1402a70cc1ddcdcfTimo Sirainen }
7a6b45405fb1544ac476e6eb1402a70cc1ddcdcfTimo Sirainen
6a1e4eb2c6a267bec1e8704ce9137bebb7792702Timo Sirainen /* and write it */
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen str = t_strdup_printf("%010u", sync_ctx->next_uid - 1);
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen if (pwrite_full(sync_ctx->write_fd, str, 10,
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen sync_ctx->base_uid_last_offset) < 0) {
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen mbox_set_syscall_error(sync_ctx->mbox, "pwrite_full()");
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen return -1;
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen }
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen mbox_sync_file_updated(sync_ctx, FALSE);
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen sync_ctx->base_uid_last = sync_ctx->next_uid - 1;
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen return 0;
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen}
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainenstatic int
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainenmbox_write_from_line(struct mbox_sync_mail_context *ctx)
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen{
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen string_t *str = ctx->sync_ctx->from_line;
1e76a5b92f9d82d557f81f080f3dfad1c9d8f200Timo Sirainen
36816b5af1472ae76a1909ae3cf29fd614b2ebfcTimo Sirainen if (pwrite_full(ctx->sync_ctx->write_fd, str_data(str), str_len(str),
829c036d4ddfbd9ea49bd8a7c54e3057177d346eTimo Sirainen ctx->mail.from_offset) < 0) {
36816b5af1472ae76a1909ae3cf29fd614b2ebfcTimo Sirainen mbox_set_syscall_error(ctx->sync_ctx->mbox, "pwrite_full()");
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen return -1;
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen }
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen
dc8552739fa29f011ab71ec383ec6d580a5a9661Timo Sirainen mbox_sync_file_updated(ctx->sync_ctx, FALSE);
dc8552739fa29f011ab71ec383ec6d580a5a9661Timo Sirainen return 0;
8d5991f5c4a8840bf1ea754093dbec505564ab78Timo Sirainen}
dc8552739fa29f011ab71ec383ec6d580a5a9661Timo Sirainen
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainenstatic void update_from_offsets(struct mbox_sync_context *sync_ctx)
39775ad03c459efe64cce924658da5094ba417e1Timo Sirainen{
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen const struct mbox_sync_mail *mails;
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen unsigned int i, count;
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen uint32_t ext_idx;
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen uint64_t offset;
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen ext_idx = sync_ctx->mbox->mbox_ext_idx;
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen mails = array_get(&sync_ctx->mails, &count);
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen for (i = 0; i < count; i++) {
4214b59ac7f3899f8d887d055ef519f5a622d249Timo Sirainen if (mails[i].idx_seq == 0 || mails[i].expunged)
7a6b45405fb1544ac476e6eb1402a70cc1ddcdcfTimo Sirainen continue;
f5b4979e2780c4df112a300967d647e2fdd73511Timo Sirainen
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen sync_ctx->moved_offsets = TRUE;
2cb565cd978aafd5714792b5161889986d49e431Timo Sirainen offset = mails[i].from_offset;
2cb565cd978aafd5714792b5161889986d49e431Timo Sirainen mail_index_update_ext(sync_ctx->t, mails[i].idx_seq,
343a527f805ca5cce78496b959d6def70e5d0cd4Timo Sirainen ext_idx, &offset, NULL);
5486e1e123484f3a8d7d895e8ac41156f2a96305Timo Sirainen }
5486e1e123484f3a8d7d895e8ac41156f2a96305Timo Sirainen}
5486e1e123484f3a8d7d895e8ac41156f2a96305Timo Sirainen
5486e1e123484f3a8d7d895e8ac41156f2a96305Timo Sirainenstatic void mbox_sync_handle_expunge(struct mbox_sync_mail_context *mail_ctx)
f5b4979e2780c4df112a300967d647e2fdd73511Timo Sirainen{
5486e1e123484f3a8d7d895e8ac41156f2a96305Timo Sirainen struct mbox_sync_context *sync_ctx = mail_ctx->sync_ctx;
5486e1e123484f3a8d7d895e8ac41156f2a96305Timo Sirainen struct mailbox *box = &sync_ctx->mbox->ibox.box;
5486e1e123484f3a8d7d895e8ac41156f2a96305Timo Sirainen
5486e1e123484f3a8d7d895e8ac41156f2a96305Timo Sirainen if (box->v.sync_notify != NULL) {
6e4cd4ba520bc22ce375de378f4751136ebcf75aTimo Sirainen box->v.sync_notify(box, mail_ctx->mail.uid,
b2ecd50bb98c44816cb07c17aa17fae2b425f941Timo Sirainen MAILBOX_SYNC_TYPE_EXPUNGE);
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen }
dc8552739fa29f011ab71ec383ec6d580a5a9661Timo Sirainen
dc8552739fa29f011ab71ec383ec6d580a5a9661Timo Sirainen mail_ctx->mail.expunged = TRUE;
dc8552739fa29f011ab71ec383ec6d580a5a9661Timo Sirainen mail_ctx->mail.offset = mail_ctx->mail.from_offset;
dc8552739fa29f011ab71ec383ec6d580a5a9661Timo Sirainen mail_ctx->mail.space =
dc8552739fa29f011ab71ec383ec6d580a5a9661Timo Sirainen mail_ctx->body_offset - mail_ctx->mail.from_offset +
dc8552739fa29f011ab71ec383ec6d580a5a9661Timo Sirainen mail_ctx->mail.body_size;
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen mail_ctx->mail.body_size = 0;
dc8552739fa29f011ab71ec383ec6d580a5a9661Timo Sirainen mail_ctx->mail.uid = 0;
b2ecd50bb98c44816cb07c17aa17fae2b425f941Timo Sirainen
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen if (sync_ctx->seq == 1) {
6a1e4eb2c6a267bec1e8704ce9137bebb7792702Timo Sirainen /* expunging first message, fix space to contain next
4214b59ac7f3899f8d887d055ef519f5a622d249Timo Sirainen message's \n header too since it will be removed. */
4214b59ac7f3899f8d887d055ef519f5a622d249Timo Sirainen mail_ctx->mail.space++;
4214b59ac7f3899f8d887d055ef519f5a622d249Timo Sirainen if (istream_raw_mbox_has_crlf_ending(sync_ctx->input)) {
4214b59ac7f3899f8d887d055ef519f5a622d249Timo Sirainen mail_ctx->mail.space++;
8d5991f5c4a8840bf1ea754093dbec505564ab78Timo Sirainen sync_ctx->first_mail_crlf_expunged = TRUE;
6a1e4eb2c6a267bec1e8704ce9137bebb7792702Timo Sirainen }
6a1e4eb2c6a267bec1e8704ce9137bebb7792702Timo Sirainen
4214b59ac7f3899f8d887d055ef519f5a622d249Timo Sirainen /* uid-last offset is invalid now */
4214b59ac7f3899f8d887d055ef519f5a622d249Timo Sirainen sync_ctx->base_uid_last_offset = 0;
6a1e4eb2c6a267bec1e8704ce9137bebb7792702Timo Sirainen }
930dcf1576f99057ad572420d9c75f3212e46a2eTimo Sirainen
930dcf1576f99057ad572420d9c75f3212e46a2eTimo Sirainen sync_ctx->expunged_space += mail_ctx->mail.space;
930dcf1576f99057ad572420d9c75f3212e46a2eTimo Sirainen}
6a1e4eb2c6a267bec1e8704ce9137bebb7792702Timo Sirainen
930dcf1576f99057ad572420d9c75f3212e46a2eTimo Sirainenstatic int mbox_sync_handle_header(struct mbox_sync_mail_context *mail_ctx)
c9a03c8a03a782488981f81bd7c6b5e01ff55f06Timo Sirainen{
829c036d4ddfbd9ea49bd8a7c54e3057177d346eTimo Sirainen struct mbox_sync_context *sync_ctx = mail_ctx->sync_ctx;
6a1e4eb2c6a267bec1e8704ce9137bebb7792702Timo Sirainen uoff_t orig_from_offset;
7a6b45405fb1544ac476e6eb1402a70cc1ddcdcfTimo Sirainen off_t move_diff;
39775ad03c459efe64cce924658da5094ba417e1Timo Sirainen int ret;
39775ad03c459efe64cce924658da5094ba417e1Timo Sirainen
6a1e4eb2c6a267bec1e8704ce9137bebb7792702Timo Sirainen if (sync_ctx->expunged_space > 0 && sync_ctx->need_space_seq == 0) {
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen /* move the header backwards to fill expunged space */
6a1e4eb2c6a267bec1e8704ce9137bebb7792702Timo Sirainen move_diff = -sync_ctx->expunged_space;
6a1e4eb2c6a267bec1e8704ce9137bebb7792702Timo Sirainen
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen orig_from_offset = mail_ctx->mail.from_offset;
7a6b45405fb1544ac476e6eb1402a70cc1ddcdcfTimo Sirainen if (sync_ctx->dest_first_mail) {
6a1e4eb2c6a267bec1e8704ce9137bebb7792702Timo Sirainen /* we're moving this mail to beginning of file.
6a1e4eb2c6a267bec1e8704ce9137bebb7792702Timo Sirainen skip the initial \n (it's already counted in
6a1e4eb2c6a267bec1e8704ce9137bebb7792702Timo Sirainen expunged_space) */
6a1e4eb2c6a267bec1e8704ce9137bebb7792702Timo Sirainen mail_ctx->mail.from_offset++;
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen if (sync_ctx->first_mail_crlf_expunged)
b2ecd50bb98c44816cb07c17aa17fae2b425f941Timo Sirainen mail_ctx->mail.from_offset++;
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen }
a614397cf1a4dde152eb1a38493a6ec3d817da16Timo Sirainen
a614397cf1a4dde152eb1a38493a6ec3d817da16Timo Sirainen /* read the From-line before rewriting overwrites it */
a614397cf1a4dde152eb1a38493a6ec3d817da16Timo Sirainen if (mbox_read_from_line(mail_ctx) < 0)
a614397cf1a4dde152eb1a38493a6ec3d817da16Timo Sirainen return -1;
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen
b2ecd50bb98c44816cb07c17aa17fae2b425f941Timo Sirainen mbox_sync_update_header(mail_ctx);
8907d617ce7c4f390c0f42f6f694db2fecdd5775Timo Sirainen ret = mbox_sync_try_rewrite(mail_ctx, move_diff);
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen if (ret < 0)
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen return -1;
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen if (ret > 0) {
01cbf4ac5d44137ab434791be7f838d98d0fcf3bTimo Sirainen /* rewrite successful, write From-line to
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen new location */
6a1e4eb2c6a267bec1e8704ce9137bebb7792702Timo Sirainen i_assert(move_diff > 0 ||
39775ad03c459efe64cce924658da5094ba417e1Timo Sirainen (off_t)mail_ctx->mail.from_offset >=
343a527f805ca5cce78496b959d6def70e5d0cd4Timo Sirainen -move_diff);
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen mail_ctx->mail.from_offset += move_diff;
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen mail_ctx->mail.offset += move_diff;
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen if (mbox_write_from_line(mail_ctx) < 0)
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen return -1;
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen } else {
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen if (sync_ctx->dest_first_mail) {
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen /* didn't have enough space, move the offset
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen back so seeking into it doesn't fail */
319944c0f35b311c998854e96d6463a084fd90aeTimo Sirainen mail_ctx->mail.from_offset = orig_from_offset;
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen }
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen }
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen } else if (mail_ctx->need_rewrite ||
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen array_count(&sync_ctx->syncs) != 0) {
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen mbox_sync_update_header(mail_ctx);
ccc895c0358108d2304239063e940b7d75f364abTimo Sirainen if (sync_ctx->delay_writes) {
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainen /* mark it dirty and do it later */
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen mail_ctx->dirty = TRUE;
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen return 0;
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen }
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen if ((ret = mbox_sync_try_rewrite(mail_ctx, 0)) < 0)
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen return -1;
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen } else {
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen /* nothing to do */
f12aabfeba81f0d741971d2b7e4a5008eb4383caTimo Sirainen return 0;
f12aabfeba81f0d741971d2b7e4a5008eb4383caTimo Sirainen }
f12aabfeba81f0d741971d2b7e4a5008eb4383caTimo Sirainen
f12aabfeba81f0d741971d2b7e4a5008eb4383caTimo Sirainen if (ret == 0 && sync_ctx->need_space_seq == 0) {
f12aabfeba81f0d741971d2b7e4a5008eb4383caTimo Sirainen /* first mail with no space to write it */
f12aabfeba81f0d741971d2b7e4a5008eb4383caTimo Sirainen sync_ctx->need_space_seq = sync_ctx->seq;
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen sync_ctx->space_diff = 0;
f12aabfeba81f0d741971d2b7e4a5008eb4383caTimo Sirainen
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen if (sync_ctx->expunged_space > 0) {
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen /* create dummy message to describe the expunged data */
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen struct mbox_sync_mail mail;
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen memset(&mail, 0, sizeof(mail));
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen mail.expunged = TRUE;
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen mail.offset = mail.from_offset =
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen (sync_ctx->dest_first_mail ? 1 : 0) +
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen mail_ctx->mail.from_offset -
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen sync_ctx->expunged_space;
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen mail.space = sync_ctx->expunged_space;
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen sync_ctx->space_diff = sync_ctx->expunged_space;
319944c0f35b311c998854e96d6463a084fd90aeTimo Sirainen sync_ctx->expunged_space = 0;
319944c0f35b311c998854e96d6463a084fd90aeTimo Sirainen i_assert(sync_ctx->space_diff < -mail_ctx->mail.space);
88187ee880b4829443e0d55ea7d145d9d5880217Timo Sirainen
88187ee880b4829443e0d55ea7d145d9d5880217Timo Sirainen sync_ctx->need_space_seq--;
319944c0f35b311c998854e96d6463a084fd90aeTimo Sirainen array_append(&sync_ctx->mails, &mail, 1);
319944c0f35b311c998854e96d6463a084fd90aeTimo Sirainen }
319944c0f35b311c998854e96d6463a084fd90aeTimo Sirainen }
a393d9d6dabdc46cf724f8cb004a652b4036d53dTimo Sirainen return 0;
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen}
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainenstatic int
cece2b9cd692c06025cc0a7a0ff54d996a8c90efTimo Sirainenmbox_sync_handle_missing_space(struct mbox_sync_mail_context *mail_ctx)
cece2b9cd692c06025cc0a7a0ff54d996a8c90efTimo Sirainen{
cece2b9cd692c06025cc0a7a0ff54d996a8c90efTimo Sirainen struct mbox_sync_context *sync_ctx = mail_ctx->sync_ctx;
cece2b9cd692c06025cc0a7a0ff54d996a8c90efTimo Sirainen uoff_t end_offset, move_diff, extra_space, needed_space;
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen uint32_t last_seq;
f0569d9fbb25c8437760be69f194595a841ad711Timo Sirainen ARRAY_TYPE(keyword_indexes) keywords_copy;
db7c9201c88e3d9bee10485194ee5b0c67249916Timo Sirainen
755fe6da51ab7f54aa1d86913cb344bffef60e79Timo Sirainen i_assert(mail_ctx->mail.uid == 0 || mail_ctx->mail.space > 0 ||
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen mail_ctx->mail.offset == mail_ctx->hdr_offset);
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen if (array_is_created(&mail_ctx->mail.keywords)) {
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen /* mail's keywords are allocated from a pool that's cleared
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen for each mail. we'll need to copy it to something more
a10e5606a9e93f49cf13b3a35c8dc3f5d6ab5909Timo Sirainen permanent. */
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen p_array_init(&keywords_copy, sync_ctx->saved_keywords_pool,
b225c3c65f360d7b833f09f9b2fb3035ed5ea600Timo Sirainen array_count(&mail_ctx->mail.keywords));
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen array_append_array(&keywords_copy, &mail_ctx->mail.keywords);
f6699a08521aacc4c2bb5b6175691dad5f715f8cTimo Sirainen mail_ctx->mail.keywords = keywords_copy;
f6699a08521aacc4c2bb5b6175691dad5f715f8cTimo Sirainen }
3da614c39dd29f536c485089e67839b4cf89fed3Timo Sirainen array_append(&sync_ctx->mails, &mail_ctx->mail, 1);
3da614c39dd29f536c485089e67839b4cf89fed3Timo Sirainen
0a6f8311541ae59381171620b77f82be58be562eTimo Sirainen sync_ctx->space_diff += mail_ctx->mail.space;
34d2ee1fa2b299267fcefd378f80690e7f601dfbTimo Sirainen if (sync_ctx->space_diff < 0) {
b225c3c65f360d7b833f09f9b2fb3035ed5ea600Timo Sirainen if (sync_ctx->expunged_space > 0) {
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen i_assert(sync_ctx->expunged_space ==
b225c3c65f360d7b833f09f9b2fb3035ed5ea600Timo Sirainen mail_ctx->mail.space);
b225c3c65f360d7b833f09f9b2fb3035ed5ea600Timo Sirainen sync_ctx->expunged_space = 0;
b225c3c65f360d7b833f09f9b2fb3035ed5ea600Timo Sirainen }
b225c3c65f360d7b833f09f9b2fb3035ed5ea600Timo Sirainen return 0;
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen }
b225c3c65f360d7b833f09f9b2fb3035ed5ea600Timo Sirainen
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen /* we have enough space now */
0a6f8311541ae59381171620b77f82be58be562eTimo Sirainen if (mail_ctx->mail.uid == 0) {
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen /* this message was expunged. fill more or less of the space.
0a6f8311541ae59381171620b77f82be58be562eTimo Sirainen space_diff now consists of a negative "bytes needed" sum,
0a6f8311541ae59381171620b77f82be58be562eTimo Sirainen plus the expunged space of this message. so it contains how
a10e5606a9e93f49cf13b3a35c8dc3f5d6ab5909Timo Sirainen many bytes of _extra_ space we have. */
df4018ae2f0a95be602f724ca70df7e0e3bd6a7dTimo Sirainen i_assert(mail_ctx->mail.space >= sync_ctx->space_diff);
0a6f8311541ae59381171620b77f82be58be562eTimo Sirainen extra_space = MBOX_HEADER_PADDING *
0a6f8311541ae59381171620b77f82be58be562eTimo Sirainen (sync_ctx->seq - sync_ctx->need_space_seq + 1);
0a6f8311541ae59381171620b77f82be58be562eTimo Sirainen needed_space = mail_ctx->mail.space - sync_ctx->space_diff;
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen if ((uoff_t)sync_ctx->space_diff > needed_space + extra_space) {
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen /* don't waste too much on padding */
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen move_diff = needed_space + extra_space;
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen sync_ctx->expunged_space =
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen mail_ctx->mail.space - move_diff;
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen } else {
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen move_diff = mail_ctx->mail.space;
022412398e56a8f31ef111cfd7271498d64af9a9Timo Sirainen extra_space = sync_ctx->space_diff;
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen sync_ctx->expunged_space = 0;
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen }
f6699a08521aacc4c2bb5b6175691dad5f715f8cTimo Sirainen last_seq = sync_ctx->seq - 1;
f6699a08521aacc4c2bb5b6175691dad5f715f8cTimo Sirainen array_delete(&sync_ctx->mails,
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen array_count(&sync_ctx->mails) - 1, 1);
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen end_offset = mail_ctx->mail.from_offset;
022412398e56a8f31ef111cfd7271498d64af9a9Timo Sirainen } else {
022412398e56a8f31ef111cfd7271498d64af9a9Timo Sirainen /* this message gave enough space from headers. rewriting stops
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen at the end of this message's headers. */
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen sync_ctx->expunged_space = 0;
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen last_seq = sync_ctx->seq;
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen end_offset = mail_ctx->body_offset;
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen move_diff = 0;
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen extra_space = sync_ctx->space_diff;
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen }
022412398e56a8f31ef111cfd7271498d64af9a9Timo Sirainen
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen mbox_sync_file_update_ext_modified(sync_ctx);
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen if (mbox_sync_rewrite(sync_ctx,
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen last_seq == sync_ctx->seq ? mail_ctx : NULL,
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen end_offset, move_diff, extra_space,
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen sync_ctx->need_space_seq, last_seq) < 0)
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen return -1;
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen update_from_offsets(sync_ctx);
022412398e56a8f31ef111cfd7271498d64af9a9Timo Sirainen
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen /* mail_ctx may contain wrong data after rewrite, so make sure we
022412398e56a8f31ef111cfd7271498d64af9a9Timo Sirainen don't try to access it */
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen memset(mail_ctx, 0, sizeof(*mail_ctx));
022412398e56a8f31ef111cfd7271498d64af9a9Timo Sirainen
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen sync_ctx->need_space_seq = 0;
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen sync_ctx->space_diff = 0;
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen array_clear(&sync_ctx->mails);
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen p_clear(sync_ctx->saved_keywords_pool);
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen return 0;
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen}
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainenstatic int
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainenmbox_sync_seek_to_seq(struct mbox_sync_context *sync_ctx, uint32_t seq)
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen{
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen struct mbox_mailbox *mbox = sync_ctx->mbox;
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen uoff_t old_offset;
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen uint32_t uid;
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen int ret;
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen bool deleted;
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen if (seq == 0) {
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen if (istream_raw_mbox_seek(mbox->mbox_stream, 0) < 0) {
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen mail_storage_set_error(&mbox->storage->storage,
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen MAIL_ERROR_NOTPOSSIBLE,
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen "Mailbox isn't a valid mbox file");
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen return -1;
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen }
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen seq++;
755fe6da51ab7f54aa1d86913cb344bffef60e79Timo Sirainen } else {
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen old_offset = istream_raw_mbox_get_start_offset(sync_ctx->input);
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen ret = mbox_file_seek(mbox, sync_ctx->sync_view, seq, &deleted);
f6699a08521aacc4c2bb5b6175691dad5f715f8cTimo Sirainen if (ret < 0) {
f6699a08521aacc4c2bb5b6175691dad5f715f8cTimo Sirainen if (deleted) {
f6699a08521aacc4c2bb5b6175691dad5f715f8cTimo Sirainen mbox_sync_set_critical(sync_ctx,
f6699a08521aacc4c2bb5b6175691dad5f715f8cTimo Sirainen "Message was expunged unexpectedly "
f6699a08521aacc4c2bb5b6175691dad5f715f8cTimo Sirainen "in mbox file %s", mbox->path);
f6699a08521aacc4c2bb5b6175691dad5f715f8cTimo Sirainen }
f6699a08521aacc4c2bb5b6175691dad5f715f8cTimo Sirainen return -1;
f6699a08521aacc4c2bb5b6175691dad5f715f8cTimo Sirainen }
f6699a08521aacc4c2bb5b6175691dad5f715f8cTimo Sirainen if (ret == 0) {
f6699a08521aacc4c2bb5b6175691dad5f715f8cTimo Sirainen if (istream_raw_mbox_seek(mbox->mbox_stream,
f6699a08521aacc4c2bb5b6175691dad5f715f8cTimo Sirainen old_offset) < 0) {
f6699a08521aacc4c2bb5b6175691dad5f715f8cTimo Sirainen mbox_sync_set_critical(sync_ctx,
f6699a08521aacc4c2bb5b6175691dad5f715f8cTimo Sirainen "Error seeking back to original "
f6699a08521aacc4c2bb5b6175691dad5f715f8cTimo Sirainen "offset %s in mbox file %s",
f6699a08521aacc4c2bb5b6175691dad5f715f8cTimo Sirainen dec2str(old_offset), mbox->path);
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen return -1;
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen }
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen return 0;
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen }
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen }
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen if (seq <= 1)
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen uid = 0;
d31c4d7c161f9d7efa59964c7c958e83e05b218cTimo Sirainen else if (mail_index_lookup_uid(sync_ctx->sync_view, seq-1, &uid) < 0) {
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen mail_storage_set_index_error(&mbox->ibox);
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen return -1;
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen }
755fe6da51ab7f54aa1d86913cb344bffef60e79Timo Sirainen
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen sync_ctx->prev_msg_uid = uid;
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen /* set to -1, since it's always increased later */
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen sync_ctx->seq = seq-1;
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen if (sync_ctx->seq == 0 &&
71a74e26cf070a205d31cf6c6fae003f90027b63Timo Sirainen istream_raw_mbox_get_start_offset(sync_ctx->input) != 0) {
a486ed03dce069ff60ab5a65d0ae24a1862f22fcTimo Sirainen /* this mbox has pseudo mail which contains the X-IMAP header */
a486ed03dce069ff60ab5a65d0ae24a1862f22fcTimo Sirainen sync_ctx->seq++;
a486ed03dce069ff60ab5a65d0ae24a1862f22fcTimo Sirainen }
a486ed03dce069ff60ab5a65d0ae24a1862f22fcTimo Sirainen
a486ed03dce069ff60ab5a65d0ae24a1862f22fcTimo Sirainen sync_ctx->idx_seq = seq;
a486ed03dce069ff60ab5a65d0ae24a1862f22fcTimo Sirainen sync_ctx->dest_first_mail = sync_ctx->seq == 0;
a486ed03dce069ff60ab5a65d0ae24a1862f22fcTimo Sirainen (void)istream_raw_mbox_get_body_offset(sync_ctx->input);
a486ed03dce069ff60ab5a65d0ae24a1862f22fcTimo Sirainen return 1;
d31c4d7c161f9d7efa59964c7c958e83e05b218cTimo Sirainen}
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainenstatic int
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainenmbox_sync_seek_to_uid(struct mbox_sync_context *sync_ctx, uint32_t uid)
f6699a08521aacc4c2bb5b6175691dad5f715f8cTimo Sirainen{
f6699a08521aacc4c2bb5b6175691dad5f715f8cTimo Sirainen struct mail_index_view *sync_view = sync_ctx->sync_view;
f6699a08521aacc4c2bb5b6175691dad5f715f8cTimo Sirainen uint32_t seq1, seq2;
f6699a08521aacc4c2bb5b6175691dad5f715f8cTimo Sirainen const struct stat *st;
f6699a08521aacc4c2bb5b6175691dad5f715f8cTimo Sirainen
319944c0f35b311c998854e96d6463a084fd90aeTimo Sirainen if (mail_index_lookup_uid_range(sync_view, uid, (uint32_t)-1,
319944c0f35b311c998854e96d6463a084fd90aeTimo Sirainen &seq1, &seq2) < 0) {
912e870dcab2dadac8a6b20b42367999cdd8b5a1Timo Sirainen mail_storage_set_index_error(&sync_ctx->mbox->ibox);
319944c0f35b311c998854e96d6463a084fd90aeTimo Sirainen return -1;
319944c0f35b311c998854e96d6463a084fd90aeTimo Sirainen }
b8765f6093ab35fc2345293d78132d35794cbff5Timo Sirainen
b8765f6093ab35fc2345293d78132d35794cbff5Timo Sirainen if (seq1 == 0) {
b8765f6093ab35fc2345293d78132d35794cbff5Timo Sirainen /* doesn't exist anymore, seek to end of file */
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen st = i_stream_stat(sync_ctx->file_input, TRUE);
b8765f6093ab35fc2345293d78132d35794cbff5Timo Sirainen if (st == NULL) {
b8765f6093ab35fc2345293d78132d35794cbff5Timo Sirainen mbox_set_syscall_error(sync_ctx->mbox,
b8765f6093ab35fc2345293d78132d35794cbff5Timo Sirainen "i_stream_stat()");
b8765f6093ab35fc2345293d78132d35794cbff5Timo Sirainen return -1;
319944c0f35b311c998854e96d6463a084fd90aeTimo Sirainen }
319944c0f35b311c998854e96d6463a084fd90aeTimo Sirainen
d31c4d7c161f9d7efa59964c7c958e83e05b218cTimo Sirainen if (istream_raw_mbox_seek(sync_ctx->mbox->mbox_stream,
d31c4d7c161f9d7efa59964c7c958e83e05b218cTimo Sirainen st->st_size) < 0) {
319944c0f35b311c998854e96d6463a084fd90aeTimo Sirainen mbox_sync_set_critical(sync_ctx,
1e76a5b92f9d82d557f81f080f3dfad1c9d8f200Timo Sirainen "Error seeking to end of mbox file %s",
907723f35f4d3dfc774ca42d00a8a7b8ef90dd5dTimo Sirainen sync_ctx->mbox->path);
907723f35f4d3dfc774ca42d00a8a7b8ef90dd5dTimo Sirainen return -1;
2ae575a66f2a302f047f6de062a70b75f8bebc7bTimo Sirainen }
cdaf255d6a3daeef0ac85edaa60bfa6d1f945bffTimo Sirainen sync_ctx->idx_seq =
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen mail_index_view_get_messages_count(sync_view) + 1;
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen return 1;
6843896c40bee4f9b6680ca7ced598c446e9f999Timo Sirainen }
7f3be7d885c75cdd77f536929a45bc9764595960Timo Sirainen
2ae575a66f2a302f047f6de062a70b75f8bebc7bTimo Sirainen return mbox_sync_seek_to_seq(sync_ctx, seq1);
2ae575a66f2a302f047f6de062a70b75f8bebc7bTimo Sirainen}
2ae575a66f2a302f047f6de062a70b75f8bebc7bTimo Sirainen
cdaf255d6a3daeef0ac85edaa60bfa6d1f945bffTimo Sirainenstatic int mbox_sync_partial_seek_next(struct mbox_sync_context *sync_ctx,
1e76a5b92f9d82d557f81f080f3dfad1c9d8f200Timo Sirainen uint32_t next_uid, bool *partial,
2ae575a66f2a302f047f6de062a70b75f8bebc7bTimo Sirainen bool *skipped_mails)
2ae575a66f2a302f047f6de062a70b75f8bebc7bTimo Sirainen{
7a6b45405fb1544ac476e6eb1402a70cc1ddcdcfTimo Sirainen uint32_t messages_count;
7a6b45405fb1544ac476e6eb1402a70cc1ddcdcfTimo Sirainen int ret;
7a6b45405fb1544ac476e6eb1402a70cc1ddcdcfTimo Sirainen
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen /* delete sync records up to next message. so if there's still
7f3be7d885c75cdd77f536929a45bc9764595960Timo Sirainen something left in array, it means the next message needs modifying */
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen mbox_sync_array_delete_to(&sync_ctx->syncs, next_uid);
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen if (array_count(&sync_ctx->syncs) > 0)
7f3be7d885c75cdd77f536929a45bc9764595960Timo Sirainen return 1;
7a6b45405fb1544ac476e6eb1402a70cc1ddcdcfTimo Sirainen
7a6b45405fb1544ac476e6eb1402a70cc1ddcdcfTimo Sirainen if (sync_ctx->sync_rec.uid1 != 0) {
7a6b45405fb1544ac476e6eb1402a70cc1ddcdcfTimo Sirainen /* we can skip forward to next record which needs updating. */
c9fe52d819c608b890620f7fe36ff509b14eb350Timo Sirainen if (sync_ctx->sync_rec.uid1 != next_uid) {
c9fe52d819c608b890620f7fe36ff509b14eb350Timo Sirainen *skipped_mails = TRUE;
c9fe52d819c608b890620f7fe36ff509b14eb350Timo Sirainen next_uid = sync_ctx->sync_rec.uid1;
b08b33d1f5ce3721dc2d83586c9cb0ca141331fdTimo Sirainen }
b08b33d1f5ce3721dc2d83586c9cb0ca141331fdTimo Sirainen ret = mbox_sync_seek_to_uid(sync_ctx, next_uid);
b08b33d1f5ce3721dc2d83586c9cb0ca141331fdTimo Sirainen } else {
c9fe52d819c608b890620f7fe36ff509b14eb350Timo Sirainen /* if there's no sync records left, we can stop. except if
1e76a5b92f9d82d557f81f080f3dfad1c9d8f200Timo Sirainen this is a dirty sync, check if there are new messages. */
c9fe52d819c608b890620f7fe36ff509b14eb350Timo Sirainen if (!sync_ctx->mbox->mbox_sync_dirty)
c9fe52d819c608b890620f7fe36ff509b14eb350Timo Sirainen return 0;
c9fe52d819c608b890620f7fe36ff509b14eb350Timo Sirainen
e9d68b41c007f0e545de361f8012f6f231bfec8bTimo Sirainen messages_count =
c9fe52d819c608b890620f7fe36ff509b14eb350Timo Sirainen mail_index_view_get_messages_count(sync_ctx->sync_view);
c9fe52d819c608b890620f7fe36ff509b14eb350Timo Sirainen if (sync_ctx->seq + 1 != messages_count) {
e9d68b41c007f0e545de361f8012f6f231bfec8bTimo Sirainen ret = mbox_sync_seek_to_seq(sync_ctx, messages_count);
e9d68b41c007f0e545de361f8012f6f231bfec8bTimo Sirainen *skipped_mails = TRUE;
7f3be7d885c75cdd77f536929a45bc9764595960Timo Sirainen } else {
7f3be7d885c75cdd77f536929a45bc9764595960Timo Sirainen ret = 1;
1e76a5b92f9d82d557f81f080f3dfad1c9d8f200Timo Sirainen }
71a74e26cf070a205d31cf6c6fae003f90027b63Timo Sirainen *partial = FALSE;
71a74e26cf070a205d31cf6c6fae003f90027b63Timo Sirainen }
a393d9d6dabdc46cf724f8cb004a652b4036d53dTimo Sirainen
71a74e26cf070a205d31cf6c6fae003f90027b63Timo Sirainen if (ret == 0) {
71a74e26cf070a205d31cf6c6fae003f90027b63Timo Sirainen /* seek failed because the offset is dirty. just ignore and
71a74e26cf070a205d31cf6c6fae003f90027b63Timo Sirainen continue from where we are now. */
a486ed03dce069ff60ab5a65d0ae24a1862f22fcTimo Sirainen *partial = FALSE;
a486ed03dce069ff60ab5a65d0ae24a1862f22fcTimo Sirainen ret = 1;
a486ed03dce069ff60ab5a65d0ae24a1862f22fcTimo Sirainen }
a486ed03dce069ff60ab5a65d0ae24a1862f22fcTimo Sirainen return ret;
a486ed03dce069ff60ab5a65d0ae24a1862f22fcTimo Sirainen}
a486ed03dce069ff60ab5a65d0ae24a1862f22fcTimo Sirainen
ccc895c0358108d2304239063e940b7d75f364abTimo Sirainenstatic int mbox_sync_loop(struct mbox_sync_context *sync_ctx,
a486ed03dce069ff60ab5a65d0ae24a1862f22fcTimo Sirainen struct mbox_sync_mail_context *mail_ctx,
a486ed03dce069ff60ab5a65d0ae24a1862f22fcTimo Sirainen bool partial)
a486ed03dce069ff60ab5a65d0ae24a1862f22fcTimo Sirainen{
a486ed03dce069ff60ab5a65d0ae24a1862f22fcTimo Sirainen const struct mail_index_record *rec;
a486ed03dce069ff60ab5a65d0ae24a1862f22fcTimo Sirainen uint32_t uid, messages_count;
a486ed03dce069ff60ab5a65d0ae24a1862f22fcTimo Sirainen uoff_t offset;
6843896c40bee4f9b6680ca7ced598c446e9f999Timo Sirainen int ret;
6843896c40bee4f9b6680ca7ced598c446e9f999Timo Sirainen bool expunged, skipped_mails, uids_broken;
6843896c40bee4f9b6680ca7ced598c446e9f999Timo Sirainen
1dd9801f7ace40777ecf8ed9a98c093bc40467a4Timo Sirainen messages_count =
6843896c40bee4f9b6680ca7ced598c446e9f999Timo Sirainen mail_index_view_get_messages_count(sync_ctx->sync_view);
1e76a5b92f9d82d557f81f080f3dfad1c9d8f200Timo Sirainen
b428c5993b01f4cffacdd24377ca2873fcc12184Timo Sirainen /* always start from first message so we can read X-IMAP or
d0e5f8252516c4d4df2fbcdae4d37f9e5f931199Timo Sirainen X-IMAPbase header */
f0569d9fbb25c8437760be69f194595a841ad711Timo Sirainen ret = mbox_sync_seek_to_seq(sync_ctx, 0);
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen if (ret <= 0)
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen return ret;
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen
8907d617ce7c4f390c0f42f6f694db2fecdd5775Timo Sirainen if (sync_ctx->renumber_uids) {
8907d617ce7c4f390c0f42f6f694db2fecdd5775Timo Sirainen /* expunge everything */
f0569d9fbb25c8437760be69f194595a841ad711Timo Sirainen while (sync_ctx->idx_seq <= messages_count) {
f0569d9fbb25c8437760be69f194595a841ad711Timo Sirainen mail_index_expunge(sync_ctx->t,
8907d617ce7c4f390c0f42f6f694db2fecdd5775Timo Sirainen sync_ctx->idx_seq++);
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen }
b2ecd50bb98c44816cb07c17aa17fae2b425f941Timo Sirainen }
1e76a5b92f9d82d557f81f080f3dfad1c9d8f200Timo Sirainen
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen skipped_mails = uids_broken = FALSE;
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen while ((ret = mbox_sync_read_next_mail(sync_ctx, mail_ctx)) > 0) {
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen uid = mail_ctx->mail.uid;
907723f35f4d3dfc774ca42d00a8a7b8ef90dd5dTimo Sirainen
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen if (mail_ctx->seq == 1 && sync_ctx->base_uid_validity != 0 &&
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen sync_ctx->hdr->uid_validity != 0 &&
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen sync_ctx->base_uid_validity !=
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen sync_ctx->hdr->uid_validity) {
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen mail_storage_set_critical(
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen &sync_ctx->mbox->storage->storage,
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen "UIDVALIDITY changed (%u -> %u) "
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen "in mbox file %s",
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen sync_ctx->hdr->uid_validity,
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen sync_ctx->base_uid_validity,
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen sync_ctx->mbox->path);
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen mail_index_mark_corrupted(sync_ctx->mbox->ibox.index);
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen return -1;
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen }
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen if (mail_ctx->mail.uid_broken && partial) {
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen /* UID ordering problems, resync everything to make
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen sure we get everything right */
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen if (sync_ctx->mbox->mbox_sync_dirty)
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen return 0;
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen mbox_sync_set_critical(sync_ctx,
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen "UIDs broken with partial sync in mbox file %s",
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen sync_ctx->mbox->path);
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen sync_ctx->mbox->mbox_sync_dirty = TRUE;
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen return 0;
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen }
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen if (mail_ctx->mail.uid_broken)
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen uids_broken = TRUE;
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen if (mail_ctx->mail.pseudo)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen uid = 0;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen rec = NULL; ret = 1;
178511b57faa7c3f8203dd8b7e4059d00cbfc23aTimo Sirainen if (uid != 0) {
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen ret = mbox_sync_read_index_rec(sync_ctx, uid, &rec);
a393d9d6dabdc46cf724f8cb004a652b4036d53dTimo Sirainen if (ret < 0)
a393d9d6dabdc46cf724f8cb004a652b4036d53dTimo Sirainen return -1;
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen }
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen if (ret == 0) {
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen /* UID found but it's broken */
d31c4d7c161f9d7efa59964c7c958e83e05b218cTimo Sirainen uid = 0;
d31c4d7c161f9d7efa59964c7c958e83e05b218cTimo Sirainen } else if (uid == 0 &&
d31c4d7c161f9d7efa59964c7c958e83e05b218cTimo Sirainen !mail_ctx->mail.pseudo &&
d31c4d7c161f9d7efa59964c7c958e83e05b218cTimo Sirainen (sync_ctx->delay_writes ||
d31c4d7c161f9d7efa59964c7c958e83e05b218cTimo Sirainen sync_ctx->idx_seq <= messages_count)) {
d31c4d7c161f9d7efa59964c7c958e83e05b218cTimo Sirainen /* If we can't use/store X-UID header, use MD5 sum.
d31c4d7c161f9d7efa59964c7c958e83e05b218cTimo Sirainen Also check for existing MD5 sums when we're actually
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen able to write X-UIDs. */
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen sync_ctx->mbox->mbox_save_md5 = TRUE;
3cfff0ca01961d885bdbd6ef08d761880116af07Timo Sirainen
f7d43647acc6dc80064c8c4cacf5bf86f754c530Timo Sirainen if (mbox_sync_find_index_md5(sync_ctx,
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen mail_ctx->hdr_md5_sum,
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen &rec) < 0)
9d2040fbb941f411d57fd850b4cdc3b1cccc1168Timo Sirainen return -1;
9d2040fbb941f411d57fd850b4cdc3b1cccc1168Timo Sirainen
b225c3c65f360d7b833f09f9b2fb3035ed5ea600Timo Sirainen if (rec != NULL)
b225c3c65f360d7b833f09f9b2fb3035ed5ea600Timo Sirainen uid = mail_ctx->mail.uid = rec->uid;
9d2040fbb941f411d57fd850b4cdc3b1cccc1168Timo Sirainen }
9d2040fbb941f411d57fd850b4cdc3b1cccc1168Timo Sirainen
9d2040fbb941f411d57fd850b4cdc3b1cccc1168Timo Sirainen /* get all sync records related to this message. with pseudo
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen message just get the first sync record so we can jump to
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen it with partial seeking. */
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen if (mbox_sync_read_index_syncs(sync_ctx,
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen mail_ctx->mail.pseudo ? 1 : uid,
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen &expunged) < 0)
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen return -1;
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen if (mail_ctx->mail.pseudo) {
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen /* if it was set, it was for the next message */
f7d43647acc6dc80064c8c4cacf5bf86f754c530Timo Sirainen expunged = FALSE;
f7d43647acc6dc80064c8c4cacf5bf86f754c530Timo Sirainen } else {
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen if (rec == NULL) {
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen /* message wasn't found from index. we have to
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen read everything from now on, no skipping */
9d2040fbb941f411d57fd850b4cdc3b1cccc1168Timo Sirainen partial = FALSE;
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen }
b225c3c65f360d7b833f09f9b2fb3035ed5ea600Timo Sirainen }
b225c3c65f360d7b833f09f9b2fb3035ed5ea600Timo Sirainen
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen if (uid == 0 && !mail_ctx->mail.pseudo) {
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen /* missing/broken X-UID. all the rest of the mails
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen need new UIDs. */
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen while (sync_ctx->idx_seq <= messages_count) {
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen mail_index_expunge(sync_ctx->t,
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen sync_ctx->idx_seq++);
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen }
b8bbfab97eed17fcb00b5a86128e1d7a3babc35cTimo Sirainen
b8bbfab97eed17fcb00b5a86128e1d7a3babc35cTimo Sirainen if (sync_ctx->next_uid == (uint32_t)-1) {
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen /* oh no, we're out of UIDs. this shouldn't
2be66b9eddad3841a1195fe9aeb1eaf0f28f1116Timo Sirainen happen normally, so just try to get it fixed
2be66b9eddad3841a1195fe9aeb1eaf0f28f1116Timo Sirainen without crashing. */
2be66b9eddad3841a1195fe9aeb1eaf0f28f1116Timo Sirainen mail_storage_set_critical(
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen &sync_ctx->mbox->storage->storage,
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen "Out of UIDs, renumbering them in mbox "
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen "file %s", sync_ctx->mbox->path);
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen sync_ctx->renumber_uids = TRUE;
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen return 0;
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen }
b225c3c65f360d7b833f09f9b2fb3035ed5ea600Timo Sirainen
f5b919e9b07dfd9d2401b998ef8759e5f0312719Timo Sirainen mail_ctx->need_rewrite = TRUE;
33ae95df45c9b5ec51332a6b39eb5322038686b9Timo Sirainen mail_ctx->mail.uid = sync_ctx->next_uid++;
178511b57faa7c3f8203dd8b7e4059d00cbfc23aTimo Sirainen }
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen sync_ctx->prev_msg_uid = mail_ctx->mail.uid;
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen if (!mail_ctx->mail.pseudo)
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen mail_ctx->mail.idx_seq = sync_ctx->idx_seq;
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen
7d207b1e77a7b5e3fda640e353acfc86d261fedfTimo Sirainen if (!expunged) {
b225c3c65f360d7b833f09f9b2fb3035ed5ea600Timo Sirainen if (mbox_sync_handle_header(mail_ctx) < 0)
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen return -1;
b225c3c65f360d7b833f09f9b2fb3035ed5ea600Timo Sirainen sync_ctx->dest_first_mail = FALSE;
b225c3c65f360d7b833f09f9b2fb3035ed5ea600Timo Sirainen } else {
8166e8a706078efc71923719ca285e84902883c0Timo Sirainen mbox_sync_handle_expunge(mail_ctx);
8166e8a706078efc71923719ca285e84902883c0Timo Sirainen }
8166e8a706078efc71923719ca285e84902883c0Timo Sirainen
8166e8a706078efc71923719ca285e84902883c0Timo Sirainen if (!mail_ctx->mail.pseudo) {
8166e8a706078efc71923719ca285e84902883c0Timo Sirainen if (!expunged) {
b225c3c65f360d7b833f09f9b2fb3035ed5ea600Timo Sirainen if (mbox_sync_update_index(mail_ctx, rec) < 0)
09b9cbde9e7a0f9adea1fb054a7c62f35ad901e1Timo Sirainen return -1;
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen }
f87702d8d147f66d3fb6c41e5695c67f6d00612eTimo Sirainen sync_ctx->idx_seq++;
09b9cbde9e7a0f9adea1fb054a7c62f35ad901e1Timo Sirainen }
09b9cbde9e7a0f9adea1fb054a7c62f35ad901e1Timo Sirainen
09b9cbde9e7a0f9adea1fb054a7c62f35ad901e1Timo Sirainen istream_raw_mbox_next(sync_ctx->input,
09b9cbde9e7a0f9adea1fb054a7c62f35ad901e1Timo Sirainen mail_ctx->mail.body_size);
09b9cbde9e7a0f9adea1fb054a7c62f35ad901e1Timo Sirainen offset = istream_raw_mbox_get_start_offset(sync_ctx->input);
5edfc0f1c3c55e906d8316d9cdeaa3b0c7000c19Timo Sirainen
686cb2418d4daa7bc747d3551783c1895e7c41b9Timo Sirainen if (sync_ctx->need_space_seq != 0) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (mbox_sync_handle_missing_space(mail_ctx) < 0)
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen return -1;
b225c3c65f360d7b833f09f9b2fb3035ed5ea600Timo Sirainen if (mbox_sync_seek(sync_ctx, offset) < 0)
b225c3c65f360d7b833f09f9b2fb3035ed5ea600Timo Sirainen return -1;
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen } else if (sync_ctx->expunged_space > 0) {
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen if (!expunged) {
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen /* move the body */
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen mbox_sync_file_update_ext_modified(sync_ctx);
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen if (mbox_move(sync_ctx,
f5b919e9b07dfd9d2401b998ef8759e5f0312719Timo Sirainen mail_ctx->body_offset -
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen sync_ctx->expunged_space,
f5b919e9b07dfd9d2401b998ef8759e5f0312719Timo Sirainen mail_ctx->body_offset,
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen mail_ctx->mail.body_size) < 0)
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen return -1;
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen if (mbox_sync_seek(sync_ctx, offset) < 0)
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen return -1;
6a1e4eb2c6a267bec1e8704ce9137bebb7792702Timo Sirainen }
fc1696e32dd732a5bbabc3c8f64810448e327043Timo Sirainen } else if (partial) {
b225c3c65f360d7b833f09f9b2fb3035ed5ea600Timo Sirainen ret = mbox_sync_partial_seek_next(sync_ctx, uid + 1,
f5b919e9b07dfd9d2401b998ef8759e5f0312719Timo Sirainen &partial,
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen &skipped_mails);
6a1e4eb2c6a267bec1e8704ce9137bebb7792702Timo Sirainen if (ret <= 0) {
b8bbfab97eed17fcb00b5a86128e1d7a3babc35cTimo Sirainen if (ret < 0)
b8bbfab97eed17fcb00b5a86128e1d7a3babc35cTimo Sirainen return -1;
b8bbfab97eed17fcb00b5a86128e1d7a3babc35cTimo Sirainen break;
b8bbfab97eed17fcb00b5a86128e1d7a3babc35cTimo Sirainen }
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen }
fc1696e32dd732a5bbabc3c8f64810448e327043Timo Sirainen }
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen
6a1e4eb2c6a267bec1e8704ce9137bebb7792702Timo Sirainen if (istream_raw_mbox_is_eof(sync_ctx->input)) {
f4b93a46e140823a64d88763ea6ef9f03c49844eTimo Sirainen /* rest of the messages in index don't exist -> expunge them */
6a1e4eb2c6a267bec1e8704ce9137bebb7792702Timo Sirainen while (sync_ctx->idx_seq <= messages_count)
6a1e4eb2c6a267bec1e8704ce9137bebb7792702Timo Sirainen mail_index_expunge(sync_ctx->t, sync_ctx->idx_seq++);
6a1e4eb2c6a267bec1e8704ce9137bebb7792702Timo Sirainen }
6a1e4eb2c6a267bec1e8704ce9137bebb7792702Timo Sirainen
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen if (!skipped_mails)
8907d617ce7c4f390c0f42f6f694db2fecdd5775Timo Sirainen sync_ctx->mbox->mbox_sync_dirty = FALSE;
99be58a447b69d62cbd9e764000a06226b9c9c89Timo Sirainen
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen if (uids_broken && sync_ctx->delay_writes) {
39775ad03c459efe64cce924658da5094ba417e1Timo Sirainen /* once we get around to writing the changes, we'll need to do
343a527f805ca5cce78496b959d6def70e5d0cd4Timo Sirainen a full sync to avoid the "UIDs broken in partial sync"
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen error */
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen sync_ctx->mbox->mbox_sync_dirty = TRUE;
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen }
b225c3c65f360d7b833f09f9b2fb3035ed5ea600Timo Sirainen return 1;
b225c3c65f360d7b833f09f9b2fb3035ed5ea600Timo Sirainen}
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen
b225c3c65f360d7b833f09f9b2fb3035ed5ea600Timo Sirainenstatic int mbox_write_pseudo(struct mbox_sync_context *sync_ctx)
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen{
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen string_t *str;
b397a802ec245a9169dab6b62efa4f7f877c07f6Timo Sirainen unsigned int uid_validity;
b397a802ec245a9169dab6b62efa4f7f877c07f6Timo Sirainen
b397a802ec245a9169dab6b62efa4f7f877c07f6Timo Sirainen i_assert(sync_ctx->write_fd != -1);
b397a802ec245a9169dab6b62efa4f7f877c07f6Timo Sirainen
b397a802ec245a9169dab6b62efa4f7f877c07f6Timo Sirainen uid_validity = sync_ctx->base_uid_validity != 0 ?
b397a802ec245a9169dab6b62efa4f7f877c07f6Timo Sirainen sync_ctx->base_uid_validity : sync_ctx->hdr->uid_validity;
5edfc0f1c3c55e906d8316d9cdeaa3b0c7000c19Timo Sirainen i_assert(uid_validity != 0);
5edfc0f1c3c55e906d8316d9cdeaa3b0c7000c19Timo Sirainen
eb276c05bf6b0a383c772d61e31cf09a8dbd36c7Timo Sirainen str = t_str_new(1024);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen str_printfa(str, "%sDate: %s\n"
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen "From: Mail System Internal Data <MAILER-DAEMON@%s>\n"
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen "Subject: DON'T DELETE THIS MESSAGE -- FOLDER INTERNAL DATA"
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen "\nMessage-ID: <%s@%s>\n"
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen "X-IMAP: %u %010u\n"
b225c3c65f360d7b833f09f9b2fb3035ed5ea600Timo Sirainen "Status: RO\n"
b225c3c65f360d7b833f09f9b2fb3035ed5ea600Timo Sirainen "\n"
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen PSEUDO_MESSAGE_BODY
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen "\n",
9aa52288a4b53186d81b0ec9afa7d9e0a8ee8753Timo Sirainen mbox_from_create("MAILER_DAEMON", ioloop_time),
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen message_date_create(ioloop_time),
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen my_hostname, dec2str(ioloop_time), my_hostname,
f7d43647acc6dc80064c8c4cacf5bf86f754c530Timo Sirainen uid_validity, sync_ctx->next_uid-1);
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen if (pwrite_full(sync_ctx->write_fd,
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen str_data(str), str_len(str), 0) < 0) {
db7c9201c88e3d9bee10485194ee5b0c67249916Timo Sirainen if (!ENOSPACE(errno)) {
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen mbox_set_syscall_error(sync_ctx->mbox,
9aa52288a4b53186d81b0ec9afa7d9e0a8ee8753Timo Sirainen "pwrite_full()");
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen return -1;
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen }
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen
af6d4a24cb6d18e50d172540cf49b1448a6f9872Timo Sirainen /* out of disk space, truncate to empty */
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen if (ftruncate(sync_ctx->write_fd, 0) < 0)
b225c3c65f360d7b833f09f9b2fb3035ed5ea600Timo Sirainen mbox_set_syscall_error(sync_ctx->mbox, "ftruncate()");
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen }
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen
7d207b1e77a7b5e3fda640e353acfc86d261fedfTimo Sirainen sync_ctx->base_uid_last_offset = 0; /* don't bother calculating */
b225c3c65f360d7b833f09f9b2fb3035ed5ea600Timo Sirainen sync_ctx->base_uid_last = sync_ctx->next_uid-1;
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen return 0;
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen}
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic int mbox_sync_handle_eof_updates(struct mbox_sync_context *sync_ctx,
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainen struct mbox_sync_mail_context *mail_ctx)
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainen{
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainen const struct stat *st;
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainen uoff_t file_size, offset, padding, trailer_size;
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainen
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainen if (!istream_raw_mbox_is_eof(sync_ctx->input)) {
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainen i_assert(sync_ctx->need_space_seq == 0);
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainen i_assert(sync_ctx->expunged_space == 0);
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainen return 0;
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainen }
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainen
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainen st = i_stream_stat(sync_ctx->file_input, TRUE);
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainen if (st == NULL) {
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainen mbox_set_syscall_error(sync_ctx->mbox, "i_stream_stat()");
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainen return -1;
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainen }
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainen file_size = st->st_size;
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainen if (file_size < sync_ctx->file_input->v_offset) {
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainen mbox_sync_set_critical(sync_ctx,
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainen "file size unexpectedly shrinked in mbox file %s "
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainen "(%"PRIuUOFF_T" vs %"PRIuUOFF_T")",
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainen sync_ctx->mbox->path, file_size,
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainen sync_ctx->file_input->v_offset);
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainen return -1;
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainen }
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainen trailer_size = file_size - sync_ctx->file_input->v_offset;
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainen i_assert(trailer_size <= 2);
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainen
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainen if (sync_ctx->need_space_seq != 0) {
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainen i_assert(sync_ctx->write_fd != -1);
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainen
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainen i_assert(sync_ctx->space_diff < 0);
dc42ce2d44e84d9d05a9310c11f8764f319eb3abTimo Sirainen padding = MBOX_HEADER_PADDING *
dc42ce2d44e84d9d05a9310c11f8764f319eb3abTimo Sirainen (sync_ctx->seq - sync_ctx->need_space_seq + 1);
8166e8a706078efc71923719ca285e84902883c0Timo Sirainen sync_ctx->space_diff -= padding;
dc42ce2d44e84d9d05a9310c11f8764f319eb3abTimo Sirainen
06fc140d5f0b03524e63a15d45d1cdc8b691372cTimo Sirainen i_assert(sync_ctx->expunged_space <= -sync_ctx->space_diff);
06fc140d5f0b03524e63a15d45d1cdc8b691372cTimo Sirainen sync_ctx->space_diff += sync_ctx->expunged_space;
06fc140d5f0b03524e63a15d45d1cdc8b691372cTimo Sirainen sync_ctx->expunged_space = 0;
06fc140d5f0b03524e63a15d45d1cdc8b691372cTimo Sirainen
06fc140d5f0b03524e63a15d45d1cdc8b691372cTimo Sirainen if (mail_ctx->have_eoh && !mail_ctx->updated)
06fc140d5f0b03524e63a15d45d1cdc8b691372cTimo Sirainen str_append_c(mail_ctx->header, '\n');
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen
fcfd317f7eb1f0216764c75c5fab3555020552d4Timo Sirainen i_assert(sync_ctx->space_diff < 0);
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen
aa38d1a0945f0bc13a225d043f53fad2eec666b1Timo Sirainen if (file_set_size(sync_ctx->write_fd,
fcfd317f7eb1f0216764c75c5fab3555020552d4Timo Sirainen file_size + -sync_ctx->space_diff) < 0) {
9e59a1f3f095b3099478562cf3f3970a24736970Timo Sirainen mbox_set_syscall_error(sync_ctx->mbox,
178511b57faa7c3f8203dd8b7e4059d00cbfc23aTimo Sirainen "file_set_size()");
b3febb0933fdce10394d25093e23ce0a5aadddd3Timo Sirainen if (ftruncate(sync_ctx->write_fd, file_size) < 0) {
b3febb0933fdce10394d25093e23ce0a5aadddd3Timo Sirainen mbox_set_syscall_error(sync_ctx->mbox,
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen "ftruncate()");
fcfd317f7eb1f0216764c75c5fab3555020552d4Timo Sirainen }
aa38d1a0945f0bc13a225d043f53fad2eec666b1Timo Sirainen return -1;
fcfd317f7eb1f0216764c75c5fab3555020552d4Timo Sirainen }
fcfd317f7eb1f0216764c75c5fab3555020552d4Timo Sirainen mbox_sync_file_updated(sync_ctx, FALSE);
b225c3c65f360d7b833f09f9b2fb3035ed5ea600Timo Sirainen
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen if (mbox_sync_rewrite(sync_ctx, mail_ctx, file_size,
b225c3c65f360d7b833f09f9b2fb3035ed5ea600Timo Sirainen -sync_ctx->space_diff, padding,
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen sync_ctx->need_space_seq,
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen sync_ctx->seq) < 0)
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen return -1;
aa38d1a0945f0bc13a225d043f53fad2eec666b1Timo Sirainen
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen update_from_offsets(sync_ctx);
b225c3c65f360d7b833f09f9b2fb3035ed5ea600Timo Sirainen
b225c3c65f360d7b833f09f9b2fb3035ed5ea600Timo Sirainen sync_ctx->need_space_seq = 0;
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen array_clear(&sync_ctx->mails);
b225c3c65f360d7b833f09f9b2fb3035ed5ea600Timo Sirainen p_clear(sync_ctx->saved_keywords_pool);
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen }
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen if (sync_ctx->expunged_space > 0) {
aa38d1a0945f0bc13a225d043f53fad2eec666b1Timo Sirainen i_assert(sync_ctx->write_fd != -1);
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen mbox_sync_file_update_ext_modified(sync_ctx);
ed14c2db5436d6d89aa65bc8e09af0c316359553Timo Sirainen
ed14c2db5436d6d89aa65bc8e09af0c316359553Timo Sirainen /* copy trailer, then truncate the file */
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen file_size = sync_ctx->last_stat.st_size;
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen if (file_size == (uoff_t)sync_ctx->expunged_space) {
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen /* everything deleted, the trailer_size still contains
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen the \n trailer though */
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen trailer_size = 0;
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen }
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen i_assert(file_size >= sync_ctx->expunged_space + trailer_size);
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen offset = file_size - sync_ctx->expunged_space - trailer_size;
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen i_assert(offset == 0 || offset > 31);
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen if (mbox_move(sync_ctx, offset,
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen offset + sync_ctx->expunged_space,
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen trailer_size) < 0)
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen return -1;
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen if (ftruncate(sync_ctx->write_fd,
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen offset + trailer_size) < 0) {
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen mbox_set_syscall_error(sync_ctx->mbox, "ftruncate()");
39775ad03c459efe64cce924658da5094ba417e1Timo Sirainen return -1;
343a527f805ca5cce78496b959d6def70e5d0cd4Timo Sirainen }
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen
022412398e56a8f31ef111cfd7271498d64af9a9Timo Sirainen if (offset == 0) {
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen if (mbox_write_pseudo(sync_ctx) < 0)
734f2ff794505007ac7310af12e1adef5643c36eTimo Sirainen return -1;
c263e92d73889da530b308c9ab28b4b74031550eTimo Sirainen }
f6699a08521aacc4c2bb5b6175691dad5f715f8cTimo Sirainen
f6699a08521aacc4c2bb5b6175691dad5f715f8cTimo Sirainen sync_ctx->expunged_space = 0;
f6699a08521aacc4c2bb5b6175691dad5f715f8cTimo Sirainen mbox_sync_file_updated(sync_ctx, FALSE);
f6699a08521aacc4c2bb5b6175691dad5f715f8cTimo Sirainen }
28b30e9bd1a0a8b9019124abd821cb2989a03910Timo Sirainen return 0;
f6699a08521aacc4c2bb5b6175691dad5f715f8cTimo Sirainen}
f6699a08521aacc4c2bb5b6175691dad5f715f8cTimo Sirainen
f0569d9fbb25c8437760be69f194595a841ad711Timo Sirainenstatic int mbox_sync_update_index_header(struct mbox_sync_context *sync_ctx)
c263e92d73889da530b308c9ab28b4b74031550eTimo Sirainen{
aa38d1a0945f0bc13a225d043f53fad2eec666b1Timo Sirainen const struct stat *st;
a393d9d6dabdc46cf724f8cb004a652b4036d53dTimo Sirainen
c263e92d73889da530b308c9ab28b4b74031550eTimo Sirainen st = i_stream_stat(sync_ctx->file_input, FALSE);
c263e92d73889da530b308c9ab28b4b74031550eTimo Sirainen if (st == NULL) {
c263e92d73889da530b308c9ab28b4b74031550eTimo Sirainen mbox_set_syscall_error(sync_ctx->mbox, "i_stream_stat()");
c263e92d73889da530b308c9ab28b4b74031550eTimo Sirainen return -1;
325f4573edfa5b751832ac01023f3e81be992bf0Timo Sirainen }
9e59a1f3f095b3099478562cf3f3970a24736970Timo Sirainen
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen if (sync_ctx->moved_offsets &&
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen ((uint64_t)st->st_size == sync_ctx->hdr->sync_size ||
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen (uint64_t)st->st_size == sync_ctx->orig_size)) {
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen /* We moved messages inside the mbox file without changing
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen the file's size. If mtime doesn't change, another process
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen not using the same index file as us can't know that the file
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen was changed. So make sure the mtime changes. This should
b225c3c65f360d7b833f09f9b2fb3035ed5ea600Timo Sirainen happen rarely enough that the sleeping doesn't become a
a486ed03dce069ff60ab5a65d0ae24a1862f22fcTimo Sirainen performance problem.
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen Note that to do this perfectly safe we should do this wait
7d207b1e77a7b5e3fda640e353acfc86d261fedfTimo Sirainen whenever mails are moved or expunged, regardless of whether
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen the file's size changed. That however could become a
34d2ee1fa2b299267fcefd378f80690e7f601dfbTimo Sirainen performance problem and the consequences of being wrong are
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen quite minimal (an extra logged error message). */
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen while (sync_ctx->orig_mtime == st->st_mtime) {
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen usleep(500000);
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainen if (utime(sync_ctx->mbox->path, NULL) < 0) {
9d0e1fa945103f2875cdf6d18b2013809f566ea7Timo Sirainen mbox_set_syscall_error(sync_ctx->mbox,
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainen "utime()");
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen return -1;
6060b7c8edf8fce73470d0df6a2479b69b01c537Timo Sirainen }
6060b7c8edf8fce73470d0df6a2479b69b01c537Timo Sirainen
6060b7c8edf8fce73470d0df6a2479b69b01c537Timo Sirainen st = i_stream_stat(sync_ctx->file_input, FALSE);
6060b7c8edf8fce73470d0df6a2479b69b01c537Timo Sirainen if (st == NULL) {
6060b7c8edf8fce73470d0df6a2479b69b01c537Timo Sirainen mbox_set_syscall_error(sync_ctx->mbox,
6060b7c8edf8fce73470d0df6a2479b69b01c537Timo Sirainen "i_stream_stat()");
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen return -1;
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen }
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen }
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen }
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen /* only reason not to have UID validity at this point is if the file
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen is entirely empty. In that case just make up a new one if needed. */
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen i_assert(sync_ctx->base_uid_validity != 0 || st->st_size == 0);
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen if (sync_ctx->base_uid_validity != sync_ctx->hdr->uid_validity ||
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen sync_ctx->base_uid_validity == 0) {
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen if (sync_ctx->base_uid_validity == 0) {
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen sync_ctx->base_uid_validity =
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen sync_ctx->hdr->uid_validity != 0 ?
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen sync_ctx->hdr->uid_validity :
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen (unsigned int)ioloop_time;
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen }
345212e8f61ebf14ff4f80df26df9e655eb5121eTimo Sirainen
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen mail_index_update_header(sync_ctx->t,
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen offsetof(struct mail_index_header, uid_validity),
a486ed03dce069ff60ab5a65d0ae24a1862f22fcTimo Sirainen &sync_ctx->base_uid_validity,
a486ed03dce069ff60ab5a65d0ae24a1862f22fcTimo Sirainen sizeof(sync_ctx->base_uid_validity), TRUE);
a486ed03dce069ff60ab5a65d0ae24a1862f22fcTimo Sirainen }
a486ed03dce069ff60ab5a65d0ae24a1862f22fcTimo Sirainen
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen if (istream_raw_mbox_is_eof(sync_ctx->input) &&
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen sync_ctx->next_uid != sync_ctx->hdr->next_uid) {
b3febb0933fdce10394d25093e23ce0a5aadddd3Timo Sirainen i_assert(sync_ctx->next_uid != 0);
a486ed03dce069ff60ab5a65d0ae24a1862f22fcTimo Sirainen mail_index_update_header(sync_ctx->t,
a486ed03dce069ff60ab5a65d0ae24a1862f22fcTimo Sirainen offsetof(struct mail_index_header, next_uid),
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen &sync_ctx->next_uid, sizeof(sync_ctx->next_uid), FALSE);
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen }
a486ed03dce069ff60ab5a65d0ae24a1862f22fcTimo Sirainen
9aa52288a4b53186d81b0ec9afa7d9e0a8ee8753Timo Sirainen if ((uint32_t)st->st_mtime != sync_ctx->hdr->sync_stamp &&
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen !sync_ctx->mbox->mbox_sync_dirty) {
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen uint32_t sync_stamp = st->st_mtime;
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen mail_index_update_header(sync_ctx->t,
db7c9201c88e3d9bee10485194ee5b0c67249916Timo Sirainen offsetof(struct mail_index_header, sync_stamp),
db7c9201c88e3d9bee10485194ee5b0c67249916Timo Sirainen &sync_stamp, sizeof(sync_stamp), TRUE);
db7c9201c88e3d9bee10485194ee5b0c67249916Timo Sirainen }
022412398e56a8f31ef111cfd7271498d64af9a9Timo Sirainen
db7c9201c88e3d9bee10485194ee5b0c67249916Timo Sirainen if ((uint64_t)st->st_size != sync_ctx->hdr->sync_size &&
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen !sync_ctx->mbox->mbox_sync_dirty) {
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen uint64_t sync_size = st->st_size;
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen mail_index_update_header(sync_ctx->t,
9aa52288a4b53186d81b0ec9afa7d9e0a8ee8753Timo Sirainen offsetof(struct mail_index_header, sync_size),
9aa52288a4b53186d81b0ec9afa7d9e0a8ee8753Timo Sirainen &sync_size, sizeof(sync_size), TRUE);
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen }
32d9a75612a5df455e4169b56538bb31dfe359e4Timo Sirainen
32d9a75612a5df455e4169b56538bb31dfe359e4Timo Sirainen sync_ctx->mbox->mbox_dirty_stamp = st->st_mtime;
32d9a75612a5df455e4169b56538bb31dfe359e4Timo Sirainen sync_ctx->mbox->mbox_dirty_size = st->st_size;
32d9a75612a5df455e4169b56538bb31dfe359e4Timo Sirainen
32d9a75612a5df455e4169b56538bb31dfe359e4Timo Sirainen return 0;
32d9a75612a5df455e4169b56538bb31dfe359e4Timo Sirainen}
32d9a75612a5df455e4169b56538bb31dfe359e4Timo Sirainen
32d9a75612a5df455e4169b56538bb31dfe359e4Timo Sirainenstatic void mbox_sync_restart(struct mbox_sync_context *sync_ctx)
0b17b95357d5d73d941f1eb4ca9fc543bc510e42Timo Sirainen{
0b17b95357d5d73d941f1eb4ca9fc543bc510e42Timo Sirainen sync_ctx->base_uid_validity = 0;
04ab375449dd97eed50ada88dd0df2abab01cfeeTimo Sirainen sync_ctx->base_uid_last = 0;
04ab375449dd97eed50ada88dd0df2abab01cfeeTimo Sirainen sync_ctx->base_uid_last_offset = 0;
0b17b95357d5d73d941f1eb4ca9fc543bc510e42Timo Sirainen
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen array_clear(&sync_ctx->mails);
04ab375449dd97eed50ada88dd0df2abab01cfeeTimo Sirainen array_clear(&sync_ctx->syncs);
7d207b1e77a7b5e3fda640e353acfc86d261fedfTimo Sirainen p_clear(sync_ctx->saved_keywords_pool);
04ab375449dd97eed50ada88dd0df2abab01cfeeTimo Sirainen
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen memset(&sync_ctx->sync_rec, 0, sizeof(sync_ctx->sync_rec));
04ab375449dd97eed50ada88dd0df2abab01cfeeTimo Sirainen mail_index_sync_reset(sync_ctx->index_sync_ctx);
04ab375449dd97eed50ada88dd0df2abab01cfeeTimo Sirainen mail_index_transaction_reset(sync_ctx->t);
04ab375449dd97eed50ada88dd0df2abab01cfeeTimo Sirainen
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen sync_ctx->prev_msg_uid = 0;
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen sync_ctx->next_uid = sync_ctx->hdr->next_uid;
04ab375449dd97eed50ada88dd0df2abab01cfeeTimo Sirainen sync_ctx->idx_next_uid = sync_ctx->hdr->next_uid;
04ab375449dd97eed50ada88dd0df2abab01cfeeTimo Sirainen sync_ctx->seq = 0;
04ab375449dd97eed50ada88dd0df2abab01cfeeTimo Sirainen sync_ctx->idx_seq = 1;
0b17b95357d5d73d941f1eb4ca9fc543bc510e42Timo Sirainen sync_ctx->need_space_seq = 0;
32d9a75612a5df455e4169b56538bb31dfe359e4Timo Sirainen sync_ctx->expunged_space = 0;
0b17b95357d5d73d941f1eb4ca9fc543bc510e42Timo Sirainen sync_ctx->space_diff = 0;
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen
04ab375449dd97eed50ada88dd0df2abab01cfeeTimo Sirainen sync_ctx->dest_first_mail = TRUE;
04ab375449dd97eed50ada88dd0df2abab01cfeeTimo Sirainen sync_ctx->ext_modified = FALSE;
04ab375449dd97eed50ada88dd0df2abab01cfeeTimo Sirainen}
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainenstatic int mbox_sync_do(struct mbox_sync_context *sync_ctx,
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen enum mbox_sync_flags flags)
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen{
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen struct mbox_sync_mail_context mail_ctx;
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainen const struct stat *st;
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainen unsigned int i;
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen int ret, partial;
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainen
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen st = i_stream_stat(sync_ctx->file_input, FALSE);
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen if (st == NULL) {
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen mbox_set_syscall_error(sync_ctx->mbox, "i_stream_stat()");
0b17b95357d5d73d941f1eb4ca9fc543bc510e42Timo Sirainen return -1;
0b17b95357d5d73d941f1eb4ca9fc543bc510e42Timo Sirainen }
ccb70ccfd9a25e490aab46d15d9b8323ad9ea3bfTimo Sirainen sync_ctx->last_stat = *st;
ccb70ccfd9a25e490aab46d15d9b8323ad9ea3bfTimo Sirainen sync_ctx->orig_size = st->st_size;
022412398e56a8f31ef111cfd7271498d64af9a9Timo Sirainen sync_ctx->orig_mtime = st->st_mtime;
ccb70ccfd9a25e490aab46d15d9b8323ad9ea3bfTimo Sirainen
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen if ((flags & MBOX_SYNC_FORCE_SYNC) != 0) {
648d24583c1574441c4fa0331a90bd4d6e7996c5Timo Sirainen /* forcing a full sync. assume file has changed. */
648d24583c1574441c4fa0331a90bd4d6e7996c5Timo Sirainen partial = FALSE;
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen sync_ctx->mbox->mbox_sync_dirty = TRUE;
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen } else if ((uint32_t)st->st_mtime == sync_ctx->hdr->sync_stamp &&
ccb70ccfd9a25e490aab46d15d9b8323ad9ea3bfTimo Sirainen (uint64_t)st->st_size == sync_ctx->hdr->sync_size) {
ccb70ccfd9a25e490aab46d15d9b8323ad9ea3bfTimo Sirainen /* file is fully synced */
ccb70ccfd9a25e490aab46d15d9b8323ad9ea3bfTimo Sirainen partial = TRUE;
4b41116563110d00330896a568eff1078c382827Timo Sirainen sync_ctx->mbox->mbox_sync_dirty = FALSE;
9aa52288a4b53186d81b0ec9afa7d9e0a8ee8753Timo Sirainen } else if ((flags & MBOX_SYNC_UNDIRTY) != 0 ||
9aa52288a4b53186d81b0ec9afa7d9e0a8ee8753Timo Sirainen (uint64_t)st->st_size == sync_ctx->hdr->sync_size) {
0b17b95357d5d73d941f1eb4ca9fc543bc510e42Timo Sirainen /* we want to do full syncing. always do this if
aa0647f2debf0d48d504a321186f66c85596aaf4Timo Sirainen file size hasn't changed but timestamp has. it most
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen likely means that someone had modified some header
2d39dc1a453546892109b35c0d9770369011a13dTimo Sirainen and we probably want to know about it */
63a61b7a739ae0f3f520215137d9c50f94d0f34fTimo Sirainen partial = FALSE;
dcd50ecbfe796bd76f2d63483c534cc0e4e94164Timo Sirainen sync_ctx->mbox->mbox_sync_dirty = TRUE;
dcd50ecbfe796bd76f2d63483c534cc0e4e94164Timo Sirainen } else {
ccb70ccfd9a25e490aab46d15d9b8323ad9ea3bfTimo Sirainen /* see if we can delay syncing the whole file.
7c6ad06e2bed011e3c0425bf1ceb5df3dc33b638Timo Sirainen normally we only notice expunges and appends
ccb70ccfd9a25e490aab46d15d9b8323ad9ea3bfTimo Sirainen in partial syncing. */
ccb70ccfd9a25e490aab46d15d9b8323ad9ea3bfTimo Sirainen partial = TRUE;
9aa52288a4b53186d81b0ec9afa7d9e0a8ee8753Timo Sirainen sync_ctx->mbox->mbox_sync_dirty = TRUE;
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen }
d252f81a2ff1bdd5439f9d2b3df715b70a4bcd3dTimo Sirainen
d252f81a2ff1bdd5439f9d2b3df715b70a4bcd3dTimo Sirainen mbox_sync_restart(sync_ctx);
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen for (i = 0; i < 3; i++) {
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen ret = mbox_sync_loop(sync_ctx, &mail_ctx, partial);
63a61b7a739ae0f3f520215137d9c50f94d0f34fTimo Sirainen if (ret > 0)
63a61b7a739ae0f3f520215137d9c50f94d0f34fTimo Sirainen break;
63a61b7a739ae0f3f520215137d9c50f94d0f34fTimo Sirainen if (ret < 0)
6060b7c8edf8fce73470d0df6a2479b69b01c537Timo Sirainen return -1;
6060b7c8edf8fce73470d0df6a2479b69b01c537Timo Sirainen
d9de52132072d80b8c268094b879c0ef5a108db3Timo Sirainen /* partial syncing didn't work, do it again. we get here
345212e8f61ebf14ff4f80df26df9e655eb5121eTimo Sirainen also if we ran out of UIDs. */
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen i_assert(sync_ctx->mbox->mbox_sync_dirty);
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen mbox_sync_restart(sync_ctx);
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen partial = FALSE;
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen }
345212e8f61ebf14ff4f80df26df9e655eb5121eTimo Sirainen
345212e8f61ebf14ff4f80df26df9e655eb5121eTimo Sirainen if (mbox_sync_handle_eof_updates(sync_ctx, &mail_ctx) < 0)
63a61b7a739ae0f3f520215137d9c50f94d0f34fTimo Sirainen return -1;
345212e8f61ebf14ff4f80df26df9e655eb5121eTimo Sirainen
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen /* only syncs left should be just appends (and their updates)
d9de52132072d80b8c268094b879c0ef5a108db3Timo Sirainen which weren't synced yet for some reason (crash). we'll just
d9de52132072d80b8c268094b879c0ef5a108db3Timo Sirainen ignore them, as we've overwritten them above. */
d9de52132072d80b8c268094b879c0ef5a108db3Timo Sirainen array_clear(&sync_ctx->syncs);
d9de52132072d80b8c268094b879c0ef5a108db3Timo Sirainen memset(&sync_ctx->sync_rec, 0, sizeof(sync_ctx->sync_rec));
d9de52132072d80b8c268094b879c0ef5a108db3Timo Sirainen
d9de52132072d80b8c268094b879c0ef5a108db3Timo Sirainen if (mbox_sync_update_index_header(sync_ctx) < 0)
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen return -1;
d9de52132072d80b8c268094b879c0ef5a108db3Timo Sirainen
d9de52132072d80b8c268094b879c0ef5a108db3Timo Sirainen return 0;
d9de52132072d80b8c268094b879c0ef5a108db3Timo Sirainen}
f2b79667fc7a8f7c2c72cad18bd71d49730e36f6Timo Sirainen
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainenint mbox_sync_has_changed(struct mbox_mailbox *mbox, bool leave_dirty)
f2b79667fc7a8f7c2c72cad18bd71d49730e36f6Timo Sirainen{
7e0bb2b365cc38645313a3513fa79bd32d34b63cTimo Sirainen const struct mail_index_header *hdr;
d9de52132072d80b8c268094b879c0ef5a108db3Timo Sirainen const struct stat *st;
d9de52132072d80b8c268094b879c0ef5a108db3Timo Sirainen struct stat statbuf;
d9de52132072d80b8c268094b879c0ef5a108db3Timo Sirainen
d9de52132072d80b8c268094b879c0ef5a108db3Timo Sirainen if (mbox->mbox_file_stream != NULL && mbox->mbox_fd == -1) {
d9de52132072d80b8c268094b879c0ef5a108db3Timo Sirainen /* read-only stream */
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen st = i_stream_stat(mbox->mbox_file_stream, FALSE);
30975737820f5855e2c26d81b574ae5f03a05407Timo Sirainen if (st == NULL) {
30975737820f5855e2c26d81b574ae5f03a05407Timo Sirainen mbox_set_syscall_error(mbox, "i_stream_stat()");
30975737820f5855e2c26d81b574ae5f03a05407Timo Sirainen return -1;
30975737820f5855e2c26d81b574ae5f03a05407Timo Sirainen }
30975737820f5855e2c26d81b574ae5f03a05407Timo Sirainen } else {
30975737820f5855e2c26d81b574ae5f03a05407Timo Sirainen if (stat(mbox->path, &statbuf) < 0) {
30975737820f5855e2c26d81b574ae5f03a05407Timo Sirainen mbox_set_syscall_error(mbox, "stat()");
30975737820f5855e2c26d81b574ae5f03a05407Timo Sirainen return -1;
30975737820f5855e2c26d81b574ae5f03a05407Timo Sirainen }
30975737820f5855e2c26d81b574ae5f03a05407Timo Sirainen st = &statbuf;
30975737820f5855e2c26d81b574ae5f03a05407Timo Sirainen }
d9de52132072d80b8c268094b879c0ef5a108db3Timo Sirainen
d9de52132072d80b8c268094b879c0ef5a108db3Timo Sirainen hdr = mail_index_get_header(mbox->ibox.view);
2d39dc1a453546892109b35c0d9770369011a13dTimo Sirainen
2d39dc1a453546892109b35c0d9770369011a13dTimo Sirainen if ((uint32_t)st->st_mtime == hdr->sync_stamp &&
2d39dc1a453546892109b35c0d9770369011a13dTimo Sirainen (uint64_t)st->st_size == hdr->sync_size) {
2d39dc1a453546892109b35c0d9770369011a13dTimo Sirainen /* fully synced */
2d39dc1a453546892109b35c0d9770369011a13dTimo Sirainen mbox->mbox_sync_dirty = FALSE;
8f8315e4b4e27ead12dd1c3da65bf4dee3762f18Timo Sirainen return 0;
8f8315e4b4e27ead12dd1c3da65bf4dee3762f18Timo Sirainen }
8f8315e4b4e27ead12dd1c3da65bf4dee3762f18Timo Sirainen
8f8315e4b4e27ead12dd1c3da65bf4dee3762f18Timo Sirainen if (!mbox->mbox_sync_dirty || !leave_dirty) {
8f8315e4b4e27ead12dd1c3da65bf4dee3762f18Timo Sirainen mbox->mbox_sync_dirty = TRUE;
8f8315e4b4e27ead12dd1c3da65bf4dee3762f18Timo Sirainen return 1;
8f8315e4b4e27ead12dd1c3da65bf4dee3762f18Timo Sirainen }
8f8315e4b4e27ead12dd1c3da65bf4dee3762f18Timo Sirainen
d6a1fa1d65c6d1996937802c2482c0f14dd821a7Timo Sirainen return st->st_mtime != mbox->mbox_dirty_stamp ||
8f8315e4b4e27ead12dd1c3da65bf4dee3762f18Timo Sirainen st->st_size != mbox->mbox_dirty_size;
8f8315e4b4e27ead12dd1c3da65bf4dee3762f18Timo Sirainen}
0b17b95357d5d73d941f1eb4ca9fc543bc510e42Timo Sirainen
0b17b95357d5d73d941f1eb4ca9fc543bc510e42Timo Sirainenstatic void mbox_sync_context_free(struct mbox_sync_context *sync_ctx)
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen{
d9de52132072d80b8c268094b879c0ef5a108db3Timo Sirainen if (sync_ctx->index_sync_ctx != NULL)
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen mail_index_sync_rollback(&sync_ctx->index_sync_ctx);
9aa52288a4b53186d81b0ec9afa7d9e0a8ee8753Timo Sirainen pool_unref(sync_ctx->mail_keyword_pool);
0b17b95357d5d73d941f1eb4ca9fc543bc510e42Timo Sirainen pool_unref(sync_ctx->saved_keywords_pool);
9aa52288a4b53186d81b0ec9afa7d9e0a8ee8753Timo Sirainen str_free(&sync_ctx->header);
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen str_free(&sync_ctx->from_line);
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen array_free(&sync_ctx->mails);
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen array_free(&sync_ctx->syncs);
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen}
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainenstatic int mbox_sync_int(struct mbox_mailbox *mbox, enum mbox_sync_flags flags)
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen{
88553367d677170a4b703b9d52aac9eabf91c656Timo Sirainen struct mail_index_sync_ctx *index_sync_ctx;
d9de52132072d80b8c268094b879c0ef5a108db3Timo Sirainen struct mail_index_view *sync_view;
7e0bb2b365cc38645313a3513fa79bd32d34b63cTimo Sirainen struct mail_index_transaction *trans;
d9de52132072d80b8c268094b879c0ef5a108db3Timo Sirainen struct mbox_sync_context sync_ctx;
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen uint32_t seq;
4d10cf8c7879ccd377e7fb136913b2a258ba8d93Timo Sirainen uoff_t offset;
4d10cf8c7879ccd377e7fb136913b2a258ba8d93Timo Sirainen unsigned int lock_id = 0;
4d10cf8c7879ccd377e7fb136913b2a258ba8d93Timo Sirainen int ret, changed;
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen bool delay_writes;
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen
4d10cf8c7879ccd377e7fb136913b2a258ba8d93Timo Sirainen delay_writes = mbox->mbox_readonly ||
4d10cf8c7879ccd377e7fb136913b2a258ba8d93Timo Sirainen ((flags & MBOX_SYNC_REWRITE) == 0 &&
88553367d677170a4b703b9d52aac9eabf91c656Timo Sirainen getenv("MBOX_LAZY_WRITES") != NULL);
88553367d677170a4b703b9d52aac9eabf91c656Timo Sirainen
88553367d677170a4b703b9d52aac9eabf91c656Timo Sirainen mbox->ibox.sync_last_check = ioloop_time;
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen if (!mbox->mbox_do_dirty_syncs)
de4288b7369945a31c4001add9445fd0195a358dTimo Sirainen flags |= MBOX_SYNC_UNDIRTY;
b20fb5b1df9d604a7541f5118fc5b4b466d211efTimo Sirainen
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen if ((flags & MBOX_SYNC_LOCK_READING) != 0) {
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen if (mbox_lock(mbox, F_RDLCK, &lock_id) <= 0)
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen return -1;
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen }
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen
aa0647f2debf0d48d504a321186f66c85596aaf4Timo Sirainen if ((flags & MBOX_SYNC_HEADER) != 0 ||
2e937ed8585299b2e879a28314902a5f644813d2Timo Sirainen (flags & MBOX_SYNC_FORCE_SYNC) != 0)
2e937ed8585299b2e879a28314902a5f644813d2Timo Sirainen changed = 1;
343a527f805ca5cce78496b959d6def70e5d0cd4Timo Sirainen else {
343a527f805ca5cce78496b959d6def70e5d0cd4Timo Sirainen bool leave_dirty = (flags & MBOX_SYNC_UNDIRTY) == 0;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if ((changed = mbox_sync_has_changed(mbox, leave_dirty)) < 0) {
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if ((flags & MBOX_SYNC_LOCK_READING) != 0)
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen (void)mbox_unlock(mbox, lock_id);
33ae95df45c9b5ec51332a6b39eb5322038686b9Timo Sirainen return -1;
91dca97b367c54a139c268b56a0c67f564bd9197Timo Sirainen }
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen }
ccb70ccfd9a25e490aab46d15d9b8323ad9ea3bfTimo Sirainen
ccb70ccfd9a25e490aab46d15d9b8323ad9ea3bfTimo Sirainen if ((flags & MBOX_SYNC_LOCK_READING) != 0) {
ccb70ccfd9a25e490aab46d15d9b8323ad9ea3bfTimo Sirainen /* we just want to lock it for reading. if mbox hasn't been
022412398e56a8f31ef111cfd7271498d64af9a9Timo Sirainen modified don't do any syncing. */
022412398e56a8f31ef111cfd7271498d64af9a9Timo Sirainen if (!changed)
022412398e56a8f31ef111cfd7271498d64af9a9Timo Sirainen return 0;
022412398e56a8f31ef111cfd7271498d64af9a9Timo Sirainen
022412398e56a8f31ef111cfd7271498d64af9a9Timo Sirainen /* have to sync to make sure offsets have stayed the same */
ccb70ccfd9a25e490aab46d15d9b8323ad9ea3bfTimo Sirainen (void)mbox_unlock(mbox, lock_id);
ccb70ccfd9a25e490aab46d15d9b8323ad9ea3bfTimo Sirainen lock_id = 0;
ccb70ccfd9a25e490aab46d15d9b8323ad9ea3bfTimo Sirainen }
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen
022412398e56a8f31ef111cfd7271498d64af9a9Timo Sirainen /* reopen input stream to make sure it has nothing buffered */
ccb70ccfd9a25e490aab46d15d9b8323ad9ea3bfTimo Sirainen mbox_file_close_stream(mbox);
b08b33d1f5ce3721dc2d83586c9cb0ca141331fdTimo Sirainen
30975737820f5855e2c26d81b574ae5f03a05407Timo Sirainen__again:
30975737820f5855e2c26d81b574ae5f03a05407Timo Sirainen if (changed) {
022412398e56a8f31ef111cfd7271498d64af9a9Timo Sirainen /* we're most likely modifying the mbox while syncing, just
ccb70ccfd9a25e490aab46d15d9b8323ad9ea3bfTimo Sirainen lock it for writing immediately. the mbox must be locked
ccb70ccfd9a25e490aab46d15d9b8323ad9ea3bfTimo Sirainen before index syncing is started to avoid deadlocks, so we
7e0bb2b365cc38645313a3513fa79bd32d34b63cTimo Sirainen don't have much choice either (well, easy ones anyway). */
ccb70ccfd9a25e490aab46d15d9b8323ad9ea3bfTimo Sirainen int lock_type = mbox->mbox_readonly ? F_RDLCK : F_WRLCK;
ccb70ccfd9a25e490aab46d15d9b8323ad9ea3bfTimo Sirainen if (mbox_lock(mbox, lock_type, &lock_id) <= 0)
ccb70ccfd9a25e490aab46d15d9b8323ad9ea3bfTimo Sirainen return -1;
ccb70ccfd9a25e490aab46d15d9b8323ad9ea3bfTimo Sirainen }
ccb70ccfd9a25e490aab46d15d9b8323ad9ea3bfTimo Sirainen
ccb70ccfd9a25e490aab46d15d9b8323ad9ea3bfTimo Sirainen if ((flags & MBOX_SYNC_LAST_COMMIT) != 0) {
ccb70ccfd9a25e490aab46d15d9b8323ad9ea3bfTimo Sirainen seq = mbox->ibox.commit_log_file_seq;
ccb70ccfd9a25e490aab46d15d9b8323ad9ea3bfTimo Sirainen offset = mbox->ibox.commit_log_file_offset;
7e0bb2b365cc38645313a3513fa79bd32d34b63cTimo Sirainen } else {
ccb70ccfd9a25e490aab46d15d9b8323ad9ea3bfTimo Sirainen seq = (uint32_t)-1;
ccb70ccfd9a25e490aab46d15d9b8323ad9ea3bfTimo Sirainen offset = (uoff_t)-1;
ccb70ccfd9a25e490aab46d15d9b8323ad9ea3bfTimo Sirainen }
ccb70ccfd9a25e490aab46d15d9b8323ad9ea3bfTimo Sirainen
ccb70ccfd9a25e490aab46d15d9b8323ad9ea3bfTimo Sirainen ret = mail_index_sync_begin(mbox->ibox.index, &index_sync_ctx,
ccb70ccfd9a25e490aab46d15d9b8323ad9ea3bfTimo Sirainen &sync_view, &trans, seq, offset,
ccb70ccfd9a25e490aab46d15d9b8323ad9ea3bfTimo Sirainen !mbox->ibox.keep_recent,
ccb70ccfd9a25e490aab46d15d9b8323ad9ea3bfTimo Sirainen (flags & MBOX_SYNC_REWRITE) != 0);
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen if (ret <= 0) {
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen if (ret < 0)
d6e2b2761c8e0b6923c883fb2ead2665ee954be5Timo Sirainen mail_storage_set_index_error(&mbox->ibox);
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen if (lock_id != 0)
7a6b45405fb1544ac476e6eb1402a70cc1ddcdcfTimo Sirainen (void)mbox_unlock(mbox, lock_id);
de4288b7369945a31c4001add9445fd0195a358dTimo Sirainen return ret;
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen }
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen
aa0647f2debf0d48d504a321186f66c85596aaf4Timo Sirainen if (!changed && !mail_index_sync_have_more(index_sync_ctx)) {
aa0647f2debf0d48d504a321186f66c85596aaf4Timo Sirainen /* nothing to do */
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen __nothing_to_do:
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen if (lock_id != 0)
36e2fa21c22452470c1509cc63de20f7415c7b5eTimo Sirainen (void)mbox_unlock(mbox, lock_id);
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen /* index may need to do internal syncing though, so commit
9aa52288a4b53186d81b0ec9afa7d9e0a8ee8753Timo Sirainen instead of rollbacking. */
b3febb0933fdce10394d25093e23ce0a5aadddd3Timo Sirainen if (mail_index_sync_commit(&index_sync_ctx) < 0) {
ccb70ccfd9a25e490aab46d15d9b8323ad9ea3bfTimo Sirainen mail_storage_set_index_error(&mbox->ibox);
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen return -1;
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen }
59ef34eafaf74d31ed88af444b22d1a0738a30aaTimo Sirainen return 0;
59ef34eafaf74d31ed88af444b22d1a0738a30aaTimo Sirainen }
59ef34eafaf74d31ed88af444b22d1a0738a30aaTimo Sirainen
59ef34eafaf74d31ed88af444b22d1a0738a30aaTimo Sirainen memset(&sync_ctx, 0, sizeof(sync_ctx));
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen sync_ctx.mbox = mbox;
b3febb0933fdce10394d25093e23ce0a5aadddd3Timo Sirainen
b3febb0933fdce10394d25093e23ce0a5aadddd3Timo Sirainen sync_ctx.hdr = mail_index_get_header(sync_view);
bfea67960b2c2b479f9869b7df9a0d179370ce5fTimo Sirainen sync_ctx.from_line = str_new(default_pool, 256);
bfea67960b2c2b479f9869b7df9a0d179370ce5fTimo Sirainen sync_ctx.header = str_new(default_pool, 4096);
9d0e1fa945103f2875cdf6d18b2013809f566ea7Timo Sirainen
9d0e1fa945103f2875cdf6d18b2013809f566ea7Timo Sirainen sync_ctx.index_sync_ctx = index_sync_ctx;
9d0e1fa945103f2875cdf6d18b2013809f566ea7Timo Sirainen sync_ctx.sync_view = sync_view;
9d0e1fa945103f2875cdf6d18b2013809f566ea7Timo Sirainen sync_ctx.t = trans;
9d0e1fa945103f2875cdf6d18b2013809f566ea7Timo Sirainen sync_ctx.mail_keyword_pool =
9d0e1fa945103f2875cdf6d18b2013809f566ea7Timo Sirainen pool_alloconly_create("mbox keywords", 256);
9d0e1fa945103f2875cdf6d18b2013809f566ea7Timo Sirainen sync_ctx.saved_keywords_pool =
9d0e1fa945103f2875cdf6d18b2013809f566ea7Timo Sirainen pool_alloconly_create("mbox saved keywords", 4096);
9d0e1fa945103f2875cdf6d18b2013809f566ea7Timo Sirainen
9d0e1fa945103f2875cdf6d18b2013809f566ea7Timo Sirainen /* make sure we've read the latest keywords in index */
9d0e1fa945103f2875cdf6d18b2013809f566ea7Timo Sirainen (void)mail_index_get_keywords(mbox->ibox.index);
9d0e1fa945103f2875cdf6d18b2013809f566ea7Timo Sirainen
9d0e1fa945103f2875cdf6d18b2013809f566ea7Timo Sirainen i_array_init(&sync_ctx.mails, 64);
9d0e1fa945103f2875cdf6d18b2013809f566ea7Timo Sirainen i_array_init(&sync_ctx.syncs, 32);
34d2ee1fa2b299267fcefd378f80690e7f601dfbTimo Sirainen
34d2ee1fa2b299267fcefd378f80690e7f601dfbTimo Sirainen sync_ctx.flags = flags;
181c1aff950e6f8e0556f8974e79d0747845ac0fTimo Sirainen sync_ctx.delay_writes = delay_writes || sync_ctx.mbox->mbox_readonly;
181c1aff950e6f8e0556f8974e79d0747845ac0fTimo Sirainen
181c1aff950e6f8e0556f8974e79d0747845ac0fTimo Sirainen if (!changed && delay_writes) {
181c1aff950e6f8e0556f8974e79d0747845ac0fTimo Sirainen /* if we have only flag changes, we don't need to open the
181c1aff950e6f8e0556f8974e79d0747845ac0fTimo Sirainen mbox file */
181c1aff950e6f8e0556f8974e79d0747845ac0fTimo Sirainen bool expunged;
181c1aff950e6f8e0556f8974e79d0747845ac0fTimo Sirainen
181c1aff950e6f8e0556f8974e79d0747845ac0fTimo Sirainen if (mbox_sync_read_index_syncs(&sync_ctx, 1, &expunged) < 0)
34d2ee1fa2b299267fcefd378f80690e7f601dfbTimo Sirainen return -1;
2c7ab05ef98c46eb70c8ba6ea85e49749aafb2a3Timo Sirainen if (sync_ctx.sync_rec.uid1 == 0) {
d9de52132072d80b8c268094b879c0ef5a108db3Timo Sirainen sync_ctx.index_sync_ctx = NULL;
2c7ab05ef98c46eb70c8ba6ea85e49749aafb2a3Timo Sirainen mbox_sync_context_free(&sync_ctx);
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen goto __nothing_to_do;
2c7ab05ef98c46eb70c8ba6ea85e49749aafb2a3Timo Sirainen }
2c7ab05ef98c46eb70c8ba6ea85e49749aafb2a3Timo Sirainen }
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen
2c7ab05ef98c46eb70c8ba6ea85e49749aafb2a3Timo Sirainen if (lock_id == 0) {
d9de52132072d80b8c268094b879c0ef5a108db3Timo Sirainen /* ok, we have something to do but no locks. we'll have to
2c7ab05ef98c46eb70c8ba6ea85e49749aafb2a3Timo Sirainen restart syncing to avoid deadlocking. */
2c7ab05ef98c46eb70c8ba6ea85e49749aafb2a3Timo Sirainen mbox_sync_context_free(&sync_ctx);
2c7ab05ef98c46eb70c8ba6ea85e49749aafb2a3Timo Sirainen changed = 1;
34d2ee1fa2b299267fcefd378f80690e7f601dfbTimo Sirainen goto __again;
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen }
9aa52288a4b53186d81b0ec9afa7d9e0a8ee8753Timo Sirainen
9aa52288a4b53186d81b0ec9afa7d9e0a8ee8753Timo Sirainen if (mbox_file_open_stream(mbox) < 0) {
9aa52288a4b53186d81b0ec9afa7d9e0a8ee8753Timo Sirainen mbox_sync_context_free(&sync_ctx);
ccb70ccfd9a25e490aab46d15d9b8323ad9ea3bfTimo Sirainen (void)mbox_unlock(mbox, lock_id);
9aa52288a4b53186d81b0ec9afa7d9e0a8ee8753Timo Sirainen return -1;
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
4b41116563110d00330896a568eff1078c382827Timo Sirainen sync_ctx.file_input = sync_ctx.mbox->mbox_file_stream;
4b41116563110d00330896a568eff1078c382827Timo Sirainen sync_ctx.input = sync_ctx.mbox->mbox_stream;
4b41116563110d00330896a568eff1078c382827Timo Sirainen sync_ctx.write_fd = sync_ctx.mbox->mbox_lock_type != F_WRLCK ? -1 :
4b41116563110d00330896a568eff1078c382827Timo Sirainen sync_ctx.mbox->mbox_fd;
4b41116563110d00330896a568eff1078c382827Timo Sirainen
4b41116563110d00330896a568eff1078c382827Timo Sirainen ret = mbox_sync_do(&sync_ctx, flags);
4b41116563110d00330896a568eff1078c382827Timo Sirainen
4b41116563110d00330896a568eff1078c382827Timo Sirainen if (ret < 0)
4b41116563110d00330896a568eff1078c382827Timo Sirainen mail_index_sync_rollback(&index_sync_ctx);
4b41116563110d00330896a568eff1078c382827Timo Sirainen else if (mail_index_sync_commit(&index_sync_ctx) < 0) {
4b41116563110d00330896a568eff1078c382827Timo Sirainen mail_storage_set_index_error(&mbox->ibox);
4b41116563110d00330896a568eff1078c382827Timo Sirainen ret = -1;
4b41116563110d00330896a568eff1078c382827Timo Sirainen } else {
d756ebcfa96bd7cff02097c8f26df9df368b81b1Timo Sirainen mbox->ibox.commit_log_file_seq = 0;
d756ebcfa96bd7cff02097c8f26df9df368b81b1Timo Sirainen mbox->ibox.commit_log_file_offset = 0;
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen }
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen sync_ctx.t = NULL;
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen sync_ctx.index_sync_ctx = NULL;
d756ebcfa96bd7cff02097c8f26df9df368b81b1Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (sync_ctx.base_uid_last != sync_ctx.next_uid-1 &&
013e3b3942e9550fde619a0b3ce6bdd04edc4268Timo Sirainen ret == 0 && !sync_ctx.delay_writes &&
013e3b3942e9550fde619a0b3ce6bdd04edc4268Timo Sirainen sync_ctx.base_uid_last_offset != 0) {
5fb3bff645380804c9db2510940c41db6b8fdb01Timo Sirainen /* Rewrite uid_last in X-IMAPbase header if we've seen it
b7ad29db8cf906da01ee4626730868881087950bTimo Sirainen (ie. the file isn't empty) */
29e945d8550f297707f3a5f627a938401046c0ccTimo Sirainen ret = mbox_rewrite_base_uid_last(&sync_ctx);
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen }
95ed89440faab05cbb4f2473f2f4af19e848bde8Timo Sirainen
e5ee67f18b03015c88b579c8c1f17ebe6ce19b76Timo Sirainen i_assert(lock_id != 0);
e5ee67f18b03015c88b579c8c1f17ebe6ce19b76Timo Sirainen
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen if (mbox->mbox_lock_type != F_RDLCK) {
95ed89440faab05cbb4f2473f2f4af19e848bde8Timo Sirainen /* drop to read lock */
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen unsigned int read_lock_id = 0;
d756ebcfa96bd7cff02097c8f26df9df368b81b1Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (mbox_lock(mbox, F_RDLCK, &read_lock_id) <= 0)
ret = -1;
else {
if (mbox_unlock(mbox, lock_id) < 0)
ret = -1;
lock_id = read_lock_id;
}
}
if ((flags & MBOX_SYNC_LOCK_READING) == 0) {
if (mbox_unlock(mbox, lock_id) < 0)
ret = -1;
}
mbox_sync_context_free(&sync_ctx);
return ret;
}
int mbox_sync(struct mbox_mailbox *mbox, enum mbox_sync_flags flags)
{
int ret;
mbox->syncing = TRUE;
ret = mbox_sync_int(mbox, flags);
mbox->syncing = FALSE;
if (mbox->ibox.box.v.sync_notify != NULL)
mbox->ibox.box.v.sync_notify(&mbox->ibox.box, 0, 0);
return ret;
}
struct mailbox_sync_context *
mbox_storage_sync_init(struct mailbox *box, enum mailbox_sync_flags flags)
{
struct mbox_mailbox *mbox = (struct mbox_mailbox *)box;
enum mbox_sync_flags mbox_sync_flags = 0;
int ret = 0;
if (!box->opened)
index_storage_mailbox_open(&mbox->ibox);
if ((flags & MAILBOX_SYNC_FLAG_FAST) == 0 ||
mbox->ibox.sync_last_check + MAILBOX_FULL_SYNC_INTERVAL <=
ioloop_time) {
if ((flags & MAILBOX_SYNC_FLAG_FULL_READ) != 0 &&
!mbox->mbox_very_dirty_syncs)
mbox_sync_flags |= MBOX_SYNC_UNDIRTY;
if ((flags & MAILBOX_SYNC_FLAG_FULL_WRITE) != 0)
mbox_sync_flags |= MBOX_SYNC_REWRITE;
ret = mbox_sync(mbox, mbox_sync_flags);
}
return index_mailbox_sync_init(box, flags, ret < 0);
}