ostream-file.c revision 7013075e208bc7aa87257df7d9664c84c7c220f3
/*
ostream-file.c : Output stream handling for files
Copyright (c) 2002 Timo Sirainen
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/* @UNSAFE: whole file */
#include "lib.h"
#include "alarm-hup.h"
#include "ioloop.h"
#include "network.h"
#include "sendfile-util.h"
#include "istream.h"
#include "ostream-internal.h"
#include <unistd.h>
#ifdef HAVE_SYS_UIO_H
#endif
#define O_STREAM_MIN_SIZE 4096
#define IS_STREAM_EMPTY(fstream) \
#define MAX_SSIZE_T(size) \
struct file_ostream {
int fd;
int priority;
unsigned char *buffer; /* ring-buffer */
int timeout_msecs;
void (*timeout_cb)(void *);
void *timeout_context;
unsigned int corked:1;
unsigned int no_socket_cork:1;
unsigned int autoclose_fd:1;
};
{
i_error("file_ostream.close() failed: %m");
}
}
}
{
/* flush output before really closing it */
}
{
}
{
}
void (*timeout_cb)(void *), void *context)
{
if (timeout_msecs != 0)
}
{
if (!fstream->no_socket_cork) {
}
}
}
{
while (size > 0) {
} else {
size = 0;
}
}
}
{
if (IS_STREAM_EMPTY(fstream))
return;
/* ...HXXXT... */
} else {
/* XXXT...HXXX */
else {
/* whole buffer is sent */
}
} else {
}
}
}
/* NOTE: modifies iov */
static ssize_t
{
iov++;
iov_size--;
}
if (ret < 0) {
return 0;
return -1;
}
return ret;
}
/* returns how much of vector was used */
{
if (IS_STREAM_EMPTY(fstream))
return 0;
return 1;
} else {
return 1;
else {
return 2;
}
}
}
{
if (size > 0) {
iov_len++;
}
if (first)
/* timeouted */
return -1;
}
return -1;
}
return 1;
}
{
int iov_len;
if (!IS_STREAM_EMPTY(fstream)) {
return -1;
if (!IS_STREAM_EMPTY(fstream)) {
return -1;
}
}
return 1;
}
{
int ret;
/* remove cork */
if (!fstream->no_socket_cork) {
i_error("net_set_cork() failed: %m");
}
}
return ret;
}
{
/* XXXT...HXXX */
/* ...HXXXT... */
} else {
/* either fully unused or fully used */
}
}
{
return 1;
if (fstream->max_buffer_size == 0)
return 1;
}
{
return -1;
}
if (ret < 0) {
return -1;
}
return -1;
}
return 1;
}
{
if (fstream->max_buffer_size != 0) {
/* limit the size */
/* use the largest possible buffer with corking */
}
}
return;
else {
}
}
}
static void stream_send_io(void *context)
{
int iov_len;
/* error / all sent */
}
}
{
int i;
sent = 0;
}
fstream);
}
return sent;
}
{
/* never try sending immediately if fd is blocking,
so we don't need to deal with timeout issues here */
return ret;
}
/* send it blocking */
return -1;
} else {
/* buffer it, at least partly */
}
}
{
if (in_fd == -1) {
return -1;
}
/* set timeout time before flushing existing buffer which may block */
/* flush out any data in buffer */
if (buffer_flush(foutstream) < 0)
return -1;
for (;;) {
if (first)
/* timeouted */
}
return -1;
}
if (ret < 0) {
/* close only if error wasn't because
sendfile() isn't supported */
}
return -1;
}
if (!STREAM_IS_BLOCKING(foutstream)) {
/* don't block */
break;
}
ret = 0;
}
/* yes, all sent */
break;
}
}
}
{
int iov_len;
const unsigned char *data;
int pos;
for (;;) {
if (size == 0) {
/* all sent */
break;
}
if (ret < 0) {
/* error */
return -1;
}
/* don't block */
break;
}
/* timeouted */
}
return -1;
}
iov_len--;
/* if we already sent the iov[0] and iov[1], we
can just remove them from future calls */
iov_len--;
}
}
}
{
return 0;
return ret;
/* sendfile() not supported (with this fd), fallback to
regular sending */
}
struct ostream *
int priority, int autoclose_fd)
{
struct file_ostream *fstream;
}