istream-sized.c revision 899653bd8261ee9fe989aec0b1221a47e0de23cd
e59faf65ce864fe95dc00f5d52b8323cdbd0608aTimo Sirainen/* Copyright (c) 2003-2016 Dovecot authors, see the included COPYING file */
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen#include "lib.h"
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen#include "istream-private.h"
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen#include "istream-sized.h"
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainenstruct sized_istream {
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen struct istream_private istream;
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen istream_sized_callback_t *error_callback;
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen void *error_context;
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainen
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen uoff_t size;
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen bool failed;
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen};
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainenstatic void i_stream_sized_destroy(struct iostream_private *stream)
029cfcdce65b284d5230adf1c920a5f526b03b5cTimo Sirainen{
029cfcdce65b284d5230adf1c920a5f526b03b5cTimo Sirainen struct sized_istream *sstream = (struct sized_istream *)stream;
029cfcdce65b284d5230adf1c920a5f526b03b5cTimo Sirainen uoff_t v_offset;
029cfcdce65b284d5230adf1c920a5f526b03b5cTimo Sirainen
029cfcdce65b284d5230adf1c920a5f526b03b5cTimo Sirainen v_offset = sstream->istream.parent_start_offset +
029cfcdce65b284d5230adf1c920a5f526b03b5cTimo Sirainen sstream->istream.istream.v_offset;
029cfcdce65b284d5230adf1c920a5f526b03b5cTimo Sirainen if (sstream->istream.parent->seekable ||
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 Sirainen }
90c23747727c85f80e4e8eed7968f0edbeac7ac5Timo Sirainen}
90c23747727c85f80e4e8eed7968f0edbeac7ac5Timo Sirainen
90c23747727c85f80e4e8eed7968f0edbeac7ac5Timo Sirainenstatic const char *
90c23747727c85f80e4e8eed7968f0edbeac7ac5Timo Siraineni_stream_create_sized_default_error_callback(
90c23747727c85f80e4e8eed7968f0edbeac7ac5Timo Sirainen const struct istream_sized_error_data *data, void *context ATTR_UNUSED)
90c23747727c85f80e4e8eed7968f0edbeac7ac5Timo Sirainen{
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 "(%"PRIuUOFF_T" < %"PRIuUOFF_T")",
90c23747727c85f80e4e8eed7968f0edbeac7ac5Timo Sirainen data->v_offset + data->new_bytes, data->wanted_size);
90c23747727c85f80e4e8eed7968f0edbeac7ac5Timo Sirainen } else {
90c23747727c85f80e4e8eed7968f0edbeac7ac5Timo Sirainen return t_strdup_printf("Stream is larger than expected "
90c23747727c85f80e4e8eed7968f0edbeac7ac5Timo Sirainen "(%"PRIuUOFF_T" > %"PRIuUOFF_T", eof=%d)",
90c23747727c85f80e4e8eed7968f0edbeac7ac5Timo Sirainen data->v_offset + data->new_bytes, data->wanted_size,
90c23747727c85f80e4e8eed7968f0edbeac7ac5Timo Sirainen data->eof);
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen }
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen}
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainenstatic ssize_t i_stream_sized_read(struct istream_private *stream)
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen{
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen struct sized_istream *sstream =
2d49f150b4bce6f2f59a84e268e4777901c3e42cTimo Sirainen (struct sized_istream *)stream;
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen struct istream_sized_error_data data;
90c23747727c85f80e4e8eed7968f0edbeac7ac5Timo Sirainen const char *error;
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen uoff_t left;
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen ssize_t ret;
19e8adccba16ff419f5675b1575358c2956dce83Timo Sirainen size_t pos;
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen
b397665e90fa0fc7c6a9156fdd6cf28b571e8e39Timo Sirainen if (sstream->failed) {
b397665e90fa0fc7c6a9156fdd6cf28b571e8e39Timo Sirainen /* avoid duplicate errors */
b397665e90fa0fc7c6a9156fdd6cf28b571e8e39Timo Sirainen stream->istream.stream_errno = EINVAL;
b397665e90fa0fc7c6a9156fdd6cf28b571e8e39Timo Sirainen return -1;
19e8adccba16ff419f5675b1575358c2956dce83Timo Sirainen }
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen if (stream->istream.v_offset +
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen (stream->pos - stream->skip) >= sstream->size) {
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen stream->istream.eof = TRUE;
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen return -1;
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen }
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen i_stream_seek(stream->parent, sstream->istream.parent_start_offset +
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen stream->istream.v_offset);
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen stream->pos -= stream->skip;
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen stream->skip = 0;
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen stream->buffer = i_stream_get_data(stream->parent, &pos);
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen if (pos > stream->pos)
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen ret = 0;
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen else do {
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen if ((ret = i_stream_read(stream->parent)) == -2)
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen return -2;
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen stream->istream.stream_errno = stream->parent->stream_errno;
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen stream->istream.eof = stream->parent->eof;
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen stream->buffer = i_stream_get_data(stream->parent, &pos);
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen } while (pos <= stream->pos && ret > 0);
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen memset(&data, 0, sizeof(data));
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen data.v_offset = stream->istream.v_offset;
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen data.new_bytes = pos;
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen data.wanted_size = sstream->size;
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen data.eof = stream->istream.eof;
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen left = sstream->size - stream->istream.v_offset;
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen if (pos == left)
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen stream->istream.eof = TRUE;
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen else if (pos > left) {
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen error = sstream->error_callback(&data, sstream->error_context);
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen io_stream_set_error(&stream->iostream, "%s", error);
0f66f12eb4cdbf47670975044c88d8f388bf92dfTimo Sirainen i_error("read(%s) failed: %s",
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen i_stream_get_name(stream->parent),
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen stream->iostream.error);
029cfcdce65b284d5230adf1c920a5f526b03b5cTimo Sirainen pos = left;
029cfcdce65b284d5230adf1c920a5f526b03b5cTimo Sirainen stream->istream.eof = TRUE;
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen stream->istream.stream_errno = EINVAL;
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen sstream->failed = TRUE;
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen return -1;
029cfcdce65b284d5230adf1c920a5f526b03b5cTimo Sirainen } else if (!stream->istream.eof) {
029cfcdce65b284d5230adf1c920a5f526b03b5cTimo Sirainen /* still more to read */
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen } else if (stream->istream.stream_errno == ENOENT) {
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen /* lost the file */
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen } else {
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen error = sstream->error_callback(&data, sstream->error_context);
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen io_stream_set_error(&stream->iostream, "%s", error);
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen i_error("read(%s) failed: %s",
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen i_stream_get_name(stream->parent),
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen stream->iostream.error);
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen stream->istream.stream_errno = EINVAL;
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen }
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen ret = pos > stream->pos ? (ssize_t)(pos - stream->pos) :
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen (ret == 0 ? 0 : -1);
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainen stream->pos = pos;
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen i_assert(ret != -1 || stream->istream.eof ||
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen stream->istream.stream_errno != 0);
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen return ret;
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen}
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainenstatic int
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Siraineni_stream_sized_stat(struct istream_private *stream, bool exact ATTR_UNUSED)
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen{
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen struct sized_istream *sstream = (struct sized_istream *)stream;
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen const struct stat *st;
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen
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 to be. */
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen if (i_stream_stat(stream->parent, FALSE, &st) < 0) {
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen stream->istream.stream_errno = stream->parent->stream_errno;
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen return -1;
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainen }
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen stream->statbuf = *st;
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen stream->statbuf.st_size = sstream->size;
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen return 0;
0bf3eac1110a902e7ec7e695c64e8e46c114e623Timo Sirainen}
0bf3eac1110a902e7ec7e695c64e8e46c114e623Timo Sirainen
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainenstatic struct sized_istream *
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Siraineni_stream_create_sized_common(struct istream *input, uoff_t size)
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen{
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen struct sized_istream *sstream;
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen sstream = i_new(struct sized_istream, 1);
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen sstream->size = size;
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen sstream->istream.max_buffer_size = input->real_stream->max_buffer_size;
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen sstream->istream.iostream.destroy = i_stream_sized_destroy;
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen sstream->istream.read = i_stream_sized_read;
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen sstream->istream.stat = i_stream_sized_stat;
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen
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,
f153a2cec0319f549388d28f8cfd4d50229d1132Timo Sirainen i_stream_get_fd(input));
f153a2cec0319f549388d28f8cfd4d50229d1132Timo Sirainen return sstream;
dffa503fd4ce31334346e539496084c80a2d8d37Timo Sirainen}
dffa503fd4ce31334346e539496084c80a2d8d37Timo Sirainen
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainenstruct istream *i_stream_create_sized(struct istream *input, uoff_t size)
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen{
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen struct sized_istream *sstream;
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen sstream = i_stream_create_sized_common(input, size);
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen sstream->error_callback = i_stream_create_sized_default_error_callback;
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen sstream->error_context = sstream;
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen return &sstream->istream.istream;
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen}
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen#undef i_stream_create_sized_with_callback
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainenstruct istream *
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Siraineni_stream_create_sized_with_callback(struct istream *input, uoff_t size,
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen istream_sized_callback_t *error_callback,
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen void *context)
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen{
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen struct sized_istream *sstream;
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen sstream = i_stream_create_sized_common(input, size);
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen sstream->error_callback = error_callback;
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen sstream->error_context = context;
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen return &sstream->istream.istream;
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen}
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen