From 2710477debeb6707d088f114299a22854eed00ab Mon Sep 17 00:00:00 2001 From: Petr Rockai Date: Mon, 18 Jul 2011 13:26:08 +0000 Subject: [PATCH] Slightly refactor the config code to allow better reuse (no functional change). --- lib/cache/lvmcache.c | 3 +-- lib/config/config.c | 50 ++++++++++++++++++++++++++++++++++++------- lib/config/config.h | 13 ++++++++--- lib/unknown/unknown.c | 2 +- 4 files changed, 54 insertions(+), 14 deletions(-) diff --git a/lib/cache/lvmcache.c b/lib/cache/lvmcache.c index b448acabc..527d79b42 100644 --- a/lib/cache/lvmcache.c +++ b/lib/cache/lvmcache.c @@ -671,8 +671,7 @@ struct volume_group *lvmcache_get_vg(const char *vgid, unsigned precommitted) /* Build config tree from vgmetadata, if not yet cached */ if (!vginfo->cft && !(vginfo->cft = - create_config_tree_from_string(fid->fmt->cmd, - vginfo->vgmetadata))) + create_config_tree_from_string(vginfo->vgmetadata))) goto_bad; if (!(vg = import_vg_from_config_tree(vginfo->cft, fid))) diff --git a/lib/config/config.c b/lib/config/config.c index cc61bbfd1..37dbfc7fc 100644 --- a/lib/config/config.c +++ b/lib/config/config.c @@ -2,7 +2,6 @@ * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved. * Copyright (C) 2004-2011 Red Hat, Inc. All rights reserved. * - * * This file is part of LVM2. * * This copyrighted material is made available to anyone wishing to use, @@ -160,8 +159,7 @@ static int _parse_config_file(struct parser *p, struct config_tree *cft) return 1; } -struct config_tree *create_config_tree_from_string(struct cmd_context *cmd __attribute__((unused)), - const char *config_settings) +struct config_tree *create_config_tree_from_string(const char *config_settings) { struct cs *c; struct config_tree *cft; @@ -192,7 +190,7 @@ struct config_tree *create_config_tree_from_string(struct cmd_context *cmd __att int override_config_tree_from_string(struct cmd_context *cmd, const char *config_settings) { - if (!(cmd->cft_override = create_config_tree_from_string(cmd,config_settings))) { + if (!(cmd->cft_override = create_config_tree_from_string(config_settings))) { log_error("Failed to set overridden configuration entries."); return 1; } @@ -1365,8 +1363,8 @@ static struct config_value *_clone_config_value(struct dm_pool *mem, const struc return new_cv; } -struct config_node *clone_config_node(struct dm_pool *mem, const struct config_node *cn, - int siblings) +struct config_node *clone_config_node_with_mem(struct dm_pool *mem, const struct config_node *cn, + int siblings) { struct config_node *new_cn; @@ -1384,9 +1382,45 @@ struct config_node *clone_config_node(struct dm_pool *mem, const struct config_n } if ((cn->v && !(new_cn->v = _clone_config_value(mem, cn->v))) || - (cn->child && !(new_cn->child = clone_config_node(mem, cn->child, 1))) || - (siblings && cn->sib && !(new_cn->sib = clone_config_node(mem, cn->sib, siblings)))) + (cn->child && !(new_cn->child = clone_config_node_with_mem(mem, cn->child, 1))) || + (siblings && cn->sib && !(new_cn->sib = clone_config_node_with_mem(mem, cn->sib, siblings)))) return_NULL; /* 'new_cn' released with mem pool */ return new_cn; } + +struct config_node *clone_config_node(struct config_tree *cft, const struct config_node *node, int sib) +{ + struct cs *c = (struct cs *) cft; + return clone_config_node_with_mem(c->mem, node, sib); +} + +struct config_node *create_config_node(struct config_tree *cft, const char *key) +{ + struct cs *c = (struct cs *) cft; + struct config_node *cn; + + if (!(cn = _create_node(c->mem))) { + log_error("Failed to create config node."); + return NULL; + } + if (!(cn->key = dm_pool_strdup(c->mem, key))) { + log_error("Failed to create config node's key."); + return NULL; + } + if (!(cn->v = _create_value(c->mem))) { + log_error("Failed to create config node's value."); + return NULL; + } + cn->parent = NULL; + cn->v->type = CFG_INT; + cn->v->v.i = 0; + cn->v->next = NULL; + return cn; +} + +struct dm_pool *config_tree_memory(struct config_tree *cft) +{ + struct cs *c = (struct cs *) cft; + return c->mem; +} diff --git a/lib/config/config.h b/lib/config/config.h index ae0c6e613..c34d6e6ac 100644 --- a/lib/config/config.h +++ b/lib/config/config.h @@ -54,8 +54,8 @@ struct config_tree_list { }; struct config_tree *create_config_tree(const char *filename, int keep_open); -struct config_tree *create_config_tree_from_string(struct cmd_context *cmd, - const char *config_settings); +struct config_tree *create_config_tree_from_string(const char *config_settings); + int override_config_tree_from_string(struct cmd_context *cmd, const char *config_settings); void destroy_config_tree(struct config_tree *cft); @@ -120,6 +120,13 @@ unsigned maybe_config_section(const char *str, unsigned len); const char *config_parent_name(const struct config_node *n); -struct config_node *clone_config_node(struct dm_pool *mem, const struct config_node *cn, +struct config_node *clone_config_node_with_mem(struct dm_pool *mem, + const struct config_node *node, + int siblings); +struct config_node *create_config_node(struct config_tree *cft, const char *key); +struct config_node *clone_config_node(struct config_tree *cft, const struct config_node *cn, int siblings); + +struct dm_pool *config_tree_memory(struct config_tree *cft); + #endif diff --git a/lib/unknown/unknown.c b/lib/unknown/unknown.c index 2fbd11c1d..105da98b9 100644 --- a/lib/unknown/unknown.c +++ b/lib/unknown/unknown.c @@ -42,7 +42,7 @@ static int _unknown_text_import(struct lv_segment *seg, const struct config_node if (!strcmp(current->key, "type") || !strcmp(current->key, "start_extent") || !strcmp(current->key, "tags") || !strcmp(current->key, "extent_count")) continue; - new = clone_config_node(seg->lv->vg->vgmem, current, 0); + new = clone_config_node_with_mem(seg->lv->vg->vgmem, current, 0); if (!new) return_0; if (last) -- 2.43.5