Introduce VIR_STREAM_RECV_STOP_AT_HOLE flag

Add a new flag to virStreamRecvFlags in order to handle being able to
stop reading from the stream so that the consumer can generate a "hole"
in stream target. Generation of a hole replaces the need to receive and
handle a sequence of zero bytes for sparse stream targets.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Michal Privoznik 2016-05-20 15:12:59 +02:00
parent b6aedd2f05
commit 87e4a842b7
2 changed files with 37 additions and 1 deletions

View File

@ -45,6 +45,10 @@ int virStreamRecv(virStreamPtr st,
char *data,
size_t nbytes);
typedef enum {
VIR_STREAM_RECV_STOP_AT_HOLE = (1 << 0),
} virStreamRecvFlagsValues;
int virStreamRecvFlags(virStreamPtr st,
char *data,
size_t nbytes,

View File

@ -290,7 +290,7 @@ virStreamRecv(virStreamPtr stream,
* @stream: pointer to the stream object
* @data: buffer to read into from stream
* @nbytes: size of @data buffer
* @flags: extra flags; not used yet, so callers should always pass 0
* @flags: bitwise-OR of virStreamRecvFlagsValues
*
* Reads a series of bytes from the stream. This method may
* block the calling application for an arbitrary amount
@ -300,6 +300,33 @@ virStreamRecv(virStreamPtr stream,
* @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...
* }
* }
*
* Returns 0 when the end of the stream is reached, at
* which time the caller should invoke virStreamFinish()
* to get confirmation of stream completion.
@ -310,6 +337,9 @@ virStreamRecv(virStreamPtr stream,
*
* 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.
*/
int
virStreamRecvFlags(virStreamPtr stream,
@ -331,6 +361,8 @@ virStreamRecvFlags(virStreamPtr stream,
ret = (stream->driver->streamRecvFlags)(stream, data, nbytes, flags);
if (ret == -2)
return -2;
if (ret == -3)
return -3;
if (ret < 0)
goto error;
return ret;