istream-concat.c revision 421973339968d444d4433cb4d47d1f150e4ab288
02c335c23bf5fa225a467c19f2c063fb0dc7b8c3Timo Sirainen/* Copyright (c) 2007-2016 Dovecot authors, see the included COPYING file */
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen#include "lib.h"
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen#include "buffer.h"
0536ccb51d41e3078c3a9fa33e509fb4b2420f95Timo Sirainen#include "istream-private.h"
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen#include "istream-concat.h"
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainenstruct concat_istream {
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen struct istream_private istream;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen struct istream **input, *cur_input;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen uoff_t *input_size;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen unsigned int cur_idx, unknown_size_idx;
5c92436a61569c0b56a9374e60e779fa4455edefTimo Sirainen size_t prev_stream_left, prev_stream_skip, prev_skip;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen};
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen
5c92436a61569c0b56a9374e60e779fa4455edefTimo Sirainenstatic void i_stream_concat_skip(struct concat_istream *cstream);
5c92436a61569c0b56a9374e60e779fa4455edefTimo Sirainen
e2ce8d4a6ac5d82a906178148453e7613fab9ba0Timo Sirainenstatic void i_stream_concat_close(struct iostream_private *stream,
e2ce8d4a6ac5d82a906178148453e7613fab9ba0Timo Sirainen bool close_parent)
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen{
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen struct concat_istream *cstream = (struct concat_istream *)stream;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen unsigned int i;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen
948b838c8e9896132e3a2d802fb5dad37e8dc716Timo Sirainen (void)i_stream_concat_skip(cstream);
948b838c8e9896132e3a2d802fb5dad37e8dc716Timo Sirainen
948b838c8e9896132e3a2d802fb5dad37e8dc716Timo Sirainen if (close_parent) {
948b838c8e9896132e3a2d802fb5dad37e8dc716Timo Sirainen for (i = 0; cstream->input[i] != NULL; i++)
5c92436a61569c0b56a9374e60e779fa4455edefTimo Sirainen i_stream_close(cstream->input[i]);
e2ce8d4a6ac5d82a906178148453e7613fab9ba0Timo Sirainen }
e2ce8d4a6ac5d82a906178148453e7613fab9ba0Timo Sirainen}
e2ce8d4a6ac5d82a906178148453e7613fab9ba0Timo Sirainen
e2ce8d4a6ac5d82a906178148453e7613fab9ba0Timo Sirainenstatic void i_stream_concat_destroy(struct iostream_private *stream)
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen{
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen struct concat_istream *cstream = (struct concat_istream *)stream;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen unsigned int i;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen for (i = 0; cstream->input[i] != NULL; i++)
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen i_stream_unref(&cstream->input[i]);
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen i_free(cstream->input);
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen i_free(cstream->input_size);
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen i_free(cstream->istream.w_buffer);
5a0ac2e5ef482016e00575a7dce83f52c1704732Timo Sirainen}
5a0ac2e5ef482016e00575a7dce83f52c1704732Timo Sirainen
7823ef73e51bb81a17dcb306aff89016d4ce258fTimo Sirainenstatic void
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Siraineni_stream_concat_set_max_buffer_size(struct iostream_private *stream,
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen size_t max_size)
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen{
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen struct concat_istream *cstream = (struct concat_istream *)stream;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen unsigned int i;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen cstream->istream.max_buffer_size = max_size;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen for (i = 0; cstream->input[i] != NULL; i++)
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen i_stream_set_max_buffer_size(cstream->input[i], max_size);
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen}
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainenstatic void i_stream_concat_read_next(struct concat_istream *cstream)
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen{
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen struct istream *prev_input = cstream->cur_input;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen const unsigned char *data;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen size_t data_size, size;
5c92436a61569c0b56a9374e60e779fa4455edefTimo Sirainen
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen i_assert(cstream->cur_input->eof);
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen if (cstream->prev_stream_skip != 0) {
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen i_stream_skip(cstream->input[cstream->cur_idx-1], cstream->prev_stream_skip);
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen cstream->prev_stream_skip = 0;
5c92436a61569c0b56a9374e60e779fa4455edefTimo Sirainen }
5c92436a61569c0b56a9374e60e779fa4455edefTimo Sirainen
5c92436a61569c0b56a9374e60e779fa4455edefTimo Sirainen data = i_stream_get_data(cstream->cur_input, &data_size);
5c92436a61569c0b56a9374e60e779fa4455edefTimo Sirainen cstream->cur_idx++;
5c92436a61569c0b56a9374e60e779fa4455edefTimo Sirainen cstream->cur_input = cstream->input[cstream->cur_idx];
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen i_stream_seek(cstream->cur_input, 0);
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen if (cstream->prev_stream_left > 0 || cstream->istream.pos == 0) {
4ce6338bf945cccfff9e4ce7cc6aa2246851b84aTimo Sirainen /* all the pending data is already in w_buffer */
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen cstream->prev_stream_skip = data_size;
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen cstream->prev_stream_left += data_size;
5c92436a61569c0b56a9374e60e779fa4455edefTimo Sirainen i_assert(cstream->prev_stream_left ==
5c92436a61569c0b56a9374e60e779fa4455edefTimo Sirainen cstream->istream.pos - cstream->istream.skip);
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen return;
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen }
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen i_assert(cstream->prev_stream_skip == 0);
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen
4ce6338bf945cccfff9e4ce7cc6aa2246851b84aTimo Sirainen /* we already verified that the data size is less than the
5c92436a61569c0b56a9374e60e779fa4455edefTimo Sirainen maximum buffer size */
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen cstream->istream.pos = 0;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen if (data_size > 0) {
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen if (!i_stream_try_alloc(&cstream->istream, data_size, &size))
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen i_unreached();
64055bc6d2ed9e25b3b1db3b5b90d0bdb77cd715Timo Sirainen i_assert(size >= data_size);
dd2df6a67f10792ce31a3666197c0b6885893a3aTimo Sirainen }
64055bc6d2ed9e25b3b1db3b5b90d0bdb77cd715Timo Sirainen
64055bc6d2ed9e25b3b1db3b5b90d0bdb77cd715Timo Sirainen cstream->prev_stream_left = data_size;
64055bc6d2ed9e25b3b1db3b5b90d0bdb77cd715Timo Sirainen memcpy(cstream->istream.w_buffer, data, data_size);
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen i_stream_skip(prev_input, data_size);
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen cstream->istream.skip = 0;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen cstream->istream.pos = data_size;
5c92436a61569c0b56a9374e60e779fa4455edefTimo Sirainen}
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen
4ce6338bf945cccfff9e4ce7cc6aa2246851b84aTimo Sirainenstatic void i_stream_concat_skip(struct concat_istream *cstream)
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen{
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen struct istream_private *stream = &cstream->istream;
0c3ec2538c366bb5583f0b4ca2ce60804756b51cTimo Sirainen size_t bytes_skipped;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen
0c3ec2538c366bb5583f0b4ca2ce60804756b51cTimo Sirainen i_assert(stream->skip >= cstream->prev_skip);
0c3ec2538c366bb5583f0b4ca2ce60804756b51cTimo Sirainen bytes_skipped = stream->skip - cstream->prev_skip;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen if (cstream->prev_stream_left == 0) {
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen /* no need to worry about buffers, skip everything */
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen } else if (bytes_skipped < cstream->prev_stream_left) {
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen /* we're still skipping inside buffer */
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen cstream->prev_stream_left -= bytes_skipped;
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen bytes_skipped = 0;
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen } else {
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen /* done with the buffer */
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen i_stream_skip(cstream->input[cstream->cur_idx-1], cstream->prev_stream_skip);
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen cstream->prev_stream_skip = 0;
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen
5c92436a61569c0b56a9374e60e779fa4455edefTimo Sirainen bytes_skipped -= cstream->prev_stream_left;
5c92436a61569c0b56a9374e60e779fa4455edefTimo Sirainen cstream->prev_stream_left = 0;
5c92436a61569c0b56a9374e60e779fa4455edefTimo Sirainen }
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen stream->pos -= bytes_skipped;
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen stream->skip -= bytes_skipped;
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen stream->buffer += bytes_skipped;
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen cstream->prev_skip = stream->skip;
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen i_stream_skip(cstream->cur_input, bytes_skipped);
4afd5082f38342fd688acb5796912329f57dd02cTimo Sirainen}
4afd5082f38342fd688acb5796912329f57dd02cTimo Sirainen
4afd5082f38342fd688acb5796912329f57dd02cTimo Sirainenstatic ssize_t i_stream_concat_read(struct istream_private *stream)
0c3ec2538c366bb5583f0b4ca2ce60804756b51cTimo Sirainen{
0c3ec2538c366bb5583f0b4ca2ce60804756b51cTimo Sirainen struct concat_istream *cstream = (struct concat_istream *)stream;
0c3ec2538c366bb5583f0b4ca2ce60804756b51cTimo Sirainen const unsigned char *data;
0c3ec2538c366bb5583f0b4ca2ce60804756b51cTimo Sirainen size_t size, data_size, cur_data_pos, new_pos;
0c3ec2538c366bb5583f0b4ca2ce60804756b51cTimo Sirainen size_t new_bytes_count;
0c3ec2538c366bb5583f0b4ca2ce60804756b51cTimo Sirainen ssize_t ret;
0c3ec2538c366bb5583f0b4ca2ce60804756b51cTimo Sirainen bool last_stream;
0c3ec2538c366bb5583f0b4ca2ce60804756b51cTimo Sirainen
0c3ec2538c366bb5583f0b4ca2ce60804756b51cTimo Sirainen if (cstream->cur_input == NULL) {
0c3ec2538c366bb5583f0b4ca2ce60804756b51cTimo Sirainen stream->istream.stream_errno = EINVAL;
0c3ec2538c366bb5583f0b4ca2ce60804756b51cTimo Sirainen return -1;
65d8efa66dca27db85f74ee2574188b51569a4c2Timo Sirainen }
0c3ec2538c366bb5583f0b4ca2ce60804756b51cTimo Sirainen i_stream_concat_skip(cstream);
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen
a91f2c465f026ca4ebb9e6c8e92800175c0dece6Timo Sirainen i_assert(stream->pos >= stream->skip + cstream->prev_stream_left);
a91f2c465f026ca4ebb9e6c8e92800175c0dece6Timo Sirainen cur_data_pos = stream->pos - (stream->skip + cstream->prev_stream_left);
a91f2c465f026ca4ebb9e6c8e92800175c0dece6Timo Sirainen
a91f2c465f026ca4ebb9e6c8e92800175c0dece6Timo Sirainen data = i_stream_get_data(cstream->cur_input, &data_size);
548d8f62722578f076b4944e30013675f2605206Timo Sirainen if (data_size > cur_data_pos)
98950c9167dc2fab6c13d6d4c968e1963ecd73d7Timo Sirainen ret = 0;
98950c9167dc2fab6c13d6d4c968e1963ecd73d7Timo Sirainen else {
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen /* need to read more */
a91f2c465f026ca4ebb9e6c8e92800175c0dece6Timo Sirainen i_assert(cur_data_pos == data_size);
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen ret = i_stream_read(cstream->cur_input);
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen if (ret == -2 || ret == 0)
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen return ret;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen
64c48ffb71f1cf99acf375768fde4cff9b512648Timo Sirainen if (ret == -1 && cstream->cur_input->stream_errno != 0) {
2c42748505ef4aed83ff59b34e50ed5606900c86Timo Sirainen io_stream_set_error(&cstream->istream.iostream,
2c42748505ef4aed83ff59b34e50ed5606900c86Timo Sirainen "read(%s) failed: %s",
2c42748505ef4aed83ff59b34e50ed5606900c86Timo Sirainen i_stream_get_name(cstream->cur_input),
2c42748505ef4aed83ff59b34e50ed5606900c86Timo Sirainen i_stream_get_error(cstream->cur_input));
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen stream->istream.stream_errno =
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen cstream->cur_input->stream_errno;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen return -1;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen }
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen /* we either read something or we're at EOF */
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen last_stream = cstream->input[cstream->cur_idx+1] == NULL;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen if (ret == -1 && !last_stream) {
a095363e41e7938afc25d149a6fad6e185cf4ee7Timo Sirainen if (stream->pos - stream->skip >= stream->max_buffer_size)
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen return -2;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen i_stream_concat_read_next(cstream);
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen cstream->prev_skip = stream->skip;
4ce6338bf945cccfff9e4ce7cc6aa2246851b84aTimo Sirainen return i_stream_concat_read(stream);
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen }
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen stream->istream.eof = cstream->cur_input->eof && last_stream;
c09f9f95db314e7482c95e502e1c56ed6c555797Timo Sirainen i_assert(ret != -1 || stream->istream.eof);
a91f2c465f026ca4ebb9e6c8e92800175c0dece6Timo Sirainen data = i_stream_get_data(cstream->cur_input, &data_size);
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen }
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen if (cstream->prev_stream_left == 0) {
a91f2c465f026ca4ebb9e6c8e92800175c0dece6Timo Sirainen /* we can point directly to the current stream's buffers */
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen stream->buffer = data;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen stream->pos -= stream->skip;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen stream->skip = 0;
a91f2c465f026ca4ebb9e6c8e92800175c0dece6Timo Sirainen new_pos = data_size;
a91f2c465f026ca4ebb9e6c8e92800175c0dece6Timo Sirainen } else if (data_size == cur_data_pos) {
a91f2c465f026ca4ebb9e6c8e92800175c0dece6Timo Sirainen /* nothing new read */
a91f2c465f026ca4ebb9e6c8e92800175c0dece6Timo Sirainen i_assert(ret == 0 || ret == -1);
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen stream->buffer = stream->w_buffer;
a91f2c465f026ca4ebb9e6c8e92800175c0dece6Timo Sirainen new_pos = stream->pos;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen } else {
a91f2c465f026ca4ebb9e6c8e92800175c0dece6Timo Sirainen /* we still have some of the previous stream left. merge the
a91f2c465f026ca4ebb9e6c8e92800175c0dece6Timo Sirainen new data with it. */
a91f2c465f026ca4ebb9e6c8e92800175c0dece6Timo Sirainen i_assert(data_size > cur_data_pos);
a91f2c465f026ca4ebb9e6c8e92800175c0dece6Timo Sirainen new_bytes_count = data_size - cur_data_pos;
a91f2c465f026ca4ebb9e6c8e92800175c0dece6Timo Sirainen if (!i_stream_try_alloc(stream, new_bytes_count, &size)) {
a91f2c465f026ca4ebb9e6c8e92800175c0dece6Timo Sirainen stream->buffer = stream->w_buffer;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen return -2;
a91f2c465f026ca4ebb9e6c8e92800175c0dece6Timo Sirainen }
a91f2c465f026ca4ebb9e6c8e92800175c0dece6Timo Sirainen stream->buffer = stream->w_buffer;
4ce6338bf945cccfff9e4ce7cc6aa2246851b84aTimo Sirainen
5c92436a61569c0b56a9374e60e779fa4455edefTimo Sirainen /* we'll copy all the new input to w_buffer. if we skip over
5c92436a61569c0b56a9374e60e779fa4455edefTimo Sirainen prev_stream_left bytes, the next read will switch to
5c92436a61569c0b56a9374e60e779fa4455edefTimo Sirainen pointing to cur_input's data directly. */
a91f2c465f026ca4ebb9e6c8e92800175c0dece6Timo Sirainen if (new_bytes_count > size)
a91f2c465f026ca4ebb9e6c8e92800175c0dece6Timo Sirainen new_bytes_count = size;
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen memcpy(stream->w_buffer + stream->pos,
a91f2c465f026ca4ebb9e6c8e92800175c0dece6Timo Sirainen data + cur_data_pos, new_bytes_count);
a91f2c465f026ca4ebb9e6c8e92800175c0dece6Timo Sirainen new_pos = stream->pos + new_bytes_count;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen }
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen
a91f2c465f026ca4ebb9e6c8e92800175c0dece6Timo Sirainen ret = new_pos > stream->pos ? (ssize_t)(new_pos - stream->pos) :
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen (ret == 0 ? 0 : -1);
a91f2c465f026ca4ebb9e6c8e92800175c0dece6Timo Sirainen stream->pos = new_pos;
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen cstream->prev_skip = stream->skip;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen return ret;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen}
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen
421973339968d444d4433cb4d47d1f150e4ab288Timo Sirainenstatic int
421973339968d444d4433cb4d47d1f150e4ab288Timo Sirainenfind_v_offset(struct concat_istream *cstream, uoff_t *v_offset,
421973339968d444d4433cb4d47d1f150e4ab288Timo Sirainen unsigned int *idx_r)
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen{
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen const struct stat *st;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen unsigned int i;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen for (i = 0; cstream->input[i] != NULL; i++) {
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen if (*v_offset == 0) {
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen /* seek to beginning of this stream */
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen break;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen }
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen if (i == cstream->unknown_size_idx) {
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen /* we'll need to figure out this stream's size */
01f4ee4a0243f3fe9af763e1a540cd5cff0d63f5Timo Sirainen if (i_stream_stat(cstream->input[i], TRUE, &st) < 0) {
1c6f6f5bef70f16546b3bc8f4cd5f93f373e82a2Timo Sirainen io_stream_set_error(&cstream->istream.iostream,
1c6f6f5bef70f16546b3bc8f4cd5f93f373e82a2Timo Sirainen "stat(%s) failed: %s",
1c6f6f5bef70f16546b3bc8f4cd5f93f373e82a2Timo Sirainen i_stream_get_name(cstream->input[i]),
1c6f6f5bef70f16546b3bc8f4cd5f93f373e82a2Timo Sirainen i_stream_get_error(cstream->input[i]));
1c6f6f5bef70f16546b3bc8f4cd5f93f373e82a2Timo Sirainen i_error("istream-concat: stat(%s) failed: %s",
1c6f6f5bef70f16546b3bc8f4cd5f93f373e82a2Timo Sirainen i_stream_get_name(cstream->input[i]),
1c6f6f5bef70f16546b3bc8f4cd5f93f373e82a2Timo Sirainen i_stream_get_error(cstream->input[i]));
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen cstream->istream.istream.stream_errno =
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen cstream->input[i]->stream_errno;
421973339968d444d4433cb4d47d1f150e4ab288Timo Sirainen return -1;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen }
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen /* @UNSAFE */
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen cstream->input_size[i] = st->st_size;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen cstream->unknown_size_idx = i + 1;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen }
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen if (*v_offset < cstream->input_size[i])
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen break;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen *v_offset -= cstream->input_size[i];
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen }
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen
421973339968d444d4433cb4d47d1f150e4ab288Timo Sirainen *idx_r = i;
421973339968d444d4433cb4d47d1f150e4ab288Timo Sirainen return 0;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen}
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainenstatic void i_stream_concat_seek(struct istream_private *stream,
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen uoff_t v_offset, bool mark ATTR_UNUSED)
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen{
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen struct concat_istream *cstream = (struct concat_istream *)stream;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen stream->istream.v_offset = v_offset;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen stream->skip = stream->pos = 0;
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen cstream->prev_stream_left = 0;
5c92436a61569c0b56a9374e60e779fa4455edefTimo Sirainen cstream->prev_stream_skip = 0;
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen cstream->prev_skip = 0;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen
421973339968d444d4433cb4d47d1f150e4ab288Timo Sirainen if (find_v_offset(cstream, &v_offset, &cstream->cur_idx) < 0) {
6cc5a850bb6c1769f4113009c2067e5a719175a0Timo Sirainen /* failed */
6cc5a850bb6c1769f4113009c2067e5a719175a0Timo Sirainen cstream->cur_input = NULL;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen stream->istream.stream_errno = EINVAL;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen return;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen }
65d8efa66dca27db85f74ee2574188b51569a4c2Timo Sirainen cstream->cur_input = cstream->input[cstream->cur_idx];
65d8efa66dca27db85f74ee2574188b51569a4c2Timo Sirainen if (cstream->cur_input != NULL)
65d8efa66dca27db85f74ee2574188b51569a4c2Timo Sirainen i_stream_seek(cstream->cur_input, v_offset);
65d8efa66dca27db85f74ee2574188b51569a4c2Timo Sirainen}
65d8efa66dca27db85f74ee2574188b51569a4c2Timo Sirainen
65d8efa66dca27db85f74ee2574188b51569a4c2Timo Sirainenstatic int
65d8efa66dca27db85f74ee2574188b51569a4c2Timo Siraineni_stream_concat_stat(struct istream_private *stream, bool exact ATTR_UNUSED)
65d8efa66dca27db85f74ee2574188b51569a4c2Timo Sirainen{
65d8efa66dca27db85f74ee2574188b51569a4c2Timo Sirainen struct concat_istream *cstream = (struct concat_istream *)stream;
65d8efa66dca27db85f74ee2574188b51569a4c2Timo Sirainen uoff_t v_offset = (uoff_t)-1;
65d8efa66dca27db85f74ee2574188b51569a4c2Timo Sirainen unsigned int i, cur_idx;
65d8efa66dca27db85f74ee2574188b51569a4c2Timo Sirainen
65d8efa66dca27db85f74ee2574188b51569a4c2Timo Sirainen /* make sure we have all sizes */
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen if (find_v_offset(cstream, &v_offset, &cur_idx) < 0)
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen return -1;
01f4ee4a0243f3fe9af763e1a540cd5cff0d63f5Timo Sirainen
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen stream->statbuf.st_size = 0;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen for (i = 0; i < cstream->unknown_size_idx; i++)
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen stream->statbuf.st_size += cstream->input_size[i];
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen return 0;
421973339968d444d4433cb4d47d1f150e4ab288Timo Sirainen}
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainenstruct istream *i_stream_create_concat(struct istream *input[])
421973339968d444d4433cb4d47d1f150e4ab288Timo Sirainen{
01f4ee4a0243f3fe9af763e1a540cd5cff0d63f5Timo Sirainen struct concat_istream *cstream;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen unsigned int count;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen size_t max_buffer_size = I_STREAM_MIN_SIZE;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen bool blocking = TRUE, seekable = TRUE;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen
01f4ee4a0243f3fe9af763e1a540cd5cff0d63f5Timo Sirainen /* if any of the streams isn't blocking or seekable, set ourself also
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen nonblocking/nonseekable */
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen for (count = 0; input[count] != NULL; count++) {
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen size_t cur_max = input[count]->real_stream->max_buffer_size;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen if (cur_max > max_buffer_size)
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen max_buffer_size = cur_max;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen if (!input[count]->blocking)
ab7b5b9286104974c2a572a499ccf8b56c5d2955Timo Sirainen blocking = FALSE;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen if (!input[count]->seekable)
ab7b5b9286104974c2a572a499ccf8b56c5d2955Timo Sirainen seekable = FALSE;
ab7b5b9286104974c2a572a499ccf8b56c5d2955Timo Sirainen i_stream_ref(input[count]);
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen }
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen i_assert(count != 0);
ab7b5b9286104974c2a572a499ccf8b56c5d2955Timo Sirainen
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen cstream = i_new(struct concat_istream, 1);
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen cstream->input = i_new(struct istream *, count + 1);
ab7b5b9286104974c2a572a499ccf8b56c5d2955Timo Sirainen cstream->input_size = i_new(uoff_t, count + 1);
ab7b5b9286104974c2a572a499ccf8b56c5d2955Timo Sirainen
ab7b5b9286104974c2a572a499ccf8b56c5d2955Timo Sirainen memcpy(cstream->input, input, sizeof(*input) * count);
ab7b5b9286104974c2a572a499ccf8b56c5d2955Timo Sirainen cstream->cur_input = cstream->input[0];
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen i_stream_seek(cstream->cur_input, 0);
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen cstream->istream.iostream.close = i_stream_concat_close;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen cstream->istream.iostream.destroy = i_stream_concat_destroy;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen cstream->istream.iostream.set_max_buffer_size =
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen i_stream_concat_set_max_buffer_size;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen cstream->istream.max_buffer_size = max_buffer_size;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen cstream->istream.read = i_stream_concat_read;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen cstream->istream.seek = i_stream_concat_seek;
4ce6338bf945cccfff9e4ce7cc6aa2246851b84aTimo Sirainen cstream->istream.stat = i_stream_concat_stat;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen cstream->istream.istream.readable_fd = FALSE;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen cstream->istream.istream.blocking = blocking;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen cstream->istream.istream.seekable = seekable;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen return i_stream_create(&cstream->istream, NULL, -1);
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen}
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen