[libvirt] [PATCH V2 03/13] libxl: move libxlSaveImageOpen to libxl_domain

Jim Fehlig jfehlig at suse.com
Thu Mar 13 22:11:08 UTC 2014


Move libxlSaveImageOpen from libxl_driver to libxl_domain for
use by other libxl modules.  For consistency, rename to
libxlDomainSaveImageOpen.

Signed-off-by: Jim Fehlig <jfehlig at suse.com>
---
 src/libxl/libxl_domain.c | 76 +++++++++++++++++++++++++++++++++++++++++++++
 src/libxl/libxl_domain.h |  8 +++++
 src/libxl/libxl_driver.c | 80 +++---------------------------------------------
 3 files changed, 88 insertions(+), 76 deletions(-)

diff --git a/src/libxl/libxl_domain.c b/src/libxl/libxl_domain.c
index 04f26f8..eaa4d10 100644
--- a/src/libxl/libxl_domain.c
+++ b/src/libxl/libxl_domain.c
@@ -23,6 +23,8 @@
 
 #include <config.h>
 
+#include <fcntl.h>
+
 #include "libxl_domain.h"
 
 #include "viralloc.h"
@@ -564,3 +566,77 @@ libxlDomainManagedSavePath(libxlDriverPrivatePtr driver, virDomainObjPtr vm) {
     virObjectUnref(cfg);
     return ret;
 }
+
+/*
+ * Open a saved image file and initialize domain definition from the header.
+ *
+ * Returns the opened fd on success, -1 on failure.
+ */
+int
+libxlDomainSaveImageOpen(libxlDriverPrivatePtr driver,
+                         libxlDriverConfigPtr cfg,
+                         const char *from,
+                         virDomainDefPtr *ret_def,
+                         libxlSavefileHeaderPtr ret_hdr)
+{
+    int fd;
+    virDomainDefPtr def = NULL;
+    libxlSavefileHeader hdr;
+    char *xml = NULL;
+
+    if ((fd = virFileOpenAs(from, O_RDONLY, 0, -1, -1, 0)) < 0) {
+        virReportSystemError(-fd,
+                             _("Failed to open domain image file '%s'"), from);
+        goto error;
+    }
+
+    if (saferead(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) {
+        virReportError(VIR_ERR_OPERATION_FAILED,
+                       "%s", _("failed to read libxl header"));
+        goto error;
+    }
+
+    if (memcmp(hdr.magic, LIBXL_SAVE_MAGIC, sizeof(hdr.magic))) {
+        virReportError(VIR_ERR_INVALID_ARG, "%s", _("image magic is incorrect"));
+        goto error;
+    }
+
+    if (hdr.version > LIBXL_SAVE_VERSION) {
+        virReportError(VIR_ERR_OPERATION_FAILED,
+                       _("image version is not supported (%d > %d)"),
+                       hdr.version, LIBXL_SAVE_VERSION);
+        goto error;
+    }
+
+    if (hdr.xmlLen <= 0) {
+        virReportError(VIR_ERR_OPERATION_FAILED,
+                       _("invalid XML length: %d"), hdr.xmlLen);
+        goto error;
+    }
+
+    if (VIR_ALLOC_N(xml, hdr.xmlLen) < 0)
+        goto error;
+
+    if (saferead(fd, xml, hdr.xmlLen) != hdr.xmlLen) {
+        virReportError(VIR_ERR_OPERATION_FAILED, "%s", _("failed to read XML"));
+        goto error;
+    }
+
+    if (!(def = virDomainDefParseString(xml, cfg->caps, driver->xmlopt,
+                                        1 << VIR_DOMAIN_VIRT_XEN,
+                                        VIR_DOMAIN_XML_INACTIVE)))
+        goto error;
+
+    VIR_FREE(xml);
+
+    *ret_def = def;
+    *ret_hdr = hdr;
+
+    return fd;
+
+error:
+    VIR_FREE(xml);
+    virDomainDefFree(def);
+    VIR_FORCE_CLOSE(fd);
+    return -1;
+}
diff --git a/src/libxl/libxl_domain.h b/src/libxl/libxl_domain.h
index 5558009..3c1f5c0 100644
--- a/src/libxl/libxl_domain.h
+++ b/src/libxl/libxl_domain.h
@@ -99,4 +99,12 @@ char *
 libxlDomainManagedSavePath(libxlDriverPrivatePtr driver,
                            virDomainObjPtr vm);
 
+int
+libxlDomainSaveImageOpen(libxlDriverPrivatePtr driver,
+                         libxlDriverConfigPtr cfg,
+                         const char *from,
+                         virDomainDefPtr *ret_def,
+                         libxlSavefileHeaderPtr ret_hdr)
+    ATTRIBUTE_NONNULL(4) ATTRIBUTE_NONNULL(5);
+
 #endif /* LIBXL_DOMAIN_H */
diff --git a/src/libxl/libxl_driver.c b/src/libxl/libxl_driver.c
index db7e954..c621f43 100644
--- a/src/libxl/libxl_driver.c
+++ b/src/libxl/libxl_driver.c
@@ -164,78 +164,6 @@ cleanup:
     return ret;
 }
 
-/*
- * This internal function expects the driver lock to already be held on
- * entry.
- */
-static int ATTRIBUTE_NONNULL(4) ATTRIBUTE_NONNULL(5)
-libxlSaveImageOpen(libxlDriverPrivatePtr driver,
-                   libxlDriverConfigPtr cfg,
-                   const char *from,
-                   virDomainDefPtr *ret_def,
-                   libxlSavefileHeaderPtr ret_hdr)
-{
-    int fd;
-    virDomainDefPtr def = NULL;
-    libxlSavefileHeader hdr;
-    char *xml = NULL;
-
-    if ((fd = virFileOpenAs(from, O_RDONLY, 0, -1, -1, 0)) < 0) {
-        virReportSystemError(-fd,
-                             _("Failed to open domain image file '%s'"), from);
-        goto error;
-    }
-
-    if (saferead(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) {
-        virReportError(VIR_ERR_OPERATION_FAILED,
-                       "%s", _("failed to read libxl header"));
-        goto error;
-    }
-
-    if (memcmp(hdr.magic, LIBXL_SAVE_MAGIC, sizeof(hdr.magic))) {
-        virReportError(VIR_ERR_INVALID_ARG, "%s", _("image magic is incorrect"));
-        goto error;
-    }
-
-    if (hdr.version > LIBXL_SAVE_VERSION) {
-        virReportError(VIR_ERR_OPERATION_FAILED,
-                       _("image version is not supported (%d > %d)"),
-                       hdr.version, LIBXL_SAVE_VERSION);
-        goto error;
-    }
-
-    if (hdr.xmlLen <= 0) {
-        virReportError(VIR_ERR_OPERATION_FAILED,
-                       _("invalid XML length: %d"), hdr.xmlLen);
-        goto error;
-    }
-
-    if (VIR_ALLOC_N(xml, hdr.xmlLen) < 0)
-        goto error;
-
-    if (saferead(fd, xml, hdr.xmlLen) != hdr.xmlLen) {
-        virReportError(VIR_ERR_OPERATION_FAILED, "%s", _("failed to read XML"));
-        goto error;
-    }
-
-    if (!(def = virDomainDefParseString(xml, cfg->caps, driver->xmlopt,
-                                        1 << VIR_DOMAIN_VIRT_XEN,
-                                        VIR_DOMAIN_XML_INACTIVE)))
-        goto error;
-
-    VIR_FREE(xml);
-
-    *ret_def = def;
-    *ret_hdr = hdr;
-
-    return fd;
-
-error:
-    VIR_FREE(xml);
-    virDomainDefFree(def);
-    VIR_FORCE_CLOSE(fd);
-    return -1;
-}
 
 /*
  * Core dump domain to default dump path.
@@ -708,9 +636,9 @@ libxlVmStart(libxlDriverPrivatePtr driver, virDomainObjPtr vm,
 
         if (virFileExists(managed_save_path)) {
 
-            managed_save_fd = libxlSaveImageOpen(driver, cfg,
-                                                 managed_save_path,
-                                                 &def, &hdr);
+            managed_save_fd = libxlDomainSaveImageOpen(driver, cfg,
+                                                       managed_save_path,
+                                                       &def, &hdr);
             if (managed_save_fd < 0)
                 goto endjob;
 
@@ -2133,7 +2061,7 @@ libxlDomainRestoreFlags(virConnectPtr conn, const char *from,
         return -1;
     }
 
-    fd = libxlSaveImageOpen(driver, cfg, from, &def, &hdr);
+    fd = libxlDomainSaveImageOpen(driver, cfg, from, &def, &hdr);
     if (fd < 0)
         goto cleanup_unlock;
 
-- 
1.8.1.4




More information about the libvir-list mailing list