[netcf-devel] [PATCH 2/3] Move get_augeas and aug_submatch to dutil.c

Jonas Eriksson jonas.j.eriksson at ericsson.com
Fri Jul 17 15:32:43 UTC 2009


In order to include augeas_xfm in the driver struct, the type of augeas_xfm had
to be reworked to a struct array. The inclusion of augeas_xfm in the driver
struct was necessary in order to move get_augeas and aug_submatch from
drv_initscripts.c to dutil.c. This also prepares for future utility functions
operating on the augeas instance.
---
 src/drv_initscripts.c |   85 ++-----------------------------------------------
 src/dutil.c           |   82 +++++++++++++++++++++++++++++++++++++++++++++++
 src/dutil.h           |   14 ++++++++
 3 files changed, 99 insertions(+), 82 deletions(-)

diff --git a/src/drv_initscripts.c b/src/drv_initscripts.c
index d51ac97..7aba42d 100644
--- a/src/drv_initscripts.c
+++ b/src/drv_initscripts.c
@@ -47,7 +47,7 @@ static const char *const ifcfg_path =
     "/files/etc/sysconfig/network-scripts/*";
 
 /* Augeas should only load the files we are interested in */
-static const char *const augeas_xfm[][2] = {
+static const struct augeas_pv augeas_xfm[] = {
     /* Ifcfg files */
     { "/augeas/load/Ifcfg/lens", "Shellvars.lns" },
     { "/augeas/load/Ifcfg/incl",
@@ -90,68 +90,6 @@ static const char *const subif_paths[] = {
     "MASTER", "BRIDGE"
 };
 
-/* Get the Augeas instance; if we already initialized it, just return
- * it. Otherwise, create a new one and return that.
- */
-static struct augeas *get_augeas(struct netcf *ncf) {
-    int r;
-
-    if (ncf->driver->augeas == NULL) {
-        struct augeas *aug;
-        char *path;
-
-        r = xasprintf(&path, "%s/lenses", ncf->data_dir);
-        ERR_COND_BAIL(r < 0, ncf, ENOMEM);
-
-        aug = aug_init(ncf->root, path, AUG_NO_MODL_AUTOLOAD);
-        FREE(path);
-        ERR_THROW(aug == NULL, ncf, EOTHER, "aug_init failed");
-        ncf->driver->augeas = aug;
-        /* Only look at a few config files */
-        r = aug_rm(aug, "/augeas/load/*");
-        ERR_THROW(r < 0, ncf, EOTHER, "aug_rm failed in get_augeas");
-
-        for (int i=0; i < ARRAY_CARDINALITY(augeas_xfm); i++) {
-            r = aug_set(aug, augeas_xfm[i][0], augeas_xfm[i][1]);
-            ERR_THROW(r < 0, ncf, EOTHER,
-                      "transform setup failed to set %s",
-                      augeas_xfm[i][0]);
-        }
-        r = aug_load(aug);
-        ERR_THROW(r < 0, ncf, EOTHER, "failed to load config files");
-
-        /* FIXME: we need to produce _much_ better diagnostics here - need
-         * to analyze what came back in /augeas//error; ultimately, we need
-         * to understand whether this is harmless or a real error. For real
-         * errors, we need to return an error.
-        */
-        r = aug_match(aug, "/augeas//error", NULL);
-        if (r > 0) {
-            fprintf(stderr, "warning: augeas initialization had errors\n");
-            fprintf(stderr, "please file a bug with the following lines in the bug report:\n");
-            aug_print(aug, stderr, "/augeas//error");
-        }
-    } else {
-        if (ncf->driver->load_augeas) {
-            struct augeas *aug = ncf->driver->augeas;
-            /* Undefine all our variables to work around bug 79 in Augeas */
-            aug_defvar(aug, "iptables", NULL);
-            aug_defvar(aug, "fw", NULL);
-            aug_defvar(aug, "fw_custom", NULL);
-            aug_defvar(aug, "ipt_filter", NULL);
-
-            r = aug_load(ncf->driver->augeas);
-            ERR_THROW(r < 0, ncf, EOTHER, "failed to reload config files");
-            ncf->driver->load_augeas = 0;
-        }
-    }
-    return ncf->driver->augeas;
- error:
-    aug_close(ncf->driver->augeas);
-    ncf->driver->augeas = NULL;
-    return NULL;
-}
-
 ATTRIBUTE_FORMAT(printf, 4, 5)
 static int defnode(struct netcf *ncf, const char *name, const char *value,
                    const char *format, ...) {
@@ -176,25 +114,6 @@ static int defnode(struct netcf *ncf, const char *name, const char *value,
     return (r < 0) ? -1 : created;
 }
 
-static int aug_submatch(struct netcf *ncf, const char *p1,
-                        const char *p2, char ***matches) {
-    struct augeas *aug = get_augeas(ncf);
-    char *path = NULL;
-    int r;
-
-    r = xasprintf(&path, "%s/%s", p1, p2);
-    ERR_COND_BAIL(r < 0, ncf, EOTHER);
-
-    r = aug_match(aug, path, matches);
-    ERR_COND_BAIL(r < 0, ncf, EOTHER);
-
-    free(path);
-    return r;
- error:
-    free(path);
-    return -1;
-}
-
 static int is_slave(struct netcf *ncf, const char *intf) {
     for (int s = 0; s < ARRAY_CARDINALITY(subif_paths); s++) {
         int r;
@@ -355,6 +274,8 @@ int drv_init(struct netcf *ncf) {
         return -1;
 
     ncf->driver->ioctl_fd = -1;
+    ncf->driver->augeas_xfm_size = ARRAY_CARDINALITY(augeas_xfm);
+    ncf->driver->augeas_xfm = augeas_xfm;
 
     // FIXME: Check for errors
     xsltInit();
diff --git a/src/dutil.c b/src/dutil.c
index c15bfbc..d0cdb22 100644
--- a/src/dutil.c
+++ b/src/dutil.c
@@ -62,6 +62,88 @@ int xasprintf(char **strp, const char *format, ...) {
   return result;
 }
 
+/* Get the Augeas instance; if we already initialized it, just return
+ * it. Otherwise, create a new one and return that.
+ */
+struct augeas *get_augeas(struct netcf *ncf) {
+    int r;
+
+    if (ncf->driver->augeas == NULL) {
+        struct augeas *aug;
+        char *path;
+
+        r = xasprintf(&path, "%s/lenses", ncf->data_dir);
+        ERR_COND_BAIL(r < 0, ncf, ENOMEM);
+
+        aug = aug_init(ncf->root, path, AUG_NO_MODL_AUTOLOAD);
+        FREE(path);
+        ERR_THROW(aug == NULL, ncf, EOTHER, "aug_init failed");
+        ncf->driver->augeas = aug;
+        /* Only look at a few config files */
+        r = aug_rm(aug, "/augeas/load/*");
+        ERR_THROW(r < 0, ncf, EOTHER, "aug_rm failed in get_augeas");
+
+        for (int i=0; i < ncf->driver->augeas_xfm_size; i++) {
+            r = aug_set(aug, ncf->driver->augeas_xfm[i].path,
+                    ncf->driver->augeas_xfm[i].value);
+            ERR_THROW(r < 0, ncf, EOTHER,
+                      "transform setup failed to set %s",
+                      ncf->driver->augeas_xfm[i].path);
+        }
+        r = aug_load(aug);
+        ERR_THROW(r < 0, ncf, EOTHER, "failed to load config files");
+
+        /* FIXME: we need to produce _much_ better diagnostics here - need
+         * to analyze what came back in /augeas//error; ultimately, we need
+         * to understand whether this is harmless or a real error. For real
+         * errors, we need to return an error.
+        */
+        r = aug_match(aug, "/augeas//error", NULL);
+        if (r > 0) {
+            fprintf(stderr, "warning: augeas initialization had errors\n");
+            fprintf(stderr, "please file a bug with the following lines in the bug report:\n");
+            aug_print(aug, stderr, "/augeas//error");
+        }
+    } else {
+        if (ncf->driver->load_augeas) {
+            struct augeas *aug = ncf->driver->augeas;
+            /* Undefine all our variables to work around bug 79 in Augeas */
+            aug_defvar(aug, "iptables", NULL);
+            aug_defvar(aug, "fw", NULL);
+            aug_defvar(aug, "fw_custom", NULL);
+            aug_defvar(aug, "ipt_filter", NULL);
+
+            r = aug_load(ncf->driver->augeas);
+            ERR_THROW(r < 0, ncf, EOTHER, "failed to reload config files");
+            ncf->driver->load_augeas = 0;
+        }
+    }
+    return ncf->driver->augeas;
+ error:
+    aug_close(ncf->driver->augeas);
+    ncf->driver->augeas = NULL;
+    return NULL;
+}
+
+int aug_submatch(struct netcf *ncf, const char *p1,
+                        const char *p2, char ***matches) {
+    struct augeas *aug = get_augeas(ncf);
+    char *path = NULL;
+    int r;
+
+    r = xasprintf(&path, "%s/%s", p1, p2);
+    ERR_COND_BAIL(r < 0, ncf, EOTHER);
+
+    r = aug_match(aug, path, matches);
+    ERR_COND_BAIL(r < 0, ncf, EOTHER);
+
+    free(path);
+    return r;
+ error:
+    free(path);
+    return -1;
+}
+
 void free_matches(int nint, char ***intf) {
     if (*intf != NULL) {
         for (int i=0; i < nint; i++)
diff --git a/src/dutil.h b/src/dutil.h
index e5ec383..8f6f293 100644
--- a/src/dutil.h
+++ b/src/dutil.h
@@ -33,11 +33,25 @@ struct driver {
     xmlRelaxNGPtr      rng;
     int                ioctl_fd;
     unsigned int       load_augeas : 1;
+    unsigned int augeas_xfm_size;
+    const struct augeas_pv *augeas_xfm;
+};
+
+struct augeas_pv {
+    const char *const path;
+    const char *const value;
 };
 
 /* Like asprintf, but set *STRP to NULL on error */
 int xasprintf(char **strp, const char *format, ...);
 
+/* Get or create the augeas instance from @ncf */ 
+struct augeas *get_augeas(struct netcf *ncf);
+
+/* Like aug_match, but match @p1/@p2 */
+int aug_submatch(struct netcf *ncf, const char *p1,
+                        const char *p2, char ***matches);
+
 /* Free matches from aug_match (or aug_submatch) */
 void free_matches(int nint, char ***intf);
 
-- 
1.6.2



More information about the netcf-devel mailing list