istream-sized.c revision 899653bd8261ee9fe989aec0b1221a47e0de23cd
e59faf65ce864fe95dc00f5d52b8323cdbd0608aTimo Sirainen/* Copyright (c) 2003-2016 Dovecot authors, see the included COPYING file */
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainenstatic void i_stream_sized_destroy(struct iostream_private *stream)
029cfcdce65b284d5230adf1c920a5f526b03b5cTimo Sirainen struct sized_istream *sstream = (struct sized_istream *)stream;
029cfcdce65b284d5230adf1c920a5f526b03b5cTimo Sirainen v_offset = sstream->istream.parent_start_offset +
029cfcdce65b284d5230adf1c920a5f526b03b5cTimo Sirainen v_offset > sstream->istream.parent->v_offset) {
90c23747727c85f80e4e8eed7968f0edbeac7ac5Timo Sirainen /* get to same position in parent stream */
90c23747727c85f80e4e8eed7968f0edbeac7ac5Timo Sirainen i_stream_seek(sstream->istream.parent, v_offset);
90c23747727c85f80e4e8eed7968f0edbeac7ac5Timo Sirainenstatic const char *
90c23747727c85f80e4e8eed7968f0edbeac7ac5Timo Sirainen const struct istream_sized_error_data *data, void *context ATTR_UNUSED)
90c23747727c85f80e4e8eed7968f0edbeac7ac5Timo Sirainen if (data->v_offset + data->new_bytes < data->wanted_size) {
90c23747727c85f80e4e8eed7968f0edbeac7ac5Timo Sirainen return t_strdup_printf("Stream is smaller than expected "
90c23747727c85f80e4e8eed7968f0edbeac7ac5Timo Sirainen data->v_offset + data->new_bytes, data->wanted_size);
90c23747727c85f80e4e8eed7968f0edbeac7ac5Timo Sirainen return t_strdup_printf("Stream is larger than expected "
90c23747727c85f80e4e8eed7968f0edbeac7ac5Timo Sirainen data->v_offset + data->new_bytes, data->wanted_size,
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainenstatic ssize_t i_stream_sized_read(struct istream_private *stream)
b397665e90fa0fc7c6a9156fdd6cf28b571e8e39Timo Sirainen /* avoid duplicate errors */
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen (stream->pos - stream->skip) >= sstream->size) {
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen i_stream_seek(stream->parent, sstream->istream.parent_start_offset +
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen stream->buffer = i_stream_get_data(stream->parent, &pos);
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen if ((ret = i_stream_read(stream->parent)) == -2)
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen stream->istream.stream_errno = stream->parent->stream_errno;
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen stream->buffer = i_stream_get_data(stream->parent, &pos);
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen left = sstream->size - stream->istream.v_offset;
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen error = sstream->error_callback(&data, sstream->error_context);
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen io_stream_set_error(&stream->iostream, "%s", error);
029cfcdce65b284d5230adf1c920a5f526b03b5cTimo Sirainen /* still more to read */
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen } else if (stream->istream.stream_errno == ENOENT) {
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen /* lost the file */
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen error = sstream->error_callback(&data, sstream->error_context);
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen io_stream_set_error(&stream->iostream, "%s", error);
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen ret = pos > stream->pos ? (ssize_t)(pos - stream->pos) :
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Siraineni_stream_sized_stat(struct istream_private *stream, bool exact ATTR_UNUSED)
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen struct sized_istream *sstream = (struct sized_istream *)stream;
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen /* parent stream may be base64-decoder. don't waste time decoding the
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen entire stream, since we already know what the size is supposed
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen if (i_stream_stat(stream->parent, FALSE, &st) < 0) {
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen stream->istream.stream_errno = stream->parent->stream_errno;
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Siraineni_stream_create_sized_common(struct istream *input, uoff_t size)
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen sstream->istream.max_buffer_size = input->real_stream->max_buffer_size;
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen sstream->istream.iostream.destroy = i_stream_sized_destroy;
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen sstream->istream.istream.readable_fd = input->readable_fd;
f153a2cec0319f549388d28f8cfd4d50229d1132Timo Sirainen sstream->istream.istream.blocking = input->blocking;
f153a2cec0319f549388d28f8cfd4d50229d1132Timo Sirainen sstream->istream.istream.seekable = input->seekable;
f153a2cec0319f549388d28f8cfd4d50229d1132Timo Sirainen (void)i_stream_create(&sstream->istream, input,
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainenstruct istream *i_stream_create_sized(struct istream *input, uoff_t size)
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen sstream = i_stream_create_sized_common(input, size);
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen sstream->error_callback = i_stream_create_sized_default_error_callback;
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Siraineni_stream_create_sized_with_callback(struct istream *input, uoff_t size,