/* Copyright (c) 2004-2018 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "array.h"
#include "buffer.h"
#include "istream.h"
#include "ostream.h"
#include "str.h"
#include "write-full.h"
#include "message-parser.h"
#include "mbox-storage.h"
#include "mbox-sync-private.h"
#include "istream-raw-mbox.h"
{
int ret;
return 0;
"o_stream_seek()");
return -1;
}
/* we're moving data within a file. it really shouldn't be failing at
this point or we're corrupted. */
if (input->stream_errno != 0) {
"read() failed with mbox: %s",
ret = -1;
} else if (output->stream_errno != 0) {
"write() failed with mbox: %s",
ret = -1;
ret = -1;
} else {
ret = 0;
}
return ret;
}
{
return -1;
}
}
return -1;
}
return 0;
}
{
const unsigned char *data;
void *p;
/* update the header using the existing offset.
otherwise we might chose wrong header and just decrease
the available space */
} else {
/* Append at the end of X-Keywords header,
or X-UID if it doesn't exist */
}
/* possibly continues in next line */
break;
}
}
/* pos points to end of header now, and start_pos to beginning
of whitespace. */
}
{
const unsigned char *data;
/* find the end of the LWSP */
/* possibly continues in next line */
break;
}
}
}
return;
/* and remove what we can */
/* remove it all */
return;
}
/* we have more space than needed. since we're removing from
the beginning of header instead of end, we don't have to
worry about multiline-headers. */
else
last_line_pos -= *size;
*size = 0;
}
}
{
};
int i;
for (i = 0; i < 3 && size > 0; i++) {
pos = space_positions[i];
&size);
}
}
/* FIXME: see if we could remove X-Keywords header completely */
}
{
/* we wrote the first mail. update last-uid offset so we can find
it later */
if (ctx->imapbase_updated) {
/* update so a) we don't try to update it later needlessly,
b) if we do actually update it, we see the correct value */
}
}
{
if (new_hdr_size <= old_hdr_size) {
/* add space. note that we must call add_space() even if we're
not adding anything so mail.offset gets fixed. */
} else if (new_hdr_size > old_hdr_size) {
/* try removing the space where we can */
if (new_hdr_size <= old_hdr_size) {
/* good, we removed enough. */
} else if (move_diff < 0 &&
/* moving backwards - we can use the extra space from
it, just update expunged_space accordingly */
} else {
/* couldn't get enough space */
return 0;
}
}
/* no changes actually. we get here if index sync record told
us to do something that was already there */
return 1;
}
if (move_diff != 0) {
/* forget about partial write optimizations */
ctx->header_first_change = 0;
ctx->header_last_change = 0;
}
ctx->header_last_change != 0)
move_diff) < 0) {
return -1;
}
if (sync_ctx->dest_first_mail &&
/* the position might have moved as a result of moving
whitespace */
}
return 1;
}
struct mbox_sync_mail_context *mail_ctx,
struct mbox_sync_mail *mails,
{
unsigned int first_mail_expunge_extra;
"Couldn't get header offset for seq=%u", seq);
return -1;
}
/* This will force the UID to be the one that we originally
assigned to it, regardless of whether it's broken or not in
the file. */
} else {
/* Pseudo mail shouldn't have X-UID header at all */
sync_ctx->prev_msg_uid = 0;
}
first_mail_expunge_extra = 1 +
first_mail_expunge_extra - expunged_space != 0) {
} else {
/* we need to skip over the initial \n (it's already counted in
expunged_space) */
}
return -1;
/* set next_uid back before updating the headers. this is important
if we're updating the first message to make X-IMAP[base] header
have the correct value. */
/* remove all possible spacing before updating */
}
} else {
/* updating might just try to add headers and mess up our
calculations completely. so only add the EOH here. */
}
return 0;
}
struct mbox_sync_mail_context *mail_ctx,
struct mbox_sync_mail *mails,
{
return -1;
expunged_space) < 0)
return -1;
mail_ctx = &new_mail_ctx;
} else {
}
/* this check works only if we're doing the first
write, or if the file size was changed externally */
"seq=%u uid=%u uid_broken=%d "
"originally needed %"PRIuUOFF_T
return -1;
}
}
if (first_nonexpunged && expunged_space > 0) {
/* move From-line (after parsing headers so we don't
overwrite them) */
return -1;
}
/* don't touch spacing */
padding);
} else {
}
/* move the body of this message and headers of next message forward,
then write the headers */
return -1;
/* the header may actually be moved backwards if there was expunged
space which we wanted to remove */
return -1;
}
if (sync_ctx->dest_first_mail) {
}
return 0;
}
struct mbox_sync_mail_context *mail_ctx,
{
unsigned int count;
int ret = 0;
/* if there's expunges in mails[], we would get more correct balancing
by counting only them here. however, that might make us overwrite
data which hasn't yet been copied backwards. to avoid too much
complexity, we just leave all the rest of the extra space to first
mail */
/* after expunge the next mail must have been missing space, or we
would have moved it backwards already */
expunged_space = 0;
for (first_nonexpunged_idx = 0;; first_nonexpunged_idx++) {
break;
}
/* start moving backwards. */
while (idx > first_nonexpunged_idx) {
idx--;
if (idx == first_nonexpunged_idx) {
/* give the rest of the extra space to first mail.
we might also have to move the mail backwards to
fill the expunged space */
}
/* give space to this mail. end_offset is left to
contain this message's From-line (ie. below we
move only headers + body). */
first_nonexpunged) < 0) {
ret = -1;
break;
}
} else {
/* this mail provides more space. just move it forward
from the extra space offset and set end_offset to
point to beginning of extra space. that way the
header will be moved along with previous mail's
body.
if this is expunged mail, we're moving following
mail's From-line and maybe headers. */
end_offset - offset) < 0) {
ret = -1;
break;
}
padding_per_mail) < 0) {
ret = -1;
break;
}
}
}
}
if (ret == 0) {
}
return ret;
}