[augeas-devel] [PATCH 3/7] * src/transform.c (text_store): new function to write string to tree
lutter at redhat.com
lutter at redhat.com
Thu May 24 21:52:13 UTC 2012
From: David Lutterkort <lutter at redhat.com>
---
src/internal.h | 4 +++
src/transform.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++--
src/transform.h | 5 ++++
3 files changed, 65 insertions(+), 3 deletions(-)
diff --git a/src/internal.h b/src/internal.h
index 7317842..064c5d1 100644
--- a/src/internal.h
+++ b/src/internal.h
@@ -65,6 +65,10 @@
* Information about files */
#define AUGEAS_META_FILES AUGEAS_META_TREE AUGEAS_FILES_TREE
+/* Define: AUGEAS_META_TEXT
+ * Information about text (see aug_text_store and aug_text_retrieve) */
+#define AUGEAS_META_TEXT AUGEAS_META_TREE "/text"
+
/* Define: AUGEAS_META_ROOT
* The root directory */
#define AUGEAS_META_ROOT AUGEAS_META_TREE "/root"
diff --git a/src/transform.c b/src/transform.c
index 4100cca..33888ca 100644
--- a/src/transform.c
+++ b/src/transform.c
@@ -325,8 +325,9 @@ static void err_set(struct augeas *aug,
}
/* Record an error in the tree. The error will show up underneath
- * /augeas/FILENAME/error. PATH is the path to the toplevel node in the
- * tree where the lens application happened. When STATUS is NULL, just
+ * /augeas/FILENAME/error if filename is not NULL, and underneath
+ * /augeas/text/PATH otherwise. PATH is the path to the toplevel node in
+ * the tree where the lens application happened. When STATUS is NULL, just
* clear any error associated with FILENAME in the tree.
*/
static int store_error(struct augeas *aug,
@@ -338,7 +339,11 @@ static int store_error(struct augeas *aug,
int r;
int result = -1;
- r = pathjoin(&fip, 2, AUGEAS_META_FILES, filename);
+ if (filename != NULL) {
+ r = pathjoin(&fip, 2, AUGEAS_META_FILES, filename);
+ } else {
+ r = pathjoin(&fip, 2, AUGEAS_META_TEXT, path);
+ }
ERR_NOMEM(r < 0, aug);
finfo = tree_find_cr(aug, fip);
@@ -582,6 +587,54 @@ static struct lens *lens_from_name(struct augeas *aug, const char *name) {
return NULL;
}
+int text_store(struct augeas *aug, const char *lens_path,
+ const char *path, const char *text) {
+ struct info *info = NULL;
+ struct lns_error *err = NULL;
+ struct tree *tree = NULL;
+ struct span *span = NULL;
+ int result = -1;
+ const char *err_status = NULL;
+ struct lens *lens = NULL;
+
+ lens = lens_from_name(aug, lens_path);
+ if (lens == NULL) {
+ goto done;
+ }
+
+ make_ref(info);
+ info->first_line = 1;
+ info->last_line = 1;
+ info->first_column = 1;
+ info->last_column = strlen(text);
+
+ tree = lns_get(info, lens, text, &err);
+ if (err != NULL) {
+ err_status = "parse_failed";
+ goto done;
+ }
+
+ unref(info, info);
+
+ tree_replace(aug, path, tree);
+
+ /* top level node span entire file length */
+ if (span != NULL && tree != NULL) {
+ tree->parent->span = span;
+ tree->parent->span->span_start = 0;
+ tree->parent->span->span_end = strlen(text);
+ }
+
+ tree = NULL;
+
+ result = 0;
+done:
+ store_error(aug, NULL, path, err_status, errno, err, text);
+ free_tree(tree);
+ free_lns_error(err);
+ return result;
+}
+
const char *xfm_lens_name(struct tree *xfm) {
struct tree *l = tree_child(xfm, s_lens);
diff --git a/src/transform.h b/src/transform.h
index 5bb1e7e..98d1711 100644
--- a/src/transform.h
+++ b/src/transform.h
@@ -80,6 +80,11 @@ int transform_applies(struct tree *xfm, const char *path);
int transform_save(struct augeas *aug, struct tree *xfm,
const char *path, struct tree *tree);
+/* Transform TEXT into a tree and store it at PATH
+ */
+int text_store(struct augeas *aug, const char *lens_name,
+ const char *path, const char *text);
+
/* 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