ostream-file.c revision 1b3ba631030085c5f5afdae503909cee9c192f96
e59faf65ce864fe95dc00f5d52b8323cdbd0608aTimo Sirainen/* Copyright (c) 2002-2003 Timo Sirainen */
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen/* @UNSAFE: whole file */
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen#include "lib.h"
5d60e31c7b701b606067a20bc88dcc8a6de7bbd6Timo Sirainen#include "ioloop.h"
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen#include "write-full.h"
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen#include "network.h"
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen#include "sendfile-util.h"
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen#include "istream.h"
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen#include "istream-internal.h"
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen#include "ostream-internal.h"
d477acb83e14a776ece4ca94dcd1869e75d0c6eeTimo Sirainen
9dd1c256910f1fb42823116a641e7edb3ad11970Timo Sirainen#include <unistd.h>
9dd1c256910f1fb42823116a641e7edb3ad11970Timo Sirainen#include <sys/stat.h>
d477acb83e14a776ece4ca94dcd1869e75d0c6eeTimo Sirainen#ifdef HAVE_SYS_UIO_H
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen# include <sys/uio.h>
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen#endif
daa7e7459749ae8f82cd3eed9c44522d81c609a3Timo Sirainen
bbadd5331f534017cf62d5183003b3d9fdad079eTimo Sirainen#ifndef UIO_MAXIOV
373492be949e159fda651807b3acda2c5c077027Timo Sirainen# define UIO_MAXIOV 16
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen#endif
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen/* try to keep the buffer size within 4k..128k. ReiserFS may actually return
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen 128k as optimal size. */
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen#define DEFAULT_OPTIMAL_BLOCK_SIZE 4096
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen#define MAX_OPTIMAL_BLOCK_SIZE (128*1024)
373492be949e159fda651807b3acda2c5c077027Timo Sirainen
bbadd5331f534017cf62d5183003b3d9fdad079eTimo Sirainen#define IS_STREAM_EMPTY(fstream) \
bbadd5331f534017cf62d5183003b3d9fdad079eTimo Sirainen ((fstream)->head == (fstream)->tail && !(fstream)->full)
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen#define MAX_SSIZE_T(size) \
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen ((size) < SSIZE_T_MAX ? (size_t)(size) : SSIZE_T_MAX)
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainenstruct file_ostream {
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen struct _ostream ostream;
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen int fd;
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen struct io *io;
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen unsigned char *buffer; /* ring-buffer */
635df5b4cbcd7b24c825e01d9dd66d3a4274c4c7Timo Sirainen size_t buffer_size, max_buffer_size, optimal_block_size;
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen size_t head, tail; /* first unsent/unused byte */
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen unsigned int full:1; /* if head == tail, is buffer empty or full? */
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen unsigned int file:1;
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen unsigned int corked:1;
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen unsigned int no_socket_cork:1;
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen unsigned int no_sendfile:1;
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen unsigned int autoclose_fd:1;
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen};
99430beb12dfbc6c9c160f08e2102aeab38a589cTimo Sirainen
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainenstatic void stream_send_io(void *context);
93a7d1ee4b518b5c85f9721dc6539e4dab6aae00Timo Sirainen
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainenstatic void stream_closed(struct file_ostream *fstream)
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen{
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen if (fstream->autoclose_fd && fstream->fd != -1) {
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen if (close(fstream->fd) < 0)
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen i_error("file_ostream.close() failed: %m");
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen fstream->fd = -1;
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen }
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen if (fstream->io != NULL) {
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen io_remove(fstream->io);
635df5b4cbcd7b24c825e01d9dd66d3a4274c4c7Timo Sirainen fstream->io = NULL;
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen }
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen fstream->ostream.ostream.closed = TRUE;
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen}
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainenstatic void _close(struct _iostream *stream)
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen{
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen struct file_ostream *fstream = (struct file_ostream *)stream;
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen /* flush output before really closing it */
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen o_stream_flush(&fstream->ostream.ostream);
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen stream_closed(fstream);
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen}
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainenstatic void _destroy(struct _iostream *stream)
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen{
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen struct file_ostream *fstream = (struct file_ostream *)stream;
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen p_free(fstream->ostream.iostream.pool, fstream->buffer);
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen}
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainenstatic void _set_max_buffer_size(struct _iostream *stream, size_t max_size)
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen{
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen struct file_ostream *fstream = (struct file_ostream *)stream;
ba8498efbf886ca8b69fdb20c0ba2f5dba9416e3Timo Sirainen
93a7d1ee4b518b5c85f9721dc6539e4dab6aae00Timo Sirainen fstream->max_buffer_size = max_size;
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen}
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainenstatic void update_buffer(struct file_ostream *fstream, size_t size)
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen{
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen size_t used;
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen if (IS_STREAM_EMPTY(fstream))
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen return;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen if (fstream->head < fstream->tail) {
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen /* ...HXXXT... */
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen used = fstream->tail - fstream->head;
7744586e3e0fd60158abfbb03a233d3bd8d6c48bTimo Sirainen fstream->head += I_MIN(used, size);
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen } else {
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen /* XXXT...HXXX */
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen used = fstream->buffer_size - fstream->head;
380dbb60ae291cbe39d1f710284562ca9167150bTimo Sirainen if (size > used) {
380dbb60ae291cbe39d1f710284562ca9167150bTimo Sirainen size -= used;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen if (size < fstream->tail)
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen fstream->head = size;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen else {
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen /* whole buffer is sent */
1b823b2b7790a1e1b7974fcf11a4c48a28e70f37Timo Sirainen fstream->head = fstream->tail = 0;
3cf67672fdc87583cb23ce088c95bb5dee60e74dTimo Sirainen }
3cf67672fdc87583cb23ce088c95bb5dee60e74dTimo Sirainen } else {
3cf67672fdc87583cb23ce088c95bb5dee60e74dTimo Sirainen fstream->head += I_MIN(used, size);
3cf67672fdc87583cb23ce088c95bb5dee60e74dTimo Sirainen }
3cf67672fdc87583cb23ce088c95bb5dee60e74dTimo Sirainen
3cf67672fdc87583cb23ce088c95bb5dee60e74dTimo Sirainen fstream->full = FALSE;
3cf67672fdc87583cb23ce088c95bb5dee60e74dTimo Sirainen }
7744586e3e0fd60158abfbb03a233d3bd8d6c48bTimo Sirainen
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen if (fstream->head == fstream->tail)
1b823b2b7790a1e1b7974fcf11a4c48a28e70f37Timo Sirainen fstream->head = fstream->tail = 0;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen
9dd1c256910f1fb42823116a641e7edb3ad11970Timo Sirainen if (fstream->head == fstream->buffer_size)
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen fstream->head = 0;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen}
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen
9dd1c256910f1fb42823116a641e7edb3ad11970Timo Sirainen/* NOTE: modifies iov */
9dd1c256910f1fb42823116a641e7edb3ad11970Timo Sirainenstatic ssize_t o_stream_writev(struct file_ostream *fstream,
9dd1c256910f1fb42823116a641e7edb3ad11970Timo Sirainen const struct const_iovec *iov, int iov_size)
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen{
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen ssize_t ret;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen size_t size, sent;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen int i;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen if (iov_size == 1)
7744586e3e0fd60158abfbb03a233d3bd8d6c48bTimo Sirainen ret = write(fstream->fd, iov->iov_base, iov->iov_len);
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen else {
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen sent = 0;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen while (iov_size > UIO_MAXIOV) {
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen size = 0;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen for (i = 0; i < UIO_MAXIOV; i++)
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen size += iov[i].iov_len;
1b823b2b7790a1e1b7974fcf11a4c48a28e70f37Timo Sirainen
3cf67672fdc87583cb23ce088c95bb5dee60e74dTimo Sirainen ret = writev(fstream->fd, (const struct iovec *)iov,
3cf67672fdc87583cb23ce088c95bb5dee60e74dTimo Sirainen UIO_MAXIOV);
3cf67672fdc87583cb23ce088c95bb5dee60e74dTimo Sirainen if (ret != (ssize_t)size)
3cf67672fdc87583cb23ce088c95bb5dee60e74dTimo Sirainen break;
7744586e3e0fd60158abfbb03a233d3bd8d6c48bTimo Sirainen
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen sent += ret;
1b823b2b7790a1e1b7974fcf11a4c48a28e70f37Timo Sirainen iov += UIO_MAXIOV;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen iov_size -= UIO_MAXIOV;
9dd1c256910f1fb42823116a641e7edb3ad11970Timo Sirainen }
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen if (iov_size <= UIO_MAXIOV) {
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen ret = writev(fstream->fd, (const struct iovec *)iov,
9dd1c256910f1fb42823116a641e7edb3ad11970Timo Sirainen iov_size);
9dd1c256910f1fb42823116a641e7edb3ad11970Timo Sirainen if (ret > 0)
9dd1c256910f1fb42823116a641e7edb3ad11970Timo Sirainen ret += sent;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen }
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen }
57d2429fae575e96ca276355af675deb66b76d00Timo Sirainen
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen if (ret < 0) {
401b0787fff2dc986a5321ddb32acb1947ff66b0Timo Sirainen if (errno == EAGAIN || errno == EINTR)
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen return 0;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen fstream->ostream.ostream.stream_errno = errno;
57d2429fae575e96ca276355af675deb66b76d00Timo Sirainen stream_closed(fstream);
401b0787fff2dc986a5321ddb32acb1947ff66b0Timo Sirainen return -1;
401b0787fff2dc986a5321ddb32acb1947ff66b0Timo Sirainen }
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen return ret;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen}
7744586e3e0fd60158abfbb03a233d3bd8d6c48bTimo Sirainen
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen/* returns how much of vector was used */
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainenstatic int o_stream_fill_iovec(struct file_ostream *fstream,
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen struct const_iovec iov[2])
61618d4c58080570f689614fec204ae14e90cef2Timo Sirainen{
50e20db49f29917fe9adcf1b56b11badf28bd0e4Timo Sirainen if (IS_STREAM_EMPTY(fstream))
50e20db49f29917fe9adcf1b56b11badf28bd0e4Timo Sirainen return 0;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen if (fstream->head < fstream->tail) {
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen iov[0].iov_base = fstream->buffer + fstream->head;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen iov[0].iov_len = fstream->tail - fstream->head;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen return 1;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen } else {
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen iov[0].iov_base = fstream->buffer + fstream->head;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen iov[0].iov_len = fstream->buffer_size - fstream->head;
50e20db49f29917fe9adcf1b56b11badf28bd0e4Timo Sirainen if (fstream->tail == 0)
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen return 1;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen else {
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen iov[1].iov_base = fstream->buffer;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen iov[1].iov_len = fstream->tail;
c6f894e1522f7b0b6068c228900914073c145175Timo Sirainen return 2;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen }
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen }
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen}
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainenstatic int buffer_flush(struct file_ostream *fstream)
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen{
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen struct const_iovec iov[2];
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen int iov_len;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen ssize_t ret;
401b0787fff2dc986a5321ddb32acb1947ff66b0Timo Sirainen
401b0787fff2dc986a5321ddb32acb1947ff66b0Timo Sirainen iov_len = o_stream_fill_iovec(fstream, iov);
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen if (iov_len > 0) {
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen ret = o_stream_writev(fstream, iov, iov_len);
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen if (ret < 0)
7744586e3e0fd60158abfbb03a233d3bd8d6c48bTimo Sirainen return -1;
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen update_buffer(fstream, ret);
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen }
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen return IS_STREAM_EMPTY(fstream) ? 1 : 0;
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen}
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainenstatic void _cork(struct _ostream *stream, int set)
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen{
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen struct file_ostream *fstream = (struct file_ostream *)stream;
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen if (fstream->corked != set) {
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen if (!fstream->no_socket_cork) {
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen if (net_set_cork(fstream->fd, set) < 0)
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen fstream->no_socket_cork = TRUE;
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen }
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen fstream->corked = set;
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen if (set && fstream->io != NULL) {
c6f894e1522f7b0b6068c228900914073c145175Timo Sirainen io_remove(fstream->io);
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen fstream->io = NULL;
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen } else if (!set && fstream->io == NULL) {
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen if (fstream->file)
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen buffer_flush(fstream);
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen else {
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen fstream->io = io_add(fstream->fd, IO_WRITE,
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen stream_send_io, fstream);
401b0787fff2dc986a5321ddb32acb1947ff66b0Timo Sirainen }
401b0787fff2dc986a5321ddb32acb1947ff66b0Timo Sirainen }
401b0787fff2dc986a5321ddb32acb1947ff66b0Timo Sirainen }
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen}
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen
7744586e3e0fd60158abfbb03a233d3bd8d6c48bTimo Sirainenstatic int _flush(struct _ostream *stream)
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen{
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen struct file_ostream *fstream = (struct file_ostream *) stream;
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen return buffer_flush(fstream);
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen}
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen
383d0e8c24451468d6bea17e4b55d74de744abe6Timo Sirainenstatic size_t get_unused_space(struct file_ostream *fstream)
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen{
383d0e8c24451468d6bea17e4b55d74de744abe6Timo Sirainen if (fstream->head > fstream->tail) {
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen /* XXXT...HXXX */
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen return fstream->head - fstream->tail;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen } else if (fstream->head < fstream->tail) {
d477acb83e14a776ece4ca94dcd1869e75d0c6eeTimo Sirainen /* ...HXXXT... */
7289c5600711b45f30fe289ab5b0293b51d87041Timo Sirainen return (fstream->buffer_size - fstream->tail) + fstream->head;
2024157e8de36edd31f5fd72f5ea7364a0955fa7Timo Sirainen } else {
d477acb83e14a776ece4ca94dcd1869e75d0c6eeTimo Sirainen /* either fully unused or fully used */
d477acb83e14a776ece4ca94dcd1869e75d0c6eeTimo Sirainen return fstream->full ? 0 : fstream->buffer_size;
7289c5600711b45f30fe289ab5b0293b51d87041Timo Sirainen }
d477acb83e14a776ece4ca94dcd1869e75d0c6eeTimo Sirainen}
15f526e5ac611b4532568d131fcd0abf664abe41Timo Sirainen
15f526e5ac611b4532568d131fcd0abf664abe41Timo Sirainenstatic size_t _get_used_size(struct _ostream *stream)
15f526e5ac611b4532568d131fcd0abf664abe41Timo Sirainen{
15f526e5ac611b4532568d131fcd0abf664abe41Timo Sirainen struct file_ostream *fstream = (struct file_ostream *)stream;
7289c5600711b45f30fe289ab5b0293b51d87041Timo Sirainen
7289c5600711b45f30fe289ab5b0293b51d87041Timo Sirainen return fstream->buffer_size - get_unused_space(fstream);
7289c5600711b45f30fe289ab5b0293b51d87041Timo Sirainen}
7289c5600711b45f30fe289ab5b0293b51d87041Timo Sirainen
7289c5600711b45f30fe289ab5b0293b51d87041Timo Sirainenstatic int _seek(struct _ostream *stream, uoff_t offset)
7289c5600711b45f30fe289ab5b0293b51d87041Timo Sirainen{
0f62889d833767acf9c2ad010c3269806b4cfae3Timo Sirainen struct file_ostream *fstream = (struct file_ostream *)stream;
7289c5600711b45f30fe289ab5b0293b51d87041Timo Sirainen off_t ret;
7289c5600711b45f30fe289ab5b0293b51d87041Timo Sirainen
7289c5600711b45f30fe289ab5b0293b51d87041Timo Sirainen if (offset > OFF_T_MAX) {
7289c5600711b45f30fe289ab5b0293b51d87041Timo Sirainen stream->ostream.stream_errno = EINVAL;
7289c5600711b45f30fe289ab5b0293b51d87041Timo Sirainen return -1;
7289c5600711b45f30fe289ab5b0293b51d87041Timo Sirainen }
7289c5600711b45f30fe289ab5b0293b51d87041Timo Sirainen
7289c5600711b45f30fe289ab5b0293b51d87041Timo Sirainen if (buffer_flush(fstream) < 0)
7289c5600711b45f30fe289ab5b0293b51d87041Timo Sirainen return -1;
7289c5600711b45f30fe289ab5b0293b51d87041Timo Sirainen
d477acb83e14a776ece4ca94dcd1869e75d0c6eeTimo Sirainen ret = lseek(fstream->fd, (off_t)offset, SEEK_SET);
d477acb83e14a776ece4ca94dcd1869e75d0c6eeTimo Sirainen if (ret < 0) {
9dd1c256910f1fb42823116a641e7edb3ad11970Timo Sirainen stream->ostream.stream_errno = errno;
9dd1c256910f1fb42823116a641e7edb3ad11970Timo Sirainen return -1;
9dd1c256910f1fb42823116a641e7edb3ad11970Timo Sirainen }
9dd1c256910f1fb42823116a641e7edb3ad11970Timo Sirainen
9dd1c256910f1fb42823116a641e7edb3ad11970Timo Sirainen if (ret != (off_t)offset) {
9dd1c256910f1fb42823116a641e7edb3ad11970Timo Sirainen stream->ostream.stream_errno = EINVAL;
9dd1c256910f1fb42823116a641e7edb3ad11970Timo Sirainen return -1;
9dd1c256910f1fb42823116a641e7edb3ad11970Timo Sirainen }
9dd1c256910f1fb42823116a641e7edb3ad11970Timo Sirainen
9dd1c256910f1fb42823116a641e7edb3ad11970Timo Sirainen stream->ostream.stream_errno = 0;
9dd1c256910f1fb42823116a641e7edb3ad11970Timo Sirainen stream->ostream.offset = offset;
9dd1c256910f1fb42823116a641e7edb3ad11970Timo Sirainen return 1;
9dd1c256910f1fb42823116a641e7edb3ad11970Timo Sirainen}
9dd1c256910f1fb42823116a641e7edb3ad11970Timo Sirainen
9dd1c256910f1fb42823116a641e7edb3ad11970Timo Sirainenstatic void o_stream_grow_buffer(struct file_ostream *fstream, size_t bytes)
9dd1c256910f1fb42823116a641e7edb3ad11970Timo Sirainen{
9dd1c256910f1fb42823116a641e7edb3ad11970Timo Sirainen size_t size, head_size;
9dd1c256910f1fb42823116a641e7edb3ad11970Timo Sirainen
9dd1c256910f1fb42823116a641e7edb3ad11970Timo Sirainen size = nearest_power(fstream->buffer_size + bytes);
9dd1c256910f1fb42823116a641e7edb3ad11970Timo Sirainen if (size > fstream->max_buffer_size) {
9dd1c256910f1fb42823116a641e7edb3ad11970Timo Sirainen /* limit the size */
272aca0a772140d3a45a425a3fd67854ae2ccec2Timo Sirainen size = fstream->max_buffer_size;
9dd1c256910f1fb42823116a641e7edb3ad11970Timo Sirainen } else if (fstream->corked) {
9dd1c256910f1fb42823116a641e7edb3ad11970Timo Sirainen /* use optimal buffer size with corking */
9dd1c256910f1fb42823116a641e7edb3ad11970Timo Sirainen size = I_MIN(fstream->optimal_block_size,
9dd1c256910f1fb42823116a641e7edb3ad11970Timo Sirainen fstream->max_buffer_size);
d477acb83e14a776ece4ca94dcd1869e75d0c6eeTimo Sirainen }
d477acb83e14a776ece4ca94dcd1869e75d0c6eeTimo Sirainen
1d22eaac93de41319918a1fc6de42bb302e25c1aTimo Sirainen if (size <= fstream->buffer_size)
1d22eaac93de41319918a1fc6de42bb302e25c1aTimo Sirainen return;
5d60e31c7b701b606067a20bc88dcc8a6de7bbd6Timo Sirainen
5d60e31c7b701b606067a20bc88dcc8a6de7bbd6Timo Sirainen fstream->buffer = p_realloc(fstream->ostream.iostream.pool,
5d60e31c7b701b606067a20bc88dcc8a6de7bbd6Timo Sirainen fstream->buffer,
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen fstream->buffer_size, size);
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
2024157e8de36edd31f5fd72f5ea7364a0955fa7Timo Sirainen if (fstream->tail <= fstream->head && !IS_STREAM_EMPTY(fstream)) {
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen head_size = I_MIN(fstream->head, size - fstream->buffer_size);
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen memcpy(fstream->buffer + fstream->buffer_size, fstream->buffer,
9ed2951bd0bb1878a27437d7c00611b2baadd614Timo Sirainen head_size);
5d60e31c7b701b606067a20bc88dcc8a6de7bbd6Timo Sirainen
b215a8a123623782554a83f3025ef4e771bd8f01Timo Sirainen if (head_size == fstream->head)
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen fstream->tail = fstream->buffer_size + head_size;
9ed2951bd0bb1878a27437d7c00611b2baadd614Timo Sirainen else {
9ed2951bd0bb1878a27437d7c00611b2baadd614Timo Sirainen memmove(fstream->buffer, fstream->buffer + head_size,
9ed2951bd0bb1878a27437d7c00611b2baadd614Timo Sirainen fstream->head - head_size);
9ed2951bd0bb1878a27437d7c00611b2baadd614Timo Sirainen fstream->tail = fstream->head - head_size;
9ed2951bd0bb1878a27437d7c00611b2baadd614Timo Sirainen }
5d60e31c7b701b606067a20bc88dcc8a6de7bbd6Timo Sirainen }
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
b215a8a123623782554a83f3025ef4e771bd8f01Timo Sirainen fstream->full = FALSE;
5d60e31c7b701b606067a20bc88dcc8a6de7bbd6Timo Sirainen fstream->buffer_size = size;
5d60e31c7b701b606067a20bc88dcc8a6de7bbd6Timo Sirainen}
5d60e31c7b701b606067a20bc88dcc8a6de7bbd6Timo Sirainen
5d60e31c7b701b606067a20bc88dcc8a6de7bbd6Timo Sirainenstatic void stream_send_io(void *context)
5d60e31c7b701b606067a20bc88dcc8a6de7bbd6Timo Sirainen{
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen struct file_ostream *fstream = context;
if (fstream->ostream.callback != NULL)
fstream->ostream.callback(fstream->ostream.context);
else {
if (_flush(&fstream->ostream) <= 0)
return;
}
if (IS_STREAM_EMPTY(fstream) && fstream->io != NULL) {
/* all sent */
io_remove(fstream->io);
fstream->io = NULL;
}
}
static size_t o_stream_add(struct file_ostream *fstream,
const void *data, size_t size)
{
size_t unused, sent;
int i;
unused = get_unused_space(fstream);
if (unused < size)
o_stream_grow_buffer(fstream, size-unused);
sent = 0;
for (i = 0; i < 2 && sent < size && !fstream->full; i++) {
unused = fstream->tail >= fstream->head ?
fstream->buffer_size - fstream->tail :
fstream->head - fstream->tail;
if (unused > size-sent)
unused = size-sent;
memcpy(fstream->buffer + fstream->tail, data, unused);
sent += unused;
fstream->tail += unused;
if (fstream->tail == fstream->buffer_size)
fstream->tail = 0;
if (fstream->head == fstream->tail)
fstream->full = TRUE;
}
if (sent != 0 && fstream->io == NULL &&
!fstream->corked && !fstream->file) {
fstream->io = io_add(fstream->fd, IO_WRITE, stream_send_io,
fstream);
}
return sent;
}
static ssize_t _sendv(struct _ostream *stream, const struct const_iovec *iov,
size_t iov_count)
{
struct file_ostream *fstream = (struct file_ostream *)stream;
size_t i, size, added, optimal_size;
ssize_t ret = 0;
stream->ostream.stream_errno = 0;
for (i = 0, size = 0; i < iov_count; i++)
size += iov[i].iov_len;
if (size > get_unused_space(fstream)) {
if (_flush(stream) < 0)
return -1;
}
optimal_size = I_MIN(fstream->optimal_block_size,
fstream->max_buffer_size);
if (IS_STREAM_EMPTY(fstream) &&
(!fstream->corked || size >= optimal_size)) {
/* send immediately */
ret = o_stream_writev(fstream, iov, iov_count);
if (ret < 0)
return -1;
size = ret;
while (size > 0 && size >= iov[0].iov_len) {
size -= iov[0].iov_len;
iov++;
iov_count--;
}
if (iov_count > 0) {
added = o_stream_add(fstream,
CONST_PTR_OFFSET(iov[0].iov_base, size),
iov[0].iov_len - size);
if (added != iov[0].iov_len - size) {
/* buffer full */
stream->ostream.offset += added;
return added;
}
iov++;
iov_count--;
}
}
/* buffer it, at least partly */
for (i = 0; i < iov_count; i++) {
added = o_stream_add(fstream, iov[i].iov_base, iov[i].iov_len);
ret += added;
if (added != iov[i].iov_len)
break;
}
stream->ostream.offset += ret;
return ret;
}
static off_t io_stream_sendfile(struct _ostream *outstream,
struct istream *instream,
int in_fd, uoff_t in_size)
{
struct file_ostream *foutstream = (struct file_ostream *)outstream;
uoff_t start_offset;
uoff_t offset, send_size, v_offset;
ssize_t ret;
/* flush out any data in buffer */
if ((ret = buffer_flush(foutstream)) <= 0)
return ret;
start_offset = v_offset = instream->v_offset;
do {
offset = instream->real_stream->abs_start_offset + v_offset;
send_size = in_size - v_offset;
ret = safe_sendfile(foutstream->fd, in_fd, &offset,
MAX_SSIZE_T(send_size));
if (ret <= 0) {
if (ret == 0 || errno == EINTR || errno == EAGAIN) {
ret = 0;
break;
}
outstream->ostream.stream_errno = errno;
if (errno != EINVAL) {
/* close only if error wasn't because
sendfile() isn't supported */
stream_closed(foutstream);
}
break;
}
v_offset += ret;
outstream->ostream.offset += ret;
} while ((uoff_t)ret != send_size);
i_stream_seek(instream, v_offset);
return ret < 0 ? -1 : (off_t)(instream->v_offset - start_offset);
}
static off_t io_stream_copy(struct _ostream *outstream,
struct istream *instream, uoff_t in_size)
{
struct file_ostream *foutstream = (struct file_ostream *)outstream;
uoff_t start_offset;
struct const_iovec iov[3];
int iov_len;
const unsigned char *data;
size_t size, skip_size, block_size;
ssize_t ret;
int pos;
iov_len = o_stream_fill_iovec(foutstream, iov);
skip_size = 0;
for (pos = 0; pos < iov_len; pos++)
skip_size += iov[pos].iov_len;
start_offset = instream->v_offset;
while (in_size > 0) {
block_size = I_MIN(foutstream->optimal_block_size, in_size);
(void)i_stream_read_data(instream, &data, &size, block_size-1);
in_size -= size;
if (size == 0) {
/* all sent */
break;
}
pos = iov_len++;
iov[pos].iov_base = (void *) data;
iov[pos].iov_len = size;
ret = o_stream_writev(foutstream, iov, iov_len);
if (ret < 0)
return -1;
if (skip_size > 0) {
update_buffer(foutstream, ret);
if ((size_t)ret < skip_size) {
skip_size -= ret;
ret = 0;
} else {
ret -= skip_size;
skip_size = 0;
}
}
outstream->ostream.offset += ret;
i_stream_skip(instream, ret);
if ((size_t)ret != iov[pos].iov_len)
break;
iov_len = 0;
}
return (off_t) (instream->v_offset - start_offset);
}
static off_t io_stream_copy_backwards(struct _ostream *outstream,
struct istream *instream, uoff_t in_size)
{
struct file_ostream *foutstream = (struct file_ostream *)outstream;
uoff_t in_start_offset, in_offset, in_limit, out_offset;
const unsigned char *data;
size_t buffer_size, size, read_size;
ssize_t ret;
i_assert(IS_STREAM_EMPTY(foutstream));
/* figure out optimal buffer size */
buffer_size = instream->real_stream->buffer_size;
if (buffer_size == 0 || buffer_size > foutstream->buffer_size) {
if (foutstream->optimal_block_size > foutstream->buffer_size) {
o_stream_grow_buffer(foutstream,
foutstream->optimal_block_size -
foutstream->buffer_size);
}
buffer_size = foutstream->buffer_size;
}
in_start_offset = instream->v_offset;
in_offset = in_limit = in_size;
out_offset = outstream->ostream.offset + (in_offset - in_start_offset);
while (in_offset > in_start_offset) {
if (in_offset - in_start_offset <= buffer_size)
read_size = in_offset - in_start_offset;
else
read_size = buffer_size;
in_offset -= read_size;
out_offset -= read_size;
for (;;) {
i_assert(in_offset <= in_limit);
i_stream_seek(instream, in_offset);
read_size = in_limit - in_offset;
(void)i_stream_read_data(instream, &data, &size,
read_size-1);
if (size >= read_size) {
size = read_size;
if (instream->mmaped) {
/* we'll have to write it through
buffer or the file gets corrupted */
i_assert(size <=
foutstream->buffer_size);
memcpy(foutstream->buffer, data, size);
data = foutstream->buffer;
}
break;
}
/* buffer too large probably, try with smaller */
read_size -= size;
in_offset += read_size;
out_offset += read_size;
buffer_size -= read_size;
}
in_limit -= size;
if (o_stream_seek(&outstream->ostream, out_offset) < 0)
return -1;
ret = write_full(foutstream->fd, data, size);
if (ret < 0) {
/* error */
outstream->ostream.stream_errno = errno;
return -1;
}
}
return (off_t) (in_size - in_start_offset);
}
static off_t _send_istream(struct _ostream *outstream, struct istream *instream)
{
struct file_ostream *foutstream = (struct file_ostream *)outstream;
uoff_t in_size;
off_t ret;
int in_fd, overlapping;
in_fd = i_stream_get_fd(instream);
in_size = i_stream_get_size(instream);
i_assert(instream->v_offset <= in_size);
outstream->ostream.stream_errno = 0;
if (in_fd != foutstream->fd)
overlapping = 0;
else {
/* copying data within same fd. we'll have to be careful with
seeks and overlapping writes. */
if (in_size == (uoff_t)-1) {
outstream->ostream.stream_errno = EINVAL;
return -1;
}
ret = (off_t)outstream->ostream.offset -
(off_t)(instream->real_stream->abs_start_offset +
instream->v_offset);
if (ret == 0) {
/* copying data over itself. we don't really
need to do that, just fake it. */
return in_size - instream->v_offset;
}
overlapping = ret < 0 ? -1 : 1;
}
if (!foutstream->no_sendfile && in_fd != -1 && overlapping <= 0) {
ret = io_stream_sendfile(outstream, instream, in_fd, in_size);
if (ret >= 0 || outstream->ostream.stream_errno != EINVAL)
return ret;
/* sendfile() not supported (with this fd), fallback to
regular sending. */
outstream->ostream.stream_errno = 0;
foutstream->no_sendfile = TRUE;
}
if (overlapping <= 0)
return io_stream_copy(outstream, instream, in_size);
else
return io_stream_copy_backwards(outstream, instream, in_size);
}
struct ostream *
o_stream_create_file(int fd, pool_t pool, size_t max_buffer_size,
int autoclose_fd)
{
struct file_ostream *fstream;
struct ostream *ostream;
struct stat st;
off_t offset;
fstream = p_new(pool, struct file_ostream, 1);
fstream->fd = fd;
fstream->max_buffer_size = max_buffer_size;
fstream->autoclose_fd = autoclose_fd;
fstream->optimal_block_size = DEFAULT_OPTIMAL_BLOCK_SIZE;
fstream->ostream.iostream.close = _close;
fstream->ostream.iostream.destroy = _destroy;
fstream->ostream.iostream.set_max_buffer_size = _set_max_buffer_size;
fstream->ostream.cork = _cork;
fstream->ostream.flush = _flush;
fstream->ostream.get_used_size = _get_used_size;
fstream->ostream.seek = _seek;
fstream->ostream.sendv = _sendv;
fstream->ostream.send_istream = _send_istream;
ostream = _o_stream_create(&fstream->ostream, pool);
offset = lseek(fd, 0, SEEK_CUR);
if (offset >= 0) {
ostream->offset = offset;
if (fstat(fd, &st) == 0) {
if ((uoff_t)st.st_blksize >
fstream->optimal_block_size) {
/* use the optimal block size, but with a
reasonable limit */
fstream->optimal_block_size =
I_MIN(st.st_blksize,
MAX_OPTIMAL_BLOCK_SIZE);
}
if (S_ISREG(st.st_mode)) {
fstream->no_socket_cork = TRUE;
fstream->file = TRUE;
}
}
#ifndef HAVE_LINUX_SENDFILE
/* only Linux supports sendfile() with non-sockets. Other
systems fail more or less gracefully if it's tried, so
don't bother to even try with them. */
fstream->no_sendfile = TRUE;
#endif
} else {
if (net_getsockname(fd, NULL, NULL) < 0) {
fstream->no_sendfile = TRUE;
fstream->no_socket_cork = TRUE;
}
}
if (max_buffer_size == 0)
fstream->max_buffer_size = fstream->optimal_block_size;
return ostream;
}