[PATCH v2 10/17] virsh: Pass virshStreamCallbackDataPtr to virshStreamSink() and virshStreamSkip()

Michal Privoznik posted 17 patches 5 years, 7 months ago
There is a newer version of this series
[PATCH v2 10/17] virsh: Pass virshStreamCallbackDataPtr to virshStreamSink() and virshStreamSkip()
Posted by Michal Privoznik 5 years, 7 months ago
These callback will need to know more that the FD they are
working on. Pass the structure that is passed to other stream
callbacks (e.g. virshStreamSource() or virshStreamSourceSkip())
instead of inventing a new one.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
---
 tools/virsh-util.c   | 10 +++++-----
 tools/virsh-volume.c |  6 +++++-
 2 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/tools/virsh-util.c b/tools/virsh-util.c
index 932d6d0849..89f15efd08 100644
--- a/tools/virsh-util.c
+++ b/tools/virsh-util.c
@@ -146,9 +146,9 @@ virshStreamSink(virStreamPtr st G_GNUC_UNUSED,
                 size_t nbytes,
                 void *opaque)
 {
-    int *fd = opaque;
+    virshStreamCallbackDataPtr cbData = opaque;
 
-    return safewrite(*fd, bytes, nbytes);
+    return safewrite(cbData->fd, bytes, nbytes);
 }
 
 
@@ -186,13 +186,13 @@ virshStreamSkip(virStreamPtr st G_GNUC_UNUSED,
                 long long offset,
                 void *opaque)
 {
-    int *fd = opaque;
+    virshStreamCallbackDataPtr cbData = opaque;
     off_t cur;
 
-    if ((cur = lseek(*fd, offset, SEEK_CUR)) == (off_t) -1)
+    if ((cur = lseek(cbData->fd, offset, SEEK_CUR)) == (off_t) -1)
         return -1;
 
-    if (ftruncate(*fd, cur) < 0)
+    if (ftruncate(cbData->fd, cur) < 0)
         return -1;
 
     return 0;
diff --git a/tools/virsh-volume.c b/tools/virsh-volume.c
index 72394915d8..5cbc2efb7a 100644
--- a/tools/virsh-volume.c
+++ b/tools/virsh-volume.c
@@ -793,6 +793,7 @@ cmdVolDownload(vshControl *ctl, const vshCmd *cmd)
     unsigned long long offset = 0, length = 0;
     bool created = false;
     virshControlPtr priv = ctl->privData;
+    virshStreamCallbackData cbData;
     unsigned int flags = 0;
 
     if (vshCommandOptULongLong(ctl, cmd, "offset", &offset) < 0)
@@ -820,6 +821,9 @@ cmdVolDownload(vshControl *ctl, const vshCmd *cmd)
         created = true;
     }
 
+    cbData.ctl = ctl;
+    cbData.fd = fd;
+
     if (!(st = virStreamNew(priv->conn, 0))) {
         vshError(ctl, _("cannot create a new stream"));
         goto cleanup;
@@ -830,7 +834,7 @@ cmdVolDownload(vshControl *ctl, const vshCmd *cmd)
         goto cleanup;
     }
 
-    if (virStreamSparseRecvAll(st, virshStreamSink, virshStreamSkip, &fd) < 0) {
+    if (virStreamSparseRecvAll(st, virshStreamSink, virshStreamSkip, &cbData) < 0) {
         vshError(ctl, _("cannot receive data from volume %s"), name);
         goto cleanup;
     }
-- 
2.26.2

Re: [PATCH v2 10/17] virsh: Pass virshStreamCallbackDataPtr to virshStreamSink() and virshStreamSkip()
Posted by Peter Krempa 5 years, 5 months ago
On Tue, Jul 07, 2020 at 21:46:28 +0200, Michal Privoznik wrote:
> These callback will need to know more that the FD they are
> working on. Pass the structure that is passed to other stream
> callbacks (e.g. virshStreamSource() or virshStreamSourceSkip())
> instead of inventing a new one.
> 
> Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
> ---
>  tools/virsh-util.c   | 10 +++++-----
>  tools/virsh-volume.c |  6 +++++-
>  2 files changed, 10 insertions(+), 6 deletions(-)

Reviewed-by: Peter Krempa <pkrempa@redhat.com>