istream.c revision 4334b9b032298defd4d3906f5357698ff016ead0
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen/* Copyright (c) 2002-2010 Dovecot authors, see the included COPYING file */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen io_stream_ref(&stream->real_stream->iostream);
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen struct istream_private *_stream = (*stream)->real_stream;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen io_stream_unref(&(*stream)->real_stream->iostream);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenvoid i_stream_set_destroy_callback(struct istream *stream,
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen struct iostream_private *iostream = &stream->real_stream->iostream;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen struct istream_private *_stream = stream->real_stream;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen io_stream_close(&stream->real_stream->iostream);
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainenvoid i_stream_set_init_buffer_size(struct istream *stream, size_t size)
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainenvoid i_stream_set_max_buffer_size(struct istream *stream, size_t max_size)
ee246b46953e4b94b2f22e093373674fa9155500Timo Sirainen io_stream_set_max_buffer_size(&stream->real_stream->iostream, max_size);
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainenvoid i_stream_set_return_partial_line(struct istream *stream, bool set)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic void i_stream_update(struct istream_private *stream)
20a802016205bbcafc90f164f769ea801f88d014Timo Sirainen stream->parent_expected_offset = stream->parent->v_offset;
8e7da21696c9f8a6d5e601243fb6172ec85d47b2Timo Sirainen struct istream_private *_stream = stream->real_stream;
024815ea2ffdda9ea79919f18e865663977f73eaTimo Sirainen i_stream_seek(_stream->parent, _stream->parent_expected_offset);
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen /* error handling should be easier if we now just
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen assume the stream is now at EOF */
de12ff295bb3d0873b4dced5840612cbacd635efTimo Sirainen i_assert((size_t)ret+old_size == _stream->pos - _stream->skip);
de12ff295bb3d0873b4dced5840612cbacd635efTimo Sirainenssize_t i_stream_read_copy_from_parent(struct istream *istream)
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen struct istream_private *stream = istream->real_stream;
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen stream->buffer = i_stream_get_data(stream->parent, &pos);
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen if ((ret = i_stream_read(stream->parent)) == -2)
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen stream->istream.stream_errno = stream->parent->stream_errno;
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen stream->buffer = i_stream_get_data(stream->parent, &pos);
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen /* check again, in case the parent stream had been seeked
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen backwards and the previous read() didn't get us far
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen ret = pos > stream->pos ? (ssize_t)(pos - stream->pos) :
f23298fea47eecbeded985ee2537a34c4c4ef56bTimo Sirainenvoid i_stream_skip(struct istream *stream, uoff_t count)
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen struct istream_private *_stream = stream->real_stream;
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen /* within buffer */
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen /* have to seek forward */
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen _stream->seek(_stream, stream->v_offset + count, FALSE);
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainenstatic bool i_stream_can_optimize_seek(struct istream_private *stream)
367c05967091a2cbfce59b7f274f55b1a0f9e8c9Timo Sirainen /* use the fast route only if the parent stream hasn't been changed */
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen return i_stream_can_optimize_seek(stream->parent->real_stream);
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainenvoid i_stream_seek(struct istream *stream, uoff_t v_offset)
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen struct istream_private *_stream = stream->real_stream;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen i_stream_skip(stream, v_offset - stream->v_offset);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainenvoid i_stream_seek_mark(struct istream *stream, uoff_t v_offset)
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen struct istream_private *_stream = stream->real_stream;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen struct istream_private *_stream = stream->real_stream;
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainenconst struct stat *i_stream_stat(struct istream *stream, bool exact)
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen struct istream_private *_stream = stream->real_stream;
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainenint i_stream_get_size(struct istream *stream, bool exact, uoff_t *size_r)
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen struct istream_private *_stream = stream->real_stream;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen return _stream->get_size(_stream, exact, size_r);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainenbool i_stream_have_bytes_left(const struct istream *stream)
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen const struct istream_private *_stream = stream->real_stream;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen return !stream->eof || _stream->skip != _stream->pos;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen const struct istream_private *_stream = stream->real_stream;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainenstatic char *i_stream_next_line_finish(struct istream_private *stream, size_t i)
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen /* modify the buffer directly */
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen ret = (char *)stream->w_buffer + stream->skip;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen /* use a temporary string to return it */
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen stream->line_str = str_new(default_pool, 256);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen str_append_n(stream->line_str, stream->buffer + stream->skip,
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainenstatic char *i_stream_last_line(struct istream_private *_stream)
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen if (_stream->istream.eof && _stream->skip != _stream->pos &&
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen /* the last line is missing LF and we want to return it. */
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen return i_stream_next_line_finish(_stream, _stream->pos);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainenchar *i_stream_next_line(struct istream *stream)
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen struct istream_private *_stream = stream->real_stream;
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen i_error("i_stream_next_line() called for unmodifiable stream");
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainen /* @UNSAFE */
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainen for (i = _stream->skip; i < _stream->pos; i++) {
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainen ret_buf = i_stream_next_line_finish(_stream, i);
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainenchar *i_stream_read_next_line(struct istream *stream)
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen return i_stream_last_line(stream->real_stream);
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainenconst unsigned char *
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Siraineni_stream_get_data(const struct istream *stream, size_t *size_r)
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen const struct istream_private *_stream = stream->real_stream;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenunsigned char *i_stream_get_modifiable_data(const struct istream *stream,
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen const struct istream_private *_stream = stream->real_stream;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (_stream->skip >= _stream->pos || _stream->w_buffer == NULL) {
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainenint i_stream_read_data(struct istream *stream, const unsigned char **data_r,
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen /* we need more data */
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen } while (ret > 0);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen /* need to read more */
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen /* we read at least some new data */
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenvoid i_stream_compress(struct istream_private *stream)
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen memmove(stream->w_buffer, stream->w_buffer + stream->skip,
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainenvoid i_stream_grow_buffer(struct istream_private *stream, size_t bytes)
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen if (stream->buffer_size <= stream->init_buffer_size)
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen stream->buffer_size = stream->init_buffer_size;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen stream->buffer_size = nearest_power(stream->buffer_size);
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen stream->buffer_size > stream->max_buffer_size)
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen stream->buffer_size = stream->max_buffer_size;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen stream->w_buffer = i_realloc(stream->w_buffer, old_size,
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenbool i_stream_get_buffer_space(struct istream_private *stream,
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (wanted_size > stream->buffer_size - stream->pos) {
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen /* remove the unused bytes from beginning of buffer */
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen stream->buffer_size < stream->max_buffer_size) {
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen /* buffer is full - grow it */
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen i_stream_grow_buffer(stream, I_STREAM_MIN_SIZE);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenbool i_stream_add_data(struct istream *_stream, const unsigned char *data,
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen struct istream_private *stream = _stream->real_stream;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen (void)i_stream_get_buffer_space(stream, size, &size2);
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainen memcpy(stream->w_buffer + stream->pos, data, size);
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Siraineni_stream_default_set_max_buffer_size(struct iostream_private *stream,
811f2e26d9782d9cb99fdf82e18ffa0a77564fe2Timo Sirainen struct istream_private *_stream = (struct istream_private *)stream;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen i_stream_set_max_buffer_size(_stream->parent, max_size);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic void i_stream_default_destroy(struct iostream_private *stream)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen struct istream_private *_stream = (struct istream_private *)stream;
b2105c78f0fd58281317e6d777ded860f33153a3Timo Siraineni_stream_default_seek(struct istream_private *stream,
92888ef30960c30ccc9e030fe7eab5d4d04a7d1cTimo Sirainen i_panic("stream doesn't support seeking backwards");
8e7da21696c9f8a6d5e601243fb6172ec85d47b2Timo Sirainen if (available <= v_offset - stream->istream.v_offset)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic const struct stat *
d482b35af87f5fd872bad007da0475813a401a49Timo Siraineni_stream_default_stat(struct istream_private *stream, bool exact ATTR_UNUSED)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Siraineni_stream_default_get_size(struct istream_private *stream,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Siraineni_stream_create(struct istream_private *_stream, struct istream *parent, int fd)
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen _stream->access_counter = parent->real_stream->access_counter;
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen _stream->parent_start_offset = parent->v_offset;
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen _stream->parent_expected_offset = parent->v_offset;
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen _stream->abs_start_offset = parent->v_offset +
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen _stream->iostream.destroy = i_stream_default_destroy;
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen _stream->get_size = i_stream_default_get_size;
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen if (_stream->iostream.set_max_buffer_size == NULL) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen _stream->init_buffer_size = I_STREAM_MIN_SIZE;
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen memset(&_stream->statbuf, 0, sizeof(_stream->statbuf));