]> xenbits.xensource.com Git - people/liuw/libxenctrl-split/libvirt.git/commitdiff
Improve virConf parse to handle LXC config format
authorCédric Bosdonnat <cbosdonnat@suse.com>
Wed, 5 Feb 2014 14:09:59 +0000 (15:09 +0100)
committerDaniel P. Berrange <berrange@redhat.com>
Wed, 12 Feb 2014 17:52:46 +0000 (17:52 +0000)
virConf now honours a VIR_CONF_FLAG_LXC_FORMAT flag to handle LXC
configuration files. The differences are that property names can
contain '.' character and values are all strings without any bounding
quotes.

Provide a new virConfWalk function calling a handler on all non-comment
values. This function will be used by the LXC conversion code to loop
over LXC configuration lines.

src/libvirt_private.syms
src/util/virconf.c
src/util/virconf.h

index 2c9536aa9a041d6bf7b08ab5426016d5cfcb64a7..0b28bac26ca932b192fd18162ea6e562f3003024 100644 (file)
@@ -1130,6 +1130,7 @@ virConfNew;
 virConfReadFile;
 virConfReadMem;
 virConfSetValue;
+virConfWalk;
 virConfWriteFile;
 virConfWriteMem;
 
index e882d150752cc813fdb56f56f6ef8543fad1148d..63aa56918db8deb826e0cd470bf0fabd1ab65efc 100644 (file)
@@ -429,6 +429,16 @@ virConfParseString(virConfParserCtxtPtr ctxt)
         if (VIR_STRNDUP(ret, base, ctxt->cur - base) < 0)
             return NULL;
         NEXT;
+    } else if (ctxt->conf->flags & VIR_CONF_FLAG_LXC_FORMAT) {
+        base = ctxt->cur;
+        /* LXC config format doesn't support comments after the value */
+        while ((ctxt->cur < ctxt->end) && (!IS_EOL(CUR)))
+            NEXT;
+        /* Reverse to exclude the trailing blanks from the value */
+        while ((ctxt->cur > base) && (c_isblank(CUR)))
+            ctxt->cur--;
+        if (VIR_STRNDUP(ret, base, ctxt->cur - base) < 0)
+            return NULL;
     }
     return ret;
 }
@@ -454,7 +464,8 @@ virConfParseValue(virConfParserCtxtPtr ctxt)
         virConfError(ctxt, VIR_ERR_CONF_SYNTAX, _("expecting a value"));
         return NULL;
     }
-    if ((CUR == '"') || (CUR == '\'')) {
+    if ((CUR == '"') || (CUR == '\'') ||
+         (ctxt->conf->flags & VIR_CONF_FLAG_LXC_FORMAT)) {
         type = VIR_CONF_STRING;
         str = virConfParseString(ctxt);
         if (str == NULL)
@@ -561,7 +572,9 @@ virConfParseName(virConfParserCtxtPtr ctxt)
     while ((ctxt->cur < ctxt->end) &&
            (c_isalnum(CUR) || (CUR == '_') ||
             ((ctxt->conf->flags & VIR_CONF_FLAG_VMX_FORMAT) &&
-             ((CUR == ':') || (CUR == '.') || (CUR == '-')))))
+             ((CUR == ':') || (CUR == '.') || (CUR == '-'))) ||
+            ((ctxt->conf->flags & VIR_CONF_FLAG_LXC_FORMAT) &&
+             (CUR == '.'))))
         NEXT;
     if (VIR_STRNDUP(ret, base, ctxt->cur - base) < 0)
         return NULL;
@@ -905,6 +918,35 @@ virConfSetValue(virConfPtr conf,
     return 0;
 }
 
+/**
+ * virConfWalk:
+ * @conf: a configuration file handle
+ * @callback: the function to call to process each entry
+ * @data: obscure data passed to callback
+ *
+ * Walk over all entries of the configuration file and run the callback
+ * for each with entry name, value and the obscure data.
+ *
+ * Returns 0 on success, or -1 on failure.
+ */
+int virConfWalk(virConfPtr conf,
+                 virConfWalkCallback callback,
+                 void *opaque)
+{
+    virConfEntryPtr cur;
+
+    if (!conf)
+        return 0;
+
+    cur = conf->entries;
+    while (cur != NULL) {
+        if (cur->name && cur->value &&
+                callback(cur->name, cur->value, opaque) < 0)
+            return -1;
+        cur = cur->next;
+    }
+    return 0;
+}
 
 /**
  * virConfWriteFile:
index 577af8c5474571d691e918489d15e5114bb3ac10..2a6b050d981212ff47286fb08fbee7255b6f2906 100644 (file)
@@ -40,6 +40,9 @@ typedef enum {
     VIR_CONF_FLAG_VMX_FORMAT = 1,  /* allow ':', '.' and '-' in names for compatibility
                                       with VMware VMX configuration file, but restrict
                                       allowed value types to string only */
+    VIR_CONF_FLAG_LXC_FORMAT = 2,  /* allow '.' in names for compatibility with LXC
+                                      configuration file, restricts allowed value types
+                                      to string only and don't expect quotes for values */
 } virConfFlags;
 
 static inline const char *
@@ -79,6 +82,10 @@ struct _virConfValue {
 typedef struct _virConf virConf;
 typedef virConf *virConfPtr;
 
+typedef int (*virConfWalkCallback)(const char* name,
+                                   virConfValuePtr value,
+                                   void *opaque);
+
 virConfPtr      virConfNew             (void);
 virConfPtr     virConfReadFile (const char *filename, unsigned int flags);
 virConfPtr     virConfReadMem          (const char *memory,
@@ -91,6 +98,9 @@ virConfValuePtr       virConfGetValue (virConfPtr conf,
 int             virConfSetValue        (virConfPtr conf,
                                          const char *setting,
                                          virConfValuePtr value);
+int virConfWalk(virConfPtr conf,
+                virConfWalkCallback callback,
+                void *opaque);
 int            virConfWriteFile        (const char *filename,
                                          virConfPtr conf);
 int            virConfWriteMem (char *memory,