istream-concat.c revision 4ce6338bf945cccfff9e4ce7cc6aa2246851b84a
e59faf65ce864fe95dc00f5d52b8323cdbd0608aTimo Sirainen/* Copyright (c) 2007 Dovecot authors, see the included COPYING file */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#include "lib.h"
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#include "buffer.h"
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#include "istream-internal.h"
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#include "istream-concat.h"
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstruct concat_istream {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct istream_private istream;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct istream **input, *cur_input;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen uoff_t *input_size;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen unsigned int cur_idx, unknown_size_idx;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen size_t prev_size;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen};
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstatic void i_stream_concat_close(struct iostream_private *stream)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen{
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct concat_istream *cstream = (struct concat_istream *)stream;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen unsigned int i;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen for (i = 0; cstream->input[i] != NULL; i++)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen i_stream_close(cstream->input[i]);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen}
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstatic void i_stream_concat_destroy(struct iostream_private *stream)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen{
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct concat_istream *cstream = (struct concat_istream *)stream;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen unsigned int i;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen for (i = 0; cstream->input[i] != NULL; i++)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen i_stream_unref(&cstream->input[i]);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen}
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstatic void
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Siraineni_stream_concat_set_max_buffer_size(struct iostream_private *stream,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen size_t max_size)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen{
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct concat_istream *cstream = (struct concat_istream *)stream;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen unsigned int i;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen cstream->istream.max_buffer_size = max_size;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen for (i = 0; cstream->input[i] != NULL; i++)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen i_stream_set_max_buffer_size(cstream->input[i], max_size);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen}
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstatic void i_stream_concat_read_next(struct concat_istream *cstream)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen{
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen const unsigned char *data;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen size_t data_size, size;
e6440616c02bb1404dc35debf45d9741260c7831Timo Sirainen
e6440616c02bb1404dc35debf45d9741260c7831Timo Sirainen i_assert(cstream->cur_input->eof);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen cstream->cur_idx++;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen cstream->cur_input = cstream->input[cstream->cur_idx];
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen i_stream_seek(cstream->cur_input, 0);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (cstream->istream.pos == cstream->istream.skip) {
e6440616c02bb1404dc35debf45d9741260c7831Timo Sirainen i_assert(cstream->prev_size == 0);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen cstream->istream.skip = 0;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen cstream->istream.pos = 0;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen return;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen }
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* we need to keep the current data */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen data = cstream->istream.buffer + cstream->istream.skip;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen data_size = cstream->istream.pos - cstream->istream.skip;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen cstream->istream.skip = cstream->istream.pos = 0;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* we already verified that the data size is less than the
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen maximum buffer size */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (!i_stream_get_buffer_space(&cstream->istream, data_size, &size))
e6440616c02bb1404dc35debf45d9741260c7831Timo Sirainen i_unreached();
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen i_assert(size >= data_size);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen cstream->prev_size = data_size;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen memcpy(cstream->istream.w_buffer, data, data_size);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen cstream->istream.pos = data_size;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen}
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstatic ssize_t i_stream_concat_read(struct istream_private *stream)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen{
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct concat_istream *cstream = (struct concat_istream *)stream;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen const unsigned char *data;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen size_t size, pos, skip;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen ssize_t ret;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen bool last_stream;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (cstream->cur_input == NULL)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen return -1;
e6440616c02bb1404dc35debf45d9741260c7831Timo Sirainen
e6440616c02bb1404dc35debf45d9741260c7831Timo Sirainen skip = stream->skip;
e6440616c02bb1404dc35debf45d9741260c7831Timo Sirainen if (cstream->prev_size > 0) {
e6440616c02bb1404dc35debf45d9741260c7831Timo Sirainen if (stream->skip < cstream->prev_size) {
e6440616c02bb1404dc35debf45d9741260c7831Timo Sirainen cstream->prev_size -= stream->skip;
e6440616c02bb1404dc35debf45d9741260c7831Timo Sirainen skip = 0;
e6440616c02bb1404dc35debf45d9741260c7831Timo Sirainen } else {
e6440616c02bb1404dc35debf45d9741260c7831Timo Sirainen /* we don't need the buffer anymore */
e6440616c02bb1404dc35debf45d9741260c7831Timo Sirainen skip -= cstream->prev_size;
e6440616c02bb1404dc35debf45d9741260c7831Timo Sirainen stream->skip -= cstream->prev_size;
e6440616c02bb1404dc35debf45d9741260c7831Timo Sirainen cstream->prev_size = 0;
e6440616c02bb1404dc35debf45d9741260c7831Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen i_free_and_null(stream->w_buffer);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen stream->buffer = NULL;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen stream->buffer_size = 0;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen }
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen }
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen i_stream_skip(cstream->cur_input, skip);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen data = i_stream_get_data(cstream->cur_input, &pos);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (pos > stream->pos)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen ret = 0;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen else {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* need to read more */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen ret = i_stream_read(cstream->cur_input);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (ret == -2 || ret == 0)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen return ret;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (ret == -1 && stream->istream.stream_errno != 0) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen stream->istream.stream_errno =
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen cstream->cur_input->stream_errno;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen return -1;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen }
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* we either read something or we're at EOF */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen last_stream = cstream->input[cstream->cur_idx+1] == NULL;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (ret == -1 && !last_stream) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (stream->pos - stream->skip >=
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen stream->max_buffer_size)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen return -2;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen i_stream_concat_read_next(cstream);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen return i_stream_concat_read(stream);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen }
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen stream->istream.eof = cstream->cur_input->eof && last_stream;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen data = i_stream_get_data(cstream->cur_input, &pos);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen }
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (stream->w_buffer == NULL) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen stream->buffer = data;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen stream->pos -= stream->skip;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen stream->skip = 0;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen } else {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (!i_stream_get_buffer_space(stream, pos, &size))
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen return -2;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (pos > size)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen pos = size;
memcpy(stream->w_buffer + stream->pos, data, pos);
}
pos += cstream->prev_size;
ret = pos > stream->pos ? (ssize_t)(pos - stream->pos) :
(ret == 0 ? 0 : -1);
stream->pos = pos;
return ret;
}
static unsigned int
find_v_offset(struct concat_istream *cstream, uoff_t *v_offset)
{
const struct stat *st;
unsigned int i;
for (i = 0; cstream->input[i] != NULL; i++) {
if (*v_offset == 0) {
/* seek to beginning of this stream */
break;
}
if (i == cstream->unknown_size_idx) {
/* we'll need to figure out this stream's size */
st = i_stream_stat(cstream->input[i], TRUE);
if (st == NULL) {
cstream->istream.istream.stream_errno =
cstream->input[i]->stream_errno;
return (unsigned int)-1;
}
/* @UNSAFE */
cstream->input_size[i] = st->st_size;
cstream->unknown_size_idx = i + 1;
}
if (*v_offset < cstream->input_size[i])
break;
*v_offset -= cstream->input_size[i];
}
return i;
}
static void i_stream_concat_seek(struct istream_private *stream,
uoff_t v_offset, bool mark ATTR_UNUSED)
{
struct concat_istream *cstream = (struct concat_istream *)stream;
stream->istream.stream_errno = 0;
stream->istream.v_offset = v_offset;
stream->skip = stream->pos = 0;
cstream->prev_size = 0;
cstream->cur_idx = find_v_offset(cstream, &v_offset);
if (cstream->cur_idx == (unsigned int)-1) {
cstream->cur_input = NULL;
return;
}
cstream->cur_input = cstream->input[cstream->cur_idx];
i_stream_seek(cstream->cur_input, v_offset);
}
static const struct stat *
i_stream_concat_stat(struct istream_private *stream, bool exact ATTR_UNUSED)
{
struct concat_istream *cstream = (struct concat_istream *)stream;
uoff_t v_offset = (uoff_t)-1;
unsigned int i;
/* make sure we have all sizes */
(void)find_v_offset(cstream, &v_offset);
stream->statbuf.st_size = 0;
for (i = 0; i < cstream->unknown_size_idx; i++)
stream->statbuf.st_size += cstream->input_size[i];
return &stream->statbuf;
}
struct istream *i_stream_create_concat(struct istream *input[])
{
struct concat_istream *cstream;
unsigned int count;
size_t max_buffer_size = I_STREAM_MIN_SIZE;
bool blocking = TRUE, seekable = TRUE;
/* if any of the streams isn't blocking or seekable, set ourself also
nonblocking/nonseekable */
for (count = 0; input[count] != NULL; count++) {
size_t cur_max = input[count]->real_stream->max_buffer_size;
if (cur_max > max_buffer_size)
max_buffer_size = cur_max;
if (!input[count]->blocking)
blocking = FALSE;
if (!input[count]->seekable)
seekable = FALSE;
i_stream_ref(input[count]);
}
i_assert(count != 0);
cstream = i_new(struct concat_istream, 1);
cstream->input = i_new(struct istream *, count + 1);
cstream->input_size = i_new(uoff_t, count + 1);
memcpy(cstream->input, input, sizeof(*input) * count);
cstream->cur_input = cstream->input[0];
i_stream_seek(cstream->cur_input, 0);
cstream->istream.iostream.close = i_stream_concat_close;
cstream->istream.iostream.destroy = i_stream_concat_destroy;
cstream->istream.iostream.set_max_buffer_size =
i_stream_concat_set_max_buffer_size;
cstream->istream.max_buffer_size = max_buffer_size;
cstream->istream.read = i_stream_concat_read;
cstream->istream.seek = i_stream_concat_seek;
cstream->istream.stat = i_stream_concat_stat;
cstream->istream.istream.blocking = blocking;
cstream->istream.istream.seekable = seekable;
return i_stream_create(&cstream->istream, -1, 0);
}