istream-private.h revision e0fab14602b73ff590b2a9c5d9e67e2dfb5d1f9e
02c335c23bf5fa225a467c19f2c063fb0dc7b8c3Timo Sirainen#ifndef ISTREAM_PRIVATE_H
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#define ISTREAM_PRIVATE_H
c977374bd4651cafc1626ebe308aa66dfd8b30e0Timo Sirainen
c977374bd4651cafc1626ebe308aa66dfd8b30e0Timo Sirainen#include "istream.h"
c977374bd4651cafc1626ebe308aa66dfd8b30e0Timo Sirainen#include "iostream-private.h"
c977374bd4651cafc1626ebe308aa66dfd8b30e0Timo Sirainen
65b94e73c305dcb209cf958f938b93ec061c67a9Timo Sirainen#define I_STREAM_MIN_SIZE IO_BLOCK_SIZE
c977374bd4651cafc1626ebe308aa66dfd8b30e0Timo Sirainen
65b94e73c305dcb209cf958f938b93ec061c67a9Timo Sirainenstruct io;
c977374bd4651cafc1626ebe308aa66dfd8b30e0Timo Sirainen
65b94e73c305dcb209cf958f938b93ec061c67a9Timo Sirainenstruct istream_private {
c977374bd4651cafc1626ebe308aa66dfd8b30e0Timo Sirainen/* inheritance: */
c977374bd4651cafc1626ebe308aa66dfd8b30e0Timo Sirainen struct iostream_private iostream;
c977374bd4651cafc1626ebe308aa66dfd8b30e0Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen/* methods: */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen ssize_t (*read)(struct istream_private *stream);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen void (*seek)(struct istream_private *stream,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen uoff_t v_offset, bool mark);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen void (*sync)(struct istream_private *stream);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen int (*stat)(struct istream_private *stream, bool exact);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen int (*get_size)(struct istream_private *stream, bool exact, uoff_t *size_r);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen void (*switch_ioloop)(struct istream_private *stream);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct istream_snapshot *
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen (*snapshot)(struct istream_private *stream,
9955f6cba7652469b1d600a3674e8d27dd4e61bdTimo Sirainen struct istream_snapshot *prev_snapshot);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen/* data: */
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen struct istream istream;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen int fd;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen uoff_t start_offset;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct stat statbuf;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* added by io_add_istream() -> i_stream_set_io() */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct io *io;
03010dbaa74ec70f062994dfe3cd39bedc99a28bTimo Sirainen
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen const unsigned char *buffer;
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen unsigned char *w_buffer; /* may be NULL */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen size_t buffer_size, max_buffer_size, init_buffer_size;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen size_t skip, pos, try_alloc_limit;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
d22301419109ed4a38351715e6760011421dadecTimo Sirainen struct istream *parent; /* for filter streams */
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen uoff_t parent_start_offset;
5b62dea2f88165f3f4d87bba9011343f3ff415ffTimo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* parent stream's expected offset is kept here. i_stream_read()
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen always seeks parent stream to here before calling read(). */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen uoff_t parent_expected_offset;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen struct memarea *memarea;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct istream_snapshot *prev_snapshot;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* increased every time the stream is changed (e.g. seek, read).
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen this way streams can check if their parent streams have been
461ffead9720d1e516b959d5e41f049c73d38c7cTimo Sirainen accessed behind them. */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen unsigned int access_counter;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen string_t *line_str; /* for i_stream_next_line() if w_buffer == NULL */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen bool line_crlf:1;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen bool return_nolf_line:1;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen bool stream_size_passthrough:1; /* stream is parent's size */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen bool nonpersistent_buffers:1;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen};
c18ff860dc22960fd37c272d929f889c7939a2c8Timo Sirainen
73a87c2ff65c6116cde6fb158dfddb8ef7346901Timo Sirainenstruct istream_snapshot {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct istream_snapshot *prev_snapshot;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct memarea *old_memarea;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen};
73a87c2ff65c6116cde6fb158dfddb8ef7346901Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenenum istream_create_flag {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* The stream guarantees that the buffer pointer stays valid when it
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen returns <= 0. */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen ISTREAM_CREATE_FLAG_NOOP_SNAPSHOT = 0x01,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen};
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstruct istream * ATTR_NOWARN_UNUSED_RESULT
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Siraineni_stream_create(struct istream_private *stream, struct istream *parent, int fd,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen enum istream_create_flag flags) ATTR_NULL(2);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen/* Initialize parent lazily after i_stream_create() has already been called. */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenvoid i_stream_init_parent(struct istream_private *_stream,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct istream *parent);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenvoid i_stream_compress(struct istream_private *stream);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenvoid i_stream_grow_buffer(struct istream_private *stream, size_t bytes);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenbool ATTR_NOWARN_UNUSED_RESULT
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Siraineni_stream_try_alloc(struct istream_private *stream,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen size_t wanted_size, size_t *size_r);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen/* Like i_stream_try_alloc(), but compress only if it's the only way to get
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen more space. This can be useful when stream is marked with
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen i_stream_seek_mark() */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenbool ATTR_NOWARN_UNUSED_RESULT
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Siraineni_stream_try_alloc_avoid_compress(struct istream_private *stream,
637455ebee0453f860c9bce0626c485e35fb83deTimo Sirainen size_t wanted_size, size_t *size_r);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenvoid *i_stream_alloc(struct istream_private *stream, size_t size);
637455ebee0453f860c9bce0626c485e35fb83deTimo Sirainen/* Free memory allocated by i_stream_*alloc() */
637455ebee0453f860c9bce0626c485e35fb83deTimo Sirainenvoid i_stream_free_buffer(struct istream_private *stream);
d22301419109ed4a38351715e6760011421dadecTimo Sirainenssize_t i_stream_read_copy_from_parent(struct istream *istream);
637455ebee0453f860c9bce0626c485e35fb83deTimo Sirainenvoid i_stream_default_seek_nonseekable(struct istream_private *stream,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen uoff_t v_offset, bool mark);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen/* Default snapshot handling: use memarea if it exists, otherwise snapshot
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen parent stream. */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstruct istream_snapshot *
637455ebee0453f860c9bce0626c485e35fb83deTimo Siraineni_stream_default_snapshot(struct istream_private *stream,
637455ebee0453f860c9bce0626c485e35fb83deTimo Sirainen struct istream_snapshot *prev_snapshot);
637455ebee0453f860c9bce0626c485e35fb83deTimo Sirainenvoid i_stream_snapshot_free(struct istream_snapshot **snapshot);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenvoid i_stream_set_io(struct istream *stream, struct io *io);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenvoid i_stream_unset_io(struct istream *stream, struct io *io);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
03010dbaa74ec70f062994dfe3cd39bedc99a28bTimo Sirainen/* Filter istreams should be calling this instead of i_stream_read() to avoid
637455ebee0453f860c9bce0626c485e35fb83deTimo Sirainen unnecessarily referencing memareas. After this call any pointers to the
637455ebee0453f860c9bce0626c485e35fb83deTimo Sirainen parent istream's content must be considered as potentially invalid and have
637455ebee0453f860c9bce0626c485e35fb83deTimo Sirainen to be updated, even if the return value is <=0. */
637455ebee0453f860c9bce0626c485e35fb83deTimo Sirainenssize_t i_stream_read_memarea(struct istream *stream);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenint i_stream_read_more_memarea(struct istream *stream,
637455ebee0453f860c9bce0626c485e35fb83deTimo Sirainen const unsigned char **data_r, size_t *size_r);
637455ebee0453f860c9bce0626c485e35fb83deTimo Sirainen
6ae329de09afb7214c906d762320847e05469d53Timo Sirainen#endif
637455ebee0453f860c9bce0626c485e35fb83deTimo Sirainen