istream-file.c revision 1060afdc2fcdf647dbb3bc11647401f1b44a3a8a
02c335c23bf5fa225a467c19f2c063fb0dc7b8c3Timo Sirainen/* Copyright (c) 2002-2003 Timo Sirainen */
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen/* @UNSAFE: whole file */
d6af1e63bc7824f1cc5b9b73a1c5f8f8789788d6Timo Sirainen struct file_istream *fstream = (struct file_istream *) stream;
d6af1e63bc7824f1cc5b9b73a1c5f8f8789788d6Timo Sirainen struct _istream *_stream = (struct _istream *) stream;
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen if (fstream->autoclose_fd && _stream->fd != -1) {
0e3f8c6edad565112d91f0a53568c0313d657e48Timo Sirainen struct _istream *_stream = (struct _istream *) stream;
d56384d5226c8860079d0d0b08b83404e8c42986Timo Sirainen p_free(_stream->iostream.pool, _stream->w_buffer);
d56384d5226c8860079d0d0b08b83404e8c42986Timo Sirainenstatic void _set_max_buffer_size(struct _iostream *stream, size_t max_size)
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen struct file_istream *fstream = (struct file_istream *) stream;
d6af1e63bc7824f1cc5b9b73a1c5f8f8789788d6Timo Sirainenstatic void i_stream_grow_buffer(struct _istream *stream, size_t bytes)
d6af1e63bc7824f1cc5b9b73a1c5f8f8789788d6Timo Sirainen struct file_istream *fstream = (struct file_istream *) stream;
6a9f9a5101b665fd2ef80c9e048a5eace78e01efTimo Sirainen stream->buffer_size = nearest_power(stream->buffer_size);
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen stream->buffer_size > fstream->max_buffer_size)
4b058f90f9e8a2c6b2eed275de4eb8cc5195a71dTimo Sirainen stream->buffer_size = fstream->max_buffer_size;
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen p_realloc(stream->iostream.pool, stream->w_buffer,
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainenstatic void i_stream_compress(struct _istream *stream)
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen memmove(stream->w_buffer, stream->w_buffer + stream->skip,
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen struct file_istream *fstream = (struct file_istream *) stream;
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen /* remove the unused bytes from beginning of buffer */
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen stream->buffer_size < fstream->max_buffer_size) {
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen /* buffer is full - grow it */
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen i_stream_grow_buffer(stream, I_STREAM_MIN_SIZE);
b09be485e9373be4288f5615bbce6ebed65a425aTimo Sirainen ret = pread(stream->fd, stream->w_buffer + stream->pos,
b09be485e9373be4288f5615bbce6ebed65a425aTimo Sirainen ret = read(stream->fd, stream->w_buffer + stream->pos, size);
return ret;