render / rpms / libvirt

Forked from rpms/libvirt 9 months ago
Clone
c480ed
From 6438640b53fef3e889c21a2ed016638b91c5ac80 Mon Sep 17 00:00:00 2001
c480ed
Message-Id: <6438640b53fef3e889c21a2ed016638b91c5ac80@dist-git>
c480ed
From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= <berrange@redhat.com>
c480ed
Date: Fri, 21 Jun 2019 09:24:44 +0200
c480ed
Subject: [PATCH] cpu: allow include files for CPU definition
c480ed
MIME-Version: 1.0
c480ed
Content-Type: text/plain; charset=UTF-8
c480ed
Content-Transfer-Encoding: 8bit
c480ed
c480ed
Allow for syntax
c480ed
c480ed
    <include filename="subdir/fooo.xml"/>
c480ed
c480ed
to reference other files in the CPU database directory
c480ed
c480ed
Reviewed-by: Jiri Denemark <jdenemar@redhat.com>
c480ed
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
c480ed
(cherry picked from commit eda5f575f2a7530fc7f6471f88496778a9bc9fcb)
c480ed
c480ed
https://bugzilla.redhat.com/show_bug.cgi?id=1686895
c480ed
c480ed
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
c480ed
Message-Id: <800e5ebbc30502fd780a3ef84fe524f1dc0ffd67.1561068591.git.jdenemar@redhat.com>
c480ed
Reviewed-by: Ján Tomko <jtomko@redhat.com>
c480ed
---
c480ed
 src/cpu/cpu_map.c | 92 +++++++++++++++++++++++++++++++++++++++++++++--
c480ed
 1 file changed, 89 insertions(+), 3 deletions(-)
c480ed
c480ed
diff --git a/src/cpu/cpu_map.c b/src/cpu/cpu_map.c
c480ed
index d263eb8cdd..333c7ef24f 100644
c480ed
--- a/src/cpu/cpu_map.c
c480ed
+++ b/src/cpu/cpu_map.c
c480ed
@@ -1,7 +1,7 @@
c480ed
 /*
c480ed
  * cpu_map.c: internal functions for handling CPU mapping configuration
c480ed
  *
c480ed
- * Copyright (C) 2009-2010 Red Hat, Inc.
c480ed
+ * Copyright (C) 2009-2018 Red Hat, Inc.
c480ed
  *
c480ed
  * This library is free software; you can redistribute it and/or
c480ed
  * modify it under the terms of the GNU Lesser General Public
c480ed
@@ -70,6 +70,89 @@ static int load(xmlXPathContextPtr ctxt,
c480ed
     return ret;
c480ed
 }
c480ed
 
c480ed
+static int
c480ed
+cpuMapLoadInclude(const char *filename,
c480ed
+                  cpuMapLoadCallback cb,
c480ed
+                  void *data)
c480ed
+{
c480ed
+    xmlDocPtr xml = NULL;
c480ed
+    xmlXPathContextPtr ctxt = NULL;
c480ed
+    int ret = -1;
c480ed
+    int element;
c480ed
+    char *mapfile;
c480ed
+
c480ed
+    if (!(mapfile = virFileFindResource(filename,
c480ed
+                                        abs_topsrcdir "/src/cpu",
c480ed
+                                        PKGDATADIR)))
c480ed
+        return -1;
c480ed
+
c480ed
+    VIR_DEBUG("Loading CPU map include from %s", mapfile);
c480ed
+
c480ed
+    if (!(xml = virXMLParseFileCtxt(mapfile, &ctxt)))
c480ed
+        goto cleanup;
c480ed
+
c480ed
+    ctxt->node = xmlDocGetRootElement(xml);
c480ed
+
c480ed
+    for (element = 0; element < CPU_MAP_ELEMENT_LAST; element++) {
c480ed
+        if (load(ctxt, element, cb, data) < 0) {
c480ed
+            virReportError(VIR_ERR_INTERNAL_ERROR,
c480ed
+                           _("cannot parse CPU map '%s'"), mapfile);
c480ed
+            goto cleanup;
c480ed
+        }
c480ed
+    }
c480ed
+
c480ed
+    ret = 0;
c480ed
+
c480ed
+ cleanup:
c480ed
+    xmlXPathFreeContext(ctxt);
c480ed
+    xmlFreeDoc(xml);
c480ed
+    VIR_FREE(mapfile);
c480ed
+
c480ed
+    return ret;
c480ed
+}
c480ed
+
c480ed
+
c480ed
+static int
c480ed
+loadIncludes(xmlXPathContextPtr ctxt,
c480ed
+             cpuMapLoadCallback callback,
c480ed
+             void *data)
c480ed
+{
c480ed
+    int ret = -1;
c480ed
+    xmlNodePtr ctxt_node;
c480ed
+    xmlNodePtr *nodes = NULL;
c480ed
+    int n;
c480ed
+    size_t i;
c480ed
+
c480ed
+    ctxt_node = ctxt->node;
c480ed
+
c480ed
+    n = virXPathNodeSet("include", ctxt, &nodes);
c480ed
+    if (n < 0)
c480ed
+        goto cleanup;
c480ed
+
c480ed
+    for (i = 0; i < n; i++) {
c480ed
+        char *filename = virXMLPropString(nodes[i], "filename");
c480ed
+        if (!filename) {
c480ed
+            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
c480ed
+                           _("Missing 'filename' in CPU map include"));
c480ed
+            goto cleanup;
c480ed
+        }
c480ed
+        VIR_DEBUG("Finding CPU map include '%s'", filename);
c480ed
+        if (cpuMapLoadInclude(filename, callback, data) < 0) {
c480ed
+            VIR_FREE(filename);
c480ed
+            goto cleanup;
c480ed
+        }
c480ed
+        VIR_FREE(filename);
c480ed
+    }
c480ed
+
c480ed
+    ret = 0;
c480ed
+
c480ed
+ cleanup:
c480ed
+    ctxt->node = ctxt_node;
c480ed
+    VIR_FREE(nodes);
c480ed
+
c480ed
+    return ret;
c480ed
+}
c480ed
+
c480ed
 
c480ed
 int cpuMapLoad(const char *arch,
c480ed
                cpuMapLoadCallback cb,
c480ed
@@ -88,7 +171,7 @@ int cpuMapLoad(const char *arch,
c480ed
                                         PKGDATADIR)))
c480ed
         return -1;
c480ed
 
c480ed
-    VIR_DEBUG("Loading CPU map from %s", mapfile);
c480ed
+    VIR_DEBUG("Loading '%s' CPU map from %s", NULLSTR(arch), mapfile);
c480ed
 
c480ed
     if (arch == NULL) {
c480ed
         virReportError(VIR_ERR_INTERNAL_ERROR,
c480ed
@@ -122,11 +205,14 @@ int cpuMapLoad(const char *arch,
c480ed
     for (element = 0; element < CPU_MAP_ELEMENT_LAST; element++) {
c480ed
         if (load(ctxt, element, cb, data) < 0) {
c480ed
             virReportError(VIR_ERR_INTERNAL_ERROR,
c480ed
-                           _("cannot parse CPU map for %s architecture"), arch);
c480ed
+                           _("cannot parse CPU map '%s'"), mapfile);
c480ed
             goto cleanup;
c480ed
         }
c480ed
     }
c480ed
 
c480ed
+    if (loadIncludes(ctxt, cb, data) < 0)
c480ed
+        goto cleanup;
c480ed
+
c480ed
     ret = 0;
c480ed
 
c480ed
  cleanup:
c480ed
-- 
c480ed
2.22.0
c480ed