istream.c revision 0e8fd7a2a52e3682ddfe1b6f52e1bd379fae4c36
5f5870385cff47efd2f58e7892f251cf13761528Timo Sirainen/* Copyright (c) 2002-2010 Dovecot authors, see the included COPYING file */
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen
08d6658a4e2ec8104cd1307f6baa75fdb07a24f8Mark Washenberger#include "lib.h"
31a12066e4cd9310d64091c81b59fb8eb1986023Timo Sirainen#include "ioloop.h"
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen#include "str.h"
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen#include "istream-internal.h"
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainenvoid i_stream_destroy(struct istream **stream)
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen{
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen i_stream_close(*stream);
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen i_stream_unref(stream);
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen}
7ace5117d5f2395bd66f20b09e77dac05492f7ceTimo Sirainen
7ace5117d5f2395bd66f20b09e77dac05492f7ceTimo Sirainenvoid i_stream_ref(struct istream *stream)
7ace5117d5f2395bd66f20b09e77dac05492f7ceTimo Sirainen{
7ace5117d5f2395bd66f20b09e77dac05492f7ceTimo Sirainen io_stream_ref(&stream->real_stream->iostream);
7ace5117d5f2395bd66f20b09e77dac05492f7ceTimo Sirainen}
9ed2951bd0bb1878a27437d7c00611b2baadd614Timo Sirainen
7ace5117d5f2395bd66f20b09e77dac05492f7ceTimo Sirainenvoid i_stream_unref(struct istream **stream)
7ace5117d5f2395bd66f20b09e77dac05492f7ceTimo Sirainen{
7ace5117d5f2395bd66f20b09e77dac05492f7ceTimo Sirainen struct istream_private *_stream = (*stream)->real_stream;
6cbe2facd40ea3461620571a1c168ce9884be3b3Timo Sirainen
7ace5117d5f2395bd66f20b09e77dac05492f7ceTimo Sirainen if (_stream->iostream.refcount == 1) {
7ace5117d5f2395bd66f20b09e77dac05492f7ceTimo Sirainen if (_stream->line_str != NULL)
7ace5117d5f2395bd66f20b09e77dac05492f7ceTimo Sirainen str_free(&_stream->line_str);
7ace5117d5f2395bd66f20b09e77dac05492f7ceTimo Sirainen }
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen io_stream_unref(&(*stream)->real_stream->iostream);
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen *stream = NULL;
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen}
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen#undef i_stream_set_destroy_callback
8759adc67109b5a12a7af3ed717c7040622a0a04Timo Sirainenvoid i_stream_set_destroy_callback(struct istream *stream,
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen istream_callback_t *callback, void *context)
145d2eef238ed8bbff635e3b06951a83f0ee5a03Timo Sirainen{
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen struct iostream_private *iostream = &stream->real_stream->iostream;
9766c242ba9c5db1e3f375860263c5f14c564d95Timo Sirainen
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen iostream->destroy_callback = callback;
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen iostream->destroy_context = context;
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen}
145d2eef238ed8bbff635e3b06951a83f0ee5a03Timo Sirainen
145d2eef238ed8bbff635e3b06951a83f0ee5a03Timo Sirainenint i_stream_get_fd(struct istream *stream)
00bde9ae9eab9e720462bf6ec9a4dd85e88c3bbfTimo Sirainen{
00bde9ae9eab9e720462bf6ec9a4dd85e88c3bbfTimo Sirainen struct istream_private *_stream = stream->real_stream;
00bde9ae9eab9e720462bf6ec9a4dd85e88c3bbfTimo Sirainen
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen return _stream->fd;
00bde9ae9eab9e720462bf6ec9a4dd85e88c3bbfTimo Sirainen}
7ace5117d5f2395bd66f20b09e77dac05492f7ceTimo Sirainen
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainenvoid i_stream_close(struct istream *stream)
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen{
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen io_stream_close(&stream->real_stream->iostream);
bcf5f1acb2e3891f951fd0848c23b86c35efe7e1Timo Sirainen stream->closed = TRUE;
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen if (stream->stream_errno == 0)
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen stream->stream_errno = ENOENT;
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen}
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen
3ab7783791bd46cdd46e9b9de3e98e8efcb6c6bfTimo Sirainenvoid i_stream_set_init_buffer_size(struct istream *stream, size_t size)
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen{
3ab7783791bd46cdd46e9b9de3e98e8efcb6c6bfTimo Sirainen stream->real_stream->init_buffer_size = size;
3ab7783791bd46cdd46e9b9de3e98e8efcb6c6bfTimo Sirainen}
bcf5f1acb2e3891f951fd0848c23b86c35efe7e1Timo Sirainen
24d7c5fc9fa1cb1f49402ec796654113199ba4e6Timo Sirainenvoid i_stream_set_max_buffer_size(struct istream *stream, size_t max_size)
24d7c5fc9fa1cb1f49402ec796654113199ba4e6Timo Sirainen{
24d7c5fc9fa1cb1f49402ec796654113199ba4e6Timo Sirainen io_stream_set_max_buffer_size(&stream->real_stream->iostream, max_size);
24d7c5fc9fa1cb1f49402ec796654113199ba4e6Timo Sirainen}
24d7c5fc9fa1cb1f49402ec796654113199ba4e6Timo Sirainen
24d7c5fc9fa1cb1f49402ec796654113199ba4e6Timo Sirainenvoid i_stream_set_return_partial_line(struct istream *stream, bool set)
24d7c5fc9fa1cb1f49402ec796654113199ba4e6Timo Sirainen{
24d7c5fc9fa1cb1f49402ec796654113199ba4e6Timo Sirainen stream->real_stream->return_nolf_line = set;
145d2eef238ed8bbff635e3b06951a83f0ee5a03Timo Sirainen}
145d2eef238ed8bbff635e3b06951a83f0ee5a03Timo Sirainen
145d2eef238ed8bbff635e3b06951a83f0ee5a03Timo Sirainenssize_t i_stream_read(struct istream *stream)
145d2eef238ed8bbff635e3b06951a83f0ee5a03Timo Sirainen{
145d2eef238ed8bbff635e3b06951a83f0ee5a03Timo Sirainen struct istream_private *_stream = stream->real_stream;
24d7c5fc9fa1cb1f49402ec796654113199ba4e6Timo Sirainen size_t old_size;
24d7c5fc9fa1cb1f49402ec796654113199ba4e6Timo Sirainen ssize_t ret;
24d7c5fc9fa1cb1f49402ec796654113199ba4e6Timo Sirainen
8759adc67109b5a12a7af3ed717c7040622a0a04Timo Sirainen if (unlikely(stream->closed))
8759adc67109b5a12a7af3ed717c7040622a0a04Timo Sirainen return -1;
8759adc67109b5a12a7af3ed717c7040622a0a04Timo Sirainen
24d7c5fc9fa1cb1f49402ec796654113199ba4e6Timo Sirainen stream->eof = FALSE;
24d7c5fc9fa1cb1f49402ec796654113199ba4e6Timo Sirainen stream->stream_errno = 0;
553308791c097219e8eb31cbd03a29e9e1333848Timo Sirainen
24d7c5fc9fa1cb1f49402ec796654113199ba4e6Timo Sirainen old_size = _stream->pos - _stream->skip;
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen ret = _stream->read(_stream);
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen switch (ret) {
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen case -2:
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen i_assert(_stream->skip != _stream->pos);
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen break;
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen case -1:
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen if (stream->stream_errno != 0) {
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen /* error handling should be easier if we now just
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen assume the stream is now at EOF */
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen stream->eof = TRUE;
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen } else {
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen i_assert(stream->eof);
8759adc67109b5a12a7af3ed717c7040622a0a04Timo Sirainen }
145d2eef238ed8bbff635e3b06951a83f0ee5a03Timo Sirainen break;
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen case 0:
9766c242ba9c5db1e3f375860263c5f14c564d95Timo Sirainen i_assert(!stream->blocking);
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen break;
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen default:
145d2eef238ed8bbff635e3b06951a83f0ee5a03Timo Sirainen i_assert(ret > 0);
145d2eef238ed8bbff635e3b06951a83f0ee5a03Timo Sirainen i_assert((size_t)ret+old_size == _stream->pos - _stream->skip);
00bde9ae9eab9e720462bf6ec9a4dd85e88c3bbfTimo Sirainen break;
00bde9ae9eab9e720462bf6ec9a4dd85e88c3bbfTimo Sirainen }
00bde9ae9eab9e720462bf6ec9a4dd85e88c3bbfTimo Sirainen return ret;
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen}
00bde9ae9eab9e720462bf6ec9a4dd85e88c3bbfTimo Sirainen
7ace5117d5f2395bd66f20b09e77dac05492f7ceTimo Sirainenssize_t i_stream_read_copy_from_parent(struct istream *istream)
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen{
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen struct istream_private *stream = istream->real_stream;
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen size_t pos;
a3dd97fb6d92a89c3de0597fed2d4b044c7aeb84Timo Sirainen ssize_t ret;
a3dd97fb6d92a89c3de0597fed2d4b044c7aeb84Timo Sirainen
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen stream->pos -= stream->skip;
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen stream->skip = 0;
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen stream->buffer = i_stream_get_data(stream->parent, &pos);
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen if (pos > stream->pos)
553308791c097219e8eb31cbd03a29e9e1333848Timo Sirainen ret = 0;
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen else do {
a3dd97fb6d92a89c3de0597fed2d4b044c7aeb84Timo Sirainen if ((ret = i_stream_read(stream->parent)) == -2)
7dcb5545370faa9d4ff83b3ede65a69fc3dd4b65Timo Sirainen return -2;
a3dd97fb6d92a89c3de0597fed2d4b044c7aeb84Timo Sirainen
cc0495b3bbe3c3e41c512274b302d6f0fa028187Timo Sirainen stream->istream.stream_errno = stream->parent->stream_errno;
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen stream->istream.eof = stream->parent->eof;
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen stream->buffer = i_stream_get_data(stream->parent, &pos);
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen /* check again, in case the parent stream had been seeked
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen backwards and the previous read() didn't get us far
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen enough. */
31a12066e4cd9310d64091c81b59fb8eb1986023Timo Sirainen } while (pos <= stream->pos && ret > 0);
31a12066e4cd9310d64091c81b59fb8eb1986023Timo Sirainen
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen ret = pos > stream->pos ? (ssize_t)(pos - stream->pos) :
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen (ret == 0 ? 0 : -1);
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen stream->pos = pos;
31a12066e4cd9310d64091c81b59fb8eb1986023Timo Sirainen i_assert(ret != -1 || stream->istream.eof ||
31a12066e4cd9310d64091c81b59fb8eb1986023Timo Sirainen stream->istream.stream_errno != 0);
31a12066e4cd9310d64091c81b59fb8eb1986023Timo Sirainen return ret;
31a12066e4cd9310d64091c81b59fb8eb1986023Timo Sirainen}
31a12066e4cd9310d64091c81b59fb8eb1986023Timo Sirainen
31a12066e4cd9310d64091c81b59fb8eb1986023Timo Sirainenvoid i_stream_skip(struct istream *stream, uoff_t count)
31a12066e4cd9310d64091c81b59fb8eb1986023Timo Sirainen{
61618d4c58080570f689614fec204ae14e90cef2Timo Sirainen struct istream_private *_stream = stream->real_stream;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen size_t data_size;
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen data_size = _stream->pos - _stream->skip;
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen if (count <= data_size) {
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen /* within buffer */
8ef7c24178fd798c3e0301c5b8afa1a9bdedd27fTimo Sirainen stream->v_offset += count;
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen _stream->skip += count;
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen return;
}
/* have to seek forward */
count -= data_size;
_stream->skip = _stream->pos;
stream->v_offset += data_size;
if (unlikely(stream->closed))
return;
stream->stream_errno = 0;
_stream->seek(_stream, stream->v_offset + count, FALSE);
}
static bool i_stream_can_optimize_seek(struct istream *stream)
{
uoff_t expected_offset;
if (stream->real_stream->parent == NULL)
return TRUE;
/* use the fast route only if the parent stream is at the
expected offset */
expected_offset = stream->real_stream->parent_start_offset +
stream->v_offset - stream->real_stream->skip;
if (stream->real_stream->parent->v_offset != expected_offset)
return FALSE;
return i_stream_can_optimize_seek(stream->real_stream->parent);
}
void i_stream_seek(struct istream *stream, uoff_t v_offset)
{
struct istream_private *_stream = stream->real_stream;
if (v_offset >= stream->v_offset &&
i_stream_can_optimize_seek(stream)) {
i_stream_skip(stream, v_offset - stream->v_offset);
return;
}
if (unlikely(stream->closed))
return;
stream->eof = FALSE;
_stream->seek(_stream, v_offset, FALSE);
}
void i_stream_seek_mark(struct istream *stream, uoff_t v_offset)
{
struct istream_private *_stream = stream->real_stream;
if (unlikely(stream->closed))
return;
stream->eof = FALSE;
_stream->seek(_stream, v_offset, TRUE);
}
void i_stream_sync(struct istream *stream)
{
struct istream_private *_stream = stream->real_stream;
if (unlikely(stream->closed))
return;
if (_stream->sync != NULL)
_stream->sync(_stream);
}
const struct stat *i_stream_stat(struct istream *stream, bool exact)
{
struct istream_private *_stream = stream->real_stream;
if (unlikely(stream->closed))
return NULL;
return _stream->stat(_stream, exact);
}
int i_stream_get_size(struct istream *stream, bool exact, uoff_t *size_r)
{
struct istream_private *_stream = stream->real_stream;
if (unlikely(stream->closed))
return -1;
return _stream->get_size(_stream, exact, size_r);
}
bool i_stream_have_bytes_left(const struct istream *stream)
{
const struct istream_private *_stream = stream->real_stream;
return !stream->eof || _stream->skip != _stream->pos;
}
bool i_stream_is_eof(struct istream *stream)
{
const struct istream_private *_stream = stream->real_stream;
if (_stream->skip == _stream->pos)
(void)i_stream_read(stream);
return !i_stream_have_bytes_left(stream);
}
static char *i_stream_next_line_finish(struct istream_private *stream, size_t i)
{
char *ret;
size_t end;
if (i > 0 && stream->buffer[i-1] == '\r')
end = i - 1;
else
end = i;
if (stream->w_buffer != NULL) {
/* modify the buffer directly */
stream->w_buffer[end] = '\0';
ret = (char *)stream->w_buffer + stream->skip;
} else {
/* use a temporary string to return it */
if (stream->line_str == NULL)
stream->line_str = str_new(default_pool, 256);
str_truncate(stream->line_str, 0);
str_append_n(stream->line_str, stream->buffer + stream->skip,
end - stream->skip);
ret = str_c_modifiable(stream->line_str);
}
if (i < stream->pos)
i++;
stream->istream.v_offset += i - stream->skip;
stream->skip = i;
return ret;
}
static char *i_stream_last_line(struct istream_private *_stream)
{
if (_stream->istream.eof && _stream->skip != _stream->pos &&
_stream->return_nolf_line) {
/* the last line is missing LF and we want to return it. */
return i_stream_next_line_finish(_stream, _stream->pos);
}
return NULL;
}
char *i_stream_next_line(struct istream *stream)
{
struct istream_private *_stream = stream->real_stream;
char *ret_buf;
size_t i;
if (_stream->skip >= _stream->pos) {
stream->stream_errno = 0;
return NULL;
}
if (unlikely(_stream->w_buffer == NULL)) {
i_error("i_stream_next_line() called for unmodifiable stream");
return NULL;
}
/* @UNSAFE */
ret_buf = NULL;
for (i = _stream->skip; i < _stream->pos; i++) {
if (_stream->buffer[i] == 10) {
/* got it */
ret_buf = i_stream_next_line_finish(_stream, i);
break;
}
}
if (ret_buf == NULL)
return i_stream_last_line(_stream);
return ret_buf;
}
char *i_stream_read_next_line(struct istream *stream)
{
char *line;
for (;;) {
line = i_stream_next_line(stream);
if (line != NULL)
break;
if (i_stream_read(stream) <= 0)
return i_stream_last_line(stream->real_stream);
}
return line;
}
const unsigned char *
i_stream_get_data(const struct istream *stream, size_t *size_r)
{
const struct istream_private *_stream = stream->real_stream;
if (_stream->skip >= _stream->pos) {
*size_r = 0;
return NULL;
}
*size_r = _stream->pos - _stream->skip;
return _stream->buffer + _stream->skip;
}
unsigned char *i_stream_get_modifiable_data(const struct istream *stream,
size_t *size_r)
{
const struct istream_private *_stream = stream->real_stream;
if (_stream->skip >= _stream->pos || _stream->w_buffer == NULL) {
*size_r = 0;
return NULL;
}
*size_r = _stream->pos - _stream->skip;
return _stream->w_buffer + _stream->skip;
}
int i_stream_read_data(struct istream *stream, const unsigned char **data_r,
size_t *size_r, size_t threshold)
{
ssize_t ret = 0;
bool read_more = FALSE;
do {
*data_r = i_stream_get_data(stream, size_r);
if (*size_r > threshold)
return 1;
/* we need more data */
ret = i_stream_read(stream);
if (ret > 0)
read_more = TRUE;
} while (ret > 0);
*data_r = i_stream_get_data(stream, size_r);
if (ret == -2)
return -2;
if (ret == 0) {
/* need to read more */
i_assert(!stream->blocking);
return 0;
}
if (stream->eof) {
if (read_more) {
/* we read at least some new data */
return 0;
}
} else {
i_assert(stream->stream_errno != 0);
}
return -1;
}
void i_stream_compress(struct istream_private *stream)
{
memmove(stream->w_buffer, stream->w_buffer + stream->skip,
stream->pos - stream->skip);
stream->pos -= stream->skip;
stream->skip = 0;
}
void i_stream_grow_buffer(struct istream_private *stream, size_t bytes)
{
size_t old_size;
old_size = stream->buffer_size;
stream->buffer_size = stream->pos + bytes;
if (stream->buffer_size <= stream->init_buffer_size)
stream->buffer_size = stream->init_buffer_size;
else
stream->buffer_size = nearest_power(stream->buffer_size);
if (stream->max_buffer_size > 0 &&
stream->buffer_size > stream->max_buffer_size)
stream->buffer_size = stream->max_buffer_size;
if (stream->buffer_size <= old_size)
stream->buffer_size = old_size;
else {
stream->w_buffer = i_realloc(stream->w_buffer, old_size,
stream->buffer_size);
stream->buffer = stream->w_buffer;
}
}
bool i_stream_get_buffer_space(struct istream_private *stream,
size_t wanted_size, size_t *size_r)
{
i_assert(wanted_size > 0);
if (wanted_size > stream->buffer_size - stream->pos) {
if (stream->skip > 0) {
/* remove the unused bytes from beginning of buffer */
i_stream_compress(stream);
} else if (stream->max_buffer_size == 0 ||
stream->buffer_size < stream->max_buffer_size) {
/* buffer is full - grow it */
i_stream_grow_buffer(stream, I_STREAM_MIN_SIZE);
}
}
if (size_r != NULL)
*size_r = stream->buffer_size - stream->pos;
return stream->pos != stream->buffer_size;
}
bool i_stream_add_data(struct istream *_stream, const unsigned char *data,
size_t size)
{
struct istream_private *stream = _stream->real_stream;
size_t size2;
(void)i_stream_get_buffer_space(stream, size, &size2);
if (size > size2)
return FALSE;
memcpy(stream->w_buffer + stream->pos, data, size);
stream->pos += size;
return TRUE;
}
static void
i_stream_default_set_max_buffer_size(struct iostream_private *stream,
size_t max_size)
{
struct istream_private *_stream = (struct istream_private *)stream;
_stream->max_buffer_size = max_size;
if (_stream->parent != NULL)
i_stream_set_max_buffer_size(_stream->parent, max_size);
}
static void i_stream_default_destroy(struct iostream_private *stream)
{
struct istream_private *_stream = (struct istream_private *)stream;
i_free(_stream->w_buffer);
if (_stream->parent != NULL)
i_stream_unref(&_stream->parent);
}
static void
i_stream_default_seek(struct istream_private *stream,
uoff_t v_offset, bool mark ATTR_UNUSED)
{
size_t available;
if (stream->istream.v_offset > v_offset)
i_panic("stream doesn't support seeking backwards");
while (stream->istream.v_offset < v_offset) {
(void)i_stream_read(&stream->istream);
available = stream->pos - stream->skip;
if (available == 0) {
stream->istream.stream_errno = ESPIPE;
return;
}
if (available <= v_offset - stream->istream.v_offset)
i_stream_skip(&stream->istream, available);
else {
i_stream_skip(&stream->istream,
v_offset - stream->istream.v_offset);
}
}
}
static const struct stat *
i_stream_default_stat(struct istream_private *stream, bool exact ATTR_UNUSED)
{
return &stream->statbuf;
}
static int
i_stream_default_get_size(struct istream_private *stream,
bool exact, uoff_t *size_r)
{
const struct stat *st;
st = stream->stat(stream, exact);
if (st == NULL)
return -1;
if (st->st_size == -1)
return 0;
*size_r = st->st_size;
return 1;
}
struct istream *
i_stream_create(struct istream_private *_stream, struct istream *parent, int fd)
{
_stream->fd = fd;
if (parent != NULL) {
_stream->parent = parent;
_stream->parent_start_offset = parent->v_offset;
_stream->abs_start_offset = parent->v_offset +
parent->real_stream->abs_start_offset;
i_stream_ref(parent);
}
_stream->istream.real_stream = _stream;
if (_stream->iostream.destroy == NULL)
_stream->iostream.destroy = i_stream_default_destroy;
if (_stream->seek == NULL) {
i_assert(!_stream->istream.seekable);
_stream->seek = i_stream_default_seek;
}
if (_stream->stat == NULL)
_stream->stat = i_stream_default_stat;
if (_stream->get_size == NULL)
_stream->get_size = i_stream_default_get_size;
if (_stream->iostream.set_max_buffer_size == NULL) {
_stream->iostream.set_max_buffer_size =
i_stream_default_set_max_buffer_size;
}
if (_stream->init_buffer_size == 0)
_stream->init_buffer_size = I_STREAM_MIN_SIZE;
memset(&_stream->statbuf, 0, sizeof(_stream->statbuf));
_stream->statbuf.st_size = -1;
_stream->statbuf.st_atime =
_stream->statbuf.st_mtime =
_stream->statbuf.st_ctime = ioloop_time;
io_stream_init(&_stream->iostream);
return &_stream->istream;
}