ostream-file.c revision 3f072ebbd4152514e4fc54ac42da535af290359b
45312f52ff3a3d4c137447be4c7556500c2f8bf2Timo Sirainen/* Copyright (c) 2002-2009 Dovecot authors, see the included COPYING file */
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen/* @UNSAFE: whole file */
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen/* try to keep the buffer size within 4k..128k. ReiserFS may actually return
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen 128k as optimal size. */
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen ((fstream)->head == (fstream)->tail && !(fstream)->full)
d1414c09cf0d58ac983054e2f4e1a1f329272dcfTimo Sirainen ((size) < SSIZE_T_MAX ? (size_t)(size) : SSIZE_T_MAX)
d1414c09cf0d58ac983054e2f4e1a1f329272dcfTimo Sirainen size_t buffer_size, max_buffer_size, optimal_block_size;
d1414c09cf0d58ac983054e2f4e1a1f329272dcfTimo Sirainen size_t head, tail; /* first unsent/unused byte */
c57776c06ec99ba9b0dafdbf9475ea72ea8ca134Timo Sirainen unsigned int full:1; /* if head == tail, is buffer empty or full? */
e70d5895795732b8247ab9abb045b438e954bc46Timo Sirainenstatic void stream_send_io(struct file_ostream *fstream);
e70d5895795732b8247ab9abb045b438e954bc46Timo Sirainenstatic void stream_closed(struct file_ostream *fstream)
e70d5895795732b8247ab9abb045b438e954bc46Timo Sirainen if (fstream->autoclose_fd && fstream->fd != -1) {
1ddec6312bc6882aeb17d4d46d19cbca1723b68bTimo Sirainenstatic void o_stream_file_close(struct iostream_private *stream)
1ddec6312bc6882aeb17d4d46d19cbca1723b68bTimo Sirainen struct file_ostream *fstream = (struct file_ostream *)stream;
1ddec6312bc6882aeb17d4d46d19cbca1723b68bTimo Sirainen /* flush output before really closing it */
704fbadd78375da18dcaf2c5d93ac8cfe2c61358Timo Sirainenstatic void o_stream_file_destroy(struct iostream_private *stream)
2c38504860da8a8de915f8e0f5f39d7e7bd00cf8Timo Sirainen struct file_ostream *fstream = (struct file_ostream *)stream;
97c339398f1aba6f315b55a9b6ee6b020e33bea4Timo Siraineno_stream_file_set_max_buffer_size(struct iostream_private *stream,
97c339398f1aba6f315b55a9b6ee6b020e33bea4Timo Sirainen struct file_ostream *fstream = (struct file_ostream *)stream;
2c38504860da8a8de915f8e0f5f39d7e7bd00cf8Timo Sirainenstatic void update_buffer(struct file_ostream *fstream, size_t size)
97c339398f1aba6f315b55a9b6ee6b020e33bea4Timo Sirainen /* ...HXXXT... */
b7c2065b3f10f9ae27787a9db5aaefbfc70d4502Timo Sirainenstatic void o_stream_socket_cork(struct file_ostream *fstream)
0469ed17dafcc56589ce00960a23f4f06817dfb5Timo Sirainen if (fstream->corked && !fstream->socket_cork_set) {
if (ret < 0) {
bool partial;
if (ret > 0)
size = 0;
for (i = 0; i < IOV_MAX; i++)
IOV_MAX);
size = 0;
for (i = 0; i < iov_size; i++)
iov_size);
if (ret > 0) {
if (ret < 0) {
iov++;
iov_size--;
if (size == 0)
if (ret2 > 0) {
if (ret2 <= 0)
return ret2;
return ret;
int iov_len;
if (iov_len > 0) {
if (ret < 0)
int ret;
else if (!set) {
/* XXXT...HXXX */
int ret;
if (ret == 0)
sent = 0;
fstream);
return sent;
unsigned int iov_count)
if (ret < 0)
iov++;
iov_count--;
if (iov_count == 0)
return ret;
iov++;
iov_count--;
for (i = 0; i < iov_count; i++) {
return ret;
return ret;
if (ret <= 0) {
ret = 0;
int iov_len;
const unsigned char *data;
int pos;
skip_size = 0;
if (size == 0) {
if (ret < 0)
if (skip_size > 0) {
ret = 0;
skip_size = 0;
iov_len = 0;
const unsigned char *data;
if (ret < 0) {
int in_fd;
if (ret == 0) {
if (ret > 0) {
return ret;
static struct file_ostream *
return fstream;
struct ostream *
if (offset >= 0) {
if (max_buffer_size == 0)
return ostream;
struct ostream *
return ostream;