istream-concat.c revision 4ce6338bf945cccfff9e4ce7cc6aa2246851b84a
a8c5a86d183db25a57bf193c06b41e092ec2e151Timo Sirainen/* Copyright (c) 2007 Dovecot authors, see the included COPYING file */
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainenstatic void i_stream_concat_close(struct iostream_private *stream)
48f78a48f2e1cf299026544444666471ae16ad97Timo Sirainen struct concat_istream *cstream = (struct concat_istream *)stream;
6e77746e501c2b45850b1c530836058ed75e09eeTimo Sirainen unsigned int i;
6e77746e501c2b45850b1c530836058ed75e09eeTimo Sirainenstatic void i_stream_concat_destroy(struct iostream_private *stream)
6e77746e501c2b45850b1c530836058ed75e09eeTimo Sirainen struct concat_istream *cstream = (struct concat_istream *)stream;
6e77746e501c2b45850b1c530836058ed75e09eeTimo Sirainen unsigned int i;
8c8f7ac580b661aee3d8b8dd37df4a9b41c77000Timo Siraineni_stream_concat_set_max_buffer_size(struct iostream_private *stream,
8c8f7ac580b661aee3d8b8dd37df4a9b41c77000Timo Sirainen struct concat_istream *cstream = (struct concat_istream *)stream;
8c8f7ac580b661aee3d8b8dd37df4a9b41c77000Timo Sirainen unsigned int i;
8c8f7ac580b661aee3d8b8dd37df4a9b41c77000Timo Sirainen i_stream_set_max_buffer_size(cstream->input[i], max_size);
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainenstatic void i_stream_concat_read_next(struct concat_istream *cstream)
48f78a48f2e1cf299026544444666471ae16ad97Timo Sirainen const unsigned char *data;
0dc7891233a973829f00371b27810f849b987c66Timo Sirainen cstream->cur_input = cstream->input[cstream->cur_idx];
0dc7891233a973829f00371b27810f849b987c66Timo Sirainen if (cstream->istream.pos == cstream->istream.skip) {
b5ff746939712c6a9bef71405fa786d5471cf177Timo Sirainen /* we need to keep the current data */
b5ff746939712c6a9bef71405fa786d5471cf177Timo Sirainen data = cstream->istream.buffer + cstream->istream.skip;
b5ff746939712c6a9bef71405fa786d5471cf177Timo Sirainen data_size = cstream->istream.pos - cstream->istream.skip;
b5ff746939712c6a9bef71405fa786d5471cf177Timo Sirainen cstream->istream.skip = cstream->istream.pos = 0;
b5ff746939712c6a9bef71405fa786d5471cf177Timo Sirainen /* we already verified that the data size is less than the
b5ff746939712c6a9bef71405fa786d5471cf177Timo Sirainen maximum buffer size */
b5ff746939712c6a9bef71405fa786d5471cf177Timo Sirainen if (!i_stream_get_buffer_space(&cstream->istream, data_size, &size))
b5ff746939712c6a9bef71405fa786d5471cf177Timo Sirainen memcpy(cstream->istream.w_buffer, data, data_size);
b5ff746939712c6a9bef71405fa786d5471cf177Timo Sirainenstatic ssize_t i_stream_concat_read(struct istream_private *stream)
b5ff746939712c6a9bef71405fa786d5471cf177Timo Sirainen struct concat_istream *cstream = (struct concat_istream *)stream;
b5ff746939712c6a9bef71405fa786d5471cf177Timo Sirainen const unsigned char *data;
b5ff746939712c6a9bef71405fa786d5471cf177Timo Sirainen /* we don't need the buffer anymore */
b5ff746939712c6a9bef71405fa786d5471cf177Timo Sirainen data = i_stream_get_data(cstream->cur_input, &pos);
b5ff746939712c6a9bef71405fa786d5471cf177Timo Sirainen /* need to read more */
b5ff746939712c6a9bef71405fa786d5471cf177Timo Sirainen if (ret == -1 && stream->istream.stream_errno != 0) {
b5ff746939712c6a9bef71405fa786d5471cf177Timo Sirainen /* we either read something or we're at EOF */
b5ff746939712c6a9bef71405fa786d5471cf177Timo Sirainen last_stream = cstream->input[cstream->cur_idx+1] == NULL;
b5ff746939712c6a9bef71405fa786d5471cf177Timo Sirainen stream->istream.eof = cstream->cur_input->eof && last_stream;
b5ff746939712c6a9bef71405fa786d5471cf177Timo Sirainen data = i_stream_get_data(cstream->cur_input, &pos);
b5ff746939712c6a9bef71405fa786d5471cf177Timo Sirainen if (!i_stream_get_buffer_space(stream, pos, &size))
4eb418849d5c6bf77b2721e4e6aef2e97deaa197Timo Sirainen memcpy(stream->w_buffer + stream->pos, data, pos);
4eb418849d5c6bf77b2721e4e6aef2e97deaa197Timo Sirainen ret = pos > stream->pos ? (ssize_t)(pos - stream->pos) :
4eb418849d5c6bf77b2721e4e6aef2e97deaa197Timo Sirainenstatic unsigned int
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainenfind_v_offset(struct concat_istream *cstream, uoff_t *v_offset)
02b32cf39a098edf60981fc228e4b034f11f3b90Timo Sirainen unsigned int i;
0dc7891233a973829f00371b27810f849b987c66Timo Sirainen /* seek to beginning of this stream */
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainen /* we'll need to figure out this stream's size */
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainen return (unsigned int)-1;
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainen /* @UNSAFE */
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainenstatic void i_stream_concat_seek(struct istream_private *stream,
0dc7891233a973829f00371b27810f849b987c66Timo Sirainen struct concat_istream *cstream = (struct concat_istream *)stream;
0dc7891233a973829f00371b27810f849b987c66Timo Sirainen cstream->cur_idx = find_v_offset(cstream, &v_offset);
9e86ad9eb313004cd4c8b5427daeb4c241b57af6Timo Sirainen cstream->cur_input = cstream->input[cstream->cur_idx];
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainenstatic const struct stat *
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Siraineni_stream_concat_stat(struct istream_private *stream, bool exact ATTR_UNUSED)
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainen struct concat_istream *cstream = (struct concat_istream *)stream;
0dc7891233a973829f00371b27810f849b987c66Timo Sirainen unsigned int i;
0dc7891233a973829f00371b27810f849b987c66Timo Sirainen /* make sure we have all sizes */
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainen for (i = 0; i < cstream->unknown_size_idx; i++)
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainen stream->statbuf.st_size += cstream->input_size[i];
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainenstruct istream *i_stream_create_concat(struct istream *input[])
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainen unsigned int count;
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainen /* if any of the streams isn't blocking or seekable, set ourself also
c2bb1764c359ce85a7f7f789ead11dd613ff9769Timo Sirainen for (count = 0; input[count] != NULL; count++) {
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainen size_t cur_max = input[count]->real_stream->max_buffer_size;
b5ff746939712c6a9bef71405fa786d5471cf177Timo Sirainen cstream->input = i_new(struct istream *, count + 1);
b5ff746939712c6a9bef71405fa786d5471cf177Timo Sirainen cstream->input_size = i_new(uoff_t, count + 1);
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainen memcpy(cstream->input, input, sizeof(*input) * count);
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainen cstream->istream.iostream.close = i_stream_concat_close;
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainen cstream->istream.iostream.destroy = i_stream_concat_destroy;
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainen cstream->istream.iostream.set_max_buffer_size =
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainen cstream->istream.max_buffer_size = max_buffer_size;
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainen return i_stream_create(&cstream->istream, -1, 0);