/*
* Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
* Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
*
* This file is part of LVM2.
*
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU Lesser General Public License v.2.1.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* Return 1 if config files ought to be reloaded
*/
int config_file_changed(struct config_tree *cft)
{
struct cs *c = (struct cs *) cft;
struct stat info;
if (!c->filename)
return 0;
if (stat(c->filename, &info) == -1) {
/* Ignore a deleted config file: still use original data */
if (errno == ENOENT) {
if (!c->exists)
return 0;
log_very_verbose("Config file %s has disappeared!",
c->filename);
goto reload;
}
log_sys_error("stat", c->filename);
log_error("Failed to reload configuration files");
return 0;
}
if (!S_ISREG(info.st_mode)) {
log_error("Configuration file %s is not a regular file",
c->filename);
goto reload;
}
/* Unchanged? */
if (c->timestamp == info.st_ctime)
return 0;
switch (v->type) {
case CFG_STRING:
if (!(buf = alloca(escaped_len(v->v.str)))) {
log_error("temporary stack allocation for a config "
"string failed");
return 0;
}
line_append("\"%s\"", escape_double_quotes(buf, v->v.str));
break;
case CFG_FLOAT:
line_append("%f", v->v.r);
break;
case CFG_INT:
line_append("%" PRId64, v->v.i);
break;
case CFG_EMPTY_ARRAY:
line_append("[]");
break;
default:
log_error("_write_value: Unknown value type: %d", v->type);
}
return 1;
}
static int _write_config(const struct config_node *n, int only_one,
struct output_line *outline, int level)
{
char space[MAX_INDENT + 1];
int l = (level < MAX_INDENT) ? level : MAX_INDENT;
int i;
if (!n)
return 1;
for (i = 0; i < l; i++)
space[i] = '\t';
space[i] = '\0';
do {
if (!_line_start(outline))
return_0;
line_append("%s%s", space, n->key);
if (!n->v) {
/* it's a sub section */
line_append(" {");
if (!_line_end(outline))
return_0;
_write_config(n->child, 0, outline, level + 1);
if (!_line_start(outline))
return_0;
line_append("%s}", space);
} else {
/* it's a value */
struct config_value *v = n->v;
line_append("=");
if (v->next) {
line_append("[");
while (v) {
if (!_write_value(outline, v))
return_0;
v = v->next;
if (v)
line_append(", ");
}
line_append("]");
} else
if (!_write_value(outline, v))
return_0;
}
if (!_line_end(outline))
return_0;
n = n->sib;
} while (n && !only_one);
/* FIXME: add error checking */
return 1;
}
log_verbose("Dumping configuration to %s", file);
if (!argc) {
if (!_write_config(cft->root, 0, &outline, 0)) {
log_error("Failure while writing to %s", file);
r = 0;
}
} else while (argc--) {
if ((cn = find_config_node(cft->root, *argv))) {
if (!_write_config(cn, 1, &outline, 0)) {
log_error("Failure while writing to %s", file);
r = 0;
}
} else {
log_error("Configuration node %s not found", *argv);
r = 0;
}
argv++;
}
if (outline.fp && lvm_fclose(outline.fp, file)) {
stack;
r = 0;
}
if (p->t == TOK_SECTION_B) {
match(TOK_SECTION_B);
while (p->t != TOK_SECTION_E) {
if (!(n = _section(p)))
return_0;
if (!root->child)
root->child = n;
else
l->sib = n;
n->parent = root;
l = n;
}
match(TOK_SECTION_E);
} else {
match(TOK_EQ);
if (!(root->v = _value(p)))
return_0;
}
return root;
}
static struct config_value *_value(struct parser *p)
{
/* '[' TYPE* ']' | TYPE */
struct config_value *h = NULL, *l, *ll = NULL;
if (p->t == TOK_ARRAY_B) {
match(TOK_ARRAY_B);
while (p->t != TOK_ARRAY_E) {
if (!(l = _type(p)))
return_0;
if (!h)
h = l;
else
ll->next = l;
ll = l;
if (p->t == TOK_COMMA)
match(TOK_COMMA);
}
match(TOK_ARRAY_E);
/*
* Special case for an empty array.
*/
if (!h) {
if (!(h = _create_value(p->mem)))
return NULL;
/* Should next token be interpreted as value instead of identifier? */
if (tok_prev == TOK_EQ || tok_prev == TOK_ARRAY_B ||
tok_prev == TOK_COMMA)
values_allowed = 1;
p->t = TOK_INT; /* fudge so the fall through for
floats works */
switch (*p->te) {
case SECTION_B_CHAR:
p->t = TOK_SECTION_B;
p->te++;
break;
case SECTION_E_CHAR:
p->t = TOK_SECTION_E;
p->te++;
break;
case '\'':
p->t = TOK_STRING;
p->te++;
while ((p->te != p->fe) && (*p->te) && (*p->te != '\''))
p->te++;
if ((p->te != p->fe) && (*p->te))
p->te++;
break;
case '.':
p->t = TOK_FLOAT;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '+':
case '-':
if (values_allowed) {
p->te++;
while ((p->te != p->fe) && (*p->te)) {
if (*p->te == '.') {
if (p->t == TOK_FLOAT)
break;
p->t = TOK_FLOAT;
} else if (!isdigit((int) *p->te))
break;
p->te++;
}
break;
}
for (tv = tn->v; tv; tv = tv->next) {
if (tv->type != CFG_STRING)
continue;
str = tv->v.str;
if (*str == '@')
str++;
if (!*str)
continue;
if (str_list_match_item(tags, str))
return 1;
}
return 0;
}
/* Destructively merge a new config tree into an existing one */
int merge_config_tree(struct cmd_context *cmd, struct config_tree *cft,
struct config_tree *newdata)
{
struct config_node *root = cft->root;
struct config_node *cn, *nextn, *oldn, *tn, *cn2;
for (cn = newdata->root; cn; cn = nextn) {
nextn = cn->sib;
/* Ignore tags section */
if (!strcmp(cn->key, "tags"))
continue;
/* If there's a tags node, skip if host tags don't match */
if ((tn = find_config_node(cn->child, "tags"))) {
if (!_match_host_tags(&cmd->tags, tn))
continue;
}
if (!(oldn = find_config_node(root, cn->key))) {
_insert_config_node(&cft->root, cn);
/* Remove any "tags" nodes */
for (cn2 = cn->child; cn2; cn2 = cn2->sib) {
if (!strcmp(cn2->key, "tags")) {
cn->child = cn2->sib;
continue;
}
if (cn2->sib && !strcmp(cn2->sib->key, "tags")) {
cn2->sib = cn2->sib->sib;
continue;
}
}
continue;
}
_merge_section(oldn, cn);
}
return 1;
}
/*
* Convert a token type to the char it represents.
*/
static char _token_type_to_char(int type)
{
switch (type) {
case TOK_SECTION_B:
return SECTION_B_CHAR;
case TOK_SECTION_E:
return SECTION_E_CHAR;
default:
return 0;
}
}
/*
* Returns:
* # of 'type' tokens in 'str'.
*/
static unsigned _count_tokens(const char *str, unsigned len, int type)
{
char c;
c = _token_type_to_char(type);
return count_chars(str, len, c);
}
const char *config_parent_name(const struct config_node *n)
{
return (n->parent ? n->parent->key : "(root)");
}
/*
* Heuristic function to make a quick guess as to whether a text
* region probably contains a valid config "section". (Useful for
* scanning areas of the disk for old metadata.)
* Config sections contain various tokens, may contain other sections
* and strings, and are delimited by begin (type 'TOK_SECTION_B') and
* end (type 'TOK_SECTION_E') tokens. As a quick heuristic, we just
* count the number of begin and end tokens, and see if they are
* non-zero and the counts match.
* Full validation of the section should be done with another function
* (for example, read_config_fd).
*
* Returns:
* 0 - probably is not a valid config section
* 1 - probably _is_ a valid config section
*/
unsigned maybe_config_section(const char *str, unsigned len)
{
int begin_count;
int end_count;