bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2002-2018 Dovecot authors, see the included COPYING file */
5254d77805cd35b9356d072ba325c356c43b0d51Timo Sirainen/* @UNSAFE: whole file */
bf132be3fe1c9e8de84f10d0b05c0b46ca542ac4Timo Sirainenvoid 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);
bf132be3fe1c9e8de84f10d0b05c0b46ca542ac4Timo Sirainenssize_t i_stream_file_read(struct istream_private *stream)
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen struct file_istream *fstream = (struct file_istream *) stream;
5024c4799b324ea15270152b775c67ccfc72d5bcTimo Sirainen offset = stream->istream.v_offset + (stream->pos - stream->skip);
03915cfec4eb1b5a65e5b6b676c8f4151bc80351Timo Sirainen ret = pread(stream->fd, stream->w_buffer + stream->pos,
03915cfec4eb1b5a65e5b6b676c8f4151bc80351Timo Sirainen /* don't try to read() again. EOF from keyboard (^D)
03915cfec4eb1b5a65e5b6b676c8f4151bc80351Timo Sirainen requires this to work right. */
03915cfec4eb1b5a65e5b6b676c8f4151bc80351Timo Sirainen ret = read(stream->fd, stream->w_buffer + stream->pos,
9f131c8b6d88ffc65d94eae63e0b3c11d7c24cb9Timo Sirainen /* if we get EBADF for a valid fd, it means something's
9f131c8b6d88ffc65d94eae63e0b3c11d7c24cb9Timo Sirainen really wrong and we'd better just crash. */
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 */
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) {
e17d72cec1c75554483d692edd687b411526f312Timo Sirainen i_error("%s", i_stream_get_error(&stream->istream));
e17d72cec1c75554483d692edd687b411526f312Timo Sirainen i_error("%s", i_stream_get_error(&stream->istream));
bf132be3fe1c9e8de84f10d0b05c0b46ca542ac4Timo Siraineni_stream_create_file_common(struct file_istream *fstream,
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 */
f363414461e991a478d015861344974714620f3bTimo Sirainen /* only the path is known for now - the fd is opened later */
331b4805d76c0b3a5a38a560276f3cf110f55ba0Timo Sirainen /* we're trying to open a directory.
331b4805d76c0b3a5a38a560276f3cf110f55ba0Timo Sirainen we're not designed for it. */
1c6f6f5bef70f16546b3bc8f4cd5f93f373e82a2Timo Sirainen io_stream_set_error(&fstream->istream.iostream,
1c6f6f5bef70f16546b3bc8f4cd5f93f373e82a2Timo Sirainen "%s is a directory, can't read it as file",
1c6f6f5bef70f16546b3bc8f4cd5f93f373e82a2Timo Sirainen path != NULL ? path : t_strdup_printf("<fd %d>", fd));
331b4805d76c0b3a5a38a560276f3cf110f55ba0Timo Sirainen fstream->istream.istream.stream_errno = EISDIR;
f363414461e991a478d015861344974714620f3bTimo Sirainen } else if ((flags = fcntl(fd, F_GETFL, 0)) < 0) {
f363414461e991a478d015861344974714620f3bTimo Sirainen /* shouldn't happen */
f363414461e991a478d015861344974714620f3bTimo Sirainen fstream->istream.istream.stream_errno = errno;
f363414461e991a478d015861344974714620f3bTimo Sirainen io_stream_set_error(&fstream->istream.iostream,
f363414461e991a478d015861344974714620f3bTimo Sirainen /* blocking socket/fifo */
2974dca6be5120e49279f06c8aa952e5fac56048Timo Sirainen input = i_stream_create(&fstream->istream, NULL, fd, 0);
9a755930537f13e9746c4fc8c1bc42a83e52275eTimo Sirainen i_stream_set_name(input, is_file ? "(file)" : "(fd)");
e93184a9055c2530366dfe617e07199603c399ddMartti Rannanjärvistruct istream *i_stream_create_fd(int fd, size_t max_buffer_size)
bf132be3fe1c9e8de84f10d0b05c0b46ca542ac4Timo Sirainen return i_stream_create_file_common(fstream, fd, NULL,
43834f87bf431198f986e86052a4f6e558fdb07dTimo Sirainenstruct istream *i_stream_create_fd_autoclose(int *fd, size_t max_buffer_size)
e93184a9055c2530366dfe617e07199603c399ddMartti Rannanjärvi input = i_stream_create_file_common(fstream, *fd, NULL,
09801f106cd531a28b4e03ec665e44c421264560Timo Sirainenstruct istream *i_stream_create_file(const char *path, size_t max_buffer_size)