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