istream-concat.c revision 4afd5082f38342fd688acb5796912329f57dd02c
e59faf65ce864fe95dc00f5d52b8323cdbd0608aTimo Sirainen/* Copyright (c) 2007-2012 Dovecot authors, see the included COPYING file */
37847ec8eaec9ad55c9df10ae109efe7b37ac573Timo Sirainenstatic void i_stream_concat_close(struct iostream_private *stream)
91b203fd2132510a47a4b34252c0ae0efd688a19Timo Sirainen struct concat_istream *cstream = (struct concat_istream *)stream;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen unsigned int i;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenstatic void i_stream_concat_destroy(struct iostream_private *stream)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen struct concat_istream *cstream = (struct concat_istream *)stream;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen unsigned int i;
de486b59018016977015ef42e6071155b60e82e1Timo Siraineni_stream_concat_set_max_buffer_size(struct iostream_private *stream,
de486b59018016977015ef42e6071155b60e82e1Timo Sirainen struct concat_istream *cstream = (struct concat_istream *)stream;
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen unsigned int i;
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen i_stream_set_max_buffer_size(cstream->input[i], max_size);
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainenstatic void i_stream_concat_read_next(struct concat_istream *cstream)
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen const unsigned char *data;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen data = i_stream_get_data(cstream->cur_input, &data_size);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen cstream->cur_input = cstream->input[cstream->cur_idx];
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen if (cstream->prev_stream_left > 0 || cstream->istream.pos == 0) {
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen cstream->istream.pos - cstream->istream.skip);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen /* we already verified that the data size is less than the
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen maximum buffer size */
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen if (!i_stream_try_alloc(&cstream->istream, data_size, &size))
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen memcpy(cstream->istream.w_buffer, data, data_size);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenstatic ssize_t i_stream_concat_read(struct istream_private *stream)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen struct concat_istream *cstream = (struct concat_istream *)stream;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen const unsigned char *data;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen size_t size, data_size, cur_data_pos, new_pos, bytes_skipped;
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen bytes_skipped = stream->skip - cstream->prev_skip;
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen /* no need to worry about buffers, skip everything */
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen } else if (bytes_skipped < cstream->prev_stream_left) {
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen /* we're still skipping inside buffer */
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen /* done with the buffer */
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen i_stream_skip(cstream->cur_input, bytes_skipped);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen i_assert(stream->pos >= stream->skip + cstream->prev_stream_left);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen cur_data_pos = stream->pos - (stream->skip + cstream->prev_stream_left);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen data = i_stream_get_data(cstream->cur_input, &data_size);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen /* need to read more */
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen if (ret == -1 && cstream->cur_input->stream_errno != 0) {
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen /* we either read something or we're at EOF */
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen last_stream = cstream->input[cstream->cur_idx+1] == NULL;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen stream->istream.eof = cstream->cur_input->eof && last_stream;
3fe67ec75ccae1230bb9eb9f16affc48377f6441Timo Sirainen data = i_stream_get_data(cstream->cur_input, &data_size);
2a6dcd984104fed84bed8795ccdfabb20e41ce52Timo Sirainen /* we can point directly to the current stream's buffers */
2a6dcd984104fed84bed8795ccdfabb20e41ce52Timo Sirainen /* nothing new read */
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen /* we still have some of the previous stream left. merge the
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen new data with it. */
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen if (!i_stream_try_alloc(stream, new_bytes_count, &size)) {
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen ret = new_pos > stream->pos ? (ssize_t)(new_pos - stream->pos) :
e5acc283bf030b0b5c79ca4e52d315c516a299faPascal Volkstatic unsigned int
e5acc283bf030b0b5c79ca4e52d315c516a299faPascal Volkfind_v_offset(struct concat_istream *cstream, uoff_t *v_offset)
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen unsigned int i;
1701e3f91107051b1704721bf1dc1e32491faaf9Timo Sirainen /* seek to beginning of this stream */
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen /* we'll need to figure out this stream's size */
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen if (i_stream_stat(cstream->input[i], TRUE, &st) < 0) {
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen "Failed to get size of stream %s",
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen return (unsigned int)-1;
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen /* @UNSAFE */
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainenstatic void i_stream_concat_seek(struct istream_private *stream,
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen struct concat_istream *cstream = (struct concat_istream *)stream;
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen cstream->cur_idx = find_v_offset(cstream, &v_offset);
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen cstream->cur_input = cstream->input[cstream->cur_idx];
2615df45a8027948a474abe5e817b34b0499c171Timo Siraineni_stream_concat_stat(struct istream_private *stream, bool exact ATTR_UNUSED)
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen struct concat_istream *cstream = (struct concat_istream *)stream;
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen unsigned int i;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen /* make sure we have all sizes */
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen for (i = 0; i < cstream->unknown_size_idx; i++)
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen stream->statbuf.st_size += cstream->input_size[i];
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainenstruct istream *i_stream_create_concat(struct istream *input[])
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen unsigned int count;
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen /* if any of the streams isn't blocking or seekable, set ourself also
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen for (count = 0; input[count] != NULL; count++) {
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen size_t cur_max = input[count]->real_stream->max_buffer_size;
02e61e13a8360a9d3ec92c5fa5ae60c0f0181b71Timo Sirainen cstream->input = i_new(struct istream *, count + 1);
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen cstream->input_size = i_new(uoff_t, count + 1);
5494a6bc149da8f02fd25c0434a9d612ac33f659Timo Sirainen memcpy(cstream->input, input, sizeof(*input) * count);
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen cstream->istream.iostream.close = i_stream_concat_close;
02e61e13a8360a9d3ec92c5fa5ae60c0f0181b71Timo Sirainen cstream->istream.iostream.destroy = i_stream_concat_destroy;
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen cstream->istream.iostream.set_max_buffer_size =
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen cstream->istream.max_buffer_size = max_buffer_size;