istream-file.c revision 09801f106cd531a28b4e03ec665e44c421264560
5f5870385cff47efd2f58e7892f251cf13761528Timo Sirainen/* Copyright (c) 2002-2010 Dovecot authors, see the included COPYING file */
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen/* @UNSAFE: whole file */
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainenstatic void i_stream_file_close(struct iostream_private *stream)
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen struct file_istream *fstream = (struct file_istream *)stream;
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen struct istream_private *_stream = (struct istream_private *)stream;
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen if (fstream->autoclose_fd && _stream->fd != -1) {
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainenstatic int i_stream_file_open(struct istream_private *stream)
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen const char *path = i_stream_get_name(&stream->istream);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen i_error("file_istream.open(%s) failed: %m", path);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainenstatic ssize_t i_stream_file_read(struct istream_private *stream)
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen struct file_istream *fstream = (struct file_istream *) stream;
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen if (!i_stream_get_buffer_space(stream, 1, &size))
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen ret = pread(stream->fd, stream->w_buffer + stream->pos,
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen /* don't try to read() again. EOF from keyboard (^D)
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen requires this to work right. */
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen ret = read(stream->fd, stream->w_buffer + stream->pos,
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen } while (unlikely(ret < 0 && errno == EINTR &&
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainenstatic void i_stream_file_seek(struct istream_private *stream, uoff_t v_offset,
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen struct file_istream *fstream = (struct file_istream *) stream;
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen i_panic("stream doesn't support seeking backwards");
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen fstream->skip_left += v_offset - stream->istream.v_offset;
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainenstatic void i_stream_file_sync(struct istream_private *stream)
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen /* can't do anything or data would be lost */
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainenstatic const struct stat *
01435c38e7d671d5a892c4b802cfb204881cd454Timo Siraineni_stream_file_stat(struct istream_private *stream, bool exact ATTR_UNUSED)
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen struct file_istream *fstream = (struct file_istream *) stream;
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen const char *name = i_stream_get_name(&stream->istream);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen /* return defaults */
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen if (fstat(stream->fd, &stream->statbuf) < 0) {
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen i_error("file_istream.fstat(%s) failed: %m", name);
9a1f68e5ab08eabd352d533315cba1c69006e2c1Timo Sirainen i_error("file_istream.fstat(%s) failed: %m", name);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainenstatic struct istream *
01435c38e7d671d5a892c4b802cfb204881cd454Timo Siraineni_stream_create_file_common(int fd, size_t max_buffer_size, bool autoclose_fd)
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen fstream->istream.iostream.close = i_stream_file_close;
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen fstream->istream.max_buffer_size = max_buffer_size;
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen /* if it's a file, set the flags properly */
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen if (fd == -1 || (fstat(fd, &st) == 0 && S_ISREG(st.st_mode))) {
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen return i_stream_create(&fstream->istream, NULL, fd);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainenstruct istream *i_stream_create_fd(int fd, size_t max_buffer_size,
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen return i_stream_create_file_common(fd, max_buffer_size, autoclose_fd);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainenstruct istream *i_stream_create_file(const char *path, size_t max_buffer_size)