size_t size;
int i, need, have;
+ if (ctx->header_first_change > pos)
+ ctx->header_first_change = pos;
+
/* how many bytes do we need? */
for (i = 0, need = 0; flags_list[i].chr != 0; i++) {
if ((ctx->mail.flags & flags_list[i].flag) != 0)
break;
}
- if (flags_list[i].chr == 0)
+ if (flags_list[i].chr != 0)
have++;
else {
/* save this one */
if (need < have)
str_delete(ctx->header, pos, have-need);
else if (need > have) {
- buffer_copy(ctx->header, pos + (have-need),
+ buffer_copy(ctx->header, pos + (need-have),
ctx->header, pos, (size_t)-1);
}
+ for (i = 0; i < MBOX_HDR_COUNT; i++) {
+ if (ctx->hdr_pos[i] > pos &&
+ ctx->hdr_pos[i] != (size_t)-1)
+ ctx->hdr_pos[i] += need - have;
+ }
+
/* @UNSAFE */
data = buffer_get_space_unsafe(ctx->header, pos, need);
for (i = 0, need = 0; flags_list[i].chr != 0; i++) {
int i, have_keywords;
old_hdr_size = ctx->body_offset - ctx->hdr_offset;
- new_hdr_size = str_len(ctx->header) + ctx->have_eoh;
+ new_hdr_size = str_len(ctx->header);
- if (ctx->seq == 1 && ctx->sync_ctx->base_uid_validity == 0) {
+ if (ctx->seq == 1 && ctx->hdr_pos[MBOX_HDR_X_IMAPBASE] == (size_t)-1) {
ctx->hdr_pos[MBOX_HDR_X_IMAPBASE] = str_len(ctx->header);
str_printfa(ctx->header, "X-IMAPbase: %u %u",
- ctx->sync_ctx->hdr->uid_validity,
- ctx->sync_ctx->next_uid);
+ ctx->sync_ctx->base_uid_validity,
+ ctx->sync_ctx->next_uid-1);
//FIXME:keywords_append(ctx, all_keywords);
str_append_c(ctx->header, '\n');
}
+ i_assert(ctx->sync_ctx->base_uid_validity != 0);
if (ctx->hdr_pos[MBOX_HDR_X_UID] == (size_t)-1) {
if (ctx->mail.uid == 0)
if (ctx->hdr_pos[MBOX_HDR_STATUS] == (size_t)-1 &&
(ctx->mail.flags & STATUS_FLAGS_MASK) != 0) {
+ ctx->mail.flags |= MBOX_NONRECENT;
ctx->hdr_pos[MBOX_HDR_STATUS] = str_len(ctx->header);
str_append(ctx->header, "Status: ");
status_flags_append(ctx, mbox_status_flags);
if (ctx->header_first_change == (size_t)-1)
ctx->header_first_change = new_hdr_size;
ctx->header_last_change = (size_t)-1;
- ctx->mail.space -= str_len(ctx->header) -
- (new_hdr_size - ctx->have_eoh);
+ ctx->mail.space -= str_len(ctx->header) - new_hdr_size;
if (ctx->mail.space > 0) {
/* we should rewrite this header, so offset
must be broken if it's used anymore. */
offset to point back to beginning of headers */
ctx->mail.offset = ctx->hdr_offset;
}
- new_hdr_size = str_len(ctx->header) + ctx->have_eoh;
}
if (ctx->header_first_change == (size_t)-1) {
if (memcmp(old_keywords, ctx->mail.keywords,
INDEX_KEYWORDS_BYTE_COUNT) != 0)
mbox_sync_update_xkeywords(ctx);
+ } else {
+ if ((ctx->mail.flags & MBOX_NONRECENT) == 0) {
+ ctx->mail.flags |= MBOX_NONRECENT;
+ mbox_sync_update_status(ctx);
+ }
}
mbox_sync_add_missing_headers(ctx);
const struct mbox_sync_mail *mail)
{
if ((ctx->mail.flags & STATUS_FLAGS_MASK) !=
- (mail->flags & STATUS_FLAGS_MASK)) {
+ (mail->flags & STATUS_FLAGS_MASK) ||
+ (ctx->mail.flags & MBOX_NONRECENT) == 0) {
ctx->mail.flags = (ctx->mail.flags & ~STATUS_FLAGS_MASK) |
- (mail->flags & STATUS_FLAGS_MASK);
+ (mail->flags & STATUS_FLAGS_MASK) | MBOX_NONRECENT;
mbox_sync_update_status(ctx);
}
if ((ctx->mail.flags & XSTATUS_FLAGS_MASK) !=
#include "istream-raw-mbox.h"
#include "mbox-storage.h"
#include "mbox-file.h"
+#include "mbox-lock.h"
#include "mbox-sync-private.h"
#include <sys/stat.h>
static int mbox_sync_grow_file(struct mbox_sync_context *sync_ctx,
- struct mbox_sync_mail *mail, uoff_t body_offset,
+ struct mbox_sync_mail_context *mail_ctx,
uoff_t grow_size)
{
- char spaces[1024];
- uoff_t offset, size;
+ uoff_t src_offset, file_size;
i_assert(grow_size > 0);
- memset(spaces, ' ', sizeof(spaces));
-
- size = sync_ctx->input->v_offset + grow_size;
- if (file_set_size(sync_ctx->fd, size) < 0)
+ /* put the extra space between last message's header and body */
+ file_size = i_stream_get_size(sync_ctx->file_input) + grow_size;
+ if (file_set_size(sync_ctx->fd, file_size) < 0)
return -1;
- if (mail->space <= 0) {
- /* no X-Keywords header - place it at the end. */
- grow_size += 13;
-
- offset = body_offset-1;
- if (mbox_move(sync_ctx, body_offset-1 + size,
- offset, (uoff_t)-1) < 0)
- return -1;
- if (pwrite_full(sync_ctx->fd, "X-Keywords: ", 12, offset) < 0)
- return -1;
- if (pwrite_full(sync_ctx->fd, "\n", 1,
- offset + grow_size-1) < 0)
- return -1;
- grow_size -= 13; offset += 12;
-
- /* FIXME: can this break anything? X-Keywords text might
- have been already included in space calculation. now we
- have more.. */
- mail->offset = offset;
- mail->space += grow_size;
- } else {
- offset = mail->offset;
- if (mbox_move(sync_ctx, offset + grow_size,
- offset, (uoff_t)-1) < 0)
- return -1;
- }
-
- while (grow_size >= sizeof(spaces)) {
- if (pwrite_full(sync_ctx->fd, spaces,
- sizeof(spaces), offset) < 0)
- return -1;
- grow_size -= sizeof(spaces);
- offset += sizeof(spaces);
- }
-
- if (grow_size > 0) {
- if (pwrite_full(sync_ctx->fd, spaces, grow_size, offset) < 0)
- return -1;
- }
+ src_offset = mail_ctx->body_offset;
+ mail_ctx->body_offset += grow_size;
+ if (mbox_move(sync_ctx, mail_ctx->body_offset, src_offset,
+ file_size - mail_ctx->body_offset) < 0)
+ return -1;
istream_raw_mbox_flush(sync_ctx->input);
return 0;
buffer_set_used_size(syncs_buf, dest * sizeof(*sync));
}
-static int
+static void
mbox_sync_next_mail(struct mbox_sync_context *sync_ctx,
struct mbox_sync_mail_context *mail_ctx, uint32_t seq)
{
istream_raw_mbox_get_header_offset(sync_ctx->input);
mbox_sync_parse_next_mail(sync_ctx->input, mail_ctx);
- if (sync_ctx->input->v_offset == from_offset) {
- /* this was the last mail */
- return 0;
- }
+ i_assert(sync_ctx->input->v_offset != from_offset);
mail_ctx->mail.body_size =
istream_raw_mbox_get_body_size(sync_ctx->input,
/* save the offset permanently with recent flag state */
from_offset <<= 1;
- if ((mail_ctx->mail.flags & MBOX_NONRECENT) == 0)
+ if ((mail_ctx->mail.flags & MBOX_NONRECENT) == 0) {
+ /* need to add 'O' flag to Status-header */
+ mail_ctx->need_rewrite = TRUE;
from_offset |= 1;
+ }
buffer_append(sync_ctx->ibox->mbox_data_buf, &from_offset,
sizeof(from_offset));
- return 1;
}
-int mbox_sync(struct index_mailbox *ibox, int last_commit)
+static void mbox_sync_apply_index_syncs(buffer_t *syncs_buf, uint8_t *flags,
+ keywords_mask_t keywords)
+{
+ const struct mail_index_sync_rec *sync;
+ size_t size, i;
+
+ sync = buffer_get_data(syncs_buf, &size);
+ size /= sizeof(*sync);
+
+ for (i = 0; i < size; i++)
+ mail_index_sync_flags_apply(&sync[i], flags, keywords);
+}
+
+static int mbox_sync_do(struct index_mailbox *ibox,
+ struct mail_index_sync_ctx *index_sync_ctx,
+ struct mail_index_view *sync_view)
{
struct mbox_sync_context sync_ctx;
struct mbox_sync_mail_context mail_ctx;
- struct mail_index_sync_ctx *index_sync_ctx;
struct mail_index_sync_rec sync_rec;
- struct mail_index_view *sync_view;
struct mail_index_transaction *t;
const struct mail_index_header *hdr;
const struct mail_index_record *rec;
struct istream *input;
uint32_t seq, need_space_seq, idx_seq, messages_count;
+ uint8_t new_flags;
off_t space_diff;
uoff_t offset, extra_space;
buffer_t *mails, *syncs;
size_t size;
struct stat st;
- int readonly, ret = 0;
-
- if (last_commit) {
- seq = ibox->commit_log_file_seq;
- offset = ibox->commit_log_file_offset;
- } else {
- seq = (uint32_t)-1;
- offset = (uoff_t)-1;
- }
-
- ret = mail_index_sync_begin(ibox->index, &index_sync_ctx, &sync_view,
- seq, offset);
- if (ret <= 0)
- return ret;
-
- if (mbox_file_open_stream(ibox) < 0)
- return -1;
-
- if (mail_index_get_header(sync_view, &hdr) < 0)
- return -1;
+ int ret = 0;
t = mail_index_transaction_begin(sync_view, FALSE);
buffer_set_used_size(ibox->mbox_data_buf, 0);
}
- readonly = FALSE; // FIXME
- // FIXME: lock the file
-
memset(&sync_ctx, 0, sizeof(sync_ctx));
sync_ctx.ibox = ibox;
sync_ctx.file_input = ibox->mbox_file_stream;
sync_ctx.input = ibox->mbox_stream;
sync_ctx.fd = ibox->mbox_fd;
- sync_ctx.hdr = hdr;
sync_ctx.header = str_new(default_pool, 4096);
input = sync_ctx.input;
messages_count = mail_index_view_get_message_count(sync_view);
space_diff = 0; need_space_seq = 0; idx_seq = 0; rec = NULL;
- for (seq = 1; !input->eof; seq++) {
- ret = 1;
+ for (seq = 1;; seq++) {
+ /* set input->eof */
+ (void)istream_raw_mbox_get_header_offset(input);
+ if (input->eof)
+ break;
/* get all sync records related to this message */
+ ret = 1;
mbox_sync_buffer_delete_old(syncs, seq);
- while (sync_rec.seq2 <= seq && ret > 0) {
- if (sync_rec.seq2 != 0) {
+ while (seq >= sync_rec.seq1 && ret > 0) {
+ if (sync_rec.seq1 != 0) {
+ i_assert(seq <= sync_rec.seq2);
buffer_append(syncs, &sync_rec,
sizeof(sync_rec));
}
ret = mail_index_sync_next(index_sync_ctx, &sync_rec);
+ if (ret == 0)
+ memset(&sync_rec, 0, sizeof(sync_rec));
}
if (ret < 0)
break;
- ret = mbox_sync_next_mail(&sync_ctx, &mail_ctx, seq);
- if (ret <= 0)
- break;
+ mbox_sync_next_mail(&sync_ctx, &mail_ctx, seq);
+
+ if (seq == 1 && sync_ctx.base_uid_validity == 0) {
+ if (mail_index_get_header(sync_view, &hdr) < 0) {
+ ret = -1;
+ break;
+ }
+ sync_ctx.base_uid_validity = hdr->uid_validity;
+ }
if ((mail_ctx.need_rewrite ||
- buffer_get_used_size(syncs) != 0) && !readonly) {
+ buffer_get_used_size(syncs) != 0) && !ibox->readonly) {
+ if (ibox->mbox_lock_type == F_RDLCK) {
+ ret = -2;
+ break;
+ }
+
mbox_sync_update_header(&mail_ctx, syncs);
if ((ret = mbox_sync_try_rewrite(&mail_ctx)) < 0)
return -1;
if (rec != NULL) {
/* see if flags changed */
- if ((rec->flags & MAIL_FLAGS_MASK) !=
- (mail_ctx.mail.flags & MAIL_FLAGS_MASK) ||
- memcmp(rec->keywords, mail_ctx.mail.keywords,
+ keywords_mask_t old_keywords;
+ uint8_t old_flags;
+
+ old_flags = rec->flags;
+ memcpy(old_keywords, rec->keywords,
+ INDEX_KEYWORDS_BYTE_COUNT);
+ mbox_sync_apply_index_syncs(syncs, &old_flags,
+ old_keywords);
+
+ new_flags = (rec->flags & ~MAIL_FLAGS_MASK) |
+ (mail_ctx.mail.flags &
+ (MAIL_FLAGS_MASK^MAIL_RECENT));
+
+ if (old_flags != new_flags ||
+ memcmp(old_keywords, mail_ctx.mail.keywords,
INDEX_KEYWORDS_BYTE_COUNT) != 0) {
- uint8_t new_flags =
- (rec->flags & ~MAIL_FLAGS_MASK) |
- (mail_ctx.mail.flags & MAIL_FLAGS_MASK);
mail_index_update_flags(t, idx_seq,
MODIFY_REPLACE,
new_flags,
mail_ctx.mail.keywords);
}
+
+ /* we used this record */
rec = NULL;
} else {
/* new message */
mail_index_append(t, mail_ctx.mail.uid, &idx_seq);
+ new_flags = mail_ctx.mail.flags &
+ (MAIL_FLAGS_MASK^MAIL_RECENT);
mail_index_update_flags(t, idx_seq, MODIFY_REPLACE,
- mail_ctx.mail.flags & MAIL_FLAGS_MASK,
- mail_ctx.mail.keywords);
+ new_flags,
+ mail_ctx.mail.keywords);
}
istream_raw_mbox_next(input, mail_ctx.mail.body_size);
- offset = input->v_offset;
+ offset = istream_raw_mbox_get_start_offset(input);
if (need_space_seq != 0) {
buffer_append(mails, &mail_ctx.mail,
rewrite, so make sure we don't try to access
it */
memset(&mail_ctx, 0, sizeof(mail_ctx));
- i_stream_seek(input, input->v_offset);
+ i_stream_seek(input, offset);
need_space_seq = 0;
}
}
i_assert(space_diff < 0);
extra_space = MBOX_HEADER_EXTRA_SPACE *
((seq-1) - need_space_seq);
- if (mbox_sync_grow_file(&sync_ctx, &mail_ctx.mail,
- mail_ctx.body_offset,
+ if (mbox_sync_grow_file(&sync_ctx, &mail_ctx,
-space_diff + extra_space) < 0)
ret = -1;
- else if (mbox_sync_rewrite(&sync_ctx, mails, need_space_seq,
- seq-1, extra_space) < 0)
+ else if (mbox_sync_try_rewrite(&mail_ctx) < 0)
ret = -1;
+ else if (--seq != need_space_seq) {
+ buffer_set_used_size(mails,
+ (seq-1) * sizeof(mail_ctx.mail));
+ buffer_append(mails, &mail_ctx.mail,
+ sizeof(mail_ctx.mail));
+
+ if (mbox_sync_rewrite(&sync_ctx, mails, need_space_seq,
+ seq, extra_space) < 0)
+ ret = -1;
+ }
}
+ if (rec != NULL)
+ mail_index_expunge(t, idx_seq);
+ while (idx_seq < messages_count)
+ mail_index_expunge(t, ++idx_seq);
+
if (sync_ctx.base_uid_last+1 != sync_ctx.next_uid) {
// FIXME: rewrite X-IMAPbase header
}
- /* only syncs left should be just appends which weren't synced yet.
- we'll just ignore them, as we've overwritten those above. */
- while ((ret = mail_index_sync_next(index_sync_ctx, &sync_rec)) > 0) {
- i_assert(sync_rec.type == MAIL_INDEX_SYNC_TYPE_APPEND);
- }
-
- if (fstat(ibox->mbox_fd, &st) < 0) {
- mbox_set_syscall_error(ibox, "fstat()");
- ret = -1;
+ if (ret >= 0) {
+ /* only syncs left should be just appends which weren't synced
+ yet. we'll just ignore them, as we've overwritten those
+ above. */
+ while ((ret = mail_index_sync_next(index_sync_ctx,
+ &sync_rec)) > 0) {
+ i_assert(sync_rec.type == MAIL_INDEX_SYNC_TYPE_APPEND);
+ }
}
if (ret < 0)
}
}
+ if (ret == 0) {
+ if (fstat(ibox->mbox_fd, &st) < 0) {
+ mbox_set_syscall_error(ibox, "fstat()");
+ ret = -1;
+ }
+ }
if (ret < 0) {
st.st_mtime = 0;
st.st_size = 0;
}
- if (mail_index_sync_end(index_sync_ctx, st.st_mtime, st.st_size) < 0)
- ret = -1;
+ if (ret != -2) {
+ if (mail_index_sync_end(index_sync_ctx,
+ st.st_mtime, st.st_size) < 0)
+ ret = -1;
+ }
if (ret == 0) {
ibox->commit_log_file_seq = 0;
str_free(sync_ctx.header);
buffer_free(mails);
buffer_free(syncs);
- return ret < 0 ? -1 : 0;
+ return ret < 0 ? ret : 0;
+}
+
+int mbox_sync(struct index_mailbox *ibox, int last_commit)
+{
+ struct mail_index_sync_ctx *index_sync_ctx;
+ struct mail_index_view *sync_view;
+ unsigned int lock_id;
+ uint32_t seq;
+ uoff_t offset;
+ int ret, lock_type;
+
+ if (last_commit) {
+ seq = ibox->commit_log_file_seq;
+ offset = ibox->commit_log_file_offset;
+ } else {
+ seq = (uint32_t)-1;
+ offset = (uoff_t)-1;
+ }
+
+ ret = mail_index_sync_begin(ibox->index, &index_sync_ctx, &sync_view,
+ seq, offset);
+ if (ret <= 0)
+ return ret;
+
+ lock_type = mail_index_sync_have_more(index_sync_ctx) ?
+ F_WRLCK : F_RDLCK;
+ if (mbox_lock(ibox, lock_type, &lock_id) > 0 &&
+ mbox_file_open_stream(ibox) == 0) {
+ ret = mbox_sync_do(ibox, index_sync_ctx, sync_view);
+ if (ret == -2) {
+ /* read lock -> write lock. do it again. */
+ (void)mbox_unlock(ibox, lock_id);
+ lock_id = 0;
+ if (mbox_lock(ibox, F_WRLCK, &lock_id) <= 0)
+ ret = -1;
+ else if (mbox_file_open_stream(ibox) < 0)
+ ret = -1;
+ else {
+ ret = mbox_sync_do(ibox, index_sync_ctx,
+ sync_view);
+ }
+ }
+ } else {
+ (void)mail_index_sync_end(index_sync_ctx, 0, 0);
+ ret = -1;
+ }
+
+ if (lock_id != 0) {
+ if (mbox_unlock(ibox, lock_id) < 0)
+ ret = -1;
+ }
+
+ return ret;
}
int mbox_storage_sync(struct mailbox *box, enum mailbox_sync_flags flags)