istream-limit.c revision 7a4c6cc0aea1fc7999ac9bbebc3a95197ac2585c
7ef3553585e556f35d5919589cfdc1de3329e4bbTimo Sirainen/* Copyright (c) 2003-2007 Dovecot authors, see the included COPYING file */
7ef3553585e556f35d5919589cfdc1de3329e4bbTimo Sirainen
7ef3553585e556f35d5919589cfdc1de3329e4bbTimo Sirainen#include "lib.h"
0531ee2f23b4562eb6c212593680c820fa9e828bTimo Sirainen#include "istream-internal.h"
0531ee2f23b4562eb6c212593680c820fa9e828bTimo Sirainen
7ef3553585e556f35d5919589cfdc1de3329e4bbTimo Sirainenstruct limit_istream {
7ef3553585e556f35d5919589cfdc1de3329e4bbTimo Sirainen struct istream_private istream;
7ef3553585e556f35d5919589cfdc1de3329e4bbTimo Sirainen
7ef3553585e556f35d5919589cfdc1de3329e4bbTimo Sirainen struct istream *input;
7ef3553585e556f35d5919589cfdc1de3329e4bbTimo Sirainen uoff_t v_start_offset, v_size;
7ef3553585e556f35d5919589cfdc1de3329e4bbTimo Sirainen};
7ef3553585e556f35d5919589cfdc1de3329e4bbTimo Sirainen
7ef3553585e556f35d5919589cfdc1de3329e4bbTimo Sirainenstatic void i_stream_limit_destroy(struct iostream_private *stream)
7ef3553585e556f35d5919589cfdc1de3329e4bbTimo Sirainen{
struct limit_istream *lstream = (struct limit_istream *) stream;
/* get to same position in parent stream */
i_stream_seek(lstream->input, lstream->v_start_offset +
lstream->istream.istream.v_offset);
i_stream_unref(&lstream->input);
}
static void
i_stream_limit_set_max_buffer_size(struct iostream_private *stream,
size_t max_size)
{
struct limit_istream *lstream = (struct limit_istream *) stream;
i_stream_set_max_buffer_size(lstream->input, max_size);
}
static ssize_t i_stream_limit_read(struct istream_private *stream)
{
struct limit_istream *lstream = (struct limit_istream *) stream;
uoff_t left;
ssize_t ret;
size_t pos;
if (stream->istream.v_offset +
(stream->pos - stream->skip) >= lstream->v_size) {
stream->istream.eof = TRUE;
return -1;
}
if (lstream->input->v_offset !=
lstream->v_start_offset + stream->istream.v_offset) {
i_stream_seek(lstream->input,
lstream->v_start_offset +
stream->istream.v_offset);
}
stream->buffer = i_stream_get_data(lstream->input, &pos);
if (pos <= stream->pos) {
if ((ret = i_stream_read(lstream->input)) == -2)
return -2;
stream->istream.stream_errno = lstream->input->stream_errno;
stream->istream.eof = lstream->input->eof;
stream->buffer = i_stream_get_data(lstream->input, &pos);
} else {
ret = 0;
}
stream->pos -= stream->skip;
stream->skip = 0;
if (lstream->v_size != (uoff_t)-1) {
left = lstream->v_size - stream->istream.v_offset;
if (pos >= left) {
pos = left;
stream->istream.eof = TRUE;
}
}
ret = pos > stream->pos ? (ssize_t)(pos - stream->pos) :
(ret == 0 ? 0 : -1);
stream->pos = pos;
return ret;
}
static void i_stream_limit_seek(struct istream_private *stream, uoff_t v_offset,
bool mark ATTR_UNUSED)
{
struct limit_istream *lstream = (struct limit_istream *) stream;
i_assert(v_offset <= lstream->v_size);
stream->istream.stream_errno = 0;
stream->istream.v_offset = v_offset;
stream->skip = stream->pos = 0;
}
static const struct stat *
i_stream_limit_stat(struct istream_private *stream, bool exact)
{
struct limit_istream *lstream = (struct limit_istream *) stream;
const struct stat *st;
st = i_stream_stat(lstream->input, exact);
if (st == NULL)
return NULL;
stream->statbuf = *st;
if (lstream->v_size != (uoff_t)-1)
stream->statbuf.st_size = lstream->v_size;
return &stream->statbuf;
}
struct istream *i_stream_create_limit(struct istream *input,
uoff_t v_start_offset, uoff_t v_size)
{
struct limit_istream *lstream;
i_stream_ref(input);
lstream = i_new(struct limit_istream, 1);
lstream->input = input;
lstream->v_start_offset = v_start_offset;
lstream->v_size = v_size;
lstream->istream.istream.v_offset =
input->v_offset < v_start_offset ? 0 :
input->v_offset - v_start_offset > v_size ? v_size :
input->v_offset - v_start_offset;
lstream->istream.iostream.destroy = i_stream_limit_destroy;
lstream->istream.iostream.set_max_buffer_size =
i_stream_limit_set_max_buffer_size;
lstream->istream.read = i_stream_limit_read;
lstream->istream.seek = i_stream_limit_seek;
lstream->istream.stat = i_stream_limit_stat;
lstream->istream.istream.blocking = input->blocking;
lstream->istream.istream.seekable = input->seekable;
return i_stream_create(&lstream->istream, i_stream_get_fd(input),
input->real_stream->abs_start_offset +
v_start_offset);
}