[augeas-devel] [PATCH] Add aug_rename to rename node labels without moving them in the tree.

Raphaël Pinson raphael.pinson at camptocamp.com
Wed Aug 1 08:54:31 UTC 2012


---
 src/augeas.c           |   26 ++++++++++++++++++++++++++
 src/augeas.h           |   10 ++++++++++
 src/augeas_sym.version |    1 +
 src/augrun.c           |   34 +++++++++++++++++++++++++++++++++-
 src/augtool.c          |    2 +-
 tests/run.tests        |   35 +++++++++++++++++++++++++++++++++++
 tests/test-api.c       |   17 +++++++++++++++++
 7 files changed, 123 insertions(+), 2 deletions(-)

diff --git a/src/augeas.c b/src/augeas.c
index 22ebd14..7ca081a 100644
--- a/src/augeas.c
+++ b/src/augeas.c
@@ -1245,6 +1245,32 @@ int aug_mv(struct augeas *aug, const char *src, const char *dst) {
     return ret;
 }
 
+int aug_rename(struct augeas *aug, const char *src, const char *lbl) {
+    struct pathx *s = NULL;
+    struct tree *ts;
+    int r, ret;
+
+    api_entry(aug);
+
+    ret = -1;
+    s = pathx_aug_parse(aug, aug->origin, tree_root_ctx(aug), src, true);
+    ERR_BAIL(aug);
+
+    r = find_one_node(s, &ts);
+    if (r < 0)
+        goto error;
+
+    ts->label = strdup(lbl);
+
+    tree_mark_dirty(ts);
+
+    ret = 0;
+ error:
+    free_pathx(s);
+    api_exit(aug);
+    return ret;
+}
+
 int aug_match(const struct augeas *aug, const char *pathin, char ***matches) {
     struct pathx *p = NULL;
     struct tree *tree;
diff --git a/src/augeas.h b/src/augeas.h
index c49308b..e2e3656 100644
--- a/src/augeas.h
+++ b/src/augeas.h
@@ -225,6 +225,16 @@ int aug_rm(augeas *aug, const char *path);
  */
 int aug_mv(augeas *aug, const char *src, const char *dst);
 
+/* Function: aug_rename
+ *
+ * Rename the node SRC to label LBL. SRC must match exactly one node in the
+ * tree.
+ *
+ * Returns:
+ * 0 on success and -1 on failure.
+ */
+int aug_rename(augeas *aug, const char *src, const char *lbl);
+
 /* Function: aug_match
  *
  * Returns:
diff --git a/src/augeas_sym.version b/src/augeas_sym.version
index 5a6ddfc..eb53941 100644
--- a/src/augeas_sym.version
+++ b/src/augeas_sym.version
@@ -56,4 +56,5 @@ AUGEAS_0.16.0 {
     global:
       aug_text_store;
       aug_text_retrieve;
+      aug_rename;
 } AUGEAS_0.15.0;
diff --git a/src/augrun.c b/src/augrun.c
index 7895252..22627ef 100644
--- a/src/augrun.c
+++ b/src/augrun.c
@@ -547,7 +547,7 @@ static void cmd_mv(struct command *cmd) {
     if (r < 0)
         ERR_REPORT(cmd, AUG_ECMDRUN,
                    "Moving %s to %s failed", src, dst);
-}
+};
 
 static const struct command_opt_def cmd_mv_opts[] = {
     { .type = CMD_PATH, .name = "src", .optional = false,
@@ -580,6 +580,37 @@ static const struct command_def cmd_move_def = {
     .help = cmd_mv_help
 };
 
+static void cmd_rename(struct command *cmd) {
+    const char *src = arg_value(cmd, "src");
+    const char *lbl = arg_value(cmd, "lbl");
+    int r;
+
+    r = aug_rename(cmd->aug, src, lbl);
+    if (r < 0)
+        ERR_REPORT(cmd, AUG_ECMDRUN,
+                   "Renaming %s to %s failed", src, lbl);
+};
+
+static const struct command_opt_def cmd_rename_opts[] = {
+    { .type = CMD_PATH, .name = "src", .optional = false,
+      .help = "the tree to rename" },
+    { .type = CMD_STR, .name = "lbl", .optional = false,
+      .help = "the new label" },
+    CMD_OPT_DEF_LAST
+};
+
+static const char const cmd_rename_help[] =
+    "Rename node SRC to label LBL.  SRC must match  exactly one node\n"
+    "in the tree.";
+
+static const struct command_def cmd_rename_def = {
+    .name = "rename",
+    .opts = cmd_rename_opts,
+    .handler = cmd_rename,
+    .synopsis = "rename a subtree label",
+    .help = cmd_rename_help
+};
+
 static void cmd_set(struct command *cmd) {
     const char *path = arg_value(cmd, "path");
     const char *val = arg_value(cmd, "value");
@@ -1088,6 +1119,7 @@ static const struct command_def const *commands[] = {
     &cmd_match_def,
     &cmd_mv_def,
     &cmd_move_def,
+    &cmd_rename_def,
     &cmd_print_def,
     &cmd_dump_xml_def,
     &cmd_rm_def,
diff --git a/src/augtool.c b/src/augtool.c
index ce4a8a3..bb0becc 100644
--- a/src/augtool.c
+++ b/src/augtool.c
@@ -167,7 +167,7 @@ static char *readline_command_generator(const char *text, int state) {
     static const char *const commands[] = {
         "quit", "clear", "defnode", "defvar",
         "get", "ins", "load", "ls", "match",
-        "mv", "print", "dump-xml", "rm", "save", "set", "setm",
+        "mv", "rename", "print", "dump-xml", "rm", "save", "set", "setm",
         "clearm", "span", "store", "retrieve", "help", NULL };
 
     static int current = 0;
diff --git a/tests/run.tests b/tests/run.tests
index 4f451a2..53bf5ab 100644
--- a/tests/run.tests
+++ b/tests/run.tests
@@ -213,6 +213,41 @@ prints
   /x/y/b/c = "value"
 
 #
+# test rename
+#
+test rename 1
+  rename /augeas/version version2
+
+test rename-not-there -1 ENOMATCH
+  rename /not-there neither-here
+
+test rename-into-self 1
+  rename /augeas augeas
+
+test rename-multiple -1 EMMATCH
+  rename /augeas/version/save/* saved
+
+test rename-tree1 3
+  set /a/b/c value
+  rename /a/b/c x
+  print /*[ label() != 'augeas' and label() != 'files']
+prints
+  /a
+  /a/b
+  /a/b/x = "value"
+
+test rename-tree2 4
+  set /a/b/c value
+  set /a/b/d value2
+  rename /a/b/c x
+  print /*[ label() != 'augeas' and label() != 'files']
+prints
+  /a
+  /a/b
+  /a/b/x = "value"
+  /a/b/d = "value2"
+
+#
 # test set
 #
 test set-not-there 2
diff --git a/tests/test-api.c b/tests/test-api.c
index 5726540..4b1f75c 100644
--- a/tests/test-api.c
+++ b/tests/test-api.c
@@ -416,6 +416,22 @@ static void testMv(CuTest *tc) {
 }
 
 
+static void testRename(CuTest *tc) {
+    struct augeas *aug;
+    int r;
+
+    aug = aug_init(root, loadpath, AUG_NO_STDINC|AUG_NO_LOAD);
+    CuAssertPtrNotNull(tc, aug);
+
+    r = aug_set(aug, "/a/b/c", "value");
+    CuAssertRetSuccess(tc, r);
+
+    r = aug_rename(aug, "/a/b/c", "d");
+    CuAssertRetSuccess(tc, r);
+
+    aug_close(aug);
+}
+
 static void testToXml(CuTest *tc) {
     struct augeas *aug;
     int r;
@@ -550,6 +566,7 @@ int main(void) {
     SUITE_ADD_TEST(suite, testDefNodeCreateMeta);
     SUITE_ADD_TEST(suite, testNodeInfo);
     SUITE_ADD_TEST(suite, testMv);
+    SUITE_ADD_TEST(suite, testRename);
     SUITE_ADD_TEST(suite, testToXml);
     SUITE_ADD_TEST(suite, testTextStore);
     SUITE_ADD_TEST(suite, testTextRetrieve);
-- 
1.7.9.5




More information about the augeas-devel mailing list