istream-concat.c revision 4afd5082f38342fd688acb5796912329f57dd02c
e59faf65ce864fe95dc00f5d52b8323cdbd0608aTimo Sirainen/* Copyright (c) 2007-2012 Dovecot authors, see the included COPYING file */
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen#include "lib.h"
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen#include "buffer.h"
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen#include "istream-private.h"
472369cba85d9f7c995dda60e7cd01d78b4a960aTimo Sirainen#include "istream-concat.h"
e28fa207d1a097fa6e4a867f74ee0761472ef1ceTimo Sirainen
37847ec8eaec9ad55c9df10ae109efe7b37ac573Timo Sirainenstruct concat_istream {
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen struct istream_private istream;
37847ec8eaec9ad55c9df10ae109efe7b37ac573Timo Sirainen
37847ec8eaec9ad55c9df10ae109efe7b37ac573Timo Sirainen struct istream **input, *cur_input;
37847ec8eaec9ad55c9df10ae109efe7b37ac573Timo Sirainen uoff_t *input_size;
37847ec8eaec9ad55c9df10ae109efe7b37ac573Timo Sirainen
37847ec8eaec9ad55c9df10ae109efe7b37ac573Timo Sirainen unsigned int cur_idx, unknown_size_idx;
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen size_t prev_stream_left, prev_skip;
ef50336eefcb9ba99f73c6af37420eaf8857a39bTimo Sirainen};
e28fa207d1a097fa6e4a867f74ee0761472ef1ceTimo Sirainen
37847ec8eaec9ad55c9df10ae109efe7b37ac573Timo Sirainenstatic void i_stream_concat_close(struct iostream_private *stream)
ef50336eefcb9ba99f73c6af37420eaf8857a39bTimo Sirainen{
91b203fd2132510a47a4b34252c0ae0efd688a19Timo Sirainen struct concat_istream *cstream = (struct concat_istream *)stream;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen unsigned int i;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen for (i = 0; cstream->input[i] != NULL; i++)
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainen i_stream_close(cstream->input[i]);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen}
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenstatic void i_stream_concat_destroy(struct iostream_private *stream)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen{
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen struct concat_istream *cstream = (struct concat_istream *)stream;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen unsigned int i;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen for (i = 0; cstream->input[i] != NULL; i++)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen i_stream_unref(&cstream->input[i]);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen i_free(cstream->input);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen i_free(cstream->input_size);
de486b59018016977015ef42e6071155b60e82e1Timo Sirainen i_free(cstream->istream.w_buffer);
de486b59018016977015ef42e6071155b60e82e1Timo Sirainen}
de486b59018016977015ef42e6071155b60e82e1Timo Sirainen
de486b59018016977015ef42e6071155b60e82e1Timo Sirainenstatic void
de486b59018016977015ef42e6071155b60e82e1Timo Siraineni_stream_concat_set_max_buffer_size(struct iostream_private *stream,
de486b59018016977015ef42e6071155b60e82e1Timo Sirainen size_t max_size)
de486b59018016977015ef42e6071155b60e82e1Timo Sirainen{
de486b59018016977015ef42e6071155b60e82e1Timo Sirainen struct concat_istream *cstream = (struct concat_istream *)stream;
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen unsigned int i;
220e21750948941dc6e33b8f11b552fa21d7f81eTimo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen cstream->istream.max_buffer_size = max_size;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen for (i = 0; cstream->input[i] != NULL; i++)
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen i_stream_set_max_buffer_size(cstream->input[i], max_size);
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen}
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainenstatic void i_stream_concat_read_next(struct concat_istream *cstream)
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen{
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen const unsigned char *data;
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen size_t data_size, size;
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen i_assert(cstream->cur_input->eof);
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen data = i_stream_get_data(cstream->cur_input, &data_size);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen cstream->cur_idx++;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen cstream->cur_input = cstream->input[cstream->cur_idx];
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen i_stream_seek(cstream->cur_input, 0);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen if (cstream->prev_stream_left > 0 || cstream->istream.pos == 0) {
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen cstream->prev_stream_left += data_size;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen i_assert(cstream->prev_stream_left ==
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen cstream->istream.pos - cstream->istream.skip);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen return;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen }
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen /* we already verified that the data size is less than the
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen maximum buffer size */
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen cstream->istream.pos = 0;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen if (data_size > 0) {
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen if (!i_stream_try_alloc(&cstream->istream, data_size, &size))
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen i_unreached();
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen i_assert(size >= data_size);
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen }
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen cstream->prev_stream_left = data_size;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen memcpy(cstream->istream.w_buffer, data, data_size);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen cstream->istream.skip = 0;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen cstream->istream.pos = data_size;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen}
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenstatic ssize_t i_stream_concat_read(struct istream_private *stream)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen{
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen struct concat_istream *cstream = (struct concat_istream *)stream;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen const unsigned char *data;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen size_t size, data_size, cur_data_pos, new_pos, bytes_skipped;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen size_t new_bytes_count;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen ssize_t ret;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen bool last_stream;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen if (cstream->cur_input == NULL) {
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen stream->istream.stream_errno = EINVAL;
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen return -1;
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen }
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen i_assert(stream->skip >= cstream->prev_skip);
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen bytes_skipped = stream->skip - cstream->prev_skip;
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen if (cstream->prev_stream_left == 0) {
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen /* no need to worry about buffers, skip everything */
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen } else if (bytes_skipped < cstream->prev_stream_left) {
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen /* we're still skipping inside buffer */
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen cstream->prev_stream_left -= bytes_skipped;
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen bytes_skipped = 0;
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen } else {
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen /* done with the buffer */
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen bytes_skipped -= cstream->prev_stream_left;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen cstream->prev_stream_left = 0;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen }
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen stream->pos -= bytes_skipped;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen stream->skip -= bytes_skipped;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen stream->buffer += bytes_skipped;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen cstream->prev_skip = stream->skip;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen i_stream_skip(cstream->cur_input, bytes_skipped);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen i_assert(stream->pos >= stream->skip + cstream->prev_stream_left);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen cur_data_pos = stream->pos - (stream->skip + cstream->prev_stream_left);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen data = i_stream_get_data(cstream->cur_input, &data_size);
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen if (data_size > cur_data_pos)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen ret = 0;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen else {
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen /* need to read more */
1701e3f91107051b1704721bf1dc1e32491faaf9Timo Sirainen i_assert(cur_data_pos == data_size);
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen ret = i_stream_read(cstream->cur_input);
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen if (ret == -2 || ret == 0)
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen return ret;
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen if (ret == -1 && cstream->cur_input->stream_errno != 0) {
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen stream->istream.stream_errno =
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen cstream->cur_input->stream_errno;
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen return -1;
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen }
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen /* we either read something or we're at EOF */
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen last_stream = cstream->input[cstream->cur_idx+1] == NULL;
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen if (ret == -1 && !last_stream) {
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen if (stream->pos >= stream->max_buffer_size)
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen return -2;
1701e3f91107051b1704721bf1dc1e32491faaf9Timo Sirainen
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen i_stream_concat_read_next(cstream);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen cstream->prev_skip = stream->skip;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen return i_stream_concat_read(stream);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen }
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen stream->istream.eof = cstream->cur_input->eof && last_stream;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen i_assert(ret != -1 || stream->istream.eof);
3fe67ec75ccae1230bb9eb9f16affc48377f6441Timo Sirainen data = i_stream_get_data(cstream->cur_input, &data_size);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen }
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen
2a6dcd984104fed84bed8795ccdfabb20e41ce52Timo Sirainen if (cstream->prev_stream_left == 0) {
2a6dcd984104fed84bed8795ccdfabb20e41ce52Timo Sirainen /* we can point directly to the current stream's buffers */
2a6dcd984104fed84bed8795ccdfabb20e41ce52Timo Sirainen stream->buffer = data;
2a6dcd984104fed84bed8795ccdfabb20e41ce52Timo Sirainen stream->pos -= stream->skip;
2a6dcd984104fed84bed8795ccdfabb20e41ce52Timo Sirainen stream->skip = 0;
2a6dcd984104fed84bed8795ccdfabb20e41ce52Timo Sirainen new_pos = data_size;
2a6dcd984104fed84bed8795ccdfabb20e41ce52Timo Sirainen } else if (data_size == cur_data_pos) {
2a6dcd984104fed84bed8795ccdfabb20e41ce52Timo Sirainen /* nothing new read */
2a6dcd984104fed84bed8795ccdfabb20e41ce52Timo Sirainen i_assert(ret == 0 || ret == -1);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen stream->buffer = stream->w_buffer;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen new_pos = stream->pos;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen } else {
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen /* we still have some of the previous stream left. merge the
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen new data with it. */
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen i_assert(data_size > cur_data_pos);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen new_bytes_count = data_size - cur_data_pos;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen if (!i_stream_try_alloc(stream, new_bytes_count, &size)) {
27586e4785d56aeb76e1fd96af8db799688dc64aTimo Sirainen stream->buffer = stream->w_buffer;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen return -2;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen }
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen stream->buffer = stream->w_buffer;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen if (new_bytes_count > size)
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen new_bytes_count = size;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen memcpy(stream->w_buffer + stream->pos,
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen data + cur_data_pos, new_bytes_count);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen new_pos = stream->pos + new_bytes_count;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen }
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen ret = new_pos > stream->pos ? (ssize_t)(new_pos - stream->pos) :
e5acc283bf030b0b5c79ca4e52d315c516a299faPascal Volk (ret == 0 ? 0 : -1);
e5acc283bf030b0b5c79ca4e52d315c516a299faPascal Volk stream->pos = new_pos;
e5acc283bf030b0b5c79ca4e52d315c516a299faPascal Volk cstream->prev_skip = stream->skip;
e5acc283bf030b0b5c79ca4e52d315c516a299faPascal Volk return ret;
e5acc283bf030b0b5c79ca4e52d315c516a299faPascal Volk}
e5acc283bf030b0b5c79ca4e52d315c516a299faPascal Volk
e5acc283bf030b0b5c79ca4e52d315c516a299faPascal Volkstatic unsigned int
e5acc283bf030b0b5c79ca4e52d315c516a299faPascal Volkfind_v_offset(struct concat_istream *cstream, uoff_t *v_offset)
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen{
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen const struct stat *st;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen unsigned int i;
5666a3d6a7ea89362b8d9e8b39b15424cd9d6388Timo Sirainen
1701e3f91107051b1704721bf1dc1e32491faaf9Timo Sirainen for (i = 0; cstream->input[i] != NULL; i++) {
1701e3f91107051b1704721bf1dc1e32491faaf9Timo Sirainen if (*v_offset == 0) {
1701e3f91107051b1704721bf1dc1e32491faaf9Timo Sirainen /* seek to beginning of this stream */
02e61e13a8360a9d3ec92c5fa5ae60c0f0181b71Timo Sirainen break;
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen }
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen if (i == cstream->unknown_size_idx) {
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen /* we'll need to figure out this stream's size */
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen if (i_stream_stat(cstream->input[i], TRUE, &st) < 0) {
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen i_error("istream-concat: "
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen "Failed to get size of stream %s",
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen i_stream_get_name(cstream->input[i]));
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen cstream->istream.istream.stream_errno =
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen cstream->input[i]->stream_errno;
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen return (unsigned int)-1;
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen }
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen /* @UNSAFE */
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen cstream->input_size[i] = st->st_size;
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen cstream->unknown_size_idx = i + 1;
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen }
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen if (*v_offset < cstream->input_size[i])
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen break;
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen *v_offset -= cstream->input_size[i];
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen }
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen return i;
02e61e13a8360a9d3ec92c5fa5ae60c0f0181b71Timo Sirainen}
02e61e13a8360a9d3ec92c5fa5ae60c0f0181b71Timo Sirainen
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainenstatic void i_stream_concat_seek(struct istream_private *stream,
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen uoff_t v_offset, bool mark ATTR_UNUSED)
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen{
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen struct concat_istream *cstream = (struct concat_istream *)stream;
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen stream->istream.v_offset = v_offset;
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen stream->skip = stream->pos = 0;
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen cstream->prev_stream_left = 0;
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen cstream->prev_skip = 0;
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen cstream->cur_idx = find_v_offset(cstream, &v_offset);
5c597df6aa8d81de4053c6986fab7739f3b44b20Timo Sirainen if (cstream->cur_idx == (unsigned int)-1) {
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen /* failed */
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen cstream->cur_input = NULL;
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen stream->istream.stream_errno = EINVAL;
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen return;
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen }
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen cstream->cur_input = cstream->input[cstream->cur_idx];
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen if (cstream->cur_input != NULL)
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen i_stream_seek(cstream->cur_input, v_offset);
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen}
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainenstatic int
2615df45a8027948a474abe5e817b34b0499c171Timo Siraineni_stream_concat_stat(struct istream_private *stream, bool exact ATTR_UNUSED)
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen{
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen struct concat_istream *cstream = (struct concat_istream *)stream;
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen uoff_t v_offset = (uoff_t)-1;
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen unsigned int i;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen /* make sure we have all sizes */
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen if (find_v_offset(cstream, &v_offset) == -1U)
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen return -1;
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen stream->statbuf.st_size = 0;
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen for (i = 0; i < cstream->unknown_size_idx; i++)
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen stream->statbuf.st_size += cstream->input_size[i];
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen return 0;
02e61e13a8360a9d3ec92c5fa5ae60c0f0181b71Timo Sirainen}
02e61e13a8360a9d3ec92c5fa5ae60c0f0181b71Timo Sirainen
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainenstruct istream *i_stream_create_concat(struct istream *input[])
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen{
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen struct concat_istream *cstream;
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen unsigned int count;
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen size_t max_buffer_size = I_STREAM_MIN_SIZE;
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen bool blocking = TRUE, seekable = TRUE;
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen /* if any of the streams isn't blocking or seekable, set ourself also
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen nonblocking/nonseekable */
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen for (count = 0; input[count] != NULL; count++) {
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen size_t cur_max = input[count]->real_stream->max_buffer_size;
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen if (cur_max > max_buffer_size)
02e61e13a8360a9d3ec92c5fa5ae60c0f0181b71Timo Sirainen max_buffer_size = cur_max;
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen if (!input[count]->blocking)
02e61e13a8360a9d3ec92c5fa5ae60c0f0181b71Timo Sirainen blocking = FALSE;
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen if (!input[count]->seekable)
02e61e13a8360a9d3ec92c5fa5ae60c0f0181b71Timo Sirainen seekable = FALSE;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen i_stream_ref(input[count]);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen }
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen i_assert(count != 0);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen cstream = i_new(struct concat_istream, 1);
02e61e13a8360a9d3ec92c5fa5ae60c0f0181b71Timo Sirainen cstream->input = i_new(struct istream *, count + 1);
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen cstream->input_size = i_new(uoff_t, count + 1);
02e61e13a8360a9d3ec92c5fa5ae60c0f0181b71Timo Sirainen
5494a6bc149da8f02fd25c0434a9d612ac33f659Timo Sirainen memcpy(cstream->input, input, sizeof(*input) * count);
5494a6bc149da8f02fd25c0434a9d612ac33f659Timo Sirainen cstream->cur_input = cstream->input[0];
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen i_stream_seek(cstream->cur_input, 0);
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen cstream->istream.iostream.close = i_stream_concat_close;
02e61e13a8360a9d3ec92c5fa5ae60c0f0181b71Timo Sirainen cstream->istream.iostream.destroy = i_stream_concat_destroy;
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen cstream->istream.iostream.set_max_buffer_size =
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen i_stream_concat_set_max_buffer_size;
02e61e13a8360a9d3ec92c5fa5ae60c0f0181b71Timo Sirainen
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen cstream->istream.max_buffer_size = max_buffer_size;
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen cstream->istream.read = i_stream_concat_read;
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen cstream->istream.seek = i_stream_concat_seek;
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen cstream->istream.stat = i_stream_concat_stat;
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen cstream->istream.istream.readable_fd = FALSE;
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen cstream->istream.istream.blocking = blocking;
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen cstream->istream.istream.seekable = seekable;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen return i_stream_create(&cstream->istream, NULL, -1);
1701e3f91107051b1704721bf1dc1e32491faaf9Timo Sirainen}
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen