[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[augeas-devel] [PATCH 5/7] aug_text_store, aug_text_retrieve: new API functions



From: David Lutterkort <lutter redhat com>

---
 src/augeas.c           |   61 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/augeas.h           |   25 +++++++++++++++++++
 src/augeas_sym.version |    6 ++++
 tests/test-api.c       |   33 ++++++++++++++++++++++++++
 4 files changed, 125 insertions(+), 0 deletions(-)

diff --git a/src/augeas.c b/src/augeas.c
index 2d0a5bd..afcb0a7 100644
--- a/src/augeas.c
+++ b/src/augeas.c
@@ -1635,6 +1635,67 @@ static int tree_to_xml(struct pathx *p, xmlNode **xml, const char *pathin) {
     return -1;
 }
 
+int aug_text_store(augeas *aug, const char *lens, const char *node,
+                   const char *path) {
+
+    struct pathx *p;
+    const char *src;
+    int result = -1, r;
+
+    api_entry(aug);
+
+    /* Validate PATH is syntactically correct */
+    p = pathx_aug_parse(aug, aug->origin, tree_root_ctx(aug), path, true);
+    ERR_BAIL(aug);
+    free_pathx(p);
+
+    r = aug_get(aug, node, &src);
+    ERR_BAIL(aug);
+    ERR_THROW(r == 0, aug, AUG_ENOMATCH,
+              "Source node %s has a NULL value", node);
+
+    result = text_store(aug, lens, path, src);
+ error:
+    api_exit(aug);
+    return result;
+}
+
+int aug_text_retrieve(struct augeas *aug, const char *lens,
+                      const char *node_in, const char *path,
+                      const char *node_out) {
+    struct tree *tree = NULL;
+    const char *src;
+    char *out = NULL;
+    struct tree *tree_out;
+    int r;
+
+    api_entry(aug);
+
+    tree = tree_find(aug, path);
+    ERR_BAIL(aug);
+
+    r = aug_get(aug, node_in, &src);
+    ERR_BAIL(aug);
+    ERR_THROW(r == 0, aug, AUG_ENOMATCH,
+              "Source node %s has a NULL value", node_in);
+
+    r = text_retrieve(aug, lens, path, tree, src, &out);
+    if (r < 0)
+        goto error;
+
+    tree_out = tree_find_cr(aug, node_out);
+    ERR_BAIL(aug);
+
+    tree_store_value(tree_out, &out);
+
+    api_exit(aug);
+    return 0;
+ error:
+    free(out);
+    api_exit(aug);
+    return -1;
+}
+
 int aug_to_xml(const struct augeas *aug, const char *pathin,
                xmlNode **xmldoc, unsigned int flags) {
     struct pathx *p;
diff --git a/src/augeas.h b/src/augeas.h
index b6fbba8..c49308b 100644
--- a/src/augeas.h
+++ b/src/augeas.h
@@ -305,6 +305,31 @@ int aug_save(augeas *aug);
  */
 int aug_load(augeas *aug);
 
+/* Function: aug_text_store
+ *
+ * Use the value of node NODE as a string and transform it into a tree
+ * using the lens LENS and store it in the tree at PATH, which will be
+ * overwritten. PATH and NODE are path expressions.
+ *
+ * Returns:
+ * 0 on success, or a negative value on failure
+ */
+int aug_text_store(augeas *aug, const char *lens, const char *node,
+                   const char *path);
+
+/* Function: aug_text_retrieve
+ *
+ * Transform the tree at PATH into a string using lens LENS and store it in
+ * the node NODE_OUT, assuming the tree was initially generated using the
+ * value of node NODE_IN. PATH, NODE_IN, and NODE_OUT are path expressions.
+ *
+ * Returns:
+ * 0 on success, or a negative value on failure
+ */
+int aug_text_retrieve(struct augeas *aug, const char *lens,
+                      const char *node_in, const char *path,
+                      const char *node_out);
+
 /* Function: aug_print
  *
  * Print each node matching PATH and its descendants to OUT.
diff --git a/src/augeas_sym.version b/src/augeas_sym.version
index 5c29ab7..5a6ddfc 100644
--- a/src/augeas_sym.version
+++ b/src/augeas_sym.version
@@ -51,3 +51,9 @@ AUGEAS_0.15.0 {
     global:
       aug_to_xml;
 } AUGEAS_0.14.0;
+
+AUGEAS_0.16.0 {
+    global:
+      aug_text_store;
+      aug_text_retrieve;
+} AUGEAS_0.15.0;
diff --git a/tests/test-api.c b/tests/test-api.c
index 6942e77..a032ac0 100644
--- a/tests/test-api.c
+++ b/tests/test-api.c
@@ -455,6 +455,38 @@ static void testToXml(CuTest *tc) {
     aug_close(aug);
 }
 
+static void testTextStore(CuTest *tc) {
+    static const char *const hosts = "192.168.0.1 rtr.example.com router\n";
+    /* Not acceptable for Hosts.lns - missing \n */
+    static const char *const hosts_bad = "192.168.0.1";
+
+    struct augeas *aug;
+    int r;
+
+    aug = aug_init(root, loadpath, AUG_NO_STDINC|AUG_NO_LOAD);
+    CuAssertPtrNotNull(tc, aug);
+
+    r = aug_text_store(aug, "Hosts.lns", "/text/t1", hosts, strlen(hosts));
+    CuAssertRetSuccess(tc, r);
+
+    r = aug_match(aug, "/text/t1/*", NULL);
+    CuAssertIntEquals(tc, 1, r);
+
+    // FIXME: Test bad lens name
+    // FIXME: Test parse error
+    r = aug_text_store(aug, "Hosts.lns", "text/t3", hosts_bad,
+                       strlen(hosts_bad));
+    CuAssertIntEquals(tc, -1, r);
+    r = aug_match(aug, "/text/t3", NULL);
+    CuAssertIntEquals(tc, 1, r);
+
+    aug_print(aug, stdout, "/augeas//error");
+    r = aug_match(aug, "/augeas/text/text/t3/error", NULL);
+    CuAssertIntEquals(tc, 1, r);
+
+    // FIXME: Test invalid PATH
+}
+
 int main(void) {
     char *output = NULL;
     CuSuite* suite = CuSuiteNew();
@@ -469,6 +501,7 @@ int main(void) {
     SUITE_ADD_TEST(suite, testNodeInfo);
     SUITE_ADD_TEST(suite, testMv);
     SUITE_ADD_TEST(suite, testToXml);
+    SUITE_ADD_TEST(suite, testTextStore);
 
     abs_top_srcdir = getenv("abs_top_srcdir");
     if (abs_top_srcdir == NULL)
-- 
1.7.7.6



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]