Skip to contentSkip to conversation

master - config: add structs to represent config definition and register config_settings.h content

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=a3d891a29042e51a0f43ed02273a6dce194d6569
Commit:        a3d891a29042e51a0f43ed02273a6dce194d6569
Parent:        e947c362dd0ae1da2d76925fe6b38244c5f46d25
Author:        Peter Rajnoha <[email protected]>
AuthorDate:    Tue Mar 5 16:49:42 2013 +0100
Committer:     Peter Rajnoha <[email protected]>
CommitterDate: Wed Mar 6 10:14:33 2013 +0100

config: add structs to represent config definition and register config_settings.h content

This patch adds basic structures that encapsulate the config_settings.h content - it takes each item and puts it in structures:

- cfg_def_type_t to define config item type

- cfg_def_value_t to define config item (default) value

- flags used to define the nature and use of the config item:
- CFG_NAME_VARIABLE for items with variable names (e.g. tags)
- CFG_ALLOW_EMPTY for items where empty value is allowed
- CFG_ADVANCED for items which are considered as "advanced settings"
- CFG_UNSUPPORTED for items which are not officially supported (config options mostly for internal use and testing/debugging)

- cfg_def_item_t to encapsulate the whole definition of the config definition itself

Each config item is referenced by named ID, e.g. "devices_dir_CFG" instead of directly typing the path "devices/dir" as it was before.

This patch also adds cfg_def_get_path helper function to get the config setting path up to the root for given config ID (it returns the path in form of "abc/def/.../xyz" where the "abc" is the topmost element).

---
 lib/config/config.c |   45 +++++++++++++++++++++++++++++++++++++
 lib/config/config.h |   62 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 107 insertions(+), 0 deletions(-)
lib/config/config.c +45 -0
index 0820594..f2ee9e7 100644
@@ -38,6 +38,21 @@ struct config_file {
 	struct device *dev;
 };
 
+static char _cfg_path[CFG_PATH_MAX_LEN];
+
+/*
+ * Map each ID to respective definition of the configuration item.
+ */
+static struct cfg_def_item _cfg_def_items[CFG_COUNT + 1] = {
+#define cfg_section(id, name, parent, flags, since_version, comment) {id, parent, name, CFG_TYPE_SECTION, {0}, flags, since_version, comment},
+#define cfg(id, name, parent, flags, type, default_value, since_version, comment) {id, parent, name, type, {.v_##type = default_value}, flags, since_version, comment},
+#define cfg_array(id, name, parent, flags, types, default_value, since_version, comment) {id, parent, name, CFG_TYPE_ARRAY | types, {.v_CFG_TYPE_STRING = default_value}, flags, since_version, comment},
+#include "config_settings.h"
+#undef cfg_section
+#undef cfg
+#undef cfg_array
+};
+
 /*
  * public interface
  */
@@ -289,6 +304,36 @@ time_t config_file_timestamp(struct dm_config_tree *cft)
 	return cf->timestamp;
 }
 
+#define cfg_def_get_item_p(id) (&_cfg_def_items[id])
+#define cfg_def_get_default_value(item,type) item->default_value.v_##type
+#define cfg_def_get_path(item) (_cfg_def_make_path(_cfg_path,CFG_PATH_MAX_LEN,item->id,item),_cfg_path)
+
+static int _cfg_def_make_path(char *buf, size_t buf_size, int id, cfg_def_item_t *item)
+{
+	int parent_id = item->parent;
+	int count, n;
+
+	if (id == parent_id)
+		return 0;
+
+	count = _cfg_def_make_path(buf, buf_size, parent_id, cfg_def_get_item_p(parent_id));
+	if ((n = dm_snprintf(buf + count, buf_size - count, "%s%s",
+			     count ? "/" : "",
+			     item->flags & CFG_NAME_VARIABLE ? "#" : item->name)) < 0) {
+		log_error(INTERNAL_ERROR "_cfg_def_make_path: supplied buffer too small for %s/%s",
+					  cfg_def_get_item_p(parent_id)->name, item->name);
+		buf[0] = '\0';
+		return 0;
+	}
+
+	return count + n;
+}
+
+int config_def_get_path(char *buf, size_t buf_size, int id)
+{
+	return _cfg_def_make_path(buf, buf_size, id, cfg_def_get_item_p(id));
+}
+
 const struct dm_config_node *find_config_tree_node(struct cmd_context *cmd,
 						   const char *path)
 {
lib/config/config.h +62 -0

74 lines not on this page · raw message