Skip to contentSkip to conversation

master - config: add config_source_t type to identify configuration source

PATCH Peter Rajnoha 1 participant 1 message Jul 2, 2013

lore
Share & tools
lore
#0 lore raw
Gitweb:        http://git.fedorahosted.org/git/?p=lvm2.git;a=commitdiff;h=da3ea66a96e83b69da60af1540492d50f11b055f
Commit:        da3ea66a96e83b69da60af1540492d50f11b055f
Parent:        2d784753c08b24e02ea0e6c1423342b355037d86
Author:        Peter Rajnoha <[email protected]>
AuthorDate:    Tue Jun 25 12:25:43 2013 +0200
Committer:     Peter Rajnoha <[email protected]>
CommitterDate: Tue Jul 2 15:19:08 2013 +0200

config: add config_source_t type to identify configuration source

A helper type that helps with identification of the configuration source which makes handling the configuration cascade a bit easier, mainly removing and adding configuration trees to cascade dynamically.

Currently, the possible types are:

CONFIG_UNDEFINED - configuration is not defined yet (not initialized) CONFIG_FILE - one file configuration
CONFIG_MERGED_FILES - configuration that is a result of merging more files into one CONFIG_STRING - configuration string typed on cmd line directly
CONFIG_PROFILE - profile configuration (the new type of configuration, patches will follow...)

Also, generalize existing "remove_overridden_config_tree" to work with configuration type identification in a cascade. Before, it was just the CONFIG_STRING we used. Now, we need some more to add in a cascade (like the CONFIG_PROFILE). So, we have:

  struct dm_config_tree *remove_config_tree_by_source(struct cmd_context *cmd, config_source_t source);
  config_source_t config_get_source_type(struct dm_config_tree *cft);

... for removing the tree by its source type from the cascade and simply getting the source type.

---
 lib/commands/toolcontext.c      |   65 +++++++-------
 lib/config/config.c             |  189 ++++++++++++++++++++++++++++++---------
 lib/config/config.h             |   20 +++--
 lib/filters/filter-persistent.c |    6 +-
 lib/format_text/import.c        |    8 +-
 tools/lvmcmdline.c              |    4 +-
 6 files changed, 204 insertions(+), 88 deletions(-)
lib/commands/toolcontext.c +33 -32
index c7b2451..718fa65 100644
@@ -1,4 +1,4 @@
-/*
+		/*
  * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
  * Copyright (C) 2004-2012 Red Hat, Inc. All rights reserved.
  *
@@ -548,7 +548,7 @@ static int _load_config_file(struct cmd_context *cmd, const char *tag)
 		return 0;
 	}
 
-	if (!(cfl->cft = config_file_open_and_read(config_file)))
+	if (!(cfl->cft = config_file_open_and_read(config_file, CONFIG_FILE)))
 		return_0;
 
 	dm_list_add(&cmd->config_files, &cfl->list);
@@ -568,7 +568,7 @@ static int _init_lvm_conf(struct cmd_context *cmd)
 {
 	/* No config file if LVM_SYSTEM_DIR is empty */
 	if (!*cmd->system_dir) {
-		if (!(cmd->cft = config_file_open(NULL, 0))) {
+		if (!(cmd->cft = config_open(CONFIG_FILE, NULL, 0))) {
 			log_error("Failed to create config tree");
 			return 0;
 		}
@@ -601,7 +601,7 @@ static struct dm_config_tree *_merge_config_files(struct cmd_context *cmd, struc
 
 	/* Replace temporary duplicate copy of lvm.conf */
 	if (cft->root) {
-		if (!(cft = config_file_open(NULL, 0))) {
+		if (!(cft = config_open(CONFIG_MERGED_FILES, NULL, 0))) {
 			log_error("Failed to create config tree");
 			return 0;
 		}
@@ -637,34 +637,33 @@ int config_files_changed(struct cmd_context *cmd)
 	return 0;
 }
 
-/*
- * Returns cmdline config_tree that overrides all others, if present.
- */
-static struct dm_config_tree *_destroy_tag_configs(struct cmd_context *cmd)
+static void _destroy_config(struct cmd_context *cmd)
 {
 	struct config_tree_list *cfl;
-	struct dm_config_tree *cft_cmdline = NULL, *cft;
-
-	cft = dm_config_remove_cascaded_tree(cmd->cft);
-	if (cft) {
-		cft_cmdline = cmd->cft;
-		cmd->cft = cft;
-	}
+	struct dm_config_tree *cft;
 
-	dm_list_iterate_items(cfl, &cmd->config_files) {
-		if (cfl->cft == cmd->cft)
-			cmd->cft = NULL;
-		config_file_destroy(cfl->cft);
-	}
+	/*
+	 * Configuration cascade:
+	 * CONFIG_STRING -> CONFIG_FILE/CONFIG_MERGED_FILES
+	 */
 
-	if (cmd->cft) {
-		config_file_destroy(cmd->cft);
-		cmd->cft = NULL;
-	}
+	/* CONFIG_FILE/CONFIG_MERGED_FILES */
+	if ((cft = remove_config_tree_by_source(cmd, CONFIG_MERGED_FILES)))
+		config_destroy(cft);
+	else
+		remove_config_tree_by_source(cmd, CONFIG_FILE);
 
+	dm_list_iterate_items(cfl, &cmd->config_files)
+		config_destroy(cfl->cft);
 	dm_list_init(&cmd->config_files);
 
-	return cft_cmdline;
+	/* CONFIG_STRING */
+	if ((cft = remove_config_tree_by_source(cmd, CONFIG_STRING)))
+		config_destroy(cft);
+
+	if (cmd->cft)
+		log_error(INTERNAL_ERROR "_destroy_config: "
+			  "cmd config tree not destroyed fully");
 }
 
 static int _init_dev_cache(struct cmd_context *cmd)
@@ -1577,7 +1576,8 @@ int refresh_toolcontext(struct cmd_context *cmd)
 	_destroy_dev_types(cmd);
 	_destroy_tags(cmd);
 
-	cft_cmdline = _destroy_tag_configs(cmd);
+	cft_cmdline = remove_config_tree_by_source(cmd, CONFIG_STRING);
+	_destroy_config(cmd);
 
 	cmd->config_valid = 0;
 
@@ -1665,8 +1665,13 @@ void destroy_toolcontext(struct cmd_context *cmd)
 	_destroy_dev_types(cmd);
 	_destroy_tags(cmd);
 
-	if ((cft_cmdline = _destroy_tag_configs(cmd)))
-		dm_config_destroy(cft_cmdline);
+	if ((cft_cmdline = remove_config_tree_by_source(cmd, CONFIG_STRING)))
+		config_destroy(cft_cmdline);
+	_destroy_config(cmd);
+
+	if (cmd->cft_def_hash)
+		dm_hash_destroy(cmd->cft_def_hash);
+
 	if (cmd->libmem)
 		dm_pool_destroy(cmd->libmem);
 
@@ -1692,10 +1697,6 @@ void destroy_toolcontext(struct cmd_context *cmd)
 		dm_free(cmd->linebuffer);
 	}
 #endif
-
-	if (cmd->cft_def_hash)
-		dm_hash_destroy(cmd->cft_def_hash);
-
 	dm_free(cmd);
 
 	lvmetad_release_token();
lib/config/config.c +148 -41

339 lines not on this page · raw message

lib/config/config.h +14 -6
index 0403924..f4c77ed 100644
@@ -26,6 +26,14 @@
 struct device;
 struct cmd_context;
 
+typedef enum {
+	CONFIG_UNDEFINED,	/* undefined/uninitialized config */
+	CONFIG_FILE,		/* one file config */
+	CONFIG_MERGED_FILES,	/* config that is a result of merging more config files */
+	CONFIG_STRING,		/* config string typed on cmdline using '--config' arg */
+	CONFIG_PROFILE		/* profile config */
+} config_source_t;
+
 #define CFG_PATH_MAX_LEN 64
 
 /*
@@ -109,23 +117,23 @@ enum {
 int config_def_get_path(char *buf, size_t buf_size, int id);
 int config_def_check(struct cmd_context *cmd, int force, int skip, int suppress_messages);
 
-int override_config_tree_from_string(struct cmd_context *cmd,
-				     const char *config_settings);
-struct dm_config_tree *remove_overridden_config_tree(struct cmd_context *cmd);
+int override_config_tree_from_string(struct cmd_context *cmd, const char *config_settings);
+struct dm_config_tree *remove_config_tree_by_source(struct cmd_context *cmd, config_source_t source);
+config_source_t config_get_source_type(struct dm_config_tree *cft);
 
 typedef uint32_t (*checksum_fn_t) (uint32_t initial, const uint8_t *buf, uint32_t size);
 
-struct dm_config_tree *config_file_open(const char *filename, int keep_open);
+struct dm_config_tree *config_open(config_source_t source, const char *filename, int keep_open);
 int config_file_read_fd(struct dm_config_tree *cft, struct device *dev,
 			off_t offset, size_t size, off_t offset2, size_t size2,
 			checksum_fn_t checksum_fn, uint32_t checksum);
 int config_file_read(struct dm_config_tree *cft);
-struct dm_config_tree *config_file_open_and_read(const char *config_file);
+struct dm_config_tree *config_file_open_and_read(const char *config_file, config_source_t source);
 int config_write(struct dm_config_tree *cft,
 		 int withcomment, int withversion,
 		 const char *file, int argc, char **argv);
 struct dm_config_tree *config_def_create_tree(struct config_def_tree_spec *spec);
-void config_file_destroy(struct dm_config_tree *cft);
+void config_destroy(struct dm_config_tree *cft);
 
 time_t config_file_timestamp(struct dm_config_tree *cft);
 int config_file_changed(struct dm_config_tree *cft);
lib/filters/filter-persistent.c +3 -3
index a59529d..2949b93 100644
@@ -114,7 +114,7 @@ int persistent_filter_load(struct dev_filter *f, struct dm_config_tree **cft_out
 		return_0;
 	}
 
-	if (!(cft = config_file_open(pf->file, 1)))
+	if (!(cft = config_open(CONFIG_FILE, pf->file, 1)))
 		return_0;
 
 	if (!config_file_read(cft))
@@ -139,7 +139,7 @@ int persistent_filter_load(struct dev_filter *f, struct dm_config_tree **cft_out
 	if (r && cft_out)
 		*cft_out = cft;
 	else
-		config_file_destroy(cft);
+		config_destroy(cft);
 	return r;
 }
 
@@ -263,7 +263,7 @@ out:
 	fcntl_unlock_file(lockfd);
 
 	if (cft)
-		config_file_destroy(cft);
+		config_destroy(cft);
 
 	return r;
 }
lib/format_text/import.c +4 -4
index 8a05ca9..6b7d589 100644
@@ -46,7 +46,7 @@ const char *text_vgname_import(const struct format_type *fmt,
 
 	_init_text_import();
 
-	if (!(cft = config_file_open(NULL, 0)))
+	if (!(cft = config_open(CONFIG_FILE, NULL, 0)))
 		return_NULL;
 
 	if ((!dev && !config_file_read(cft)) ||
@@ -69,7 +69,7 @@ const char *text_vgname_import(const struct format_type *fmt,
 	}
 
       out:
-	config_file_destroy(cft);
+	config_destroy(cft);
 	return vgname;
 }
 
@@ -92,7 +92,7 @@ struct volume_group *text_vg_import_fd(struct format_instance *fid,
 	*desc = NULL;
 	*when = 0;
 
-	if (!(cft = config_file_open(file, 0)))
+	if (!(cft = config_open(CONFIG_FILE, file, 0)))
 		return_NULL;
 
 	if ((!dev && !config_file_read(cft)) ||
@@ -117,7 +117,7 @@ struct volume_group *text_vg_import_fd(struct format_instance *fid,
 	}
 
       out:
-	config_file_destroy(cft);
+	config_destroy(cft);
 	return vg;
 }
 
tools/lvmcmdline.c +2 -2
index 827b072..0372434 100644
@@ -1081,7 +1081,7 @@ int lvm_run_command(struct cmd_context *cmd, int argc, char **argv)
 	if (arg_count(cmd, config_ARG) || !cmd->config_valid || config_files_changed(cmd)) {
 		/* Reinitialise various settings inc. logging, filters */
 		if (!refresh_toolcontext(cmd)) {
-			old_cft = remove_overridden_config_tree(cmd);
+			old_cft = remove_config_tree_by_source(cmd, CONFIG_STRING);
 			if (old_cft)
 				dm_config_destroy(old_cft);
 			log_error("Updated config file invalid. Aborting.");
@@ -1136,7 +1136,7 @@ int lvm_run_command(struct cmd_context *cmd, int argc, char **argv)
 		lvmcache_destroy(cmd, 1);
 	}
 
-	if ((old_cft = remove_overridden_config_tree(cmd))) {
+	if ((old_cft = remove_config_tree_by_source(cmd, CONFIG_STRING))) {
 		dm_config_destroy(old_cft);
 		/* Move this? */
 		if (!refresh_toolcontext(cmd))