[Libguestfs] [PATCH] osinfo: simplify reading of attributes from <media>

Pino Toscano ptoscano at redhat.com
Thu Jul 28 14:21:09 UTC 2016


Directly query the attributes in the media node, instead of using an
XPath expression to read it.

Saves ~10ms when parsing the libosinfo DB on a Fedora 24 installation
(other than making the code easier).
---
 src/osinfo.c | 30 ++++++++----------------------
 1 file changed, 8 insertions(+), 22 deletions(-)

diff --git a/src/osinfo.c b/src/osinfo.c
index a5026f3..22f1e92 100644
--- a/src/osinfo.c
+++ b/src/osinfo.c
@@ -74,6 +74,8 @@ static struct osinfo *osinfo_db = NULL;
 static int read_osinfo_db (guestfs_h *g);
 static void free_osinfo_db_entry (struct osinfo *);
 
+#define XMLSTREQ(a,b) (xmlStrEqual((a),(b)) == 1)
+
 /* Given one or more fields from the header of a CD/DVD/ISO, look up
  * the media in the libosinfo database and return our best guess for
  * the operating system.
@@ -483,30 +485,14 @@ static int
 read_media_node (guestfs_h *g, xmlXPathContextPtr xpathCtx,
                  xmlNodePtr media_node, struct osinfo *osinfo)
 {
-  CLEANUP_XMLXPATHFREEOBJECT xmlXPathObjectPtr xp = NULL, xp2 = NULL;
-  xmlNodePtr attr;
-
-  xpathCtx->node = media_node;
-  xp = xmlXPathEvalExpression (BAD_CAST "./@arch", xpathCtx);
-  if (xp && xp->nodesetval && xp->nodesetval->nodeNr > 0) {
-    attr = xp->nodesetval->nodeTab[0];
-    assert (attr);
-    assert (attr->type == XML_ATTRIBUTE_NODE);
-    osinfo->arch = (char *) xmlNodeGetContent (attr);
-  }
+  osinfo->arch = (char *) xmlGetProp (media_node, BAD_CAST "arch");
 
   osinfo->is_live_disk = 0; /* If no 'live' attr, defaults to false. */
-
-  xpathCtx->node = media_node;
-  xp2 = xmlXPathEvalExpression (BAD_CAST "./@live", xpathCtx);
-  if (xp2 && xp2->nodesetval && xp2->nodesetval->nodeNr > 0) {
-    CLEANUP_FREE char *content = NULL;
-
-    attr = xp2->nodesetval->nodeTab[0];
-    assert (attr);
-    assert (attr->type == XML_ATTRIBUTE_NODE);
-    content = (char *) xmlNodeGetContent (attr);
-    osinfo->is_live_disk = STREQ (content, "true");
+  {
+    CLEANUP_XMLFREE xmlChar *content = NULL;
+    content = xmlGetProp (media_node, BAD_CAST "live");
+    if (content)
+      osinfo->is_live_disk = XMLSTREQ (content, BAD_CAST "true");
   }
 
   return 0;
-- 
2.7.4




More information about the Libguestfs mailing list