src/server/util/io.c

Sun, 26 Feb 2012 19:51:14 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 26 Feb 2012 19:51:14 +0100
changeset 26
37ff8bf54b89
parent 25
5dee29c7c530
child 32
ebba53de8b18
permissions
-rw-r--r--

Minimal webdav support

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2011 Olaf Wintermann. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <unistd.h>
#include <stdlib.h>
#include <sys/uio.h>

#include "io.h"
#include "pool.h"

IOStream native_io_funcs = {
    system_write,
    system_read
};

IOStream net_io_funcs = {
    net_stream_write,
    net_stream_read
};


IOStream* stream_new_from_fd(int fd) {
    SystemIOStream *st = malloc(sizeof(SystemIOStream));
    st->st = native_io_funcs;
    st->fd = fd;
    return (IOStream*)st;
}

ssize_t system_write(IOStream *st, void *buf, size_t nbytes) {
    return write(((SystemIOStream*)st)->fd, buf, nbytes);
}

ssize_t system_read(IOStream *st, void *buf, size_t nbytes) {
    return read(((SystemIOStream*)st)->fd, buf, nbytes);
}


IOStream* net_stream_from_fd(int fd) {
    NetIOStream *st = malloc(sizeof(NetIOStream));
    st->st = net_io_funcs;
    st->fd = fd;
    st->max_read = 0;
    st->rd = 0;
    return (IOStream*)st;
}

ssize_t net_stream_write(IOStream *st, void *buf, size_t nbytes) {
    // TODO: implement
}

ssize_t net_stream_read(IOStream *st, void *buf, size_t nbytes) {
    NetIOStream *n = (NetIOStream*)st;
    if(n->max_read != 0 && n->rd >= n->max_read) {
        return 0;
    }
    ssize_t r = read(n->fd, buf, nbytes);
    n->rd += r;
    return r;
}


ssize_t net_read(SYS_NETFD fd, void *buf, size_t nbytes) {
    ssize_t r = ((IOStream*)fd)->read(fd, buf, nbytes);
    if(r == 0) {
        return IO_EOF;
    }
    return r;
}

ssize_t net_write(SYS_NETFD fd, void *buf, size_t nbytes) {
    ssize_t r = ((IOStream*)fd)->write(fd, buf, nbytes);
    if(r < 0) {
        return IO_ERROR;
    }  
    return r;
}

ssize_t net_printf(SYS_NETFD fd, char *format, ...) {
    va_list arg;
    va_start(arg, format);
    char *buf;
    ssize_t len = vasprintf(&buf, format, arg);
    net_write(fd, buf, len);
    free(buf);
    return len;
}


/* iovec buffer */
iovec_buf_t *iovec_buf_create(pool_handle_t *pool) {
    iovec_buf_t *buf = pool_malloc(pool, sizeof(iovec_buf_t));

    buf->pool = pool;
    buf->iov  = pool_calloc(pool, 32, sizeof(struct iovec));
    buf->maxiovec = 32;
    buf->iovctn = 0;

    return buf;
}

void iovec_buf_write(iovec_buf_t *io, void *buf, size_t nbyte) {
    if(io->iovctn >= io->maxiovec) {
        io->iov = pool_realloc(
                io->pool,
                io->iov,
                (io->maxiovec + 16) * sizeof(struct iovec));
    }

    io->iov[io->iovctn].iov_base = buf;
    io->iov[io->iovctn].iov_len = nbyte;
    io->iovctn++;
}

ssize_t iovec_buf_flush(iovec_buf_t *io, int fd) {
    return writev(fd, io->iov, io->iovctn);
}

mercurial