[netcf-devel] [PATCH 1/2] Use new function aug_fmt_match instead of printf+aug_match
David Lutterkort
lutter at redhat.com
Thu Jul 23 05:13:45 UTC 2009
In several places, we did an asprintf of some path and then an aug_match on
the path; the new function aug_fmt_match makes that pattern a little
easier.
* src/dutil.h: add aug_fmt_match
* src/dutil.c: add aug_fmt_match
* src/drv_initscripts: use aug_fmt_match where possible
---
src/drv_initscripts.c | 61 +++++++++++++++---------------------------------
src/dutil.c | 27 +++++++++++++++++++++
src/dutil.h | 5 ++++
3 files changed, 51 insertions(+), 42 deletions(-)
diff --git a/src/drv_initscripts.c b/src/drv_initscripts.c
index 2cfc0cb..df26b12 100644
--- a/src/drv_initscripts.c
+++ b/src/drv_initscripts.c
@@ -524,27 +524,21 @@ static int aug_put_xml(struct netcf *ncf, xmlDocPtr xml) {
}
char *drv_xml_desc(struct netcf_if *nif) {
- char *path = NULL, *result = NULL;
+ char *result = NULL;
struct augeas *aug;
struct netcf *ncf;
char **intf = NULL;
xmlDocPtr aug_xml = NULL, ncf_xml = NULL;
int nint = 0;
- int r;
ncf = nif->ncf;
aug = get_augeas(ncf);
ERR_BAIL(ncf);
- r = xasprintf(&path,
- "%s[ DEVICE = '%s' or BRIDGE = '%s' or MASTER = '%s']",
- ifcfg_path, nif->name, nif->name, nif->name);
- ERR_COND_BAIL(r < 0, ncf, ENOMEM);
-
- nint = aug_match(aug, path, &intf);
- ERR_THROW(nint < 0, ncf, EINTERNAL,
- "no nodes match '%s'", path);
- FREE(path);
+ nint = aug_fmt_match(ncf, &intf,
+ "%s[ DEVICE = '%s' or BRIDGE = '%s' or MASTER = '%s']",
+ ifcfg_path, nif->name, nif->name, nif->name);
+ ERR_BAIL(ncf);
aug_xml = aug_get_xml(ncf, nint, intf);
ncf_xml = xsltApplyStylesheet(ncf->driver->put, aug_xml, NULL);
@@ -552,7 +546,6 @@ char *drv_xml_desc(struct netcf_if *nif) {
xmlDocDumpFormatMemory(ncf_xml, (xmlChar **) &result, NULL, 1);
done:
- FREE(path);
free_matches(nint, &intf);
xmlFreeDoc(aug_xml);
xmlFreeDoc(ncf_xml);
@@ -578,47 +571,33 @@ static char *device_name_from_xml(xmlDocPtr xml) {
* other devices config file
*/
static bool is_bond(struct netcf *ncf, const char *name) {
- char *path = NULL;
- struct augeas *aug = get_augeas(ncf);
- int r, nmatches = 0;
+ int nmatches = 0;
- r = xasprintf(&path, "%s[ MASTER = '%s']", ifcfg_path, name);
- ERR_COND_BAIL(r < 0, ncf, ENOMEM);
- nmatches = aug_match(aug, path, NULL);
- free(path);
- error:
+ nmatches = aug_fmt_match(ncf, NULL,
+ "%s[ MASTER = '%s']", ifcfg_path, name);
return nmatches > 0;
}
/* The device NAME is a bridge if it has an entry TYPE=Bridge */
static bool is_bridge(struct netcf *ncf, const char *name) {
- char *path = NULL;
- struct augeas *aug = get_augeas(ncf);
- int r, nmatches = 0;
+ int nmatches = 0;
- r = xasprintf(&path, "%s[ DEVICE = '%s' and TYPE = 'Bridge']",
- ifcfg_path, name);
- ERR_COND_BAIL(r < 0, ncf, ENOMEM);
- nmatches = aug_match(aug, path, NULL);
- free(path);
- error:
+ nmatches = aug_fmt_match(ncf, NULL,
+ "%s[ DEVICE = '%s' and TYPE = 'Bridge']",
+ ifcfg_path, name);
return nmatches > 0;
}
static int bridge_slaves(struct netcf *ncf, const char *name, char ***slaves) {
struct augeas *aug = NULL;
- char *path = NULL;
int r, nslaves = 0;
aug = get_augeas(ncf);
ERR_BAIL(ncf);
- r = xasprintf(&path, "%s[ BRIDGE = '%s' ]/DEVICE", ifcfg_path, name);
- ERR_COND_BAIL(r < 0, ncf, ENOMEM);
-
- nslaves = aug_match(aug, path, slaves);
- free(path);
- ERR_COND_BAIL(nslaves < 0, ncf, EOTHER);
+ nslaves = aug_fmt_match(ncf, slaves,
+ "%s[ BRIDGE = '%s' ]/DEVICE", ifcfg_path, name);
+ ERR_BAIL(ncf);
for (int i=0; i < nslaves; i++) {
char *p = (*slaves)[i];
const char *dev;
@@ -644,13 +623,11 @@ static void modprobe_alias_bond(struct netcf *ncf, const char *name) {
struct augeas *aug = get_augeas(ncf);
int r, nmatches;
- r = xasprintf(&path, "/files/etc/modprobe.d/*/alias[ . = '%s']", name);
- ERR_COND_BAIL(r < 0, ncf, ENOMEM);
-
- nmatches = aug_match(aug, path, NULL);
- ERR_COND_BAIL(nmatches < 0, ncf, EOTHER);
+ nmatches = aug_fmt_match(ncf, NULL,
+ "/files/etc/modprobe.d/*/alias[ . = '%s']",
+ name);
+ ERR_BAIL(ncf);
- FREE(path);
if (nmatches == 0) {
/* Add a new alias node; this probably deserves better API support
in Augeas, it's too convoluted */
diff --git a/src/dutil.c b/src/dutil.c
index d431840..7f20995 100644
--- a/src/dutil.c
+++ b/src/dutil.c
@@ -144,6 +144,33 @@ int aug_submatch(struct netcf *ncf, const char *p1,
return -1;
}
+int aug_fmt_match(struct netcf *ncf, char ***matches, const char *fmt, ...) {
+ struct augeas *aug = NULL;
+ char *path = NULL;
+ va_list args;
+ int r;
+
+ aug = get_augeas(ncf);
+ ERR_BAIL(ncf);
+
+ va_start(args, fmt);
+ r = vasprintf(&path, fmt, args);
+ va_end(args);
+ if (r < 0) {
+ path = NULL;
+ ERR_COND_BAIL(1, ncf, ENOMEM);
+ }
+
+ 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 50a3363..34a90dc 100644
--- a/src/dutil.h
+++ b/src/dutil.h
@@ -52,6 +52,11 @@ struct augeas *get_augeas(struct netcf *ncf);
int aug_submatch(struct netcf *ncf, const char *p1,
const char *p2, char ***matches);
+/* Format a path by doing a printf of FMT and the var args, then call
+ AUG_MATCH on that path. Sets NCF->ERRCODE on error */
+ATTRIBUTE_FORMAT(printf, 3, 4)
+int aug_fmt_match(struct netcf *ncf, char ***matches, const char *fmt, ...);
+
/* Free matches from aug_match (or aug_submatch) */
void free_matches(int nint, char ***intf);
--
1.6.0.6
More information about the netcf-devel
mailing list