istream-concat.c revision a095363e41e7938afc25d149a6fad6e185cf4ee7
45312f52ff3a3d4c137447be4c7556500c2f8bf2Timo Sirainen/* Copyright (c) 2007-2014 Dovecot authors, see the included COPYING file */
1d4f710106fb498750456724628da6063e012e6dTimo Sirainenstatic void i_stream_concat_close(struct iostream_private *stream,
9439bed2f07d6475febd8a247cd2f0990fb32a13Timo Sirainen struct concat_istream *cstream = (struct concat_istream *)stream;
6fdfa4d4cf14d1d7764d7faa8258f112e39c8dbeTimo Sirainen unsigned int i;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenstatic void i_stream_concat_destroy(struct iostream_private *stream)
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen struct concat_istream *cstream = (struct concat_istream *)stream;
ad004e44be109684521494b5af2ad1da39b8bb27Timo Sirainen unsigned int i;
f059a046515f4b2b15a6c2a10a6f12f6166e39a5Timo Siraineni_stream_concat_set_max_buffer_size(struct iostream_private *stream,
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen struct concat_istream *cstream = (struct concat_istream *)stream;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen unsigned int i;
46ec792dd4ccf6c34706c4774228301fafde6aa9Timo Sirainen i_stream_set_max_buffer_size(cstream->input[i], max_size);
46ec792dd4ccf6c34706c4774228301fafde6aa9Timo Sirainenstatic void i_stream_concat_read_next(struct concat_istream *cstream)
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen const unsigned char *data;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen data = i_stream_get_data(cstream->cur_input, &data_size);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen cstream->cur_input = cstream->input[cstream->cur_idx];
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen if (cstream->prev_stream_left > 0 || cstream->istream.pos == 0) {
6fdfa4d4cf14d1d7764d7faa8258f112e39c8dbeTimo Sirainen cstream->istream.pos - cstream->istream.skip);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen /* we already verified that the data size is less than the
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen maximum buffer size */
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen if (!i_stream_try_alloc(&cstream->istream, data_size, &size))
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen memcpy(cstream->istream.w_buffer, data, data_size);
ad004e44be109684521494b5af2ad1da39b8bb27Timo Sirainenstatic ssize_t i_stream_concat_read(struct istream_private *stream)
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen struct concat_istream *cstream = (struct concat_istream *)stream;
ad004e44be109684521494b5af2ad1da39b8bb27Timo Sirainen const unsigned char *data;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen size_t size, data_size, cur_data_pos, new_pos, bytes_skipped;
54533aa265f5c87730022cc7576090bc51370f97Timo Sirainen bytes_skipped = stream->skip - cstream->prev_skip;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen /* no need to worry about buffers, skip everything */
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen } else if (bytes_skipped < cstream->prev_stream_left) {
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen /* we're still skipping inside buffer */
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen /* done with the buffer */
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen i_stream_skip(cstream->cur_input, bytes_skipped);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen i_assert(stream->pos >= stream->skip + cstream->prev_stream_left);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen cur_data_pos = stream->pos - (stream->skip + cstream->prev_stream_left);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen data = i_stream_get_data(cstream->cur_input, &data_size);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen /* need to read more */
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen if (ret == -1 && cstream->cur_input->stream_errno != 0) {
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen io_stream_set_error(&cstream->istream.iostream,
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen "read(%s) failed: %s",
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen /* we either read something or we're at EOF */
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen last_stream = cstream->input[cstream->cur_idx+1] == NULL;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen if (stream->pos - stream->skip >= stream->max_buffer_size)
4de2a17e0a2aed3b57a6c1057329b6a132b56ae2Timo Sirainen stream->istream.eof = cstream->cur_input->eof && last_stream;
4de2a17e0a2aed3b57a6c1057329b6a132b56ae2Timo Sirainen data = i_stream_get_data(cstream->cur_input, &data_size);
54533aa265f5c87730022cc7576090bc51370f97Timo Sirainen /* we can point directly to the current stream's buffers */
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen /* nothing new read */
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen /* we still have some of the previous stream left. merge the
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen new data with it. */
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen if (!i_stream_try_alloc(stream, new_bytes_count, &size)) {
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen ret = new_pos > stream->pos ? (ssize_t)(new_pos - stream->pos) :
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenstatic unsigned int
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenfind_v_offset(struct concat_istream *cstream, uoff_t *v_offset)
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen unsigned int i;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen /* seek to beginning of this stream */
54533aa265f5c87730022cc7576090bc51370f97Timo Sirainen /* we'll need to figure out this stream's size */
ec23e16ed879e289d12c6e1a5f9745dd3979004aTimo Sirainen if (i_stream_stat(cstream->input[i], TRUE, &st) < 0) {
54533aa265f5c87730022cc7576090bc51370f97Timo Sirainen io_stream_set_error(&cstream->istream.iostream,
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen "stat(%s) failed: %s",
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen i_error("istream-concat: stat(%s) failed: %s",
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen /* @UNSAFE */
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenstatic void i_stream_concat_seek(struct istream_private *stream,
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen struct concat_istream *cstream = (struct concat_istream *)stream;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen cstream->cur_idx = find_v_offset(cstream, &v_offset);
ec23e16ed879e289d12c6e1a5f9745dd3979004aTimo Sirainen cstream->cur_input = cstream->input[cstream->cur_idx];
e248fe370c4047cee921a91b48edc37944ab0526Timo Siraineni_stream_concat_stat(struct istream_private *stream, bool exact ATTR_UNUSED)
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen struct concat_istream *cstream = (struct concat_istream *)stream;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen unsigned int i;
ec23e16ed879e289d12c6e1a5f9745dd3979004aTimo Sirainen /* make sure we have all sizes */
cec3230c9b2a96bac1ea42c69475e8aea4b91eabTimo Sirainen if (find_v_offset(cstream, &v_offset) == UINT_MAX)
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen for (i = 0; i < cstream->unknown_size_idx; i++)
54533aa265f5c87730022cc7576090bc51370f97Timo Sirainen stream->statbuf.st_size += cstream->input_size[i];
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenstruct istream *i_stream_create_concat(struct istream *input[])
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen unsigned int count;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen /* if any of the streams isn't blocking or seekable, set ourself also
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen for (count = 0; input[count] != NULL; count++) {
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen size_t cur_max = input[count]->real_stream->max_buffer_size;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen cstream->input = i_new(struct istream *, count + 1);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen cstream->input_size = i_new(uoff_t, count + 1);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen memcpy(cstream->input, input, sizeof(*input) * count);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen cstream->istream.iostream.close = i_stream_concat_close;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen cstream->istream.iostream.destroy = i_stream_concat_destroy;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen cstream->istream.iostream.set_max_buffer_size =
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen cstream->istream.max_buffer_size = max_buffer_size;