istream-limit.c revision e50c7afe297ab10e07a8acc816c76ce9d45ef409
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen/* Copyright (C) 2003 Timo Sirainen */
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen#include "lib.h"
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen#include "istream-internal.h"
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainenstruct limit_istream {
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen struct _istream istream;
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen struct istream *input;
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen uoff_t v_start_offset, v_size;
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen};
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainenstatic void _close(struct _iostream *stream __attr_unused__)
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen{
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen}
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainenstatic void _destroy(struct _iostream *stream)
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen{
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen struct limit_istream *lstream = (struct limit_istream *) stream;
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen /* get to same position in parent stream */
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen i_stream_seek(lstream->input, lstream->v_start_offset +
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen lstream->istream.istream.v_offset);
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen i_stream_unref(lstream->input);
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen}
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainenstatic void _set_max_buffer_size(struct _iostream *stream, size_t max_size)
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen{
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen struct limit_istream *lstream = (struct limit_istream *) stream;
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen i_stream_set_max_buffer_size(lstream->input, max_size);
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen}
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainenstatic ssize_t _read(struct _istream *stream)
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen{
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen struct limit_istream *lstream = (struct limit_istream *) stream;
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen uoff_t left;
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen ssize_t ret;
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen size_t pos;
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen if (stream->istream.v_offset +
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen (stream->pos - stream->skip) >= lstream->v_size) {
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen stream->istream.eof = TRUE;
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen return -1;
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen }
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen if (lstream->input->v_offset !=
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen lstream->v_start_offset + stream->istream.v_offset) {
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen i_stream_seek(lstream->input,
fb79b36eb34532dbe67caf99eefe3660b8c841e0Timo Sirainen lstream->v_start_offset +
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen stream->istream.v_offset);
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen }
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen stream->buffer = i_stream_get_data(lstream->input, &pos);
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen if (pos <= stream->pos) {
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen if ((ret = i_stream_read(lstream->input)) == -2) {
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen if (stream->skip == 0)
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen return -2;
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen }
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen stream->istream.eof = lstream->input->eof;
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen stream->buffer = i_stream_get_data(lstream->input, &pos);
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen } else {
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen ret = 0;
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen }
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen stream->pos -= stream->skip;
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen stream->skip = 0;
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen if (lstream->v_size != (uoff_t)-1) {
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen left = lstream->v_size - stream->istream.v_offset;
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen if (pos >= left) {
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen pos = left;
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen stream->istream.eof = TRUE;
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen }
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen }
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen ret = pos > stream->pos ? (ssize_t)(pos - stream->pos) :
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen (ret == 0 ? 0 : -1);
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen stream->pos = pos;
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen return ret;
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen}
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainenstatic void _seek(struct _istream *stream, uoff_t v_offset)
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen{
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen struct limit_istream *lstream = (struct limit_istream *) stream;
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen i_assert(v_offset <= lstream->v_size);
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen stream->istream.stream_errno = 0;
fb79b36eb34532dbe67caf99eefe3660b8c841e0Timo Sirainen stream->istream.v_offset = v_offset;
fb79b36eb34532dbe67caf99eefe3660b8c841e0Timo Sirainen stream->skip = stream->pos = 0;
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen}
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainenstatic const struct stat *_stat(struct _istream *stream)
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen{
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen struct limit_istream *lstream = (struct limit_istream *) stream;
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen const struct stat *st;
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen st = i_stream_stat(lstream->input);
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen if (st == NULL)
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen return NULL;
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen stream->statbuf = *st;
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen if (lstream->v_size != (uoff_t)-1)
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen stream->statbuf.st_size = lstream->v_size;
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen return &stream->statbuf;
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen}
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainenstruct istream *i_stream_create_limit(pool_t pool, struct istream *input,
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen uoff_t v_start_offset, uoff_t v_size)
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen{
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen struct limit_istream *lstream;
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen i_stream_ref(input);
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen lstream = p_new(pool, struct limit_istream, 1);
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen lstream->input = input;
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen lstream->v_start_offset = v_start_offset;
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen lstream->v_size = v_size;
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen lstream->istream.istream.v_offset =
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen input->v_offset < v_start_offset ? 0 :
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen input->v_offset - v_start_offset > v_size ? v_size :
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen input->v_offset - v_start_offset;
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen lstream->istream.iostream.close = _close;
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen lstream->istream.iostream.destroy = _destroy;
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen lstream->istream.iostream.set_max_buffer_size = _set_max_buffer_size;
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen lstream->istream.read = _read;
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen lstream->istream.seek = _seek;
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen lstream->istream.stat = _stat;
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen lstream->istream.istream.seekable = input->seekable;
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen return _i_stream_create(&lstream->istream, pool, i_stream_get_fd(input),
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen input->real_stream->abs_start_offset +
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen v_start_offset);
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen}
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen