bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2007-2018 Dovecot authors, see the included COPYING file */
5c92436a61569c0b56a9374e60e779fa4455edefTimo Sirainen size_t prev_stream_left, prev_stream_skip, prev_skip;
5c92436a61569c0b56a9374e60e779fa4455edefTimo Sirainenstatic void i_stream_concat_skip(struct concat_istream *cstream);
e2ce8d4a6ac5d82a906178148453e7613fab9ba0Timo Sirainenstatic void i_stream_concat_close(struct iostream_private *stream,
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen struct concat_istream *cstream = (struct concat_istream *)stream;
0327191888a058542f065526e82bce30319579a4Phil Carmody i_assert(cstream->cur_input == cstream->input[cstream->cur_idx]);
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen unsigned int i;
948b838c8e9896132e3a2d802fb5dad37e8dc716Timo Sirainen if (cstream->istream.istream.stream_errno == 0) {
948b838c8e9896132e3a2d802fb5dad37e8dc716Timo Sirainen /* get the parent streams to the wanted offset */
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainenstatic void i_stream_concat_destroy(struct iostream_private *stream)
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen struct concat_istream *cstream = (struct concat_istream *)stream;
0327191888a058542f065526e82bce30319579a4Phil Carmody i_assert(cstream->cur_input == cstream->input[cstream->cur_idx]);
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen unsigned int i;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Siraineni_stream_concat_set_max_buffer_size(struct iostream_private *stream,
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen struct concat_istream *cstream = (struct concat_istream *)stream;
0327191888a058542f065526e82bce30319579a4Phil Carmody i_assert(cstream->cur_input == cstream->input[cstream->cur_idx]);
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen unsigned int i;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen i_stream_set_max_buffer_size(cstream->input[i], max_size);
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainenstatic void i_stream_concat_read_next(struct concat_istream *cstream)
5c92436a61569c0b56a9374e60e779fa4455edefTimo Sirainen struct istream *prev_input = cstream->cur_input;
5c92436a61569c0b56a9374e60e779fa4455edefTimo Sirainen i_stream_skip(cstream->input[cstream->cur_idx-1], cstream->prev_stream_skip);
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen data = i_stream_get_data(cstream->cur_input, &data_size);
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen cstream->cur_input = cstream->input[cstream->cur_idx];
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen if (cstream->prev_stream_left > 0 || cstream->istream.pos == 0) {
5c92436a61569c0b56a9374e60e779fa4455edefTimo Sirainen /* all the pending data is already in w_buffer */
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen cstream->istream.pos - cstream->istream.skip);
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen /* we already verified that the data size is less than the
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen maximum buffer size */
dd2df6a67f10792ce31a3666197c0b6885893a3aTimo Sirainen if (!i_stream_try_alloc(&cstream->istream, data_size, &size))
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen memcpy(cstream->istream.w_buffer, data, data_size);
0c3ec2538c366bb5583f0b4ca2ce60804756b51cTimo Sirainenstatic void i_stream_concat_skip(struct concat_istream *cstream)
0c3ec2538c366bb5583f0b4ca2ce60804756b51cTimo Sirainen struct istream_private *stream = &cstream->istream;
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen bytes_skipped = stream->skip - cstream->prev_skip;
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 /* done with the buffer */
5c92436a61569c0b56a9374e60e779fa4455edefTimo Sirainen i_stream_skip(cstream->input[cstream->cur_idx-1], cstream->prev_stream_skip);
4afd5082f38342fd688acb5796912329f57dd02cTimo Sirainen i_stream_skip(cstream->cur_input, bytes_skipped);
0c3ec2538c366bb5583f0b4ca2ce60804756b51cTimo Sirainenstatic ssize_t i_stream_concat_read(struct istream_private *stream)
0c3ec2538c366bb5583f0b4ca2ce60804756b51cTimo Sirainen struct concat_istream *cstream = (struct concat_istream *)stream;
0327191888a058542f065526e82bce30319579a4Phil Carmody i_assert(cstream->cur_input == cstream->input[cstream->cur_idx]);
0c3ec2538c366bb5583f0b4ca2ce60804756b51cTimo Sirainen size_t size, data_size, cur_data_pos, new_pos;
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 data = i_stream_get_data(cstream->cur_input, &data_size);
bcd286622779a93f809b11993db0550f8c7cc9b5Timo Sirainen /* need to read more - NOTE: Can't use i_stream_read_memarea()
bcd286622779a93f809b11993db0550f8c7cc9b5Timo Sirainen here, because our stream->buffer may point to the parent
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",
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen /* we either read something or we're at EOF */
16b5dc27e7db42849510403d37e3629aba14de21Timo Sirainen last_stream = cstream->cur_idx+1 >= cstream->input_count;
0928812e725cd3a4debab2a93d0c9b0436a4de9fTimo Sirainen if (stream->pos - stream->skip >= i_stream_get_max_buffer_size(&stream->istream))
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen stream->istream.eof = cstream->cur_input->eof && last_stream;
a91f2c465f026ca4ebb9e6c8e92800175c0dece6Timo Sirainen data = i_stream_get_data(cstream->cur_input, &data_size);
59a63791d4ec70a134cb0dcbad1255d952075efeTimo Sirainen /* nothing new read - preserve the buffer as it was */
a91f2c465f026ca4ebb9e6c8e92800175c0dece6Timo Sirainen /* we can point directly to the current stream's buffers */
a91f2c465f026ca4ebb9e6c8e92800175c0dece6Timo Sirainen /* we still have some of the previous stream left. merge the
a91f2c465f026ca4ebb9e6c8e92800175c0dece6Timo Sirainen new data with it. */
a91f2c465f026ca4ebb9e6c8e92800175c0dece6Timo Sirainen if (!i_stream_try_alloc(stream, new_bytes_count, &size)) {
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. */
421973339968d444d4433cb4d47d1f150e4ab288Timo Sirainenfind_v_offset(struct concat_istream *cstream, uoff_t *v_offset,
421973339968d444d4433cb4d47d1f150e4ab288Timo Sirainen unsigned int *idx_r)
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen unsigned int i;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen /* seek to beginning of this stream */
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_error("istream-concat: stat(%s) failed: %s",
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen /* @UNSAFE */
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainenstatic void i_stream_concat_seek(struct istream_private *stream,
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen struct concat_istream *cstream = (struct concat_istream *)stream;
0327191888a058542f065526e82bce30319579a4Phil Carmody i_assert(cstream->cur_input == cstream->input[cstream->cur_idx]);
421973339968d444d4433cb4d47d1f150e4ab288Timo Sirainen if (find_v_offset(cstream, &v_offset, &cstream->cur_idx) < 0) {
16b5dc27e7db42849510403d37e3629aba14de21Timo Sirainen cstream->cur_input = cstream->input[cstream->cur_idx];
65d8efa66dca27db85f74ee2574188b51569a4c2Timo Sirainen /* we allow seeking to EOF, but not past it. */
65d8efa66dca27db85f74ee2574188b51569a4c2Timo Sirainen io_stream_set_error(&cstream->istream.iostream,
65d8efa66dca27db85f74ee2574188b51569a4c2Timo Sirainen "Seeking past EOF by %"PRIuUOFF_T" bytes", v_offset);
65d8efa66dca27db85f74ee2574188b51569a4c2Timo Sirainen cstream->istream.istream.stream_errno = EINVAL;
13fc4d6d28f05076ffe6f5a925e8f6269e4b548ePhil Carmody /* Position ourselves at the EOF of the last actual stream. */
13fc4d6d28f05076ffe6f5a925e8f6269e4b548ePhil Carmody cstream->cur_input = cstream->input[cstream->cur_idx];
13fc4d6d28f05076ffe6f5a925e8f6269e4b548ePhil Carmody v_offset = cstream->input_size[cstream->cur_idx];
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Siraineni_stream_concat_stat(struct istream_private *stream, bool exact ATTR_UNUSED)
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen struct concat_istream *cstream = (struct concat_istream *)stream;
0327191888a058542f065526e82bce30319579a4Phil Carmody i_assert(cstream->cur_input == cstream->input[cstream->cur_idx]);
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen /* make sure we have all sizes */
421973339968d444d4433cb4d47d1f150e4ab288Timo Sirainen if (find_v_offset(cstream, &v_offset, &cur_idx) < 0)
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen for (i = 0; i < cstream->unknown_size_idx; i++)
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen stream->statbuf.st_size += cstream->input_size[i];
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainenstruct istream *i_stream_create_concat(struct istream *input[])
ab7b5b9286104974c2a572a499ccf8b56c5d2955Timo Sirainen /* if any of the streams isn't blocking or seekable, set ourself also
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen for (count = 0; input[count] != NULL; count++) {
0928812e725cd3a4debab2a93d0c9b0436a4de9fTimo Sirainen size_t cur_max = i_stream_get_max_buffer_size(input[count]);
16b5dc27e7db42849510403d37e3629aba14de21Timo Sirainen cstream->input = p_memdup(default_pool, input, sizeof(*input) * count);
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 cstream->istream.max_buffer_size = max_buffer_size;