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

[augeas-devel] [PATCH 03/10] Add new API function aug_text_store



From: David Lutterkort <lutter redhat com>

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

diff --git a/src/augeas.c b/src/augeas.c
index 2d0a5bd..76cf272 100644
--- a/src/augeas.c
+++ b/src/augeas.c
@@ -1635,6 +1635,25 @@ 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 *path,
+                   const char *text, unsigned int text_len) {
+
+    struct pathx *p;
+
+    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);
+
+    api_exit(aug);
+    return text_store(aug, lens, path, text, text_len);
+ error:
+    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..c181cfc 100644
--- a/src/augeas.h
+++ b/src/augeas.h
@@ -305,6 +305,17 @@ int aug_save(augeas *aug);
  */
 int aug_load(augeas *aug);
 
+/* Function: aug_text_store
+ *
+ * Transform TEXT into a tree using the lens LENS and store it in the
+ * tree at PATH, which will be overwritten.
+ *
+ * Returns:
+ * 0 on success, or a negative value on failure
+ */
+int aug_text_store(augeas *aug, const char *lens, const char *path,
+                   const char *text, unsigned int text_len);
+
 /* 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]