# Module libvirt-stream from libvirt

Provides APIs for the management of streams

## Table of Contents

### Types

    typedef enum virStreamEventType
    typedef enum virStreamFlags
    typedef enum virStreamRecvFlagsValues

### Functions

    int	virStreamAbort			(virStreamPtr stream)
    int	virStreamEventAddCallback	(virStreamPtr stream,
    					 int events,
    					 virStreamEventCallback cb,
    					 void * opaque,
    					 virFreeCallback ff)
    typedef virStreamEventCallback
    void	virStreamEventCallback		(virStreamPtr stream,
    					 int events,
    					 void * opaque)

    int	virStreamEventRemoveCallback	(virStreamPtr stream)
    int	virStreamEventUpdateCallback	(virStreamPtr stream,
    					 int events)
    int	virStreamFinish			(virStreamPtr stream)
    int	virStreamFree			(virStreamPtr stream)
    virStreamPtr	virStreamNew		(virConnectPtr conn,
    					 unsigned int flags)
    int	virStreamRecv			(virStreamPtr stream,
    					 char * data,
    					 size_t nbytes)
    int	virStreamRecvAll		(virStreamPtr stream,
    					 virStreamSinkFunc handler,
    					 void * opaque)
    int	virStreamRecvFlags		(virStreamPtr stream,
    					 char * data,
    					 size_t nbytes,
    					 unsigned int flags)
    int	virStreamRecvHole		(virStreamPtr stream,
    					 long long * length,
    					 unsigned int flags)
    int	virStreamRef			(virStreamPtr stream)
    int	virStreamSend			(virStreamPtr stream,
    					 const char * data,
    					 size_t nbytes)
    int	virStreamSendAll		(virStreamPtr stream,
    					 virStreamSourceFunc handler,
    					 void * opaque)
    int	virStreamSendHole		(virStreamPtr stream,
    					 long long length,
    					 unsigned int flags)
    typedef virStreamSinkFunc
    int	virStreamSinkFunc		(virStreamPtr st,
    					 const char * data,
    					 size_t nbytes,
    					 void * opaque)

    typedef virStreamSinkHoleFunc
    int	virStreamSinkHoleFunc		(virStreamPtr st,
    					 long long length,
    					 void * opaque)

    typedef virStreamSourceFunc
    int	virStreamSourceFunc		(virStreamPtr st,
    					 char * data,
    					 size_t nbytes,
    					 void * opaque)

    typedef virStreamSourceHoleFunc
    int	virStreamSourceHoleFunc		(virStreamPtr st,
    					 int * inData,
    					 long long * length,
    					 void * opaque)

    typedef virStreamSourceSkipFunc
    int	virStreamSourceSkipFunc		(virStreamPtr st,
    					 long long length,
    					 void * opaque)

    int	virStreamSparseRecvAll		(virStreamPtr stream,
    					 virStreamSinkFunc handler,
    					 virStreamSinkHoleFunc holeHandler,
    					 void * opaque)
    int	virStreamSparseSendAll		(virStreamPtr stream,
    					 virStreamSourceFunc handler,
    					 virStreamSourceHoleFunc holeHandler,
    					 virStreamSourceSkipFunc skipHandler,
    					 void * opaque)

## Description

### Types

### `virStreamEventType`

    enum virStreamEventType {

VIR_STREAM_EVENT_READABLE | = | 1 (0x1; 1 << 0)
VIR_STREAM_EVENT_WRITABLE | = | 2 (0x2; 1 << 1)
VIR_STREAM_EVENT_ERROR | = | 4 (0x4; 1 << 2)
VIR_STREAM_EVENT_HANGUP | = | 8 (0x8; 1 << 3)

    }

### `virStreamFlags`

    enum virStreamFlags {

VIR_STREAM_NONBLOCK | = | 1 (0x1; 1 << 0)

    }

### `virStreamRecvFlagsValues`

    enum virStreamRecvFlagsValues {

VIR_STREAM_RECV_STOP_AT_HOLE | = | 1 (0x1; 1 << 0)

    }

### Functions

### `virStreamAbort`

    int	virStreamAbort			(virStreamPtr stream)

Request that the in progress data transfer be cancelled abnormally before the end of the stream has been reached. For output streams this can be used to inform the driver that the stream is being terminated early. For input streams this can be used to inform the driver that it should stop sending data.

If the stream is non-blocking, any callback must be removed beforehand.

stream
  pointer to the stream object
Returns
  0 on success, -1 upon error

### `virStreamEventAddCallback`

    int	virStreamEventAddCallback	(virStreamPtr stream,
    					 int events,
    					 virStreamEventCallback cb,
    					 void * opaque,
    					 virFreeCallback ff)

Register a callback to be notified when a stream becomes writable, or readable. This is most commonly used in conjunction with non-blocking data streams to integrate into an event loop

stream
  pointer to the stream object
events
  set of events to monitor
cb
  callback to invoke when an event occurs
opaque
  application defined data
ff
  callback to free `opaque` data
Returns
  0 on success, -1 upon error

### `virStreamEventCallback`

    typedef void	(*virStreamEventCallback	)	(virStreamPtr stream,
    					 int events,
    					 void * opaque)

Callback for receiving stream events. The callback will be invoked once for each event which is pending.

stream
  stream on which the event occurred
events
  bitset of events from virEventHandleType constants
opaque
  user data registered with handle

### `virStreamEventRemoveCallback`

    int	virStreamEventRemoveCallback	(virStreamPtr stream)

Remove an event callback from the stream

stream
  pointer to the stream object
Returns
  0 on success, -1 on error

### `virStreamEventUpdateCallback`

    int	virStreamEventUpdateCallback	(virStreamPtr stream,
    					 int events)

Changes the set of events to monitor for a stream. This allows for event notification to be changed without having to unregister & register the callback completely. This method is guaranteed to succeed if a callback is already registered

stream
  pointer to the stream object
events
  set of events to monitor
Returns
  0 on success, -1 if no callback is registered

### `virStreamFinish`

    int	virStreamFinish			(virStreamPtr stream)

Indicate that there is no further data to be transmitted on the stream. For output streams this should be called once all data has been written. For input streams this should be called once virStreamRecv returns end-of-file.

This method is a synchronization point for all asynchronous errors, so if this returns a success code the application can be sure that all data has been successfully processed.

If the stream is non-blocking, any callback must be removed beforehand.

stream
  pointer to the stream object
Returns
  0 on success, -1 upon error

### `virStreamFree`

    int	virStreamFree			(virStreamPtr stream)

Decrement the reference count on a stream, releasing the stream object if the reference count has hit zero.

There must not be an active data transfer in progress when releasing the stream. If a stream needs to be disposed of prior to end of stream being reached, then the virStreamAbort function should be called first.

stream
  pointer to the stream object
Returns
  0 upon success, or -1 on error

### `virStreamNew`

    virStreamPtr	virStreamNew		(virConnectPtr conn,
    					 unsigned int flags)

Creates a new stream object which can be used to perform streamed I/O with other public API function.

When no longer needed, a stream object must be released with virStreamFree. If a data stream has been used, then the application must call virStreamFinish or virStreamAbort before free'ing to, in order to notify the driver of termination.

If a non-blocking data stream is required passed VIR_STREAM_NONBLOCK for flags, otherwise pass 0.

conn
  pointer to the connection
flags
  bitwise-OR of virStreamFlags
Returns
  the new stream, or NULL upon error

### `virStreamRecv`

    int	virStreamRecv			(virStreamPtr stream,
    					 char * data,
    					 size_t nbytes)

Reads a series of bytes from the stream. This method may block the calling application for an arbitrary amount of time.

Errors are not guaranteed to be reported synchronously with the call, but may instead be delayed until a subsequent call.

An example using this with a hypothetical file download API looks like

      virStreamPtr st = virStreamNew(conn, 0);
      int fd = open("demo.iso", O_WRONLY, 0600);

      virConnectDownloadFile(conn, "demo.iso", st);

      while (1) {
          char buf[1024];
          int got = virStreamRecv(st, buf, 1024);
          if (got < 0) {
             virStreamAbort(st);
             break;
          }
          if (got == 0) {
             virStreamFinish(st);
             break;
          }
          int offset = 0;
          while (offset < got) {
             int sent = write(fd, buf + offset, got - offset);
             if (sent < 0) {
                virStreamAbort(st);
                goto done;
             }
             offset += sent;
          }
      }
      if (virStreamFinish(st) < 0)
         ... report an error ....
    done:
      virStreamFree(st);
      close(fd);

stream
  pointer to the stream object
data
  buffer to read into from stream
nbytes
  size of `data` buffer
Returns
  the number of bytes read, which may be less than requested. Returns 0 when the end of the stream is reached, at which time the caller should invoke virStreamFinish() to get confirmation of stream completion. Returns -1 upon error, at which time the stream will be marked as aborted, and the caller should now release the stream with virStreamFree. Returns -2 if there is no data pending to be read & the stream is marked as non-blocking.

### `virStreamRecvAll`

    int	virStreamRecvAll		(virStreamPtr stream,
    					 virStreamSinkFunc handler,
    					 void * opaque)

Receive the entire data stream, sending the data to the requested data sink. This is simply a convenient alternative to virStreamRecv, for apps that do blocking-I/O.

An example using this with a hypothetical file download API looks like

    int mysink(virStreamPtr st, const char *buf, int nbytes, void *opaque) {
        int *fd = opaque;

        return write(*fd, buf, nbytes);
    }

    virStreamPtr st = virStreamNew(conn, 0);
    int fd = open("demo.iso", O_WRONLY);

    virConnectUploadFile(conn, st);
    if (virStreamRecvAll(st, mysink, &fd) < 0) {
       ...report an error ...
       goto done;
    }
    if (virStreamFinish(st) < 0)
       ...report an error...
    virStreamFree(st);
    close(fd);

stream
  pointer to the stream object
handler
  sink callback for writing data to application
opaque
  application defined data
Returns
  0 if all the data was successfully received. The caller should invoke virStreamFinish(st) to flush the stream upon success and then virStreamFree Returns -1 upon any error, with virStreamAbort() already having been called, so the caller need only call virStreamFree()

### `virStreamRecvFlags`

    int	virStreamRecvFlags		(virStreamPtr stream,
    					 char * data,
    					 size_t nbytes,
    					 unsigned int flags)

Reads a series of bytes from the stream. This method may block the calling application for an arbitrary amount of time.

This is just like virStreamRecv except this one has extra `flags`. Calling this function with no `flags` set (equal to zero) is equivalent to calling virStreamRecv(stream, data, nbytes).

If flag VIR_STREAM_RECV_STOP_AT_HOLE is set, this function will stop reading from stream if it has reached a hole. In that case, -3 is returned and virStreamRecvHole() should be called to get the hole size. An example using this flag might look like this:

    while (1) {
      char buf[4096];

      int ret = virStreamRecvFlags(st, buf, len, VIR_STREAM_STOP_AT_HOLE);
      if (ret < 0) {
        if (ret == -3) {
          long long len;
          ret = virStreamRecvHole(st, &len, 0);
          if (ret < 0) {
            ...error..
          } else {
            ...seek len bytes in target...
          }
        } else {
          return -1;
        }
      } else {
          ...write buf to target...
      }
    }

stream
  pointer to the stream object
data
  buffer to read into from stream
nbytes
  size of `data` buffer
flags
  bitwise-OR of virStreamRecvFlagsValues
Returns
  0 when the end of the stream is reached, at which time the caller should invoke virStreamFinish() to get confirmation of stream completion. Returns -1 upon error, at which time the stream will be marked as aborted, and the caller should now release the stream with virStreamFree. Returns -2 if there is no data pending to be read & the stream is marked as non-blocking. Returns -3 if there is a hole in stream and caller requested to stop at a hole.

### `virStreamRecvHole`

    int	virStreamRecvHole		(virStreamPtr stream,
    					 long long * length,
    					 unsigned int flags)

This API is used to determine the `length` in bytes of the empty space to be created in a `stream's` target file when uploading or downloading sparsely populated files. This is the counterpart to virStreamSendHole().

stream
  pointer to the stream object
length
  number of bytes to skip
flags
  extra flags; not used yet, so callers should always pass 0
Returns
  0 on success, -1 on error or when there's currently no hole in the stream

### `virStreamRef`

    int	virStreamRef			(virStreamPtr stream)

Increment the reference count on the stream. For each additional call to this method, there shall be a corresponding call to virStreamFree to release the reference count, once the caller no longer needs the reference to this object.

stream
  pointer to the stream
Returns
  0 in case of success, -1 in case of failure

### `virStreamSend`

    int	virStreamSend			(virStreamPtr stream,
    					 const char * data,
    					 size_t nbytes)

Write a series of bytes to the stream. This method may block the calling application for an arbitrary amount of time. Once an application has finished sending data it should call virStreamFinish to wait for successful confirmation from the driver, or detect any error.

This method may not be used if a stream source has been registered.

Errors are not guaranteed to be reported synchronously with the call, but may instead be delayed until a subsequent call.

An example using this with a hypothetical file upload API looks like

      virStreamPtr st = virStreamNew(conn, 0);
      int fd = open("demo.iso", O_RDONLY);

      virConnectUploadFile(conn, "demo.iso", st);

      while (1) {
           char buf[1024];
           int got = read(fd, buf, 1024);
           if (got < 0) {
              virStreamAbort(st);
              break;
           }
           if (got == 0) {
              virStreamFinish(st);
              break;
           }
           int offset = 0;
           while (offset < got) {
              int sent = virStreamSend(st, buf+offset, got-offset);
              if (sent < 0) {
                 virStreamAbort(st);
                 goto done;
              }
              offset += sent;
           }
       }
       if (virStreamFinish(st) < 0)
          ... report an error ....
     done:
       virStreamFree(st);
       close(fd);

stream
  pointer to the stream object
data
  buffer to write to stream
nbytes
  size of `data` buffer
Returns
  the number of bytes written, which may be less than requested. Returns -1 upon error, at which time the stream will be marked as aborted, and the caller should now release the stream with virStreamFree. Returns -2 if the outgoing transmit buffers are full & the stream is marked as non-blocking.

### `virStreamSendAll`

    int	virStreamSendAll		(virStreamPtr stream,
    					 virStreamSourceFunc handler,
    					 void * opaque)

Send the entire data stream, reading the data from the requested data source. This is simply a convenient alternative to virStreamSend, for apps that do blocking-I/O.

An example using this with a hypothetical file upload API looks like

    int mysource(virStreamPtr st, char *buf, int nbytes, void *opaque) {
        int *fd = opaque;

        return read(*fd, buf, nbytes);
    }

    virStreamPtr st = virStreamNew(conn, 0);
    int fd = open("demo.iso", O_RDONLY);

    virConnectUploadFile(conn, st);
    if (virStreamSendAll(st, mysource, &fd) < 0) {
       ...report an error ...
       goto done;
    }
    if (virStreamFinish(st) < 0)
       ...report an error...
    virStreamFree(st);
    close(fd);

stream
  pointer to the stream object
handler
  source callback for reading data from application
opaque
  application defined data
Returns
  0 if all the data was successfully sent. The caller should invoke virStreamFinish(st) to flush the stream upon success and then virStreamFree Returns -1 upon any error, with virStreamAbort() already having been called, so the caller need only call virStreamFree().

### `virStreamSendHole`

    int	virStreamSendHole		(virStreamPtr stream,
    					 long long length,
    					 unsigned int flags)

Rather than transmitting empty file space, this API directs the `stream` target to create `length` bytes of empty space. This API would be used when uploading or downloading sparsely populated files to avoid the needless copy of empty file space.

An example using this with a hypothetical file upload API looks like:

    virStream st;

    while (1) {
      char buf[4096];
      size_t len;
      if (..in hole...) {
        ..get hole size...
        virStreamSendHole(st, len, 0);
      } else {
        ...read len bytes...
        virStreamSend(st, buf, len);
      }
    }

stream
  pointer to the stream object
length
  number of bytes to skip
flags
  extra flags; not used yet, so callers should always pass 0
Returns
  0 on success, -1 error

### `virStreamSinkFunc`

    typedef int	(*virStreamSinkFunc	)	(virStreamPtr st,
    					 const char * data,
    					 size_t nbytes,
    					 void * opaque)

The virStreamSinkFunc callback is used together with the virStreamRecvAll or virStreamSparseRecvAll functions for libvirt to provide the data that has been received.

The callback will be invoked multiple times, providing data in small chunks. The application should consume up 'nbytes' from the 'data' array of data and then return the number actual number of bytes consumed. The callback will continue to be invoked until it indicates the end of the stream has been reached. A return value of -1 at any time will abort the receive operation

Please note that for more accurate error reporting the callback should set appropriate errno on failure.

st
  the stream object
data
  preallocated array to be filled with data
nbytes
  size of the data array
opaque
  optional application provided data
Returns
  the number of bytes consumed or -1 upon error

### `virStreamSinkHoleFunc`

    typedef int	(*virStreamSinkHoleFunc	)	(virStreamPtr st,
    					 long long length,
    					 void * opaque)

This callback is used together with the virStreamSparseRecvAll function for libvirt to provide the size of a hole that occurred in the stream.

The callback may be invoked multiple times as holes are found during processing a stream. The application should create the hole in the stream target and then return. A return value of -1 at any time will abort the receive operation.

Please note that for more accurate error reporting the callback should set appropriate errno on failure.

st
  the stream object
length
  stream hole size
opaque
  optional application provided data
Returns
  0 on success, -1 upon error

### `virStreamSourceFunc`

    typedef int	(*virStreamSourceFunc	)	(virStreamPtr st,
    					 char * data,
    					 size_t nbytes,
    					 void * opaque)

The virStreamSourceFunc callback is used together with the virStreamSendAll and virStreamSparseSendAll functions for libvirt to obtain the data that is to be sent.

The callback will be invoked multiple times, fetching data in small chunks. The application should fill the 'data' array with up to 'nbytes' of data and then return the number actual number of bytes. The callback will continue to be invoked until it indicates the end of the source has been reached by returning 0. A return value of -1 at any time will abort the send operation.

Please note that for more accurate error reporting the callback should set appropriate errno on failure.

st
  the stream object
data
  preallocated array to be filled with data
nbytes
  size of the data array
opaque
  optional application provided data
Returns
  the number of bytes filled, 0 upon end of file, or -1 upon error

### `virStreamSourceHoleFunc`

    typedef int	(*virStreamSourceHoleFunc	)	(virStreamPtr st,
    					 int * inData,
    					 long long * length,
    					 void * opaque)

The virStreamSourceHoleFunc callback is used together with the virStreamSparseSendAll function for libvirt to obtain the length of section stream is currently in.

Moreover, upon successful return, `length` should be updated with how many bytes are left until the current section ends (either data section or hole section). Also the stream is currently in data section, `inData` should be set to a non-zero value and vice versa.

NB: there's an implicit hole at the end of each file. If that's the case, `inData` and `length` should be both set to 0.

This function should not adjust the current position within the file.

Please note that for more accurate error reporting the callback should set appropriate errno on failure.

st
  the stream object
inData
  are we in data section
length
  how long is the section we are currently in
opaque
  optional application provided data
Returns
  0 on success, -1 upon error

### `virStreamSourceSkipFunc`

    typedef int	(*virStreamSourceSkipFunc	)	(virStreamPtr st,
    					 long long length,
    					 void * opaque)

This callback is used together with the virStreamSparseSendAll to skip holes in the underlying file as reported by virStreamSourceHoleFunc.

The callback may be invoked multiple times as holes are found during processing a stream. The application should skip processing the hole in the stream source and then return. A return value of -1 at any time will abort the send operation.

Please note that for more accurate error reporting the callback should set appropriate errno on failure.

st
  the stream object
length
  stream hole size
opaque
  optional application provided data
Returns
  0 on success, -1 upon error.

### `virStreamSparseRecvAll`

    int	virStreamSparseRecvAll		(virStreamPtr stream,
    					 virStreamSinkFunc handler,
    					 virStreamSinkHoleFunc holeHandler,
    					 void * opaque)

Receive the entire data stream, sending the data to the requested data sink `handler` and calling the skip `holeHandler` to generate holes for sparse stream targets. This is simply a convenient alternative to virStreamRecvFlags, for apps that do blocking-I/O.

An example using this with a hypothetical file download API looks like:

    int mysink(virStreamPtr st, const char *buf, int nbytes, void *opaque) {
        int *fd = opaque;

        return write(*fd, buf, nbytes);
    }

    int myskip(virStreamPtr st, long long offset, void *opaque) {
        int *fd = opaque;

        return lseek(*fd, offset, SEEK_CUR) == (off_t) -1 ? -1 : 0;
    }

    virStreamPtr st = virStreamNew(conn, 0);
    int fd = open("demo.iso", O_WRONLY);

    virConnectDownloadSparseFile(conn, st);
    if (virStreamSparseRecvAll(st, mysink, myskip, &fd) < 0) {
       ...report an error ...
       goto done;
    }
    if (virStreamFinish(st) < 0)
       ...report an error...
    virStreamFree(st);
    close(fd);

Note that `opaque` data is shared between both `handler` and `holeHandler` callbacks.

stream
  pointer to the stream object
handler
  sink callback for writing data to application
holeHandler
  stream hole callback for skipping holes
opaque
  application defined data
Returns
  0 if all the data was successfully received. The caller should invoke virStreamFinish(st) to flush the stream upon success and then virStreamFree(st). Returns -1 upon any error, with virStreamAbort() already having been called, so the caller need only call virStreamFree().

### `virStreamSparseSendAll`

    int	virStreamSparseSendAll		(virStreamPtr stream,
    					 virStreamSourceFunc handler,
    					 virStreamSourceHoleFunc holeHandler,
    					 virStreamSourceSkipFunc skipHandler,
    					 void * opaque)

Send the entire data stream, reading the data from the requested data source. This is simply a convenient alternative to virStreamSend, for apps that do blocking-I/O.

An example using this with a hypothetical file upload API looks like

    int mysource(virStreamPtr st, char *buf, int nbytes, void *opaque) {
        int *fd = opaque;

        return read(*fd, buf, nbytes);
    }

    int myskip(virStreamPtr st, long long offset, void *opaque) {
        int *fd = opaque;

        return lseek(*fd, offset, SEEK_CUR) == (off_t) -1 ? -1 : 0;
    }

    int myindata(virStreamPtr st, int *inData,
                 long long *offset, void *opaque) {
        int *fd = opaque;

        if (@fd in hole) {
            *inData = 0;
            *offset = holeSize;
        } else {
            *inData = 1;
            *offset = dataSize;
        }

        return 0;
    }

    virStreamPtr st = virStreamNew(conn, 0);
    int fd = open("demo.iso", O_RDONLY);

    virConnectUploadSparseFile(conn, st);
    if (virStreamSparseSendAll(st,
                               mysource,
                               myindata,
                               myskip,
                               &fd) < 0) {
       ...report an error ...
       goto done;
    }
    if (virStreamFinish(st) < 0)
       ...report an error...
    virStreamFree(st);
    close(fd);

Note that `opaque` data are shared between `handler`, `holeHandler` and `skipHandler`.

stream
  pointer to the stream object
handler
  source callback for reading data from application
holeHandler
  source callback for determining holes
skipHandler
  skip holes as reported by `holeHandler`
opaque
  application defined data
Returns
  0 if all the data was successfully sent. The caller should invoke virStreamFinish(st) to flush the stream upon success and then virStreamFree. Returns -1 upon any error, with virStreamAbort() already having been called, so the caller need only call virStreamFree().