[augeas-devel] [PATCH 4/7] * src/transform.c (text_retrieve): new function to turn tree into text

lutter at redhat.com lutter at redhat.com
Thu May 24 21:52:14 UTC 2012


From: David Lutterkort <lutter at redhat.com>

---
 src/transform.c |   66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/transform.h |    7 +++++
 2 files changed, 73 insertions(+), 0 deletions(-)

diff --git a/src/transform.c b/src/transform.c
index 33888ca..39bfa50 100644
--- a/src/transform.c
+++ b/src/transform.c
@@ -1150,6 +1150,72 @@ int transform_save(struct augeas *aug, struct tree *xfm,
     return result;
 }
 
+int text_retrieve(struct augeas *aug, const char *lens_name,
+                  const char *path, struct tree *tree,
+                  const char *text_in, char **text_out) {
+    struct memstream ms;
+    bool ms_open;
+    const char *err_status = NULL;
+    char *dyn_err_status = NULL;
+    struct lns_error *err = NULL;
+    struct lens *lens = NULL;
+    int result = -1, r;
+
+    MEMZERO(&ms, 1);
+    errno = 0;
+
+    lens = lens_from_name(aug, lens_name);
+    if (lens == NULL) {
+        err_status = "lens_name";
+        goto done;
+    }
+
+    r = init_memstream(&ms);
+    if (r < 0) {
+        err_status = "init_memstream";
+        goto done;
+    }
+    ms_open = true;
+
+    if (tree != NULL)
+        lns_put(ms.stream, lens, tree->children, text_in, &err);
+
+    r = close_memstream(&ms);
+    ms_open = false;
+    if (r < 0) {
+        err_status = "close_memstream";
+        goto done;
+    }
+
+    *text_out = ms.buf;
+    ms.buf = NULL;
+
+    if (err != NULL) {
+        err_status = err->pos >= 0 ? "parse_skel_failed" : "put_failed";
+        goto done;
+    }
+
+    result = 0;
+
+ done:
+    {
+        const char *emsg =
+            dyn_err_status == NULL ? err_status : dyn_err_status;
+        store_error(aug, NULL, path, emsg, errno, err, text_in);
+    }
+    free(dyn_err_status);
+    lens_release(lens);
+    if (result < 0) {
+        free(*text_out);
+        *text_out = NULL;
+    }
+    free_lns_error(err);
+
+    if (ms_open)
+        close_memstream(&ms);
+    return result;
+}
+
 int remove_file(struct augeas *aug, struct tree *tree) {
     char *path = NULL;
     const char *filename = NULL;
diff --git a/src/transform.h b/src/transform.h
index 98d1711..7f100f5 100644
--- a/src/transform.h
+++ b/src/transform.h
@@ -85,6 +85,13 @@ int transform_save(struct augeas *aug, struct tree *xfm,
 int text_store(struct augeas *aug, const char *lens_name,
                const char *path, const char *text);
 
+/* Transform the tree at PATH back into TEXT_OUT, assuming TEXT_IN was
+ * used to initially generate the tree
+ */
+int text_retrieve(struct augeas *aug, const char *lens_name,
+                  const char *path, struct tree *tree,
+                  const char *text_in, char **text_out);
+
 /* Remove the file for TREE, either by moving it to a .augsave file or by
  * unlinking it, depending on aug->flags. TREE must be the node underneath
  * /augeas/files corresponding to the file to be removed.
-- 
1.7.7.6




More information about the augeas-devel mailing list