Skip to contentSkip to conversation

master - config: add support for enhanced config node output

PATCH Peter Rajnoha 1 participant 1 message Mar 6, 2013

lore
Share & tools
lore
#0 lore raw
Gitweb:        http://git.fedorahosted.org/git/?p=lvm2.git;a=commitdiff;h=e29cd366a24859195f92b874d4bf260a3cc63bc2
Commit:        e29cd366a24859195f92b874d4bf260a3cc63bc2
Parent:        34350963d1b02d54205b1bdaa0c7778f4c5601fe
Author:        Peter Rajnoha <[email protected]>
AuthorDate:    Tue Mar 5 18:02:13 2013 +0100
Committer:     Peter Rajnoha <[email protected]>
CommitterDate: Wed Mar 6 10:46:36 2013 +0100

config: add support for enhanced config node output

There's a possibility to interconnect the dm_config_node with an ID, which in our case is used to reference the configuration definition ID from config_settings.h. So simply interconnecting struct dm_config_node with struct cfg_def_item.

This patch also adds support for enhanced config node output besides existing "output line by line". This patch adds a possibility to register a callback that gets called *before* the config node is processed line by line (for example to include any headers on output) and *after* the config node is processed line by line (to include any footers on output). Also, it adds the config node reference itself as the callback arg in addition to have a possibility to extract more information from the config node itself if needed when processing the output callback (e.g. the key name, the id, or whether this is a section or a value etc...).

If the config node from lvm.conf/--config tree is recognized and valid, it's always coupled with the config node definition ID from config_settings.h:

struct dm_config_node {
int id;
const char *key;
struct dm_config_node *parent, *sib, *child;
struct dm_config_value *v;
}

For example if the dm_config_node *cn holds "devices/dev" configuration, then the cn->id holds "devices_dev_CFG" ID from config_settings.h, -1 if not found in config_settings.h and 0 if matching has not yet been done.

To support the enhanced config node output, a new structure has been defined in libdevmapper to register it:

struct dm_config_node_out_spec {
dm_config_node_out_fn prefix_fn; /* called before processing config node lines */ dm_config_node_out_fn line_fn; /* called for each config node line */ dm_config_node_out_fn suffix_fn; /* called after processing config node lines */
};

Where dm_config_node_out_fn is:

  typedef int (*dm_config_node_out_fn)(const struct dm_config_node *cn, const char *line, void *baton);

(so in comparison to existing callbacks for config node output, it has an extra dm_config_node *cn arg in addition)

This patch also adds these functions to libdevmapper:
- dm_config_write_node_out
- dm_config_write_one_node_out

...which have exactly the same functionality as their counterparts without the "out" suffix. The "*_out" functions adds the extra hooks for enhanced config output (prefix_fn and suffix_fn mentioned above).

One can still use the old interface for config node output, this is just an enhancement for those who'd like to modify the output more extensively.

---
 lib/config/config.c  |    6 +++
 libdm/libdevmapper.h |   17 +++++++++
 libdm/libdm-config.c |   91 +++++++++++++++++++++++++++++++++-----------------
 3 files changed, 83 insertions(+), 31 deletions(-)
lib/config/config.c +6 -0
index a02f478..5a51a29 100644
@@ -472,6 +472,7 @@ static int _config_def_check_node(const char *vp, char *pvp, char *rp, char *prp
 		if (cn->v) {
 			log_warn_suppress(suppress_messages,
 				"Configuration setting \"%s\" unknown.", rp);
+			cn->id = -1;
 			return 0;
 		}
 
@@ -482,11 +483,14 @@ static int _config_def_check_node(const char *vp, char *pvp, char *rp, char *prp
 		if (!(def = (cfg_def_item_t *) dm_hash_lookup(ht, vp))) {
 			log_warn_suppress(suppress_messages,
 				"Configuration section \"%s\" unknown.", rp);
+			cn->id = -1;
 			return 0;
 		}
 	}
 
 	def->flags |= CFG_USED;
+	cn->id = def->id;
+
 	if (!_config_def_check_node_value(rp, cn->v, def, suppress_messages))
 	return 0;
 
@@ -962,6 +966,8 @@ static struct dm_config_node *_add_def_node(struct dm_config_tree *cft,
 		return NULL;
 	}
 
+	cn->id = def->id;
+
 	if (!(def->type & CFG_TYPE_ARRAY)) {
 		switch (def->type) {
 			case CFG_TYPE_SECTION:
libdm/libdevmapper.h +17 -0
index 8c349c5..0460afa 100644
@@ -1496,6 +1496,7 @@ struct dm_config_value {
 };
 
 struct dm_config_node {
+	int id;
 	const char *key;
 	struct dm_config_node *parent, *sib, *child;
 	struct dm_config_value *v;
@@ -1528,11 +1529,27 @@ struct dm_config_tree *dm_config_remove_cascaded_tree(struct dm_config_tree *cft
 
 void dm_config_destroy(struct dm_config_tree *cft);
 
+/* Simple output line by line. */
 typedef int (*dm_putline_fn)(const char *line, void *baton);
+/* More advaced output with config node reference. */
+typedef int (*dm_config_node_out_fn)(const struct dm_config_node *cn, const char *line, void *baton);
+
+/*
+ * Specification for advanced config node output.
+ */
+struct dm_config_node_out_spec {
+	dm_config_node_out_fn prefix_fn; /* called before processing config node lines */
+	dm_config_node_out_fn line_fn; /* called for each config node line */
+	dm_config_node_out_fn suffix_fn; /* called after processing config node lines */
+};
+
 /* Write the node and any subsequent siblings it has. */
 int dm_config_write_node(const struct dm_config_node *cn, dm_putline_fn putline, void *baton);
+int dm_config_write_node_out(const struct dm_config_node *cn, const struct dm_config_node_out_spec *out_spec, void *baton);
+
 /* Write given node only without subsequent siblings. */
 int dm_config_write_one_node(const struct dm_config_node *cn, dm_putline_fn putline, void *baton);
+int dm_config_write_one_node_out(const struct dm_config_node *cn, const struct dm_config_node_out_spec *out_spec, void *baton);
 
 struct dm_config_node *dm_config_find_node(const struct dm_config_node *cn, const char *path);
 int dm_config_has_node(const struct dm_config_node *cn, const char *path);
libdm/libdm-config.c +60 -31

191 lines not on this page · raw message