Exemple #1
0
static int zio_close (zio_t *zio)
{
    if (zio->flags & ZIO_CLOSED) {
        /* Already closed */
        errno = EINVAL;
        return (-1);
    }
    zio_debug (zio, "zio_close\n");
    if (zio_reader (zio)) {
        close (zio->srcfd);
        zio->srcfd = -1;
    }
    else if (zio_writer (zio)) {
        close (zio->dstfd);
        zio->dstfd = -1;
        /* For writer zio object, consider close(dstfd)
         *  as "EOF sent"
         */
        zio->flags |= ZIO_EOF_SENT;
    }
    zio->flags |= ZIO_CLOSED;
    if (zio->close)
        return (*zio->close) (zio, zio->arg);
    return (0);
}
Exemple #2
0
/*
 *   Flush any buffered output and EOF from zio READER object
 *    to destination.
 */
int zio_flush (zio_t *zio)
{
    int len;
    int rc = 0;

    if ((zio == NULL) || (zio->magic != ZIO_MAGIC))
        return (-1);
    if (zio_reader (zio) && !zio->send)
       return (-1);

    zio_debug (zio, "zio_flush\n");

    /*
     *  Nothing to flush if EOF already sent to consumer:
     */
    if (zio_eof_sent (zio))
        return (0);

    if (zio_writer (zio))
        return zio_writer_flush_all (zio);

    /* else zio reader:
    */

    while (((len = zio_data_to_flush (zio)) > 0) || zio_eof (zio)) {
        char * buf = NULL;
        int n = 0;
        zio_debug (zio, "zio_flush: len = %d, eof = %d\n", len, zio_eof (zio));
        if (len > 0) {
            buf = xzmalloc (len + 1);
            if ((n = zio_fd_read (zio, buf, len + 1)) <= 0) {
                if (n < 0) {
                    zio_debug (zio, "zio_read: %s", strerror (errno));
                    rc = -1;
                }
                /*
                 *  We may not be able to read any data from the buffer
                 *   because we are line buffering and there is not yet
                 *   a full line in the buffer. In this case just exit
                 *   so we can buffer more data.
                 */
                free (buf);
                return (rc);

            }
        }
        zio_debug (zio, "zio_data_to_flush = %d\n", zio_data_to_flush (zio));
        zio_debug (zio, "zio_flush: Sending %d (%s) [eof=%d]\n", n, buf, zio_eof(zio));
        rc = zio_send (zio, buf, n);
        if (buf)
            free (buf);
        if (zio_eof_sent (zio))
            break;
    }
    return (rc);
}
Exemple #3
0
static int zio_bootstrap (zio_t *zio)
{
    if (zio_reader (zio))
        zio_reader_poll (zio);
    else if (zio_writer (zio)) {
        /*
         *  Add writer to poll loop only if there is data pending to
         *   be written
         */
        if (zio_write_pending (zio))
            zio_writer_schedule (zio);
    }
    return (0);
}