[PATCH v2 07/32] Introduce virStreamSparseSendAll

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Signed-off-by: Michal Privoznik <mprivozn@xxxxxxxxxx>
---
 include/libvirt/libvirt-stream.h |  43 ++++++++++++++--
 src/libvirt-stream.c             | 104 +++++++++++++++++++++++++++++++++++++++
 src/libvirt_public.syms          |   1 +
 3 files changed, 145 insertions(+), 3 deletions(-)

diff --git a/include/libvirt/libvirt-stream.h b/include/libvirt/libvirt-stream.h
index e5f5126..9aa728e 100644
--- a/include/libvirt/libvirt-stream.h
+++ b/include/libvirt/libvirt-stream.h
@@ -69,9 +69,9 @@ int virStreamHoleSize(virStreamPtr,
  * @nbytes: size of the data array
  * @opaque: optional application provided data
  *
- * The virStreamSourceFunc callback is used together
- * with the virStreamSendAll function for libvirt to
- * obtain the data that is to be sent.
+ * 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
@@ -95,6 +95,43 @@ int virStreamSendAll(virStreamPtr st,
                      void *opaque);
 
 /**
+ * virStreamSourceHoleFunc:
+ * @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
+ *
+ * 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 much bytes are there left until current
+ * section ends (be it data section or hole section) and if
+ * the stream is currently in data section, @inData should
+ * be set to a non-zero value and vice versa.
+ * As a corner case, there's an implicit hole at the end of
+ * each file. If that's the case, @inData should be set to 0
+ * as well as @length.
+ * Moreover, this function should always leave the stream in
+ * data section. Either the one that we have been to prior
+ * calling this function, or the one that follows the hole
+ * we are in.
+ *
+ * Returns 0 on success,
+ *        -1 upon error
+ */
+typedef int (*virStreamSourceHoleFunc)(virStreamPtr st,
+                                       int *inData,
+                                       unsigned long long *length,
+                                       void *opaque);
+
+int virStreamSparseSendAll(virStreamPtr st,
+                           virStreamSourceFunc handler,
+                           virStreamSourceHoleFunc holeHandler,
+                           void *opaque);
+
+/**
  * virStreamSinkFunc:
  *
  * @st: the stream object
diff --git a/src/libvirt-stream.c b/src/libvirt-stream.c
index 2e3e319..707c0ed 100644
--- a/src/libvirt-stream.c
+++ b/src/libvirt-stream.c
@@ -565,7 +565,111 @@ virStreamSendAll(virStreamPtr stream,
 }
 
 
+
 /**
+ * virStreamSparseSendAll:
+ * @stream: pointer to the stream object
+ * @handler: source callback for reading data from application
+ * @holeHandler: source callback for determining holes
+ * @opaque: application defined data
+ *
+ * Some dummy description here.
+ *
+ * Opaque data in @opaque are shared between @handler and @holeHandler.
+ *
+ * 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()
+ */
+int virStreamSparseSendAll(virStreamPtr stream,
+                           virStreamSourceFunc handler,
+                           virStreamSourceHoleFunc holeHandler,
+                           void *opaque)
+{
+    char *bytes = NULL;
+    size_t want = VIR_NET_MESSAGE_LEGACY_PAYLOAD_MAX;
+    int ret = -1;
+    unsigned long long dataLen = 0;
+
+    VIR_DEBUG("stream=%p handler=%p holeHandler=%p opaque=%p",
+              stream, handler, holeHandler, opaque);
+
+    virResetLastError();
+
+    virCheckStreamReturn(stream, -1);
+    virCheckNonNullArgGoto(handler, cleanup);
+    virCheckNonNullArgGoto(holeHandler, cleanup);
+
+    if (stream->flags & VIR_STREAM_NONBLOCK) {
+        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
+                       _("data sources cannot be used for non-blocking streams"));
+        goto cleanup;
+    }
+
+    if (VIR_ALLOC_N(bytes, want) < 0)
+        goto cleanup;
+
+    for (;;) {
+        int inData, got, offset = 0;
+        unsigned long long sectionLen;
+
+        if (!dataLen) {
+            if (holeHandler(stream, &inData, &sectionLen, opaque) < 0) {
+                virStreamAbort(stream);
+                goto cleanup;
+            }
+
+            if (!inData) {
+                if (sectionLen && virStreamSkip(stream, sectionLen) < 0) {
+                    virStreamAbort(stream);
+                    goto cleanup;
+                }
+
+                if (sectionLen)
+                    continue;
+
+                /* Here inData == 0 and sectionLen == 0 as well.
+                 * This means, we are in the trailing hole. Don't
+                 * start new iteration but let virStreamSend()
+                 * close the stream gracefully. */
+            } else {
+                dataLen = sectionLen;
+            }
+        }
+
+        if (want > dataLen)
+            want = dataLen;
+
+        got = (handler)(stream, bytes, want, opaque);
+        if (got < 0) {
+            virStreamAbort(stream);
+            goto cleanup;
+        }
+        if (got == 0)
+            break;
+        while (offset < got) {
+            int done;
+            done = virStreamSend(stream, bytes + offset, got - offset);
+            if (done < 0)
+                goto cleanup;
+            offset += done;
+            dataLen -= done;
+        }
+    }
+    ret = 0;
+
+ cleanup:
+    VIR_FREE(bytes);
+
+    if (ret != 0)
+        virDispatchError(stream->conn);
+
+    return ret;
+}/**
  * virStreamRecvAll:
  * @stream: pointer to the stream object
  * @handler: sink callback for writing data to application
diff --git a/src/libvirt_public.syms b/src/libvirt_public.syms
index 2e7a9a7..575bb7f 100644
--- a/src/libvirt_public.syms
+++ b/src/libvirt_public.syms
@@ -738,6 +738,7 @@ LIBVIRT_1.3.5 {
         virStreamRecvFlags;
         virStreamSkip;
         virStreamSparseRecvAll;
+        virStreamSparseSendAll;
 } LIBVIRT_1.3.3;
 
 
-- 
2.8.3

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]