istream-concat.c revision 421973339968d444d4433cb4d47d1f150e4ab288
02c335c23bf5fa225a467c19f2c063fb0dc7b8c3Timo Sirainen/* Copyright (c) 2007-2016 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;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen unsigned int i;
e2ce8d4a6ac5d82a906178148453e7613fab9ba0Timo Sirainenstatic void i_stream_concat_destroy(struct iostream_private *stream)
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen struct concat_istream *cstream = (struct concat_istream *)stream;
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;
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)
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen struct istream *prev_input = cstream->cur_input;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen const unsigned char *data;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen i_stream_skip(cstream->input[cstream->cur_idx-1], cstream->prev_stream_skip);
5c92436a61569c0b56a9374e60e779fa4455edefTimo Sirainen data = i_stream_get_data(cstream->cur_input, &data_size);
5c92436a61569c0b56a9374e60e779fa4455edefTimo Sirainen cstream->cur_input = cstream->input[cstream->cur_idx];
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen if (cstream->prev_stream_left > 0 || cstream->istream.pos == 0) {
4ce6338bf945cccfff9e4ce7cc6aa2246851b84aTimo Sirainen /* all the pending data is already in w_buffer */
5c92436a61569c0b56a9374e60e779fa4455edefTimo Sirainen cstream->istream.pos - cstream->istream.skip);
4ce6338bf945cccfff9e4ce7cc6aa2246851b84aTimo Sirainen /* we already verified that the data size is less than the
5c92436a61569c0b56a9374e60e779fa4455edefTimo Sirainen maximum buffer size */
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen if (!i_stream_try_alloc(&cstream->istream, data_size, &size))
64055bc6d2ed9e25b3b1db3b5b90d0bdb77cd715Timo Sirainen memcpy(cstream->istream.w_buffer, data, data_size);
4ce6338bf945cccfff9e4ce7cc6aa2246851b84aTimo Sirainenstatic void i_stream_concat_skip(struct concat_istream *cstream)
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen struct istream_private *stream = &cstream->istream;
0c3ec2538c366bb5583f0b4ca2ce60804756b51cTimo 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 */
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen i_stream_skip(cstream->input[cstream->cur_idx-1], cstream->prev_stream_skip);
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen i_stream_skip(cstream->cur_input, bytes_skipped);
4afd5082f38342fd688acb5796912329f57dd02cTimo Sirainenstatic ssize_t i_stream_concat_read(struct istream_private *stream)
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;
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);
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen /* need to read more */
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 */
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen last_stream = cstream->input[cstream->cur_idx+1] == NULL;
a095363e41e7938afc25d149a6fad6e185cf4ee7Timo Sirainen if (stream->pos - stream->skip >= stream->max_buffer_size)
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen stream->istream.eof = cstream->cur_input->eof && last_stream;
a91f2c465f026ca4ebb9e6c8e92800175c0dece6Timo Sirainen data = i_stream_get_data(cstream->cur_input, &data_size);
a91f2c465f026ca4ebb9e6c8e92800175c0dece6Timo Sirainen /* we can point directly to the current stream's buffers */
a91f2c465f026ca4ebb9e6c8e92800175c0dece6Timo Sirainen /* nothing new read */
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. */
a91f2c465f026ca4ebb9e6c8e92800175c0dece6Timo Sirainen ret = new_pos > stream->pos ? (ssize_t)(new_pos - stream->pos) :
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;
421973339968d444d4433cb4d47d1f150e4ab288Timo Sirainen if (find_v_offset(cstream, &v_offset, &cstream->cur_idx) < 0) {
65d8efa66dca27db85f74ee2574188b51569a4c2Timo Sirainen cstream->cur_input = cstream->input[cstream->cur_idx];
65d8efa66dca27db85f74ee2574188b51569a4c2Timo Siraineni_stream_concat_stat(struct istream_private *stream, bool exact ATTR_UNUSED)
65d8efa66dca27db85f74ee2574188b51569a4c2Timo Sirainen struct concat_istream *cstream = (struct concat_istream *)stream;
65d8efa66dca27db85f74ee2574188b51569a4c2Timo Sirainen unsigned int i, cur_idx;
65d8efa66dca27db85f74ee2574188b51569a4c2Timo Sirainen /* make sure we have all sizes */
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo 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[])
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen unsigned int count;
01f4ee4a0243f3fe9af763e1a540cd5cff0d63f5Timo Sirainen /* if any of the streams isn't blocking or seekable, set ourself also
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen for (count = 0; input[count] != NULL; count++) {
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen size_t cur_max = input[count]->real_stream->max_buffer_size;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen cstream->input = i_new(struct istream *, count + 1);
ab7b5b9286104974c2a572a499ccf8b56c5d2955Timo Sirainen cstream->input_size = i_new(uoff_t, count + 1);
ab7b5b9286104974c2a572a499ccf8b56c5d2955Timo Sirainen memcpy(cstream->input, 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;