istream-file.c revision 331b4805d76c0b3a5a38a560276f3cf110f55ba0
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen/* Copyright (c) 2002-2010 Dovecot authors, see the included COPYING file */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen/* @UNSAFE: whole file */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic void i_stream_file_close(struct iostream_private *stream)
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen struct file_istream *fstream = (struct file_istream *)stream;
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen struct istream_private *_stream = (struct istream_private *)stream;
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen if (fstream->autoclose_fd && _stream->fd != -1) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic int i_stream_file_open(struct istream_private *stream)
7c5b51bdf43a98e12c654ad437e0b258c5fffbc1Timo Sirainen const char *path = i_stream_get_name(&stream->istream);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen i_error("file_istream.open(%s) failed: %m", path);
ea546eaab672d441e180b7619d4750be813c08d8Timo Sirainenstatic ssize_t i_stream_file_read(struct istream_private *stream)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen struct file_istream *fstream = (struct file_istream *) stream;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (!i_stream_get_buffer_space(stream, 1, &size))
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen ret = pread(stream->fd, stream->w_buffer + stream->pos,
0add8c99ca65e56dbf613595fc37c41aafff3f7fTimo Sirainen /* don't try to read() again. EOF from keyboard (^D)
0add8c99ca65e56dbf613595fc37c41aafff3f7fTimo Sirainen requires this to work right. */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ret = read(stream->fd, stream->w_buffer + stream->pos,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen } while (unlikely(ret < 0 && errno == EINTR &&
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic void i_stream_file_seek(struct istream_private *stream, uoff_t v_offset,
7c5b51bdf43a98e12c654ad437e0b258c5fffbc1Timo Sirainen struct file_istream *fstream = (struct file_istream *) stream;
7c5b51bdf43a98e12c654ad437e0b258c5fffbc1Timo Sirainen i_panic("stream doesn't support seeking backwards");
7c5b51bdf43a98e12c654ad437e0b258c5fffbc1Timo Sirainen fstream->skip_left += v_offset - stream->istream.v_offset;
4b058f90f9e8a2c6b2eed275de4eb8cc5195a71dTimo Sirainenstatic void i_stream_file_sync(struct istream_private *stream)
7c5b51bdf43a98e12c654ad437e0b258c5fffbc1Timo Sirainen /* can't do anything or data would be lost */
7c5b51bdf43a98e12c654ad437e0b258c5fffbc1Timo Sirainenstatic const struct stat *
7c5b51bdf43a98e12c654ad437e0b258c5fffbc1Timo Siraineni_stream_file_stat(struct istream_private *stream, bool exact ATTR_UNUSED)
7c5b51bdf43a98e12c654ad437e0b258c5fffbc1Timo Sirainen struct file_istream *fstream = (struct file_istream *) stream;
7c5b51bdf43a98e12c654ad437e0b258c5fffbc1Timo Sirainen const char *name = i_stream_get_name(&stream->istream);
7c5b51bdf43a98e12c654ad437e0b258c5fffbc1Timo Sirainen /* return defaults */
7c5b51bdf43a98e12c654ad437e0b258c5fffbc1Timo Sirainen if (fstat(stream->fd, &stream->statbuf) < 0) {
7c5b51bdf43a98e12c654ad437e0b258c5fffbc1Timo Sirainen i_error("file_istream.fstat(%s) failed: %m", name);
24fc71a693331ffe77e2b6d81c70aca6fa055e47Timo Sirainen i_error("file_istream.fstat(%s) failed: %m", name);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic struct istream *
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Siraineni_stream_create_file_common(int fd, size_t max_buffer_size, bool autoclose_fd)
df4018ae2f0a95be602f724ca70df7e0e3bd6a7dTimo Sirainen fstream->istream.iostream.close = i_stream_file_close;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen fstream->istream.max_buffer_size = max_buffer_size;
024815ea2ffdda9ea79919f18e865663977f73eaTimo Sirainen /* if it's a file, set the flags properly */
024815ea2ffdda9ea79919f18e865663977f73eaTimo Sirainen /* we're trying to open a directory.
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen we're not designed for it. */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen fstream->istream.istream.stream_errno = EISDIR;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return i_stream_create(&fstream->istream, NULL, fd);
024815ea2ffdda9ea79919f18e865663977f73eaTimo Sirainenstruct istream *i_stream_create_fd(int fd, size_t max_buffer_size,
7c5b51bdf43a98e12c654ad437e0b258c5fffbc1Timo Sirainen return i_stream_create_file_common(fd, max_buffer_size, autoclose_fd);
b5ea11802f2bafbec06282a7b3b6704dc5fae584Timo Sirainenstruct istream *i_stream_create_file(const char *path, size_t max_buffer_size)