mbox-sync.c revision 4b058f90f9e8a2c6b2eed275de4eb8cc5195a71d
45312f52ff3a3d4c137447be4c7556500c2f8bf2Timo Sirainen/* Copyright (C) 2004 Timo 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 - 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 Here's how we do it:
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 - 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
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainenint mbox_sync_seek(struct mbox_sync_context *sync_ctx, uoff_t from_offset)
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainen if (istream_raw_mbox_seek(sync_ctx->input, from_offset) < 0) {
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen mail_storage_set_critical(sync_ctx->ibox->box.storage,
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen "Unexpectedly lost From-line at offset %"PRIuUOFF_T
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainenstatic void mbox_sync_buffer_delete_old(buffer_t *syncs_buf, uint32_t uid)
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen sync = buffer_get_modifyable_data(syncs_buf, &size);
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen /* keep it */
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen buffer_set_used_size(syncs_buf, dest * sizeof(*sync));
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainenmbox_sync_read_next_mail(struct mbox_sync_context *sync_ctx,
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen /* get EOF */
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen (void)istream_raw_mbox_get_header_offset(sync_ctx->input);
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen istream_raw_mbox_get_start_offset(sync_ctx->input);
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen istream_raw_mbox_get_header_offset(sync_ctx->input);
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen if (mail_ctx->seq > 1 && sync_ctx->dest_first_mail) {
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen /* First message was expunged and this is the next one.
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen Skip \n header */
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen mbox_sync_parse_next_mail(sync_ctx->input, mail_ctx);
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen i_assert(sync_ctx->input->v_offset != mail_ctx->mail.from_offset ||
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen istream_raw_mbox_get_body_size(sync_ctx->input,
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen i_assert(mail_ctx->mail.body_size < OFF_T_MAX);
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen if ((mail_ctx->mail.flags & MBOX_NONRECENT) == 0 && !mail_ctx->pseudo) {
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen /* need to add 'O' flag to Status-header */
9e59a1f3f095b3099478562cf3f3970a24736970Timo Sirainenstatic int mbox_sync_buf_have_expunges(buffer_t *syncs_buf)
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen for (i = 0; i < size; i++) {
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen if (sync[i].type == MAIL_INDEX_SYNC_TYPE_EXPUNGE)
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainenstatic int mbox_sync_read_index_syncs(struct mbox_sync_context *sync_ctx,
c3412ddeb9abc13f99d3caf50faf76cd99f7e9d2Timo Sirainen struct mail_index_sync_rec *sync_rec = &sync_ctx->sync_rec;
ced118ac5caf6fe83d34339c2c65c63b2aa768acTimo Sirainen /* nothing for this or the future ones */
e8a35266a5ceacdfafeeffd6bddae77931ff97ebTimo Sirainen mbox_sync_buffer_delete_old(sync_ctx->syncs, uid);
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen sync_rec->type != MAIL_INDEX_SYNC_TYPE_APPEND &&
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen (sync_rec->type != MAIL_INDEX_SYNC_TYPE_EXPUNGE ||
0c909e3461607eadcd66f4eac69b7f34e37fccf1Timo Sirainen if (sync_rec->type == MAIL_INDEX_SYNC_TYPE_EXPUNGE)
6843896c40bee4f9b6680ca7ced598c446e9f999Timo Sirainen ret = mail_index_sync_next(sync_ctx->index_sync_ctx, sync_rec);
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen if (sync_rec->type == MAIL_INDEX_SYNC_TYPE_APPEND) {
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen sync_ctx->update_base_uid_last = sync_rec->uid2;
f6699a08521aacc4c2bb5b6175691dad5f715f8cTimo Sirainen *sync_expunge_r = mbox_sync_buf_have_expunges(sync_ctx->syncs);
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainenstatic void mbox_sync_apply_index_syncs(buffer_t *syncs_buf, uint8_t *flags,
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen for (i = 0; i < size; i++) {
2bf7bb14894faf721518e2122a14a2389ef94078Timo Sirainen if (sync[i].type != MAIL_INDEX_SYNC_TYPE_FLAGS)
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen mail_index_sync_flags_apply(&sync[i], flags, keywords);
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainenmbox_sync_read_index_rec(struct mbox_sync_context *sync_ctx,
9a06cabdfdf4d5e2f19a07e506c3c7d08a7e7038Timo Sirainen uint32_t uid, const struct mail_index_record **rec_r)
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen messages_count = mail_index_view_get_message_count(sync_ctx->sync_view);
7f3be7d885c75cdd77f536929a45bc9764595960Timo Sirainen /* externally expunged message, remove from index */
7f3be7d885c75cdd77f536929a45bc9764595960Timo Sirainen mail_index_expunge(sync_ctx->t, sync_ctx->idx_seq);
f6699a08521aacc4c2bb5b6175691dad5f715f8cTimo Sirainen if (ret == 0 && uid < sync_ctx->hdr->next_uid) {
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen /* this UID was already in index and it was expunged */
f6699a08521aacc4c2bb5b6175691dad5f715f8cTimo Sirainen mail_storage_set_critical(sync_ctx->ibox->box.storage,
f6699a08521aacc4c2bb5b6175691dad5f715f8cTimo Sirainen "mbox sync: Expunged message reappeared in mailbox %s "
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen /* new UID in the middle of the mailbox - shouldn't happen */
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen mail_storage_set_critical(sync_ctx->ibox->box.storage,
ea5f188fc29dfaa0c4071e6413e16e1d04263722Timo Sirainen "mbox sync: UID inserted in the middle of mailbox %s "
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen "(%u > %u)", sync_ctx->ibox->path, rec->uid, uid);
7f3be7d885c75cdd77f536929a45bc9764595960Timo Sirainenstatic int mbox_sync_find_index_md5(struct mbox_sync_context *sync_ctx,
7f3be7d885c75cdd77f536929a45bc9764595960Timo Sirainen unsigned char hdr_md5_sum[],
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen messages_count = mail_index_view_get_message_count(sync_ctx->sync_view);
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen if (mail_index_lookup_ext(sync_ctx->sync_view,
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (data != NULL && memcmp(data, hdr_md5_sum, 16) == 0)
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen /* externally expunged message, remove from index */
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen mail_index_expunge(sync_ctx->t, sync_ctx->idx_seq);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenmbox_sync_update_from_offset(struct mbox_sync_context *sync_ctx,
ea5f188fc29dfaa0c4071e6413e16e1d04263722Timo Sirainen /* see if from_offset needs updating */
a0b0d629931773c17a236f6214adbe0e13b9b3fdTimo Sirainen if (mail_index_lookup_ext(sync_ctx->sync_view,
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen *((const uint64_t *)data) == mail->from_offset)
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen mail_index_update_ext(sync_ctx->t, sync_ctx->idx_seq,
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainenstatic int mbox_sync_update_index(struct mbox_sync_context *sync_ctx,
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen struct mbox_sync_mail *mail = &mail_ctx->mail;
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen /* new message */
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen mail_index_append(sync_ctx->t, mail->uid, &sync_ctx->idx_seq);
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen mbox_flags = mail->flags & (MAIL_FLAGS_MASK^MAIL_RECENT);
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen mail_index_update_flags(sync_ctx->t, sync_ctx->idx_seq,
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen mail_index_update_ext(sync_ctx->t, sync_ctx->idx_seq,
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen /*FIXME:mail_cache_add(sync_ctx->cache_trans,
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen MAIL_CACHE_UID_STRING,
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen str_data(mail_ctx->uidl),
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen str_len(mail_ctx->uidl));*/
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen /* see if flags changed */
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen memcpy(idx_keywords, rec->keywords, INDEX_KEYWORDS_BYTE_COUNT);
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen if ((idx_flags & MAIL_INDEX_MAIL_FLAG_DIRTY) != 0) {
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen /* flags are dirty, ignore whatever was in the file.
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen but remove recent flag if needed. */
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen mbox_flags = (rec->flags & ~MAIL_FLAGS_MASK) |
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen if ((idx_flags & ~MAIL_INDEX_MAIL_FLAG_DIRTY) ==
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen /* dirty flag state changed */
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen mail_index_update_flags(sync_ctx->t, sync_ctx->idx_seq,
5d98a279f2726a8c38ef1e8e3c54d6c62d523a65Timo Sirainen } else if (((idx_flags ^ mbox_flags) & MAIL_RECENT) != 0) {
5d98a279f2726a8c38ef1e8e3c54d6c62d523a65Timo Sirainen /* drop recent flag */
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen memset(idx_keywords, 0, INDEX_KEYWORDS_BYTE_COUNT);
d0d7fcf3ce44f26fdf34c1542a25cec644c5c4c7Timo Sirainen mail_index_update_flags(sync_ctx->t, sync_ctx->idx_seq,
7f3be7d885c75cdd77f536929a45bc9764595960Timo Sirainen index_mailbox_set_recent(sync_ctx->ibox, sync_ctx->idx_seq);
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen /* update from_offsets, but not if we're going to rewrite this message.
ea5f188fc29dfaa0c4071e6413e16e1d04263722Timo Sirainen rewriting would just move it anyway. */
7f3be7d885c75cdd77f536929a45bc9764595960Timo Sirainen int nocheck = rec == NULL || sync_ctx->expunged_space > 0;
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen if (mbox_sync_update_from_offset(sync_ctx, mail, nocheck) < 0)
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainenstatic int mbox_read_from_line(struct mbox_sync_mail_context *ctx)
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen struct istream *input = ctx->sync_ctx->file_input;
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen const unsigned char *data;
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen buffer_set_used_size(ctx->sync_ctx->from_line, 0);
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen from_line_size = ctx->hdr_offset - ctx->mail.from_offset;
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen buffer_append(ctx->sync_ctx->from_line, data, size);
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainenmbox_write_from_line(struct mbox_sync_mail_context *ctx)
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen if (pwrite_full(ctx->sync_ctx->fd, str_data(str), str_len(str),
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen mbox_set_syscall_error(ctx->sync_ctx->ibox, "pwrite_full()");
4d25408732be27e91f0430f71e87242760c2517cTimo Sirainenstatic void update_from_offsets(struct mbox_sync_context *sync_ctx)
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen mails = buffer_get_modifyable_data(sync_ctx->mails, &size);
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen mail_index_update_ext(sync_ctx->t, mails[idx].idx_seq,
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainenstatic void mbox_sync_handle_expunge(struct mbox_sync_mail_context *mail_ctx)
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen mail_ctx->mail.offset = mail_ctx->mail.from_offset;
16aba431c576c1dbd99cbaae4f9d65eea9ad73c2Timo Sirainen mail_ctx->body_offset - mail_ctx->mail.from_offset +
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen /* expunging first message, fix space to contain next
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen message's \n header too since it will be removed. */
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen mail_ctx->sync_ctx->expunged_space += mail_ctx->mail.space;
16aba431c576c1dbd99cbaae4f9d65eea9ad73c2Timo Sirainenstatic int mbox_sync_handle_header(struct mbox_sync_mail_context *mail_ctx)
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen struct mbox_sync_context *sync_ctx = mail_ctx->sync_ctx;
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen if (sync_ctx->expunged_space > 0 && sync_ctx->need_space_seq == 0) {
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen /* move the header backwards to fill expunged space */
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen /* read the From-line before rewriting overwrites it */
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen mbox_sync_update_header(mail_ctx, sync_ctx->syncs);
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen ret = mbox_sync_try_rewrite(mail_ctx, move_diff);
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen /* rewrite successful, write From-line to
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen new location */
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen mbox_sync_update_header(mail_ctx, sync_ctx->syncs);
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen /* mark it dirty and do it later */
b225c3c65f360d7b833f09f9b2fb3035ed5ea600Timo Sirainen if ((ret = mbox_sync_try_rewrite(mail_ctx, 0)) < 0)
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen /* nothing to do */
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen if (ret == 0 && sync_ctx->need_space_seq == 0) {
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen /* first mail with no space to write it */
39775ad03c459efe64cce924658da5094ba417e1Timo Sirainen /* create dummy message to describe the expunged data */
e5c08648676d1989f6e70b95e5990c26b3e8b96bTimo Sirainen buffer_append(sync_ctx->mails, &mail, sizeof(mail));
01cbf4ac5d44137ab434791be7f838d98d0fcf3bTimo Sirainenmbox_sync_handle_missing_space(struct mbox_sync_mail_context *mail_ctx)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen struct mbox_sync_context *sync_ctx = mail_ctx->sync_ctx;
c35d0c3eb4ba89432896e1f9770ab31cc63c232bTimo Sirainen uoff_t end_offset, move_diff, extra_space, needed_space;
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen buffer_append(sync_ctx->mails, &mail_ctx->mail, sizeof(mail_ctx->mail));
1e76a5b92f9d82d557f81f080f3dfad1c9d8f200Timo Sirainen /* we have enough space now */
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen /* this message was expunged. fill more or less of the space. */
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen (sync_ctx->seq - sync_ctx->need_space_seq + 1);
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen needed_space = mail_ctx->mail.space - sync_ctx->space_diff;
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen if ((uoff_t)sync_ctx->space_diff > needed_space + extra_space) {
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen /* don't waste too much on padding */
c35d0c3eb4ba89432896e1f9770ab31cc63c232bTimo Sirainen sync_ctx->expunged_space = mail_ctx->mail.space -
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen sync_ctx->space_diff = needed_space + extra_space;
c35d0c3eb4ba89432896e1f9770ab31cc63c232bTimo Sirainen extra_space = sync_ctx->space_diff - needed_space;
c35d0c3eb4ba89432896e1f9770ab31cc63c232bTimo Sirainen buffer_set_used_size(sync_ctx->mails, sync_ctx->mails->used -
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen /* this message gave enough space from headers. rewriting stops
c35d0c3eb4ba89432896e1f9770ab31cc63c232bTimo Sirainen at the end of this message's headers. */
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen if (mbox_sync_rewrite(sync_ctx, end_offset, move_diff, extra_space,
7fd72a47d7ddfbd38c8697e228b6951f495dfb61Timo Sirainen /* mail_ctx may contain wrong data after rewrite, so make sure we
7fd72a47d7ddfbd38c8697e228b6951f495dfb61Timo Sirainen don't try to access it */
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainenmbox_sync_seek_to_seq(struct mbox_sync_context *sync_ctx, uint32_t seq)
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen if (istream_raw_mbox_seek(ibox->mbox_stream, 0) < 0) {
c35d0c3eb4ba89432896e1f9770ab31cc63c232bTimo Sirainen "Mailbox isn't a valid mbox file");
064bfeee2f9156683b191cc0f3f7b242720942f7Timo Sirainen old_offset = istream_raw_mbox_get_start_offset(sync_ctx->input);
7fd72a47d7ddfbd38c8697e228b6951f495dfb61Timo Sirainen ret = mbox_file_seek(ibox, sync_ctx->sync_view, seq, &deleted);
7a6b45405fb1544ac476e6eb1402a70cc1ddcdcfTimo Sirainen "Error seeking back to original "
7a6b45405fb1544ac476e6eb1402a70cc1ddcdcfTimo Sirainen "offset %s in mbox file %s",
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen else if (mail_index_lookup_uid(sync_ctx->sync_view, seq-1, &uid) < 0) {
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen /* set to -1, since it's always increased later */
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen istream_raw_mbox_get_start_offset(sync_ctx->input) != 0) {
1e76a5b92f9d82d557f81f080f3dfad1c9d8f200Timo Sirainen /* this mbox has pseudo mail which contains the X-IMAP header */
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen sync_ctx->dest_first_mail = sync_ctx->seq == 0;
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen (void)istream_raw_mbox_get_body_offset(sync_ctx->input);
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainenmbox_sync_seek_to_uid(struct mbox_sync_context *sync_ctx, uint32_t uid)
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen if (mail_index_lookup_uid_range(sync_ctx->sync_view, uid, (uint32_t)-1,
7a6b45405fb1544ac476e6eb1402a70cc1ddcdcfTimo Sirainen /* doesn't exist anymore, seek to end of file */
f5b4979e2780c4df112a300967d647e2fdd73511Timo Sirainen file_size = i_stream_get_size(sync_ctx->ibox->mbox_file_stream);
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen if (istream_raw_mbox_seek(sync_ctx->ibox->mbox_stream,
2cb565cd978aafd5714792b5161889986d49e431Timo Sirainen mail_storage_set_critical(sync_ctx->ibox->box.storage,
343a527f805ca5cce78496b959d6def70e5d0cd4Timo Sirainen "Error seeking to end of mbox file %s",
6e4cd4ba520bc22ce375de378f4751136ebcf75aTimo Sirainenstatic int mbox_sync_loop(struct mbox_sync_context *sync_ctx,
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen messages_count = mail_index_view_get_message_count(sync_ctx->sync_view);
b2ecd50bb98c44816cb07c17aa17fae2b425f941Timo Sirainen if (!mail_index_sync_have_more(sync_ctx->index_sync_ctx) ||
6a1e4eb2c6a267bec1e8704ce9137bebb7792702Timo Sirainen ret = mbox_sync_seek_to_seq(sync_ctx, partial ?
4214b59ac7f3899f8d887d055ef519f5a622d249Timo Sirainen /* we sync only what we need to. jump to first record that
4214b59ac7f3899f8d887d055ef519f5a622d249Timo Sirainen needs updating */
4214b59ac7f3899f8d887d055ef519f5a622d249Timo Sirainen if (buffer_get_used_size(sync_ctx->syncs) == 0 &&
6a1e4eb2c6a267bec1e8704ce9137bebb7792702Timo Sirainen if (buffer_get_used_size(sync_ctx->syncs) == 0 &&
c9a03c8a03a782488981f81bd7c6b5e01ff55f06Timo Sirainen /* nothing to do */
39775ad03c459efe64cce924658da5094ba417e1Timo Sirainen sync_rec = buffer_get_data(sync_ctx->syncs, &size);
6a1e4eb2c6a267bec1e8704ce9137bebb7792702Timo Sirainen ret = mbox_sync_seek_to_uid(sync_ctx, sync_rec->uid1);
6a1e4eb2c6a267bec1e8704ce9137bebb7792702Timo Sirainen while ((ret = mbox_sync_read_next_mail(sync_ctx, mail_ctx)) > 0) {
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen if (mail_ctx->seq == 1 && sync_ctx->base_uid_validity != 0 &&
a614397cf1a4dde152eb1a38493a6ec3d817da16Timo Sirainen mail_storage_set_critical(sync_ctx->ibox->box.storage,
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen "UIDVALIDITY changed (%u -> %u) "
b2ecd50bb98c44816cb07c17aa17fae2b425f941Timo Sirainen "in mbox file %s",
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen mail_index_mark_corrupted(sync_ctx->ibox->index);
39775ad03c459efe64cce924658da5094ba417e1Timo Sirainen /* UID ordering problems, resync everything to make
343a527f805ca5cce78496b959d6def70e5d0cd4Timo Sirainen sure we get everything right */
319944c0f35b311c998854e96d6463a084fd90aeTimo Sirainen ret = mbox_sync_read_index_rec(sync_ctx, uid, &rec);
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen /* If we can't use/store X-UID header, use MD5 sum.
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen Also check for existing MD5 sums when we're actually
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen able to write X-UIDs. */
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen /* get all sync records related to this message */
88187ee880b4829443e0d55ea7d145d9d5880217Timo Sirainen /* missing/broken X-UID. all the rest of the mails
319944c0f35b311c998854e96d6463a084fd90aeTimo Sirainen need new UIDs. */
4b9f99761df5014c659cd87fddaf6854af428cfcTimo Sirainen if (mbox_sync_update_index(sync_ctx, mail_ctx,
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen offset = istream_raw_mbox_get_start_offset(sync_ctx->input);
b225c3c65f360d7b833f09f9b2fb3035ed5ea600Timo Sirainen if (mbox_sync_handle_missing_space(mail_ctx) < 0)
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen /* move the body */
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen } else if (sync_ctx->seq >= min_message_count) {
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen mbox_sync_buffer_delete_old(sync_ctx->syncs, uid);
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen if (buffer_get_used_size(sync_ctx->syncs) == 0) {
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen /* if there's no sync records left,
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen we can stop */
f6699a08521aacc4c2bb5b6175691dad5f715f8cTimo Sirainen /* we can skip forward to next record which
f6699a08521aacc4c2bb5b6175691dad5f715f8cTimo Sirainen needs updating. if it failes because the
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen offset is dirty, just ignore and continue
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen from where we are now. */
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen if (istream_raw_mbox_is_eof(sync_ctx->input)) {
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen /* rest of the messages in index don't exist -> expunge them */
022412398e56a8f31ef111cfd7271498d64af9a9Timo Sirainen mail_index_expunge(sync_ctx->t, sync_ctx->idx_seq++);
022412398e56a8f31ef111cfd7271498d64af9a9Timo Sirainenstatic int mbox_sync_handle_eof_updates(struct mbox_sync_context *sync_ctx,
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen uoff_t offset, padding, trailer_size, old_file_size;
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen if (!istream_raw_mbox_is_eof(sync_ctx->input)) {
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen trailer_size = i_stream_get_size(sync_ctx->file_input) -
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen (sync_ctx->seq - sync_ctx->need_space_seq + 1);
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen sync_ctx->space_diff += sync_ctx->expunged_space;
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen if (sync_ctx->expunged_space <= -sync_ctx->space_diff)
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen sync_ctx->expunged_space -= -sync_ctx->space_diff;
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen old_file_size = i_stream_get_size(sync_ctx->file_input);
17ad2164c747cedbf81dae1893063e71a3df0356Timo Sirainen "file_set_size()");
17ad2164c747cedbf81dae1893063e71a3df0356Timo Sirainen if (mbox_sync_rewrite(sync_ctx, old_file_size,
f6699a08521aacc4c2bb5b6175691dad5f715f8cTimo Sirainen /* copy trailer, then truncate the file */
f6699a08521aacc4c2bb5b6175691dad5f715f8cTimo Sirainen offset = i_stream_get_size(sync_ctx->file_input) -
f6699a08521aacc4c2bb5b6175691dad5f715f8cTimo Sirainen if (ftruncate(sync_ctx->fd, offset + trailer_size) < 0) {
f6699a08521aacc4c2bb5b6175691dad5f715f8cTimo Sirainen mbox_set_syscall_error(sync_ctx->ibox, "ftruncate()");
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainenstatic int mbox_sync_update_index_header(struct mbox_sync_context *sync_ctx)
d31c4d7c161f9d7efa59964c7c958e83e05b218cTimo Sirainen mbox_set_syscall_error(sync_ctx->ibox, "fstat()");
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen sync_ctx->base_uid_validity != sync_ctx->hdr->uid_validity) ||
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen (sync_ctx->hdr->uid_validity == 0 && sync_ctx->seen_first_mail)) {
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen /* we didn't rewrite X-IMAPbase header because
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen a) mbox is read-only, b) we're lazy-writing */
a486ed03dce069ff60ab5a65d0ae24a1862f22fcTimo Sirainen offsetof(struct mail_index_header, uid_validity),
d31c4d7c161f9d7efa59964c7c958e83e05b218cTimo Sirainen if (istream_raw_mbox_is_eof(sync_ctx->input) &&
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen sync_ctx->next_uid != sync_ctx->hdr->next_uid) {
17ad2164c747cedbf81dae1893063e71a3df0356Timo Sirainen &sync_ctx->next_uid, sizeof(sync_ctx->next_uid));
f6699a08521aacc4c2bb5b6175691dad5f715f8cTimo Sirainen if ((uint32_t)st.st_mtime != sync_ctx->hdr->sync_stamp &&
319944c0f35b311c998854e96d6463a084fd90aeTimo Sirainen offsetof(struct mail_index_header, sync_stamp),
b8765f6093ab35fc2345293d78132d35794cbff5Timo Sirainen if ((uint64_t)st.st_size != sync_ctx->hdr->sync_size &&
319944c0f35b311c998854e96d6463a084fd90aeTimo Sirainen offsetof(struct mail_index_header, sync_size),
319944c0f35b311c998854e96d6463a084fd90aeTimo Sirainen sync_ctx->ibox->mbox_dirty_stamp = st.st_mtime;
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainenstatic void mbox_sync_restart(struct mbox_sync_context *sync_ctx)
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainenstatic int mbox_sync_do(struct mbox_sync_context *sync_ctx,
c9fe52d819c608b890620f7fe36ff509b14eb350Timo Sirainen mbox_set_syscall_error(sync_ctx->ibox, "stat()");
c9fe52d819c608b890620f7fe36ff509b14eb350Timo Sirainen if ((uint32_t)st.st_mtime == sync_ctx->hdr->sync_stamp &&
e9d68b41c007f0e545de361f8012f6f231bfec8bTimo Sirainen (uint64_t)st.st_size == sync_ctx->hdr->sync_size) {
c9fe52d819c608b890620f7fe36ff509b14eb350Timo Sirainen /* file is fully synced */
e9d68b41c007f0e545de361f8012f6f231bfec8bTimo Sirainen } else if ((flags & MBOX_SYNC_UNDIRTY) != 0 ||
7f3be7d885c75cdd77f536929a45bc9764595960Timo Sirainen (uint64_t)st.st_size == sync_ctx->hdr->sync_size) {
7f3be7d885c75cdd77f536929a45bc9764595960Timo Sirainen /* we want to do full syncing. always do this if
1e76a5b92f9d82d557f81f080f3dfad1c9d8f200Timo Sirainen file size hasn't changed but timestamp has. it most
71a74e26cf070a205d31cf6c6fae003f90027b63Timo Sirainen likely means that someone had modified some header
71a74e26cf070a205d31cf6c6fae003f90027b63Timo Sirainen and we probably want to know about it */
71a74e26cf070a205d31cf6c6fae003f90027b63Timo Sirainen /* see if we can delay syncing the whole file.
a486ed03dce069ff60ab5a65d0ae24a1862f22fcTimo Sirainen normally we only notice expunges and appends
a486ed03dce069ff60ab5a65d0ae24a1862f22fcTimo Sirainen in partial syncing. */
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen ret = mbox_sync_loop(sync_ctx, &mail_ctx, min_msg_count, partial);
6843896c40bee4f9b6680ca7ced598c446e9f999Timo Sirainen /* partial syncing didn't work, do it again */
6843896c40bee4f9b6680ca7ced598c446e9f999Timo Sirainen sync_ctx->t = mail_index_transaction_begin(sync_ctx->sync_view,
d0e5f8252516c4d4df2fbcdae4d37f9e5f931199Timo Sirainen ret = mbox_sync_loop(sync_ctx, &mail_ctx, (uint32_t)-1, FALSE);
f0569d9fbb25c8437760be69f194595a841ad711Timo Sirainen if (mbox_sync_handle_eof_updates(sync_ctx, &mail_ctx) < 0)
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen /* only syncs left should be just appends (and their updates)
b2ecd50bb98c44816cb07c17aa17fae2b425f941Timo Sirainen which weren't synced yet for some reason (crash). we'll just
1e76a5b92f9d82d557f81f080f3dfad1c9d8f200Timo Sirainen ignore them, as we've overwritten them above. */
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen memset(&sync_ctx->sync_rec, 0, sizeof(sync_ctx->sync_rec));
907723f35f4d3dfc774ca42d00a8a7b8ef90dd5dTimo Sirainen if (mbox_sync_update_index_header(sync_ctx) < 0)
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainenint mbox_sync_has_changed(struct index_mailbox *ibox, int leave_dirty)
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen if (mail_index_get_header(ibox->view, &hdr) < 0) {
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen if ((uint32_t)st.st_mtime == hdr->sync_stamp &&
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen /* fully synced */
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen return st.st_mtime != ibox->mbox_dirty_stamp ||
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic int mbox_sync_update_imap_base(struct mbox_sync_context *sync_ctx)
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen sync_ctx->t = mail_index_transaction_begin(sync_ctx->sync_view, FALSE);
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen sync_ctx->update_base_uid_last = sync_ctx->next_uid-1;
d31c4d7c161f9d7efa59964c7c958e83e05b218cTimo Sirainen if (mbox_sync_loop(sync_ctx, &mail_ctx, 1, 0) < 0)
d31c4d7c161f9d7efa59964c7c958e83e05b218cTimo Sirainen if (mbox_sync_handle_eof_updates(sync_ctx, &mail_ctx) < 0)
d31c4d7c161f9d7efa59964c7c958e83e05b218cTimo Sirainen if (mbox_sync_update_index_header(sync_ctx) < 0)
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainenint mbox_sync(struct index_mailbox *ibox, enum mbox_sync_flags flags)
9d2040fbb941f411d57fd850b4cdc3b1cccc1168Timo Sirainen unsigned int lock_id = 0;
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen int leave_dirty = (flags & MBOX_SYNC_UNDIRTY) == 0;
b225c3c65f360d7b833f09f9b2fb3035ed5ea600Timo Sirainen if ((changed = mbox_sync_has_changed(ibox, leave_dirty)) < 0) {
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen /* we just want to lock it for reading. if mbox hasn't been
b8bbfab97eed17fcb00b5a86128e1d7a3babc35cTimo Sirainen modified don't do any syncing. */
2be66b9eddad3841a1195fe9aeb1eaf0f28f1116Timo Sirainen /* have to sync to make sure offsets have stayed the same */
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen /* we're most likely modifying the mbox while syncing, just
f5b919e9b07dfd9d2401b998ef8759e5f0312719Timo Sirainen lock it for writing immediately. the mbox must be locked
4b9f99761df5014c659cd87fddaf6854af428cfcTimo Sirainen before index syncing is started to avoid deadlocks, so we
33ae95df45c9b5ec51332a6b39eb5322038686b9Timo Sirainen don't have much choice either (well, easy ones anyway). */
178511b57faa7c3f8203dd8b7e4059d00cbfc23aTimo Sirainen int lock_type = ibox->mbox_readonly ? F_RDLCK : F_WRLCK;
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen if (mbox_lock(ibox, lock_type, &lock_id) <= 0)
8166e8a706078efc71923719ca285e84902883c0Timo Sirainen ret = mail_index_sync_begin(ibox->index, &index_sync_ctx, &sync_view,
686cb2418d4daa7bc747d3551783c1895e7c41b9Timo Sirainen if (!changed && !mail_index_sync_have_more(index_sync_ctx)) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* nothing to do */
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen /* index may need to do internal syncing though, so commit
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen instead of rollbacking. */
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen if (mail_index_sync_commit(index_sync_ctx) < 0) {
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen /* ok, we have something to do but no locks. we'll have to
e68309fcfa2eaa88217fd51e7b4900fc9c20ef5dTimo Sirainen restart syncing to avoid deadlocking. */
6a1e4eb2c6a267bec1e8704ce9137bebb7792702Timo Sirainen sync_ctx.from_line = str_new(default_pool, 256);
f4b93a46e140823a64d88763ea6ef9f03c49844eTimo Sirainen sync_ctx.header = str_new(default_pool, 4096);
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen sync_ctx.t = mail_index_transaction_begin(sync_view, FALSE);
99be58a447b69d62cbd9e764000a06226b9c9c89Timo Sirainen sync_ctx.mails = buffer_create_dynamic(default_pool, 4096);
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen sync_ctx.syncs = buffer_create_dynamic(default_pool, 256);
343a527f805ca5cce78496b959d6def70e5d0cd4Timo Sirainen ret = mail_index_get_header(sync_view, &sync_ctx.hdr);
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen sync_ctx.file_input = sync_ctx.ibox->mbox_file_stream;
b225c3c65f360d7b833f09f9b2fb3035ed5ea600Timo Sirainen sync_ctx.delay_writes = sync_ctx.ibox->mbox_readonly ||
988922bdf2d461021d210697f1f118956ca388e1Timo Sirainen else if (mail_index_transaction_commit(sync_ctx.t, &seq, &offset) < 0) {
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen else if (mail_index_sync_commit(index_sync_ctx) < 0) {
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen sync_ctx.base_uid_last != sync_ctx.next_uid-1 &&
f7d43647acc6dc80064c8c4cacf5bf86f754c530Timo Sirainen /* rewrite X-IMAPbase header. do it after mail_index_sync_end()
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen so previous transactions have been committed. */
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen /* FIXME: ugly .. */
690bafa70767e3f6e98bbfd62ad4a26be2387ea9Timo Sirainen (void)mail_index_get_header(sync_ctx.sync_view,
17ad2164c747cedbf81dae1893063e71a3df0356Timo Sirainen if ((ret = mbox_sync_update_imap_base(&sync_ctx)) < 0)
690bafa70767e3f6e98bbfd62ad4a26be2387ea9Timo Sirainen else if (mail_index_transaction_commit(sync_ctx.t,
690bafa70767e3f6e98bbfd62ad4a26be2387ea9Timo Sirainen if (ret == 0 && ibox->mbox_lock_type == F_WRLCK) {
279cc94ab086f6a3cb764b1b98ff6b936efa3eaeTimo Sirainen if (lock_id != 0 && ibox->mbox_lock_type != F_RDLCK) {
b225c3c65f360d7b833f09f9b2fb3035ed5ea600Timo Sirainen /* drop to read lock */
279cc94ab086f6a3cb764b1b98ff6b936efa3eaeTimo Sirainen unsigned int read_lock_id = 0;
7d207b1e77a7b5e3fda640e353acfc86d261fedfTimo Sirainen if (mbox_lock(ibox, F_RDLCK, &read_lock_id) <= 0)
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainen if (lock_id != 0 && (flags & MBOX_SYNC_LOCK_READING) == 0) {
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainen /* FIXME: keep the lock MBOX_SYNC_SECS+1 to make sure we
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainen notice changes made by others .. and this has to be done
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainen even if lock_reading is set.. except if
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainen mbox_sync_dirty = TRUE */
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainenmbox_storage_sync_init(struct mailbox *box, enum mailbox_sync_flags flags)
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainen struct index_mailbox *ibox = (struct index_mailbox *)box;
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainen ibox->sync_last_check + MAILBOX_FULL_SYNC_INTERVAL <= ioloop_time) {
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainen if ((flags & MAILBOX_SYNC_FLAG_FULL_READ) != 0)
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainen if ((flags & MAILBOX_SYNC_FLAG_FULL_WRITE) != 0)