mbox-sync.c revision e05ea8311ae16687295048e88ca205dfe29fbcbf
02c335c23bf5fa225a467c19f2c063fb0dc7b8c3Timo Sirainen/* Copyright (C) 2004 Timo Sirainen */
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen Modifying mbox can be slow, so we try to do it all at once minimizing the
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen required disk I/O. We may need to:
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen - Update message flags in Status, X-Status and X-Keywords headers
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen - Write missing X-UID and X-IMAPbase headers
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen - Write missing or broken Content-Length header if there's space
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen - Expunge specified messages
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen Here's how we do it:
7676a044f800a539bb0cbbc59fb9d1ad3f30ba7fTimo Sirainen - Start reading the mails from the beginning
7676a044f800a539bb0cbbc59fb9d1ad3f30ba7fTimo Sirainen - X-Keywords, X-UID and X-IMAPbase headers may contain padding at the end
7676a044f800a539bb0cbbc59fb9d1ad3f30ba7fTimo Sirainen of them, remember how much each message has and offset to beginning of the
7676a044f800a539bb0cbbc59fb9d1ad3f30ba7fTimo Sirainen - If header needs to be rewritten and there's enough space, do it
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen - If we didn't have enough space, remember how much was missing
5a470af15391d6fc73bebb41512a671b3d847644Timo Sirainen - Continue reading and counting the padding in each message. If available
5a470af15391d6fc73bebb41512a671b3d847644Timo Sirainen padding is enough to rewrite all the previous messages needing it, do it
5a470af15391d6fc73bebb41512a671b3d847644Timo Sirainen - When we encounter expunged message, treat all of it as padding and
5a470af15391d6fc73bebb41512a671b3d847644Timo Sirainen rewrite previous messages if needed (and there's enough space).
5a470af15391d6fc73bebb41512a671b3d847644Timo Sirainen Afterwards keep moving messages backwards to fill the expunged space.
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen Moving is done by rewriting each message's headers, with possibly adding
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen missing Content-Length header and padding. Message bodies are moved
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen without modifications.
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen - If we encounter end of file, grow the file and rewrite needed messages
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen - Rewriting is done by moving message body forward, rewriting message's
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen header and doing the same for previous message, until all of them are
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen/* The text below was taken exactly as c-client wrote it to my mailbox,
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen so it's probably copyrighted by University of Washington. */
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen"This text is part of the internal format of your mail folder, and is not\n" \
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen"a real message. It is created automatically by the mail system software.\n" \
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen"If deleted, important folder data will be lost, and it will be re-created\n" \
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen"with the data reset to initial values.\n"
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainenint mbox_sync_seek(struct mbox_sync_context *sync_ctx, uoff_t from_offset)
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen if (istream_raw_mbox_seek(sync_ctx->input, from_offset) < 0) {
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen mail_storage_set_critical(STORAGE(sync_ctx->mbox->storage),
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen "Unexpectedly lost From-line at offset %"PRIuUOFF_T
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainenstatic void mbox_sync_array_delete_to(ARRAY_TYPE(sync_recs) *syncs_arr,
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen syncs = array_get_modifiable(syncs_arr, &count);
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen /* keep it */
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainenmbox_sync_read_next_mail(struct mbox_sync_context *sync_ctx,
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen /* get EOF */
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen (void)istream_raw_mbox_get_header_offset(sync_ctx->input);
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen istream_raw_mbox_get_start_offset(sync_ctx->input);
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen istream_raw_mbox_get_header_offset(sync_ctx->input);
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen mbox_sync_parse_next_mail(sync_ctx->input, mail_ctx);
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen i_assert(sync_ctx->input->v_offset != mail_ctx->mail.from_offset ||
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen istream_raw_mbox_get_body_size(sync_ctx->input,
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen i_assert(mail_ctx->mail.body_size < OFF_T_MAX);
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen if ((mail_ctx->mail.flags & MAIL_RECENT) != 0 && !mail_ctx->pseudo) {
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen /* need to add 'O' flag to Status-header */
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainenstatic bool mbox_sync_buf_have_expunges(ARRAY_TYPE(sync_recs) *syncs_arr)
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen unsigned int i, count;
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen for (i = 0; i < count; i++) {
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen if (syncs[i].type == MAIL_INDEX_SYNC_TYPE_EXPUNGE)
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainenstatic int mbox_sync_read_index_syncs(struct mbox_sync_context *sync_ctx,
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen struct mail_index_sync_rec *sync_rec = &sync_ctx->sync_rec;
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen /* nothing for this or the future ones */
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen mbox_sync_array_delete_to(&sync_ctx->syncs, uid);
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen sync_rec->type != MAIL_INDEX_SYNC_TYPE_APPEND &&
28542ffed61d0b860e0d1f9c837e0eb622df6b51Timo Sirainen (sync_rec->type != MAIL_INDEX_SYNC_TYPE_EXPUNGE ||
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen if (sync_rec->type == MAIL_INDEX_SYNC_TYPE_EXPUNGE)
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen ret = mail_index_sync_next(sync_ctx->index_sync_ctx, sync_rec);
e3df4d9063a06e0cd228a1713677ec105b0a4aa2Timo Sirainen mail_storage_set_index_error(&sync_ctx->mbox->ibox);
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen /* we're not going to write these yet */
7676a044f800a539bb0cbbc59fb9d1ad3f30ba7fTimo Sirainen *sync_expunge_r = mbox_sync_buf_have_expunges(&sync_ctx->syncs);
7676a044f800a539bb0cbbc59fb9d1ad3f30ba7fTimo Sirainenvoid mbox_sync_apply_index_syncs(struct mbox_sync_context *sync_ctx,
7676a044f800a539bb0cbbc59fb9d1ad3f30ba7fTimo Sirainen unsigned int i, count;
7676a044f800a539bb0cbbc59fb9d1ad3f30ba7fTimo Sirainen for (i = 0; i < count; i++) {
7676a044f800a539bb0cbbc59fb9d1ad3f30ba7fTimo Sirainen mail_index_sync_flags_apply(&syncs[i], &mail->flags);
28542ffed61d0b860e0d1f9c837e0eb622df6b51Timo Sirainen /* no existing keywords */
7676a044f800a539bb0cbbc59fb9d1ad3f30ba7fTimo Sirainen /* adding, create the array */
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainenmbox_sync_read_index_rec(struct mbox_sync_context *sync_ctx,
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen uint32_t uid, const struct mail_index_record **rec_r)
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen mail_index_view_get_messages_count(sync_ctx->sync_view);
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen mail_storage_set_index_error(&sync_ctx->mbox->ibox);
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen i_assert(ret != 0); /* we should be looking at head index */
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen /* externally expunged message, remove from index */
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen mail_index_expunge(sync_ctx->t, sync_ctx->idx_seq);
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen if (rec == NULL && uid < sync_ctx->idx_next_uid) {
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen /* this UID was already in index and it was expunged */
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen mail_storage_set_critical(STORAGE(sync_ctx->mbox->storage),
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen "mbox sync: Expunged message reappeared in mailbox %s "
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen "(UID %u < %u, seq=%u, idx_msgs=%u)",
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen sync_ctx->mbox->path, uid, sync_ctx->idx_next_uid,
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen /* new UID in the middle of the mailbox - shouldn't happen */
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen mail_storage_set_critical(STORAGE(sync_ctx->mbox->storage),
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen "mbox sync: UID inserted in the middle of mailbox %s "
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen "(%u > %u, seq=%u, idx_msgs=%u)", sync_ctx->mbox->path,
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen rec->uid, uid, sync_ctx->seq, messages_count);
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainenstatic int mbox_sync_find_index_md5(struct mbox_sync_context *sync_ctx,
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen unsigned char hdr_md5_sum[],
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen mail_index_view_get_messages_count(sync_ctx->sync_view);
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen mail_storage_set_index_error(&sync_ctx->mbox->ibox);
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen if (mail_index_lookup_ext(sync_ctx->sync_view,
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen mail_storage_set_index_error(&sync_ctx->mbox->ibox);
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen if (data != NULL && memcmp(data, hdr_md5_sum, 16) == 0)
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen /* externally expunged message, remove from index */
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen mail_index_expunge(sync_ctx->t, sync_ctx->idx_seq);
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainenmbox_sync_update_from_offset(struct mbox_sync_context *sync_ctx,
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen /* see if from_offset needs updating */
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen if (mail_index_lookup_ext(sync_ctx->sync_view,
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen mail_storage_set_index_error(&sync_ctx->mbox->ibox);
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen *((const uint64_t *)data) == mail->from_offset)
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen mail_index_update_ext(sync_ctx->t, sync_ctx->idx_seq,
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainenmbox_sync_update_index_keywords(struct mbox_sync_mail_context *mail_ctx)
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen struct mbox_sync_context *sync_ctx = mail_ctx->sync_ctx;
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen keywords = !array_is_created(&mail_ctx->mail.keywords) ?
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen mail_index_keywords_create(sync_ctx->t, NULL) :
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen mail_index_keywords_create_from_indexes(sync_ctx->t,
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen mail_index_update_keywords(sync_ctx->t, sync_ctx->idx_seq,
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainenmbox_sync_update_md5_if_changed(struct mbox_sync_mail_context *mail_ctx)
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen struct mbox_sync_context *sync_ctx = mail_ctx->sync_ctx;
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen if (mail_index_lookup_ext(sync_ctx->sync_view, sync_ctx->idx_seq,
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen mail_storage_set_index_error(&sync_ctx->mbox->ibox);
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen memcmp(mail_ctx->hdr_md5_sum, ext_data, 16) != 0) {
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen mail_index_update_ext(sync_ctx->t, sync_ctx->idx_seq,
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainenstatic int mbox_sync_update_index(struct mbox_sync_mail_context *mail_ctx,
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen struct mbox_sync_context *sync_ctx = mail_ctx->sync_ctx;
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen struct mbox_sync_mail *mail = &mail_ctx->mail;
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen /* new message */
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen mail_index_append(sync_ctx->t, mail->uid, &sync_ctx->idx_seq);
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen mail_index_update_flags(sync_ctx->t, sync_ctx->idx_seq,
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen mail_index_update_ext(sync_ctx->t, sync_ctx->idx_seq,
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen /* see if we need to update flags in index file. the flags in
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen sync records are automatically applied to rec->flags at the
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen end of index syncing, so calculate those new flags first */
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen /* get old keywords */
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen if (mail_index_lookup_keywords(sync_ctx->sync_view,
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen mail_storage_set_index_error(&sync_ctx->mbox->ibox);
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen mbox_sync_apply_index_syncs(sync_ctx, &idx_mail,
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen#define SYNC_FLAGS (MAIL_RECENT | MAIL_INDEX_MAIL_FLAG_DIRTY)
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen if ((idx_mail.flags & MAIL_INDEX_MAIL_FLAG_DIRTY) != 0) {
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen /* flags are dirty. ignore whatever was in the mbox,
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen but update recent/dirty flag states if needed. */
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen /* keep index's internal flags */
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen /* flags other than recent/dirty have changed */
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen mail_index_update_flags(sync_ctx->t, sync_ctx->idx_seq,
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen /* drop recent flag (it can only be dropped) */
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen /* dirty flag state changed */
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen if ((idx_mail.flags & MAIL_INDEX_MAIL_FLAG_DIRTY) == 0 &&
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen /* see if we need to update md5 sum. */
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen if (mbox_sync_update_md5_if_changed(mail_ctx) < 0)
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen (rec == NULL || (rec->flags & MAIL_INDEX_MAIL_FLAG_DIRTY) == 0 ||
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen index_mailbox_set_recent(&sync_ctx->mbox->ibox,
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen /* update from_offsets, but not if we're going to rewrite this message.
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen rewriting would just move it anyway. */
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen bool nocheck = rec == NULL || sync_ctx->expunged_space > 0;
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen if (mbox_sync_update_from_offset(sync_ctx, mail, nocheck) < 0)
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainenstatic int mbox_read_from_line(struct mbox_sync_mail_context *ctx)
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen struct istream *input = ctx->sync_ctx->file_input;
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen const unsigned char *data;
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen buffer_set_used_size(ctx->sync_ctx->from_line, 0);
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen from_line_size = ctx->hdr_offset - ctx->mail.from_offset;
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen buffer_append(ctx->sync_ctx->from_line, data, size);
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainenstatic int mbox_rewrite_base_uid_last(struct mbox_sync_context *sync_ctx)
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen const char *str;
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen unsigned int i;
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen i_assert(sync_ctx->base_uid_last_offset != 0);
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen /* first check that the 10 bytes are there and they're exactly as
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen expected. just an extra safety check to make sure we never write
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen to wrong location in the mbox file. */
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen ret = pread_full(sync_ctx->write_fd, buf, sizeof(buf),
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen mbox_set_syscall_error(sync_ctx->mbox, "pread_full()");
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen mail_storage_set_critical(STORAGE(sync_ctx->mbox->storage),
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen "X-IMAPbase uid-last unexpectedly points outside "
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen for (i = 0, uid_last = 0; i < sizeof(buf); i++) {
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen mail_storage_set_critical(STORAGE(sync_ctx->mbox->storage),
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen "X-IMAPbase uid-last unexpectedly lost in mbox file %s",
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen /* and write it */
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen str = t_strdup_printf("%010u", sync_ctx->next_uid - 1);
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen mbox_set_syscall_error(sync_ctx->mbox, "pwrite_full()");
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen sync_ctx->base_uid_last = sync_ctx->next_uid - 1;
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainenmbox_write_from_line(struct mbox_sync_mail_context *ctx)
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen if (pwrite_full(ctx->sync_ctx->write_fd, str_data(str), str_len(str),
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen mbox_set_syscall_error(ctx->sync_ctx->mbox, "pwrite_full()");
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainenstatic void update_from_offsets(struct mbox_sync_context *sync_ctx)
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen unsigned int i, count;
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen for (i = 0; i < count; i++) {
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen mail_index_update_ext(sync_ctx->t, mails[i].idx_seq,
28542ffed61d0b860e0d1f9c837e0eb622df6b51Timo Sirainenstatic void mbox_sync_handle_expunge(struct mbox_sync_mail_context *mail_ctx)
28542ffed61d0b860e0d1f9c837e0eb622df6b51Timo Sirainen struct mbox_sync_context *sync_ctx = mail_ctx->sync_ctx;
28542ffed61d0b860e0d1f9c837e0eb622df6b51Timo Sirainen mail_ctx->mail.offset = mail_ctx->mail.from_offset;
28542ffed61d0b860e0d1f9c837e0eb622df6b51Timo Sirainen mail_ctx->body_offset - mail_ctx->mail.from_offset +
28542ffed61d0b860e0d1f9c837e0eb622df6b51Timo Sirainen /* expunging first message, fix space to contain next
d229d26d263a57a77eec8fe7cba24fbfd9509966Timo Sirainen message's \n header too since it will be removed. */
28542ffed61d0b860e0d1f9c837e0eb622df6b51Timo Sirainen if (istream_raw_mbox_has_crlf_ending(sync_ctx->input)) {
d12ea923bd1d532c6f7fee44304dde8d0f321dd0Timo Sirainen /* uid-last offset is invalid now */
d12ea923bd1d532c6f7fee44304dde8d0f321dd0Timo Sirainen sync_ctx->expunged_space += mail_ctx->mail.space;
d12ea923bd1d532c6f7fee44304dde8d0f321dd0Timo Sirainenstatic int mbox_sync_handle_header(struct mbox_sync_mail_context *mail_ctx)
d12ea923bd1d532c6f7fee44304dde8d0f321dd0Timo Sirainen struct mbox_sync_context *sync_ctx = mail_ctx->sync_ctx;
d12ea923bd1d532c6f7fee44304dde8d0f321dd0Timo Sirainen if (sync_ctx->expunged_space > 0 && sync_ctx->need_space_seq == 0) {
d12ea923bd1d532c6f7fee44304dde8d0f321dd0Timo Sirainen /* move the header backwards to fill expunged space */
d12ea923bd1d532c6f7fee44304dde8d0f321dd0Timo Sirainen orig_from_offset = mail_ctx->mail.from_offset;
d12ea923bd1d532c6f7fee44304dde8d0f321dd0Timo Sirainen /* we're moving this mail to beginning of file.
088bfb6b26ee72122f2ed8a632c741b19d8821d7Timo Sirainen skip the initial \n (it's already counted in
088bfb6b26ee72122f2ed8a632c741b19d8821d7Timo Sirainen expunged_space) */
088bfb6b26ee72122f2ed8a632c741b19d8821d7Timo Sirainen /* read the From-line before rewriting overwrites it */
088bfb6b26ee72122f2ed8a632c741b19d8821d7Timo Sirainen ret = mbox_sync_try_rewrite(mail_ctx, move_diff);
088bfb6b26ee72122f2ed8a632c741b19d8821d7Timo Sirainen /* rewrite successful, write From-line to
088bfb6b26ee72122f2ed8a632c741b19d8821d7Timo Sirainen new location */
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen /* didn't have enough space, move the offset
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen back so seeking into it doesn't fail */
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen mail_ctx->mail.from_offset = orig_from_offset;
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen /* mark it dirty and do it later */
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen if ((ret = mbox_sync_try_rewrite(mail_ctx, 0)) < 0)
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen /* nothing to do */
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen if (ret == 0 && sync_ctx->need_space_seq == 0) {
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen /* first mail with no space to write it */
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen /* create dummy message to describe the expunged data */
move_diff = 0;
int ret;
bool deleted;
if (seq == 0) {
seq++;
if (ret < 0)
if (ret == 0) {
old_offset) < 0) {
uid = 0;
if (seq1 == 0) {
bool *skipped_mails)
int ret;
if (ret == 0) {
return ret;
bool partial)
int ret;
if (ret <= 0)
return ret;
uid = 0;
if (uid != 0) {
if (ret < 0)
if (ret == 0) {
uid = 0;
} else if (uid == 0 &&
&rec) < 0)
&expunged) < 0)
if (!expunged) {
if (!expunged) {
if (!expunged) {
} else if (partial) {
&partial,
if (ret <= 0) {
if (ret < 0)
if (!skipped_mails)
unsigned int uid_validity;
trailer_size = 0;
trailer_size) < 0)
if (offset == 0) {
(unsigned int)ioloop_time;
if (ret > 0)
if (ret < 0)
unsigned int lock_id = 0;
bool delay_writes;
if (!changed)
lock_id = 0;
if (changed) {
if (ret <= 0) {
if (ret < 0)
if (lock_id != 0)
return ret;
if (lock_id != 0)
bool expunged;
if (lock_id != 0)
goto __nothing_to_do;
if (lock_id == 0) {
goto __again;
if (ret < 0)
if (ret < 0)
unsigned int read_lock_id = 0;
return ret;
struct mailbox_sync_context *
int ret = 0;
ioloop_time) {