And switch fdstream to call them.
Signed-off-by: Michal Privoznik <mprivozn(a)redhat.com>
---
po/POTFILES.in | 1 +
src/Makefile.am | 5 ++-
src/fdstream.c | 29 +++++++------
src/iohelper/iohelper_message.c | 94 +++++++++++++++++++++++++++++++++++++++++
src/iohelper/iohelper_message.h | 26 ++++++++++++
5 files changed, 139 insertions(+), 16 deletions(-)
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 9f4866c..7f40200 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -64,6 +64,7 @@ src/interface/interface_backend_netcf.c
src/interface/interface_backend_udev.c
src/internal.h
src/iohelper/iohelper.c
+src/iohelper/iohelper_message.c
src/libvirt-admin.c
src/libvirt-domain-snapshot.c
src/libvirt-domain.c
diff --git a/src/Makefile.am b/src/Makefile.am
index af5ef72..1cce603 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1170,9 +1170,10 @@ libvirt_driver_la_SOURCES = $(DRIVER_SOURCES)
libvirt_driver_la_CFLAGS = \
$(GNUTLS_CFLAGS) $(CURL_CFLAGS) \
- -I$(srcdir)/conf $(AM_CFLAGS)
+ -I$(srcdir)/conf -I$(srcdir)/iohelper $(AM_CFLAGS)
libvirt_driver_la_LIBADD = \
- $(GNUTLS_LIBS) $(CURL_LIBS) $(DLOPEN_LIBS)
+ $(GNUTLS_LIBS) $(CURL_LIBS) $(DLOPEN_LIBS) \
+ libvirt-iohelper.la
# All .syms files should be placed in exactly one of these three lists,
# depending on whether they are stored in git and/or used in the build.
diff --git a/src/fdstream.c b/src/fdstream.c
index 8ab7cd5..dc41164 100644
--- a/src/fdstream.c
+++ b/src/fdstream.c
@@ -44,6 +44,7 @@
#include "virstring.h"
#include "virtime.h"
#include "virprocess.h"
+#include "iohelper_message.h"
#define VIR_FROM_THIS VIR_FROM_STREAMS
@@ -61,6 +62,7 @@ struct virFDStreamData {
unsigned long long offset;
unsigned long long length;
bool formatted; /* True if formatted messages are read from/written to @fd. */
+ iohelperCtlPtr ioCtl;
int watch;
int events; /* events the stream callback is subscribed for */
@@ -89,7 +91,7 @@ virFDStreamDataDispose(void *obj)
{
virFDStreamDataPtr fdst = obj;
- VIR_DEBUG("obj=%p", fdst);
+ virObjectUnref(fdst->ioCtl);
}
static int virFDStreamDataOnceInit(void)
@@ -405,9 +407,7 @@ virFDStreamWriteInternal(virFDStreamDataPtr fdst,
size_t nbytes)
{
if (fdst->formatted) {
- virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
- _("sparse stream not supported"));
- return -1;
+ return iohelperWrite(fdst->ioCtl, bytes, nbytes);
} else {
return write(fdst->fd, bytes, nbytes);
}
@@ -474,9 +474,7 @@ virFDStreamReadInternal(virFDStreamDataPtr fdst,
size_t nbytes)
{
if (fdst->formatted) {
- virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
- _("sparse stream not supported"));
- return -1;
+ return iohelperRead(fdst->ioCtl, bytes, nbytes);
} else {
return read(fdst->fd, bytes, nbytes);
}
@@ -541,9 +539,7 @@ virFDStreamSkipInternal(virFDStreamDataPtr fdst,
{
off_t off;
if (fdst->formatted) {
- virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
- _("sparse stream not supported"));
- return -1;
+ return iohelperSkip(fdst->ioCtl, length);
} else {
off = lseek(fdst->fd, length, SEEK_CUR);
return off == (off_t) -1 ? -1 : 0;
@@ -586,9 +582,7 @@ virFDStreamInDataInternal(virFDStreamDataPtr fdst,
unsigned long long *length)
{
if (fdst->formatted) {
- virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
- _("sparse stream not supported"));
- return -1;
+ return iohelperInData(fdst->ioCtl, inData, length);
} else {
return virFileInData(fdst->fd, inData, length);
}
@@ -651,11 +645,18 @@ static int virFDStreamOpenInternal(virStreamPtr st,
fdst->errfd = errfd;
fdst->length = length;
fdst->formatted = formatted;
+ if (formatted &&
+ !(fdst->ioCtl = iohelperCtlNew(fd)))
+ goto error;
+
st->driver = &virFDStreamDrv;
st->privateData = fdst;
-
return 0;
+
+ error:
+ virObjectUnref(fdst);
+ return -1;
}
diff --git a/src/iohelper/iohelper_message.c b/src/iohelper/iohelper_message.c
index 54b9355..51b283d 100644
--- a/src/iohelper/iohelper_message.c
+++ b/src/iohelper/iohelper_message.c
@@ -22,3 +22,97 @@
#include <config.h>
#include "iohelper_message.h"
+#include "virobject.h"
+#include "virlog.h"
+
+#define VIR_FROM_THIS VIR_FROM_STREAMS
+
+VIR_LOG_INIT("iohelperCtl");
+
+struct iohelperCtl {
+ virObject parent;
+
+ int fd;
+};
+
+static virClassPtr iohelperCtlClass;
+
+static void
+iohelperCtlDispose(void *obj)
+{
+ iohelperCtlPtr ctl = obj;
+
+ VIR_DEBUG("obj = %p", ctl);
+}
+
+static int iohelperCtlOnceInit(void)
+{
+ if (!(iohelperCtlClass = virClassNew(virClassForObject(),
+ "iohelperCtl",
+ sizeof(iohelperCtl),
+ iohelperCtlDispose)))
+ return -1;
+
+ return 0;
+}
+
+VIR_ONCE_GLOBAL_INIT(iohelperCtl)
+
+iohelperCtlPtr
+iohelperCtlNew(int fd)
+{
+ iohelperCtlPtr ret;
+
+ if (iohelperCtlInitialize() < 0)
+ return NULL;
+
+ if (!(ret = virObjectNew(iohelperCtlClass)))
+ return NULL;
+
+ ret->fd = fd;
+
+ return ret;
+}
+
+
+ssize_t
+iohelperRead(iohelperCtlPtr ctl ATTRIBUTE_UNUSED,
+ char *bytes ATTRIBUTE_UNUSED,
+ size_t nbytes ATTRIBUTE_UNUSED)
+{
+ virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+ _("sparse stream not supported"));
+ return -1;
+}
+
+
+ssize_t
+iohelperWrite(iohelperCtlPtr ctl ATTRIBUTE_UNUSED,
+ const char *bytes ATTRIBUTE_UNUSED,
+ size_t nbytes ATTRIBUTE_UNUSED)
+{
+ virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+ _("sparse stream not supported"));
+ return -1;
+}
+
+
+int
+iohelperSkip(iohelperCtlPtr ctl ATTRIBUTE_UNUSED,
+ unsigned long long length ATTRIBUTE_UNUSED)
+{
+ virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+ _("sparse stream not supported"));
+ return -1;
+}
+
+
+int
+iohelperInData(iohelperCtlPtr ctl ATTRIBUTE_UNUSED,
+ int *inData ATTRIBUTE_UNUSED,
+ unsigned long long *length ATTRIBUTE_UNUSED)
+{
+ virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+ _("sparse stream not supported"));
+ return -1;
+}
diff --git a/src/iohelper/iohelper_message.h b/src/iohelper/iohelper_message.h
index 791a645..74afd49 100644
--- a/src/iohelper/iohelper_message.h
+++ b/src/iohelper/iohelper_message.h
@@ -23,4 +23,30 @@
#ifndef __VIR_IOHELPER_MESSAGE_H__
# define __VIR_IOHELPER_MESSAGE_H__
+# include "internal.h"
+
+typedef struct iohelperCtl iohelperCtl;
+typedef iohelperCtl *iohelperCtlPtr;
+
+iohelperCtlPtr iohelperCtlNew(int fd);
+
+ssize_t
+iohelperRead(iohelperCtlPtr ctl,
+ char *bytes,
+ size_t nbytes);
+
+ssize_t
+iohelperWrite(iohelperCtlPtr ctl,
+ const char *bytes,
+ size_t nbytes);
+
+int
+iohelperSkip(iohelperCtlPtr ctl,
+ unsigned long long length);
+
+int
+iohelperInData(iohelperCtlPtr ctl,
+ int *inData,
+ unsigned long long *length);
+
#endif /* __VIR_IOHELPER_H__ */
--
2.8.4