Skip to contentSkip to conversation

dev-peter-config-profiles - config: add config_source_t type to identify configuration source

PATCH Peter Rajnoha 1 participant 1 message Jun 26, 2013

lore
Share & tools
lore
#0 lore raw
Gitweb:        http://git.fedorahosted.org/git/?p=lvm2.git;a=commitdiff;h=6c2bda4b433c55e0af05124f5157977621b93f50
Commit:        6c2bda4b433c55e0af05124f5157977621b93f50
Parent:        e09cffe14e6710ba3bb3598a6719ff3b2d70a440
Author:        Peter Rajnoha <[email protected]>
AuthorDate:    Tue Jun 25 12:25:43 2013 +0200
Committer:     Peter Rajnoha <[email protected]>
CommitterDate: Wed Jun 26 16:53:49 2013 +0200

config: add config_source_t type to identify configuration source

A helper type that helps with identification of the configuration source which make 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 one 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      |   76 +++++++++--------
 lib/config/config.c             |  174 ++++++++++++++++++++++++++++++--------
 lib/config/config.h             |   18 +++-
 lib/filters/filter-persistent.c |    6 +-
 lib/format_text/import.c        |    8 +-
 tools/lvmcmdline.c              |    4 +-
 6 files changed, 200 insertions(+), 86 deletions(-)
lib/commands/toolcontext.c +41 -35
index e0da663..4299d8c 100644
@@ -37,6 +37,7 @@
 #include "lvmetad.h"
 #include "dev-cache.h"
 #include "archiver.h"
+#include "assert.h"
 
 #ifdef HAVE_LIBDL
 #include "sharedlib.h"
@@ -528,19 +529,21 @@ static int _init_tags(struct cmd_context *cmd, struct dm_config_tree *cft)
 	return 1;
 }
 
-static struct dm_config_tree *_open_and_read_config_file(const char *config_file)
+static struct dm_config_tree *_open_and_read_config_file(const char *config_file,
+							 config_source_t source)
 {
 	struct dm_config_tree *cft;
 	struct stat info;
 
-	if (!(cft = config_file_open(config_file, 0))) {
+	if (!(cft = config_open(source, config_file, 0))) {
 		log_error("config_tree allocation failed");
 		return NULL;
 	}
 
 	/* Is there a config file? */
 	if (stat(config_file, &info) == -1) {
-		if (errno == ENOENT)
+		/* Profile file must be present! */
+		if (errno == ENOENT && (source != CONFIG_PROFILE))
 			return cft;
 		log_sys_error("stat", config_file);
 		goto bad;
@@ -554,7 +557,7 @@ static struct dm_config_tree *_open_and_read_config_file(const char *config_file
 
 	return cft;
 bad:
-	config_file_destroy(cft);
+	config_destroy(cft);
 	return NULL;
 }
 
@@ -579,7 +582,7 @@ static int _load_config_file(struct cmd_context *cmd, const char *tag)
 		return 0;
 	}
 
-	if (!(cfl->cft = _open_and_read_config_file(config_file)))
+	if (!(cfl->cft = _open_and_read_config_file(config_file, CONFIG_FILE)))
 		return_0;
 
 	dm_list_add(&cmd->config_files, &cfl->list);
@@ -599,7 +602,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;
 		}
@@ -632,7 +635,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;
 		}
@@ -668,34 +671,35 @@ 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
+	 *
+	 * CONFIG_STRING - the configuration string passed in command line via '--config'
+	 * CONFIG_MERGED_FILES - merged master lvm.conf and tag configurations
+	 * CONFIG_FILE - master lvm.conf only
+	 */
 
-	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);
+
+	assert(!cmd->cft);
 }
 
 static int _init_dev_cache(struct cmd_context *cmd)
@@ -1608,7 +1612,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;
 
@@ -1696,8 +1701,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);
 
@@ -1723,10 +1733,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 +137 -37

304 lines not on this page · raw message

lib/config/config.h +13 -5
index 155d69a..2f5cee6 100644
@@ -26,6 +26,14 @@
 struct device;
 struct cmd_context;
 
+typedef enum {
+	CONFIG_UNDEFINED,
+	CONFIG_FILE,
+	CONFIG_MERGED_FILES,
+	CONFIG_STRING,
+	CONFIG_PROFILE
+} config_source_t;
+
 #define CFG_PATH_MAX_LEN 64
 
 /*
@@ -109,13 +117,13 @@ 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);
@@ -124,7 +132,7 @@ 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 89ead2c..51bbcd2 100644
@@ -1083,7 +1083,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.");
@@ -1138,7 +1138,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))