Description: Change flow of aug_put_xml in drv_debian
 Make sure we aug_set and aug_save all on the same aug link
 to avoid losing changes
Author: Serge Hallyn <serge.hallyn@canonical.com>
Forwarded: yes

Index: netcf-0.1.9/src/drv_debian.c
===================================================================
--- netcf-0.1.9.orig/src/drv_debian.c	2011-10-27 22:13:28.620665206 -0500
+++ netcf-0.1.9/src/drv_debian.c	2011-10-27 22:32:49.252710229 -0500
@@ -98,7 +98,7 @@
 static bool is_bond(struct netcf *ncf, const char *name) {
     int nmatches = 0;
 
-    nmatches = aug_fmt_match(ncf, NULL,
+    nmatches = aug_fmt_match(ncf, NULL, NULL,
                              "%s/iface[. = '%s' and count(./bond_slaves)> 0]",
                              network_interfaces_path,
                              name);
@@ -109,7 +109,7 @@
 static bool is_bridge(struct netcf *ncf, const char *name) {
     int nmatches = 0;
 
-    nmatches = aug_fmt_match(ncf, NULL,
+    nmatches = aug_fmt_match(ncf, NULL, NULL,
                              "%s/iface[. = '%s' and count(./bridge_ports)> 0]",
                              network_interfaces_path,
                              name);
@@ -246,7 +246,7 @@
 static bool has_config(struct netcf *ncf, const char *name) {
     int nmatches;
 
-    nmatches = aug_fmt_match(ncf, NULL,
+    nmatches = aug_fmt_match(ncf, NULL, NULL,
                              "%s/iface[. = '%s']",
                              network_interfaces_path, name);
     if (nmatches == 0)
@@ -312,7 +312,7 @@
     int result = 0, ndevs;
     char **devs = NULL;
 
-    ndevs = aug_fmt_match(ncf, &devs, "%s/iface", network_interfaces_path);
+    ndevs = aug_fmt_match(ncf, NULL, &devs, "%s/iface", network_interfaces_path);
     ERR_COND_BAIL(ndevs < 0, ncf, EOTHER);
 
     result = uniq_device_names(ncf, ndevs, devs, intf);
@@ -501,7 +501,7 @@
     ERR_BAIL(ncf);
 
 
-    nintf = aug_fmt_match(ncf, &intf, "%s/%s[. = '%s']",
+    nintf = aug_fmt_match(ncf, aug, &intf, "%s/%s[. = '%s']",
                           network_interfaces_path, "iface", name);
     ERR_BAIL(ncf);
     for (int i = 0 ; i < nintf ; i++) {
@@ -511,7 +511,7 @@
         element = xmlNewChild(array, NULL, BAD_CAST "element", NULL);
         xmlNewProp(element, BAD_CAST "key", BAD_CAST name);
 
-        nmatches = aug_fmt_match(ncf, &matches, "%s/%s", intf[i], "*");
+        nmatches = aug_fmt_match(ncf, aug, &matches, "%s/%s", intf[i], "*");
         ERR_COND_BAIL(nmatches < 0, ncf, EOTHER);
 
         for (int j = 0; j < nmatches; j++) {
@@ -553,7 +553,7 @@
     tree = xmlNewChild(root, NULL, BAD_CAST "tree", NULL);
     xmlNewProp(tree, BAD_CAST "path", BAD_CAST network_interfaces_path);
 
-    nmatches = aug_fmt_match(ncf, &matches, "%s/%s",
+    nmatches = aug_fmt_match(ncf, NULL, &matches, "%s/%s",
                              network_interfaces_path, "auto");
     if (nmatches) {
         free_matches(nmatches, &matches);
@@ -623,6 +623,10 @@
     int result = -1;
     int r;
     int n;
+    struct augeas *aug = NULL;
+    
+    aug = get_augeas(ncf);
+    ERR_BAIL(ncf);
 
     forest = xmlDocGetRootElement(xml);
     ERR_THROW(forest == NULL, ncf, EINTERNAL, "missing root element");
@@ -643,7 +647,7 @@
 
             arraylabel = xml_prop(array, "label");
 
-            nmatches = aug_fmt_match(ncf, &matches, "%s/%s[last()]",
+            nmatches = aug_fmt_match(ncf, aug, &matches, "%s/%s[last()]",
                                      network_interfaces_path,
                                      arraylabel);
             ERR_COND_BAIL(nmatches < 0, ncf, EOTHER);
@@ -667,7 +671,7 @@
                 key = xml_prop(element, "key");
 
                 if (key) {
-                    r = aug_fmt_set(ncf, key, "%s/%s[%d]",
+                    r = aug_fmt_set(ncf, aug, key, "%s/%s[%d]",
                                     network_interfaces_path,
                                     arraylabel, ++n);
                     ERR_COND_BAIL(r < 0, ncf, EOTHER);
@@ -679,7 +683,7 @@
                     label = xml_prop(node, "label");
                     value = xml_prop(node, "value");
 
-                    r = aug_fmt_set(ncf, value, "%s/%s[%d]/%s",
+                    r = aug_fmt_set(ncf, aug, value, "%s/%s[%d]/%s",
                                     network_interfaces_path,
                                     arraylabel, n, label ? label : "1");
                     ERR_COND_BAIL(r < 0, ncf, EOTHER);
@@ -692,6 +696,13 @@
         xmlFree(path);
         path = NULL;
     }
+    r = aug_save(aug);
+    if (r < 0 && NCF_DEBUG(ncf)) {
+        fprintf(stderr, "Errors from aug_save:\n");
+        aug_print(aug, stderr, "/augeas//error");
+    }
+    ERR_THROW(r < 0, ncf, EOTHER, "aug_save failed");
+
     result = 0;
  error:
     xmlFree(arraylabel);
@@ -904,7 +915,6 @@
     xmlDocPtr ncf_xml = NULL, aug_xml = NULL;
     char *name = NULL;
     int r;
-    struct augeas *aug = get_augeas(ncf);
 
     ncf_xml = parse_xml(ncf, xml_str);
     ERR_BAIL(ncf);
@@ -927,13 +937,6 @@
     bond_setup(ncf, name, true);
     ERR_BAIL(ncf);
 
-    r = aug_save(aug);
-    if (r < 0 && NCF_DEBUG(ncf)) {
-        fprintf(stderr, "Errors from aug_save:\n");
-        aug_print(aug, stderr, "/augeas//error");
-    }
-    ERR_THROW(r < 0, ncf, EOTHER, "aug_save failed");
-
     result = make_netcf_if(ncf, name);
     ERR_BAIL(ncf);
 
Index: netcf-0.1.9/src/dutil_linux.c
===================================================================
--- netcf-0.1.9.orig/src/dutil_linux.c	2011-10-27 22:13:35.632665478 -0500
+++ netcf-0.1.9/src/dutil_linux.c	2011-10-27 22:34:25.876713978 -0500
@@ -423,16 +423,13 @@
     return (r < 0) ? -1 : created;
 }
 
-int aug_fmt_set(struct netcf *ncf, const char *value, const char *fmt, ...)
+int aug_fmt_set(struct netcf *ncf, struct augeas *aug, const char *value,
+                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);
@@ -480,14 +477,15 @@
     return -1;
 }
 
-int aug_fmt_match(struct netcf *ncf, char ***matches, const char *fmt, ...) {
-    struct augeas *aug = NULL;
+int aug_fmt_match(struct netcf *ncf, struct augeas *aug, char ***matches, const char *fmt, ...) {
     char *path = NULL;
     va_list args;
     int r;
 
-    aug = get_augeas(ncf);
-    ERR_BAIL(ncf);
+    if (aug == NULL) {
+	    aug = get_augeas(ncf);
+	    ERR_BAIL(ncf);
+    }
 
     va_start(args, fmt);
     r = vasprintf(&path, fmt, args);
@@ -525,7 +523,7 @@
     for (char *s = mac_lower; *s != '\0'; s++)
         *s = c_tolower(*s);
 
-    nmatches = aug_fmt_match(ncf, matches,
+    nmatches = aug_fmt_match(ncf, NULL, matches,
             "/files/sys/class/net/*[address/content = '%s']", mac_lower);
     ERR_BAIL(ncf);
 
@@ -577,7 +575,7 @@
     struct augeas *aug = get_augeas(ncf);
     int r, nmatches;
 
-    nmatches = aug_fmt_match(ncf, NULL,
+    nmatches = aug_fmt_match(ncf, aug, NULL,
                              "/files/etc/modprobe.d/*/alias[ . = '%s']",
                              name);
     ERR_BAIL(ncf);
Index: netcf-0.1.9/src/dutil_linux.h
===================================================================
--- netcf-0.1.9.orig/src/dutil_linux.h	2011-10-27 22:13:34.628665441 -0500
+++ netcf-0.1.9/src/dutil_linux.h	2011-10-27 22:35:04.508715474 -0500
@@ -87,11 +87,11 @@
 
 /* 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, ...);
+ATTRIBUTE_FORMAT(printf, 4, 5)
+int aug_fmt_match(struct netcf *ncf, struct augeas *aug, char ***matches, const char *fmt, ...);
 
-ATTRIBUTE_FORMAT(printf, 3, 4)
-int aug_fmt_set(struct netcf *ncf, const char *value, const char *fmt, ...);
+ATTRIBUTE_FORMAT(printf, 4, 5)
+int aug_fmt_set(struct netcf *ncf, struct augeas *aug, const char *value, const char *fmt, ...);
 
 ATTRIBUTE_FORMAT(printf, 2, 3)
 int aug_fmt_rm(struct netcf *ncf, const char *fmt, ...);
