istream-file.c revision 331b4805d76c0b3a5a38a560276f3cf110f55ba0
e59faf65ce864fe95dc00f5d52b8323cdbd0608aTimo Sirainen/* Copyright (c) 2002-2010 Dovecot authors, see the included COPYING file */
5254d77805cd35b9356d072ba325c356c43b0d51Timo Sirainen/* @UNSAFE: whole file */
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainenstatic void i_stream_file_close(struct iostream_private *stream)
252db51b6c0a605163326b3ea5d09e9936ca3b29Timo Sirainen struct file_istream *fstream = (struct file_istream *)stream;
252db51b6c0a605163326b3ea5d09e9936ca3b29Timo Sirainen struct istream_private *_stream = (struct istream_private *)stream;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen if (fstream->autoclose_fd && _stream->fd != -1) {
09801f106cd531a28b4e03ec665e44c421264560Timo Sirainenstatic int i_stream_file_open(struct istream_private *stream)
09801f106cd531a28b4e03ec665e44c421264560Timo Sirainen const char *path = i_stream_get_name(&stream->istream);
09801f106cd531a28b4e03ec665e44c421264560Timo Sirainen i_error("file_istream.open(%s) failed: %m", path);
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainenstatic ssize_t i_stream_file_read(struct istream_private *stream)
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen struct file_istream *fstream = (struct file_istream *) stream;
14175321ddb88619015866978c05a27786ca4814Timo Sirainen if (!i_stream_get_buffer_space(stream, 1, &size))
1060afdc2fcdf647dbb3bc11647401f1b44a3a8aTimo Sirainen ret = pread(stream->fd, stream->w_buffer + stream->pos,
8edc373587d75f8040e3c4416e50638aa2a32188Timo Sirainen /* don't try to read() again. EOF from keyboard (^D)
8edc373587d75f8040e3c4416e50638aa2a32188Timo Sirainen requires this to work right. */
c06f4017027263cf3a08becc551f5126409e2a83Timo Sirainen ret = read(stream->fd, stream->w_buffer + stream->pos,
c9bf63e9094761767a63ac6b189bcf60bcffdc44Timo Sirainen } while (unlikely(ret < 0 && errno == EINTR &&
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainenstatic void i_stream_file_seek(struct istream_private *stream, uoff_t v_offset,
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen struct file_istream *fstream = (struct file_istream *) stream;
4ae354df6e08998137b527f495bfaaf3daf9eddcTimo Sirainen i_panic("stream doesn't support seeking backwards");
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen fstream->skip_left += v_offset - stream->istream.v_offset;
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainenstatic void i_stream_file_sync(struct istream_private *stream)
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen /* can't do anything or data would be lost */
7d207b1e77a7b5e3fda640e353acfc86d261fedfTimo Sirainenstatic const struct stat *
a94936bafd127680184da114c6a177b37ff656e5Timo Siraineni_stream_file_stat(struct istream_private *stream, bool exact ATTR_UNUSED)
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen struct file_istream *fstream = (struct file_istream *) stream;
09801f106cd531a28b4e03ec665e44c421264560Timo Sirainen const char *name = i_stream_get_name(&stream->istream);
09801f106cd531a28b4e03ec665e44c421264560Timo Sirainen /* return defaults */
09801f106cd531a28b4e03ec665e44c421264560Timo Sirainen if (fstat(stream->fd, &stream->statbuf) < 0) {
09801f106cd531a28b4e03ec665e44c421264560Timo Sirainen i_error("file_istream.fstat(%s) failed: %m", name);
09801f106cd531a28b4e03ec665e44c421264560Timo Sirainen i_error("file_istream.fstat(%s) failed: %m", name);
09801f106cd531a28b4e03ec665e44c421264560Timo Sirainenstatic struct istream *
09801f106cd531a28b4e03ec665e44c421264560Timo Siraineni_stream_create_file_common(int fd, size_t max_buffer_size, bool autoclose_fd)
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen fstream->istream.iostream.close = i_stream_file_close;
7ef5ca6fb59a318c821a852ae48a2edbb671d7ddTimo Sirainen fstream->istream.max_buffer_size = max_buffer_size;
c06f4017027263cf3a08becc551f5126409e2a83Timo Sirainen /* if it's a file, set the flags properly */
331b4805d76c0b3a5a38a560276f3cf110f55ba0Timo Sirainen /* we're trying to open a directory.
331b4805d76c0b3a5a38a560276f3cf110f55ba0Timo Sirainen we're not designed for it. */
331b4805d76c0b3a5a38a560276f3cf110f55ba0Timo Sirainen fstream->istream.istream.stream_errno = EISDIR;
9511a40d933181045343110c8101b75887062aaeTimo Sirainen return i_stream_create(&fstream->istream, NULL, fd);
09801f106cd531a28b4e03ec665e44c421264560Timo Sirainenstruct istream *i_stream_create_fd(int fd, size_t max_buffer_size,
09801f106cd531a28b4e03ec665e44c421264560Timo Sirainen return i_stream_create_file_common(fd, max_buffer_size, autoclose_fd);
09801f106cd531a28b4e03ec665e44c421264560Timo Sirainenstruct istream *i_stream_create_file(const char *path, size_t max_buffer_size)