diff options
Diffstat (limited to 'lib/config/config.c')
-rw-r--r-- | lib/config/config.c | 543 |
1 files changed, 482 insertions, 61 deletions
diff --git a/lib/config/config.c b/lib/config/config.c index 58efb3131..ac1049d1e 100644 --- a/lib/config/config.c +++ b/lib/config/config.c @@ -53,7 +53,7 @@ struct config_file { struct config_source { config_source_t type; - time_t timestamp; + struct timespec timestamp; union { struct config_file *file; struct config_file *profile; @@ -65,11 +65,11 @@ struct config_source { * 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_runtime(id, name, parent, flags, type, since_version, comment) {id, parent, name, type, {.fn_##type = get_default_##id}, flags | CFG_DEFAULT_RUN_TIME, 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}, -#define cfg_array_runtime(id, name, parent, flags, types, since_version, comment) {id, parent, name, CFG_TYPE_ARRAY | types, {.fn_CFG_TYPE_STRING = get_default_##id}, flags | CFG_DEFAULT_RUN_TIME, since_version, comment}, +#define cfg_section(id, name, parent, flags, since_version, deprecated_since_version, deprecation_comment, comment) {id, parent, name, CFG_TYPE_SECTION, {0}, flags, since_version, {0}, deprecated_since_version, deprecation_comment, comment}, +#define cfg(id, name, parent, flags, type, default_value, since_version, unconfigured_value, deprecated_since_version, deprecation_comment, comment) {id, parent, name, type, {.v_##type = default_value}, flags, since_version, {.v_UNCONFIGURED = unconfigured_value}, deprecated_since_version, deprecation_comment, comment}, +#define cfg_runtime(id, name, parent, flags, type, since_version, deprecated_since_version, deprecation_comment, comment) {id, parent, name, type, {.fn_##type = get_default_##id}, flags | CFG_DEFAULT_RUN_TIME, since_version, {.fn_UNCONFIGURED = get_default_unconfigured_##id}, deprecated_since_version, deprecation_comment, comment}, +#define cfg_array(id, name, parent, flags, types, default_value, since_version, unconfigured_value, deprecated_since_version, deprecation_comment, comment) {id, parent, name, CFG_TYPE_ARRAY | types, {.v_CFG_TYPE_STRING = default_value}, flags, since_version, {.v_UNCONFIGURED = unconfigured_value}, deprecated_since_version, deprecation_comment, comment}, +#define cfg_array_runtime(id, name, parent, flags, types, since_version, deprecated_since_version, deprecation_comment, comment) {id, parent, name, CFG_TYPE_ARRAY | types, {.fn_CFG_TYPE_STRING = get_default_##id}, flags | CFG_DEFAULT_RUN_TIME, since_version, {.fn_UNCONFIGURED = get_default_unconfigured_##id}, deprecated_since_version, deprecation_comment, comment}, #include "config_settings.h" #undef cfg_section #undef cfg @@ -173,7 +173,7 @@ int config_file_check(struct dm_config_tree *cft, const char **filename, struct return 0; } - cs->timestamp = info->st_ctime; + lvm_stat_ctim(&cs->timestamp, info); cf->exists = 1; cf->st_size = info->st_size; @@ -193,6 +193,7 @@ int config_file_changed(struct dm_config_tree *cft) struct config_source *cs = dm_config_get_custom(cft); struct config_file *cf; struct stat info; + struct timespec ts; if (cs->type != CONFIG_FILE) { log_error(INTERNAL_ERROR "config_file_changed: expected file config source, " @@ -226,7 +227,9 @@ int config_file_changed(struct dm_config_tree *cft) } /* Unchanged? */ - if (cs->timestamp == info.st_ctime && cf->st_size == info.st_size) + lvm_stat_ctim(&ts, &info); + if ((timespeccmp(&cs->timestamp, &ts, ==)) && + cf->st_size == info.st_size) return 0; reload: @@ -478,9 +481,15 @@ int override_config_tree_from_profile(struct cmd_context *cmd, return 0; } +/* + * When checksum_only is set, the checksum of buffer is only matched + * and function avoids parsing of mda into config tree which + * remains unmodified and should not be used. + */ 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) + checksum_fn_t checksum_fn, uint32_t checksum, + int checksum_only) { char *fb, *fe; int r = 0; @@ -529,9 +538,11 @@ int config_file_read_fd(struct dm_config_tree *cft, struct device *dev, goto out; } - fe = fb + size + size2; - if (!dm_config_parse(cft, fb, fe)) - goto_out; + if (!checksum_only) { + fe = fb + size + size2; + if (!dm_config_parse(cft, fb, fe)) + goto_out; + } r = 1; @@ -570,12 +581,15 @@ int config_file_read(struct dm_config_tree *cft) if (!(cf->dev = dev_create_file(filename, NULL, NULL, 1))) return_0; - if (!dev_open_readonly_buffered(cf->dev)) + if (!dev_open_readonly_buffered(cf->dev)) { + dev_destroy_file(cf->dev); + cf->dev = NULL; return_0; + } } r = config_file_read_fd(cft, cf->dev, 0, (size_t) info.st_size, 0, 0, - (checksum_fn_t) NULL, 0); + (checksum_fn_t) NULL, 0, 0); if (!cf->keep_open) { if (!dev_close(cf->dev)) @@ -586,13 +600,14 @@ int config_file_read(struct dm_config_tree *cft) return r; } -time_t config_file_timestamp(struct dm_config_tree *cft) +struct timespec config_file_timestamp(struct dm_config_tree *cft) { struct config_source *cs = dm_config_get_custom(cft); return cs->timestamp; } #define cfg_def_get_item_p(id) (&_cfg_def_items[id]) +#define cfg_def_get_default_unconfigured_value_hint(cmd,item) ((item->flags & CFG_DEFAULT_RUN_TIME) ? item->default_unconfigured_value.fn_UNCONFIGURED(cmd) : item->default_unconfigured_value.v_UNCONFIGURED) #define cfg_def_get_default_value_hint(cmd,item,type,profile) ((item->flags & CFG_DEFAULT_RUN_TIME) ? item->default_value.fn_##type(cmd,profile) : item->default_value.v_##type) #define cfg_def_get_default_value(cmd,item,type,profile) (item->flags & CFG_DEFAULT_UNDEFINED ? 0 : cfg_def_get_default_value_hint(cmd,item,type,profile)) @@ -649,27 +664,33 @@ static void _log_type_error(const char *path, cfg_def_type_t actual, _get_type_name(actual_type_name, sizeof(actual_type_name), actual); _get_type_name(expected_type_name, sizeof(expected_type_name), expected); - log_warn_suppress(suppress_messages, "Configuration setting \"%s\" has invalid type. " - "Found%s, expected%s.", path, + log_warn_suppress(suppress_messages, "WARNING: Configuration setting \"%s\" has invalid type. " + "Found%s but expected%s.", path, actual_type_name, expected_type_name); } -static struct dm_config_value *_get_def_array_values(struct dm_config_tree *cft, - const cfg_def_item_t *def) +static struct dm_config_value *_get_def_array_values(struct cmd_context *cmd, + struct dm_config_tree *cft, + const cfg_def_item_t *def, + uint32_t format_flags) { + const char *def_enc_value; char *enc_value, *token, *p, *r; struct dm_config_value *array = NULL, *v = NULL, *oldv = NULL; - if (!def->default_value.v_CFG_TYPE_STRING) { + def_enc_value = cfg_def_get_default_value(cmd, def, CFG_TYPE_ARRAY, NULL); + + if (!def_enc_value) { if (!(array = dm_config_create_value(cft))) { log_error("Failed to create default empty array for %s.", def->name); return NULL; } array->type = DM_CFG_EMPTY_ARRAY; + dm_config_value_set_format_flags(array, format_flags); return array; } - if (!(p = token = enc_value = dm_strdup(def->default_value.v_CFG_TYPE_STRING))) { + if (!(p = token = enc_value = dm_strdup(def_enc_value))) { log_error("_get_def_array_values: dm_strdup failed"); return NULL; } @@ -698,6 +719,9 @@ static struct dm_config_value *_get_def_array_values(struct dm_config_tree *cft, dm_free(enc_value); return NULL; } + + dm_config_value_set_format_flags(v, format_flags); + if (oldv) oldv->next = v; if (!array) @@ -790,6 +814,11 @@ static int _config_def_check_node_single_value(struct cft_check_handle *handle, } else if (!(def->type & CFG_TYPE_STRING)) { _log_type_error(rp, CFG_TYPE_STRING, def->type, handle->suppress_messages); return 0; + } else if (!(def->flags & CFG_ALLOW_EMPTY) && !*v->v.str) { + log_warn_suppress(handle->suppress_messages, + "Configuration setting \"%s\" invalid. " + "It cannot be set to an empty value.", rp); + return 0; } break; default: ; @@ -809,6 +838,12 @@ static int _check_value_differs_from_default(struct cft_check_handle *handle, float f; const char *str; + if ((handle->ignoreunsupported && (def->flags & CFG_UNSUPPORTED)) || + (handle->ignoreadvanced && (def->flags & CFG_ADVANCED))) { + diff = 0; + goto out; + } + /* if default value is undefined, the value used differs from default */ if (def->flags & CFG_DEFAULT_UNDEFINED) { diff = 1; @@ -816,7 +851,7 @@ static int _check_value_differs_from_default(struct cft_check_handle *handle, } if (!v_def && (def->type & CFG_TYPE_ARRAY)) { - if (!(v_def_array = v_def_iter = _get_def_array_values(handle->cft, def))) + if (!(v_def_array = v_def_iter = _get_def_array_values(handle->cmd, handle->cft, def, 0))) return_0; do { /* iterate over each element of the array and check its value */ @@ -1008,9 +1043,14 @@ static int _config_def_check_tree(struct cft_check_handle *handle, size_t buf_size, struct dm_config_node *root) { struct dm_config_node *cn; + cfg_def_item_t *def; int valid, r = 1; size_t len; + def = cfg_def_get_item_p(root->id); + if (def->flags & CFG_SECTION_NO_CHECK) + return 1; + for (cn = root->child; cn; cn = cn->sib) { if ((valid = _config_def_check_node(handle, vp, pvp, rp, prp, buf_size, cn)) && !cn->v) { @@ -1140,6 +1180,29 @@ static int _apply_local_profile(struct cmd_context *cmd, struct profile *profile return override_config_tree_from_profile(cmd, profile); } +static int _config_disabled(struct cmd_context *cmd, cfg_def_item_t *item, const char *path) +{ + if ((item->flags & CFG_DISABLED) && dm_config_tree_find_node(cmd->cft, path)) { + log_warn("WARNING: Configuration setting %s is disabled. Using default value.", path); + return 1; + } + + return 0; +} + +const struct dm_config_node *find_config_node(struct cmd_context *cmd, struct dm_config_tree *cft, int id) +{ + cfg_def_item_t *item = cfg_def_get_item_p(id); + char path[CFG_PATH_MAX_LEN]; + const struct dm_config_node *cn; + + _cfg_def_make_path(path, sizeof(path), item->id, item, 0); + + cn = dm_config_tree_find_node(cft, path); + + return cn; +} + const struct dm_config_node *find_config_tree_node(struct cmd_context *cmd, int id, struct profile *profile) { cfg_def_item_t *item = cfg_def_get_item_p(id); @@ -1171,7 +1234,8 @@ const char *find_config_tree_str(struct cmd_context *cmd, int id, struct profile if (item->type != CFG_TYPE_STRING) log_error(INTERNAL_ERROR "%s cfg tree element not declared as string.", path); - str = dm_config_tree_find_str(cmd->cft, path, cfg_def_get_default_value(cmd, item, CFG_TYPE_STRING, profile)); + str = _config_disabled(cmd, item, path) ? cfg_def_get_default_value(cmd, item, CFG_TYPE_STRING, profile) + : dm_config_tree_find_str(cmd->cft, path, cfg_def_get_default_value(cmd, item, CFG_TYPE_STRING, profile)); if (profile_applied) remove_config_tree_by_source(cmd, profile->source); @@ -1194,7 +1258,8 @@ const char *find_config_tree_str_allow_empty(struct cmd_context *cmd, int id, st if (!(item->flags & CFG_ALLOW_EMPTY)) log_error(INTERNAL_ERROR "%s cfg tree element not declared to allow empty values.", path); - str = dm_config_tree_find_str_allow_empty(cmd->cft, path, cfg_def_get_default_value(cmd, item, CFG_TYPE_STRING, profile)); + str = _config_disabled(cmd, item, path) ? cfg_def_get_default_value(cmd, item, CFG_TYPE_STRING, profile) + : dm_config_tree_find_str_allow_empty(cmd->cft, path, cfg_def_get_default_value(cmd, item, CFG_TYPE_STRING, profile)); if (profile_applied) remove_config_tree_by_source(cmd, profile->source); @@ -1215,7 +1280,8 @@ int find_config_tree_int(struct cmd_context *cmd, int id, struct profile *profil if (item->type != CFG_TYPE_INT) log_error(INTERNAL_ERROR "%s cfg tree element not declared as integer.", path); - i = dm_config_tree_find_int(cmd->cft, path, cfg_def_get_default_value(cmd, item, CFG_TYPE_INT, profile)); + i = _config_disabled(cmd, item, path) ? cfg_def_get_default_value(cmd, item, CFG_TYPE_INT, profile) + : dm_config_tree_find_int(cmd->cft, path, cfg_def_get_default_value(cmd, item, CFG_TYPE_INT, profile)); if (profile_applied) remove_config_tree_by_source(cmd, profile->source); @@ -1236,7 +1302,8 @@ int64_t find_config_tree_int64(struct cmd_context *cmd, int id, struct profile * if (item->type != CFG_TYPE_INT) log_error(INTERNAL_ERROR "%s cfg tree element not declared as integer.", path); - i64 = dm_config_tree_find_int64(cmd->cft, path, cfg_def_get_default_value(cmd, item, CFG_TYPE_INT, profile)); + i64 = _config_disabled(cmd, item, path) ? cfg_def_get_default_value(cmd, item, CFG_TYPE_INT, profile) + : dm_config_tree_find_int64(cmd->cft, path, cfg_def_get_default_value(cmd, item, CFG_TYPE_INT, profile)); if (profile_applied) remove_config_tree_by_source(cmd, profile->source); @@ -1257,7 +1324,8 @@ float find_config_tree_float(struct cmd_context *cmd, int id, struct profile *pr if (item->type != CFG_TYPE_FLOAT) log_error(INTERNAL_ERROR "%s cfg tree element not declared as float.", path); - f = dm_config_tree_find_float(cmd->cft, path, cfg_def_get_default_value(cmd, item, CFG_TYPE_FLOAT, profile)); + f = _config_disabled(cmd, item, path) ? cfg_def_get_default_value(cmd, item, CFG_TYPE_FLOAT, profile) + : dm_config_tree_find_float(cmd->cft, path, cfg_def_get_default_value(cmd, item, CFG_TYPE_FLOAT, profile)); if (profile_applied) remove_config_tree_by_source(cmd, profile->source); @@ -1265,6 +1333,23 @@ float find_config_tree_float(struct cmd_context *cmd, int id, struct profile *pr return f; } +int find_config_bool(struct cmd_context *cmd, struct dm_config_tree *cft, int id) +{ + cfg_def_item_t *item = cfg_def_get_item_p(id); + char path[CFG_PATH_MAX_LEN]; + int b; + + _cfg_def_make_path(path, sizeof(path), item->id, item, 0); + + if (item->type != CFG_TYPE_BOOL) + log_error(INTERNAL_ERROR "%s cfg tree element not declared as boolean.", path); + + b = _config_disabled(cmd, item, path) ? cfg_def_get_default_value(cmd, item, CFG_TYPE_BOOL, NULL) + : dm_config_tree_find_bool(cft, path, cfg_def_get_default_value(cmd, item, CFG_TYPE_BOOL, NULL)); + + return b; +} + int find_config_tree_bool(struct cmd_context *cmd, int id, struct profile *profile) { cfg_def_item_t *item = cfg_def_get_item_p(id); @@ -1278,7 +1363,8 @@ int find_config_tree_bool(struct cmd_context *cmd, int id, struct profile *profi if (item->type != CFG_TYPE_BOOL) log_error(INTERNAL_ERROR "%s cfg tree element not declared as boolean.", path); - b = dm_config_tree_find_bool(cmd->cft, path, cfg_def_get_default_value(cmd, item, CFG_TYPE_BOOL, profile)); + b = _config_disabled(cmd, item, path) ? cfg_def_get_default_value(cmd, item, CFG_TYPE_BOOL, profile) + : dm_config_tree_find_bool(cmd->cft, path, cfg_def_get_default_value(cmd, item, CFG_TYPE_BOOL, profile)); if (profile_applied) remove_config_tree_by_source(cmd, profile->source); @@ -1286,6 +1372,106 @@ int find_config_tree_bool(struct cmd_context *cmd, int id, struct profile *profi return b; } +static struct dm_config_node *_get_array_def_node(struct cmd_context *cmd, + cfg_def_item_t *def, + struct profile *profile) +{ + struct dm_config_node *cn; + + if (def->flags & CFG_DEFAULT_UNDEFINED) + return NULL; + + if (!(cn = dm_config_create_node(cmd->cft, def->name))) { + log_error("Failed to create default array node for %s.", def->name); + return NULL; + } + + if (!(cn->v = _get_def_array_values(cmd, cmd->cft, def, 0))) { + dm_pool_free(cmd->cft->mem, cn); + return_NULL; + } + + return cn; +} + +struct _config_array_out_handle { + struct dm_pool *mem; + char *str; +}; + +static int _config_array_line(const struct dm_config_node *cn, const char *line, void *baton) +{ + struct _config_array_out_handle *handle = (struct _config_array_out_handle *) baton; + + if (!(handle->str = dm_pool_strdup(handle->mem, line))) { + log_error("_config_array_line: dm_pool_strdup failed"); + return 0; + } + + return 1; +} + +static void _log_array_value_used(struct dm_pool *mem, const struct dm_config_node *cn, + const char *path, int default_used) +{ + struct _config_array_out_handle out_handle = { 0 }; + struct dm_config_node_out_spec out_spec = { 0 }; + uint32_t old_format_flags; + + out_handle.mem = mem; + out_spec.line_fn = _config_array_line; + + old_format_flags = dm_config_value_get_format_flags(cn->v); + dm_config_value_set_format_flags(cn->v, + DM_CONFIG_VALUE_FMT_COMMON_EXTRA_SPACES | + DM_CONFIG_VALUE_FMT_COMMON_ARRAY); + + if (!dm_config_write_one_node_out(cn, &out_spec, &out_handle)) { + log_error("_log_array_value_used: failed to write node value"); + out_handle.mem = NULL; + } + + if (default_used) + log_very_verbose("%s not found in config: defaulting to %s", + path, out_handle.mem ? out_handle.str : "<unknown>"); + else + log_very_verbose("Setting %s to %s", + path, out_handle.mem ? out_handle.str : "<unknown>"); + + if (out_handle.mem) + dm_pool_free(out_handle.mem, out_handle.str); + dm_config_value_set_format_flags(cn->v, old_format_flags); +} + +const struct dm_config_node *find_config_tree_array(struct cmd_context *cmd, int id, struct profile *profile) +{ + cfg_def_item_t *item = cfg_def_get_item_p(id); + char path[CFG_PATH_MAX_LEN]; + int profile_applied; + const struct dm_config_node *cn = NULL, *cn_def = NULL; + profile_applied = _apply_local_profile(cmd, profile); + _cfg_def_make_path(path, sizeof(path), item->id, item, 0); + + if (!(item->type & CFG_TYPE_ARRAY)) + log_error(INTERNAL_ERROR "%s cfg tree element not declared as array.", path); + + if (_config_disabled(cmd, item, path) || + !(cn = find_config_tree_node(cmd, id, profile))) + cn_def = _get_array_def_node(cmd, item, profile); + + if (cn) + _log_array_value_used(cmd->cft->mem, cn, path, 0); + else if (cn_def) { + _log_array_value_used(cmd->cft->mem, cn_def, path, 1); + cn = cn_def; + } + + if (profile_applied) + remove_config_tree_by_source(cmd, profile->source); + + return cn; +} + /* Insert cn2 after cn1 */ static void _insert_config_node(struct dm_config_node **cn1, struct dm_config_node *cn2) @@ -1414,7 +1600,7 @@ int merge_config_tree(struct cmd_context *cmd, struct dm_config_tree *cft, cs = dm_config_get_custom(cft); csn = dm_config_get_custom(newdata); - if (cs && csn && (cs->timestamp < csn->timestamp)) + if (cs && csn && timespeccmp(&cs->timestamp, &csn->timestamp, <)) cs->timestamp = csn->timestamp; return 1; @@ -1426,6 +1612,55 @@ struct out_baton { struct dm_pool *mem; }; +#define MAX_COMMENT_LINE 512 + +static int _copy_one_line(const char *comment, char *line, int *pos, int len) +{ + int p; + int i = 0; + char c; + + if (*pos >= len) + return 0; + + memset(line, 0, MAX_COMMENT_LINE+1); + + for (p = *pos; ; p++) { + c = comment[p]; + + (*pos)++; + + if (c == '\n' || c == '\0') + break; + + line[i++] = c; + + if (i == MAX_COMMENT_LINE) + break; + } + + return i; +} + +static int _get_config_node_version(uint16_t version_enc, char *version) +{ + if (dm_snprintf(version, 9, "%u.%u.%u", + (version_enc & 0xE000) >> 13, + (version_enc & 0x1E00) >> 9, + (version_enc & 0x1FF)) == -1) { + log_error("_get_config_node_version: couldn't create version string"); + return 0; + } + + return 1; +} + +static int _def_node_is_deprecated(cfg_def_item_t *def, struct config_def_tree_spec *spec) +{ + return def->deprecated_since_version && + (spec->version >= def->deprecated_since_version); +} + static int _out_prefix_fn(const struct dm_config_node *cn, const char *line, void *baton) { struct out_baton *out = baton; @@ -1433,15 +1668,13 @@ static int _out_prefix_fn(const struct dm_config_node *cn, const char *line, voi char version[9]; /* 8+1 chars for max version of 7.15.511 */ const char *node_type_name = cn->v ? "option" : "section"; char path[CFG_PATH_MAX_LEN]; + char commentline[MAX_COMMENT_LINE+1]; - - if (cn->id < 0) + if (cn->id <= 0) return 1; - if (!cn->id) { - log_error(INTERNAL_ERROR "Configuration node %s has invalid id.", cn->key); - return 0; - } + if (out->tree_spec->type == CFG_DEF_TREE_LIST) + return 1; if ((out->tree_spec->type == CFG_DEF_TREE_DIFF) && (!(out->tree_spec->check_status[cn->id] & CFG_DIFF))) @@ -1449,12 +1682,32 @@ static int _out_prefix_fn(const struct dm_config_node *cn, const char *line, voi cfg_def = cfg_def_get_item_p(cn->id); - if (out->tree_spec->withcomments) { + if (out->tree_spec->withsummary || out->tree_spec->withcomments) { _cfg_def_make_path(path, sizeof(path), cfg_def->id, cfg_def, 1); + fprintf(out->fp, "\n"); fprintf(out->fp, "%s# Configuration %s %s.\n", line, node_type_name, path); - if (cfg_def->comment) - fprintf(out->fp, "%s# %s\n", line, cfg_def->comment); + if (out->tree_spec->withcomments && + _def_node_is_deprecated(cfg_def, out->tree_spec)) + fprintf(out->fp, "%s# %s", line, cfg_def->deprecation_comment); + + if (cfg_def->comment) { + int pos = 0; + while (_copy_one_line(cfg_def->comment, commentline, &pos, strlen(cfg_def->comment))) { + if ((commentline[0] == '#') && (strlen(commentline) == 1)) { + if (!out->tree_spec->withspaces) + continue; + commentline[0] = '\0'; + } + fprintf(out->fp, "%s# %s\n", line, commentline); + /* withsummary prints only the first comment line. */ + if (!out->tree_spec->withcomments) + break; + } + } + + if (_def_node_is_deprecated(cfg_def, out->tree_spec)) + fprintf(out->fp, "%s# This configuration %s is deprecated.\n", line, node_type_name); if (cfg_def->flags & CFG_ADVANCED) fprintf(out->fp, "%s# This configuration %s is advanced.\n", line, node_type_name); @@ -1467,34 +1720,101 @@ static int _out_prefix_fn(const struct dm_config_node *cn, const char *line, voi if (cfg_def->flags & CFG_DEFAULT_UNDEFINED) fprintf(out->fp, "%s# This configuration %s does not have a default value defined.\n", line, node_type_name); + + if (cfg_def->flags & CFG_DEFAULT_COMMENTED) + fprintf(out->fp, "%s# This configuration %s has an automatic default value.\n", line, node_type_name); + + if ((out->tree_spec->type == CFG_DEF_TREE_FULL) && + (out->tree_spec->check_status[cn->id] & CFG_USED)) + fprintf(out->fp, "%s# Value defined in existing configuration has been used for this setting.\n", line); } if (out->tree_spec->withversions) { - if (dm_snprintf(version, 9, "%u.%u.%u", - (cfg_def->since_version & 0xE000) >> 13, - (cfg_def->since_version & 0x1E00) >> 9, - (cfg_def->since_version & 0x1FF)) == -1) { - log_error("_out_prefix_fn: couldn't create version string"); - return 0; + if (!_get_config_node_version(cfg_def->since_version, version)) + return_0; + fprintf(out->fp, "%s# Available since version %s.\n", line, version); + + if (_def_node_is_deprecated(cfg_def, out->tree_spec)) { + if (!_get_config_node_version(cfg_def->deprecated_since_version, version)) + return_0; + fprintf(out->fp, "%s# Deprecated since version %s.\n", line, version); } - fprintf(out->fp, "%s# Since version %s.\n", line, version); } return 1; } +static int _should_print_cfg_with_undef_def_val(struct out_baton *out, cfg_def_item_t *cfg_def, + const struct dm_config_node *cn) +{ + if (!(cfg_def->flags & CFG_DEFAULT_UNDEFINED)) + return 1; + + /* print it only if the value is directly defined in some config = it's used */ + return out->tree_spec->check_status && (out->tree_spec->check_status[cn->id] & CFG_USED); +} + static int _out_line_fn(const struct dm_config_node *cn, const char *line, void *baton) { struct out_baton *out = baton; - struct cfg_def_item *cfg_def = cfg_def_get_item_p(cn->id); + struct cfg_def_item *cfg_def; + char config_path[CFG_PATH_MAX_LEN]; + char summary[MAX_COMMENT_LINE+1]; + char version[9]; + int pos = 0; + size_t len; + char *space_prefix; if ((out->tree_spec->type == CFG_DEF_TREE_DIFF) && (!(out->tree_spec->check_status[cn->id] & CFG_DIFF))) return 1; - fprintf(out->fp, "%s%s\n", (out->tree_spec->type != CFG_DEF_TREE_CURRENT) && - (out->tree_spec->type != CFG_DEF_TREE_DIFF) && - (cfg_def->flags & CFG_DEFAULT_UNDEFINED) ? "#" : "", line); + cfg_def = cfg_def_get_item_p(cn->id); + + if (out->tree_spec->type == CFG_DEF_TREE_LIST) { + /* List view with node paths and summary. */ + if (cfg_def->type & CFG_TYPE_SECTION) + return 1; + if (!_cfg_def_make_path(config_path, CFG_PATH_MAX_LEN, cfg_def->id, cfg_def, 1)) + return_0; + if (out->tree_spec->withversions && !_get_config_node_version(cfg_def->since_version, version)) + return_0; + + summary[0] = '\0'; + if (out->tree_spec->withsummary && cfg_def->comment) + _copy_one_line(cfg_def->comment, summary, &pos, strlen(cfg_def->comment)); + + fprintf(out->fp, "%s%s%s%s%s%s%s\n", config_path, + *summary || out->tree_spec->withversions ? " - ": "", + *summary ? summary : "", + *summary ? " " : "", + out->tree_spec->withversions ? "[" : "", + out->tree_spec->withversions ? version : "", + out->tree_spec->withversions ? "]" : ""); + + return 1; + } + + /* Usual tree view with nodes and their values. */ + + if ((out->tree_spec->type != CFG_DEF_TREE_CURRENT) && + (out->tree_spec->type != CFG_DEF_TREE_DIFF) && + (out->tree_spec->type != CFG_DEF_TREE_FULL) && + (cfg_def->flags & (CFG_DEFAULT_UNDEFINED | CFG_DEFAULT_COMMENTED))) { + /* print with # at the front to comment out the line */ + if (_should_print_cfg_with_undef_def_val(out, cfg_def, cn)) { + space_prefix = ((len = strspn(line, "\t "))) ? dm_pool_strndup(out->mem, line, len) : NULL; + fprintf(out->fp, "%s%s%s\n", space_prefix ? : "", "# ", line + len); + if (space_prefix) + dm_pool_free(out->mem, space_prefix); + } + return 1; + } + + /* print the line as it is */ + if (_should_print_cfg_with_undef_def_val(out, cfg_def, cn)) + fprintf(out->fp, "%s\n", line); + return 1; } @@ -1562,20 +1882,31 @@ static struct dm_config_node *_add_def_node(struct dm_config_tree *cft, { struct dm_config_node *cn; const char *str; + uint32_t format_flags = 0; if (!(cn = dm_config_create_node(cft, def->name))) { log_error("Failed to create default config setting node."); return NULL; } - if (!(def->type & CFG_TYPE_SECTION) && (!(cn->v = dm_config_create_value(cft)))) { - log_error("Failed to create default config setting node value."); - return NULL; + if (!(def->type & CFG_TYPE_SECTION) && !(def->type & CFG_TYPE_ARRAY)) { + if (!(cn->v = dm_config_create_value(cft))) { + log_error("Failed to create default config setting node value."); + return NULL; + } + if (spec->withspaces) + format_flags |= DM_CONFIG_VALUE_FMT_COMMON_EXTRA_SPACES; } cn->id = def->id; - if (!(def->type & CFG_TYPE_ARRAY)) { + if (spec->unconfigured && def->default_unconfigured_value.v_UNCONFIGURED) { + cn->v->type = DM_CFG_STRING; + cn->v->v.str = cfg_def_get_default_unconfigured_value_hint(spec->cmd, def); + if (def->type != CFG_TYPE_STRING) + format_flags |= DM_CONFIG_VALUE_FMT_STRING_NO_QUOTES; + dm_config_value_set_format_flags(cn->v, format_flags); + } else if (!(def->type & CFG_TYPE_ARRAY)) { switch (def->type) { case CFG_TYPE_SECTION: cn->v = NULL; @@ -1587,6 +1918,8 @@ static struct dm_config_node *_add_def_node(struct dm_config_tree *cft, case CFG_TYPE_INT: cn->v->type = DM_CFG_INT; cn->v->v.i = cfg_def_get_default_value_hint(spec->cmd, def, CFG_TYPE_INT, NULL); + if (def->flags & CFG_FORMAT_INT_OCTAL) + format_flags |= DM_CONFIG_VALUE_FMT_INT_OCTAL; break; case CFG_TYPE_FLOAT: cn->v->type = DM_CFG_FLOAT; @@ -1603,8 +1936,13 @@ static struct dm_config_node *_add_def_node(struct dm_config_tree *cft, return NULL; break; } - } else - cn->v = _get_def_array_values(cft, def); + dm_config_value_set_format_flags(cn->v, format_flags); + } else { + if (spec->withspaces) + format_flags |= DM_CONFIG_VALUE_FMT_COMMON_EXTRA_SPACES; + format_flags |= DM_CONFIG_VALUE_FMT_COMMON_ARRAY; + cn->v = _get_def_array_values(spec->cmd, cft, def, format_flags); + } cn->child = NULL; if (parent) { @@ -1620,6 +1958,11 @@ static struct dm_config_node *_add_def_node(struct dm_config_tree *cft, return cn; } +static int _should_skip_deprecated_def_node(cfg_def_item_t *def, struct config_def_tree_spec *spec) +{ + return spec->ignoredeprecated && _def_node_is_deprecated(def, spec); +} + static int _should_skip_def_node(struct config_def_tree_spec *spec, int section_id, int id) { cfg_def_item_t *def = cfg_def_get_item_p(id); @@ -1631,6 +1974,8 @@ static int _should_skip_def_node(struct config_def_tree_spec *spec, int section_ return 1; switch (spec->type) { + case CFG_DEF_TREE_FULL: + /* fall through */ case CFG_DEF_TREE_MISSING: if (!spec->check_status) { log_error_once(INTERNAL_ERROR "couldn't determine missing " @@ -1638,19 +1983,27 @@ static int _should_skip_def_node(struct config_def_tree_spec *spec, int section_ return 1; } if ((spec->check_status[id] & CFG_USED) || - (def->flags & CFG_NAME_VARIABLE) || - (def->since_version > spec->version)) + (def->flags & CFG_NAME_VARIABLE)) + return 1; + + if ((spec->type == CFG_DEF_TREE_MISSING) && + ((def->since_version > spec->version) || + _should_skip_deprecated_def_node(def, spec))) return 1; break; case CFG_DEF_TREE_NEW: - if (def->since_version != spec->version) + if ((def->since_version != spec->version) || + _should_skip_deprecated_def_node(def, spec)) return 1; break; case CFG_DEF_TREE_PROFILABLE: + /* fall through */ case CFG_DEF_TREE_PROFILABLE_CMD: + /* fall through */ case CFG_DEF_TREE_PROFILABLE_MDA: if (!(def->flags & CFG_PROFILABLE) || - (def->since_version > spec->version)) + (def->since_version > spec->version) || + _should_skip_deprecated_def_node(def, spec)) return 1; flags = def->flags & ~CFG_PROFILABLE; if (spec->type == CFG_DEF_TREE_PROFILABLE_CMD) { @@ -1662,7 +2015,8 @@ static int _should_skip_def_node(struct config_def_tree_spec *spec, int section_ } break; default: - if (def->since_version > spec->version) + if ((def->since_version > spec->version) || + _should_skip_deprecated_def_node(def, spec)) return 1; break; } @@ -1701,7 +2055,7 @@ bad: struct dm_config_tree *config_def_create_tree(struct config_def_tree_spec *spec) { - struct dm_config_tree *cft; + struct dm_config_tree *cft = NULL, *tmp_cft = NULL; struct dm_config_node *root = NULL, *relay = NULL, *tmp; int id; @@ -1714,6 +2068,9 @@ struct dm_config_tree *config_def_create_tree(struct config_def_tree_spec *spec) if (cfg_def_get_item_p(id)->parent != root_CFG_SECTION) continue; + if (spec->ignorelocal && (id == local_CFG_SECTION)) + continue; + if ((tmp = _add_def_section_subtree(cft, spec, root, relay, id))) { relay = tmp; if (!root) @@ -1722,7 +2079,33 @@ struct dm_config_tree *config_def_create_tree(struct config_def_tree_spec *spec) } cft->root = root; + + if (spec->type == CFG_DEF_TREE_FULL) { + if (!(tmp_cft = dm_config_create())) { + log_error("Failed to create temporary config tree while creating full tree."); + goto bad; + } + + if (!(tmp_cft->root = dm_config_clone_node_with_mem(cft->mem, spec->current_cft->root, 1))) { + log_error("Failed to clone current config tree."); + goto bad; + } + + if (!merge_config_tree(spec->cmd, cft, tmp_cft, CONFIG_MERGE_TYPE_RAW)) { + log_error("Failed to merge default and current config tree."); + goto bad; + } + + dm_config_destroy(tmp_cft); + } + return cft; +bad: + if (cft) + dm_config_destroy(cft); + if (tmp_cft) + dm_config_destroy(tmp_cft); + return NULL; } static int _check_profile(struct cmd_context *cmd, struct profile *profile) @@ -1901,6 +2284,11 @@ const char *get_default_devices_cache_dir_CFG(struct cmd_context *cmd, struct pr return dm_pool_strdup(cmd->mem, buf); } +const char *get_default_unconfigured_devices_cache_dir_CFG(struct cmd_context *cmd) +{ + return "@DEFAULT_SYS_DIR@/@DEFAULT_CACHE_SUBDIR@"; +} + const char *get_default_devices_cache_CFG(struct cmd_context *cmd, struct profile *profile) { const char *cache_dir = NULL, *cache_file_prefix = NULL; @@ -1935,6 +2323,24 @@ const char *get_default_devices_cache_CFG(struct cmd_context *cmd, struct profil return dm_pool_strdup(cmd->mem, buf); } +const char *get_default_unconfigured_devices_cache_CFG(struct cmd_context *cmd) +{ + const char *cache_file_prefix = NULL; + static char buf[PATH_MAX]; + + if (find_config_tree_node(cmd, devices_cache_file_prefix_CFG, NULL)) + cache_file_prefix = find_config_tree_str_allow_empty(cmd, devices_cache_file_prefix_CFG, NULL); + + if (dm_snprintf(buf, sizeof(buf), "%s/%s.cache", + get_default_unconfigured_devices_cache_dir_CFG(cmd), + cache_file_prefix ? : DEFAULT_CACHE_FILE_PREFIX) < 0) { + log_error("Persistent cache filename too long."); + return NULL; + } + + return dm_pool_strdup(cmd->mem, buf); +} + const char *get_default_backup_backup_dir_CFG(struct cmd_context *cmd, struct profile *profile) { static char buf[PATH_MAX]; @@ -1948,6 +2354,11 @@ const char *get_default_backup_backup_dir_CFG(struct cmd_context *cmd, struct pr return dm_pool_strdup(cmd->mem, buf); } +const char *get_default_unconfigured_backup_backup_dir_CFG(struct cmd_context *cmd) +{ + return "@DEFAULT_SYS_DIR@/@DEFAULT_BACKUP_SUBDIR@"; +} + const char *get_default_backup_archive_dir_CFG(struct cmd_context *cmd, struct profile *profile) { static char buf[PATH_MAX]; @@ -1961,6 +2372,11 @@ const char *get_default_backup_archive_dir_CFG(struct cmd_context *cmd, struct p return dm_pool_strdup(cmd->mem, buf); } +const char *get_default_unconfigured_backup_archive_dir_CFG(struct cmd_context *cmd) +{ + return "@DEFAULT_SYS_DIR@/@DEFAULT_ARCHIVE_SUBDIR@"; +} + const char *get_default_config_profile_dir_CFG(struct cmd_context *cmd, struct profile *profile) { static char buf[PATH_MAX]; @@ -1974,6 +2390,11 @@ const char *get_default_config_profile_dir_CFG(struct cmd_context *cmd, struct p return dm_pool_strdup(cmd->mem, buf); } +const char *get_default_unconfigured_config_profile_dir_CFG(struct cmd_context *cmd) +{ + return "@DEFAULT_SYS_DIR@/@DEFAULT_PROFILE_SUBDIR@"; +} + const char *get_default_activation_mirror_image_fault_policy_CFG(struct cmd_context *cmd, struct profile *profile) { return find_config_tree_str(cmd, activation_mirror_device_fault_policy_CFG, profile); |