[augeas-devel] [PATCH 7/8] Report errors when exactly one match is expected

David Lutterkort lutter at redhat.com
Tue Sep 22 00:49:04 UTC 2009


aug_mv and aug_insert expect that exactly one node matches the path
expressions they are given. Report new errors if none or more than one node
match.
---
 src/augeas.c   |   27 +++++++++++++++++++++++----
 src/augeas.h   |    4 +++-
 src/internal.h |    5 +++++
 src/pathx.c    |   11 ++++-------
 4 files changed, 35 insertions(+), 12 deletions(-)

diff --git a/src/augeas.c b/src/augeas.c
index f6c1c3d..fce8117 100644
--- a/src/augeas.c
+++ b/src/augeas.c
@@ -55,7 +55,9 @@ static const char *const errcodes[] = {
     "No error",                                         /* AUG_NOERROR */
     "Cannot allocate memory",                           /* AUG_ENOMEM */
     "Internal error (please file a bug)",               /* AUG_EINTERNAL */
-    "Invalid path expression"                           /* AUG_EPATHX */
+    "Invalid path expression",                          /* AUG_EPATHX */
+    "No match for path expression",                     /* AUG_ENOMATCH */
+    "Too many matches for path expression"              /* AUG_EMMATCH */
 };
 
 static void tree_mark_dirty(struct tree *tree) {
@@ -392,6 +394,23 @@ int aug_load(struct augeas *aug) {
     return -1;
 }
 
+static int find_one_node(struct pathx *p, struct tree **match) {
+    struct error *err = err_of_pathx(p);
+    int r = pathx_find_one(p, match);
+
+    if (r == 1)
+        return 0;
+
+    if (r == 0) {
+        report_error(err, AUG_ENOMATCH, NULL);
+    } else {
+        /* r > 1 */
+        report_error(err, AUG_EMMATCH, NULL);
+    }
+
+    return -1;
+}
+
 int aug_get(const struct augeas *aug, const char *path, const char **value) {
     struct pathx *p;
     struct tree *match;
@@ -515,7 +534,7 @@ int tree_insert(struct pathx *p, const char *label, int before) {
     if (strchr(label, SEP) != NULL)
         return -1;
 
-    if (pathx_find_one(p, &match) != 1)
+    if (find_one_node(p, &match) < 0)
         goto error;
 
     new = make_tree(strdup(label), NULL, match->parent, NULL);
@@ -711,8 +730,8 @@ int aug_mv(struct augeas *aug, const char *src, const char *dst) {
     d = parse_user_pathx(aug, true, dst);
     ERR_BAIL(aug);
 
-    r = pathx_find_one(s, &ts);
-    if (r != 1)
+    r = find_one_node(s, &ts);
+    if (r < 0)
         goto error;
 
     r = pathx_expand_tree(d, &td);
diff --git a/src/augeas.h b/src/augeas.h
index 7ffe678..d31f845 100644
--- a/src/augeas.h
+++ b/src/augeas.h
@@ -280,7 +280,9 @@ typedef enum {
     AUG_NOERROR,        /* No error */
     AUG_ENOMEM,         /* Out of memory */
     AUG_EINTERNAL,      /* Internal error (bug) */
-    AUG_EPATHX          /* Invalid path expression */
+    AUG_EPATHX,         /* Invalid path expression */
+    AUG_ENOMATCH,       /* No match for path expression */
+    AUG_EMMATCH         /* Too many matches for path expression */
 } aug_errcode_t;
 
 /* Return the error code from the last API call */
diff --git a/src/internal.h b/src/internal.h
index 918a047..ee51c96 100644
--- a/src/internal.h
+++ b/src/internal.h
@@ -452,8 +452,13 @@ int pathx_parse(const struct tree *origin,
                 bool need_nodeset,
                 struct pathx_symtab *symtab,
                 struct pathx **px);
+/* Return the error struct that was passed into pathx_parse */
+struct error *err_of_pathx(struct pathx *px);
 struct tree *pathx_first(struct pathx *path);
 struct tree *pathx_next(struct pathx *path);
+/* Return -1 if evalutating PATH runs into trouble, otherwise return the
+ * number of nodes matching PATH and set MATCH to the first matching
+ * node */
 int pathx_find_one(struct pathx *path, struct tree **match);
 int pathx_expand_tree(struct pathx *path, struct tree **tree);
 void free_pathx(struct pathx *path);
diff --git a/src/pathx.c b/src/pathx.c
index 832c2df..f24fdde 100644
--- a/src/pathx.c
+++ b/src/pathx.c
@@ -2236,14 +2236,11 @@ int pathx_find_one(struct pathx *path, struct tree **tree) {
     *tree = pathx_first(path);
     if (HAS_ERROR(path->state))
         return -1;
-    if (*tree == NULL)
-        return 0;
+    return path->nodeset->used;
+}
 
-    if (pathx_next(path) != NULL) {
-        *tree = NULL;
-        return -1;
-    }
-    return 1;
+struct error *err_of_pathx(struct pathx *px) {
+    return px->state->error;
 }
 
 const char *pathx_error(struct pathx *path, const char **txt, int *pos) {
-- 
1.6.2.5




More information about the augeas-devel mailing list