1
0
mirror of git://sourceware.org/git/lvm2.git synced 2024-12-21 13:34:40 +03:00

cleanup: report: use internal wrapper for various variables used for handling reserved values

Just a cleanup - wrap several variables we use to handle reserved
values into a structure for easier manipulation in the code.
This commit is contained in:
Peter Rajnoha 2015-06-30 10:23:35 +02:00
parent d8996a17d1
commit d7b9349ce7

View File

@ -188,6 +188,12 @@ struct selection_node {
} selection; } selection;
}; };
struct reserved_value_wrapper {
const char *matched_name;
const struct dm_report_reserved_value *reserved;
const void *value;
};
/* /*
* Report data field * Report data field
*/ */
@ -2080,14 +2086,14 @@ static const char *_reserved_name(const char **names, const char *s, size_t len)
static const char *_get_reserved(struct dm_report *rh, unsigned type, static const char *_get_reserved(struct dm_report *rh, unsigned type,
uint32_t field_num, int implicit, uint32_t field_num, int implicit,
const char *s, const char **begin, const char **end, const char *s, const char **begin, const char **end,
const struct dm_report_reserved_value **reserved) struct reserved_value_wrapper *rvw)
{ {
const struct dm_report_reserved_value *iter = implicit ? NULL : rh->reserved_values; const struct dm_report_reserved_value *iter = implicit ? NULL : rh->reserved_values;
const char *tmp_begin, *tmp_end, *tmp_s = s; const char *tmp_begin, *tmp_end, *tmp_s = s;
const char *name = NULL; const char *name = NULL;
char c; char c;
*reserved = NULL; rvw->reserved = NULL;
if (!iter) if (!iter)
return s; return s;
@ -2115,7 +2121,8 @@ static const char *_get_reserved(struct dm_report *rh, unsigned type,
*begin = tmp_begin; *begin = tmp_begin;
*end = tmp_end; *end = tmp_end;
s = tmp_s; s = tmp_s;
*reserved = iter; rvw->reserved = iter;
rvw->matched_name = name;
} }
return s; return s;
@ -2213,10 +2220,10 @@ static const char *_tok_value_regex(struct dm_report *rh,
const struct dm_report_field_type *ft, const struct dm_report_field_type *ft,
const char *s, const char **begin, const char *s, const char **begin,
const char **end, uint32_t *flags, const char **end, uint32_t *flags,
const struct dm_report_reserved_value **reserved) struct reserved_value_wrapper *rvw)
{ {
char c; char c;
*reserved = NULL; rvw->reserved = NULL;
s = _skip_space(s); s = _skip_space(s);
@ -2439,7 +2446,7 @@ static const char *_tok_value(struct dm_report *rh,
const char *s, const char *s,
const char **begin, const char **end, const char **begin, const char **end,
uint32_t *flags, uint32_t *flags,
const struct dm_report_reserved_value **reserved, struct reserved_value_wrapper *rvw,
struct dm_pool *mem, void *custom) struct dm_pool *mem, void *custom)
{ {
int expected_type = ft->flags & DM_REPORT_FIELD_TYPE_MASK; int expected_type = ft->flags & DM_REPORT_FIELD_TYPE_MASK;
@ -2450,8 +2457,8 @@ static const char *_tok_value(struct dm_report *rh,
s = _skip_space(s); s = _skip_space(s);
s = _get_reserved(rh, expected_type, field_num, implicit, s, begin, end, reserved); s = _get_reserved(rh, expected_type, field_num, implicit, s, begin, end, rvw);
if (*reserved) { if (rvw->reserved) {
*flags |= expected_type; *flags |= expected_type;
return s; return s;
} }
@ -2553,12 +2560,23 @@ static const char *_tok_field_name(const char *s,
return s; return s;
} }
static const void *_get_reserved_value(const struct dm_report_reserved_value *reserved) static int _get_reserved_value(struct dm_report *rh, uint32_t field_num,
struct reserved_value_wrapper *rvw)
{ {
if (reserved->type & DM_REPORT_FIELD_TYPE_MASK) const void *tmp_value;
return reserved->value;
if (!rvw->reserved) {
rvw->value = NULL;
return 1;
}
if (rvw->reserved->type & DM_REPORT_FIELD_TYPE_MASK)
tmp_value = rvw->reserved->value;
else else
return ((const struct dm_report_field_reserved_value *) reserved->value)->value; tmp_value = ((const struct dm_report_field_reserved_value *) rvw->reserved->value)->value;
rvw->value = tmp_value;
return 1;
} }
static struct field_selection *_create_field_selection(struct dm_report *rh, static struct field_selection *_create_field_selection(struct dm_report *rh,
@ -2567,7 +2585,7 @@ static struct field_selection *_create_field_selection(struct dm_report *rh,
const char *v, const char *v,
size_t len, size_t len,
uint32_t flags, uint32_t flags,
const struct dm_report_reserved_value *reserved, struct reserved_value_wrapper *rvw,
void *custom) void *custom)
{ {
static const char *_out_of_range_msg = "Field selection value %s out of supported range for field %s."; static const char *_out_of_range_msg = "Field selection value %s out of supported range for field %s.";
@ -2576,7 +2594,6 @@ static struct field_selection *_create_field_selection(struct dm_report *rh,
: rh->fields; : rh->fields;
struct field_properties *fp, *found = NULL; struct field_properties *fp, *found = NULL;
struct field_selection *fs; struct field_selection *fs;
const void *reserved_value;
const char *field_id; const char *field_id;
uint64_t factor; uint64_t factor;
char *s; char *s;
@ -2615,7 +2632,7 @@ static struct field_selection *_create_field_selection(struct dm_report *rh,
goto error; goto error;
} }
if (reserved && (reserved->type & DM_REPORT_FIELD_RESERVED_VALUE_RANGE) && if (rvw->reserved && (rvw->reserved->type & DM_REPORT_FIELD_RESERVED_VALUE_RANGE) &&
!(fs->value->next = dm_pool_zalloc(rh->selection->mem, sizeof(struct field_selection_value)))) { !(fs->value->next = dm_pool_zalloc(rh->selection->mem, sizeof(struct field_selection_value)))) {
log_error(_field_selection_value_alloc_failed_msg, field_id); log_error(_field_selection_value_alloc_failed_msg, field_id);
goto error; goto error;
@ -2624,7 +2641,11 @@ static struct field_selection *_create_field_selection(struct dm_report *rh,
fs->fp = found; fs->fp = found;
fs->flags = flags; fs->flags = flags;
reserved_value = reserved ? _get_reserved_value(reserved) : NULL; if (!_get_reserved_value(rh, field_num, rvw)) {
log_error("dm_report: could not get reserved value "
"while processing selection field %s", field_id);
goto error;
}
/* store comparison operand */ /* store comparison operand */
if (flags & FLD_CMP_REGEX) { if (flags & FLD_CMP_REGEX) {
@ -2654,10 +2675,10 @@ static struct field_selection *_create_field_selection(struct dm_report *rh,
switch (flags & DM_REPORT_FIELD_TYPE_MASK) { switch (flags & DM_REPORT_FIELD_TYPE_MASK) {
case DM_REPORT_FIELD_TYPE_STRING: case DM_REPORT_FIELD_TYPE_STRING:
if (reserved_value) { if (rvw->value) {
fs->value->v.s = (const char *) reserved_value; fs->value->v.s = (const char *) rvw->value;
if (reserved->type & DM_REPORT_FIELD_RESERVED_VALUE_RANGE) if (rvw->reserved->type & DM_REPORT_FIELD_RESERVED_VALUE_RANGE)
fs->value->next->v.s = (((const char **) reserved_value)[1]); fs->value->next->v.s = (((const char **) rvw->value)[1]);
dm_pool_free(rh->selection->mem, s); dm_pool_free(rh->selection->mem, s);
} else { } else {
fs->value->v.s = s; fs->value->v.s = s;
@ -2668,10 +2689,10 @@ static struct field_selection *_create_field_selection(struct dm_report *rh,
} }
break; break;
case DM_REPORT_FIELD_TYPE_NUMBER: case DM_REPORT_FIELD_TYPE_NUMBER:
if (reserved_value) { if (rvw->value) {
fs->value->v.i = *(uint64_t *) reserved_value; fs->value->v.i = *(uint64_t *) rvw->value;
if (reserved->type & DM_REPORT_FIELD_RESERVED_VALUE_RANGE) if (rvw->reserved->type & DM_REPORT_FIELD_RESERVED_VALUE_RANGE)
fs->value->next->v.i = (((uint64_t *) reserved_value)[1]); fs->value->next->v.i = (((uint64_t *) rvw->value)[1]);
} else { } else {
if (((fs->value->v.i = strtoull(s, NULL, 10)) == ULLONG_MAX) && if (((fs->value->v.i = strtoull(s, NULL, 10)) == ULLONG_MAX) &&
(errno == ERANGE)) { (errno == ERANGE)) {
@ -2686,10 +2707,10 @@ static struct field_selection *_create_field_selection(struct dm_report *rh,
dm_pool_free(rh->selection->mem, s); dm_pool_free(rh->selection->mem, s);
break; break;
case DM_REPORT_FIELD_TYPE_SIZE: case DM_REPORT_FIELD_TYPE_SIZE:
if (reserved_value) { if (rvw->value) {
fs->value->v.d = *(double *) reserved_value; fs->value->v.d = *(double *) rvw->value;
if (reserved->type & DM_REPORT_FIELD_RESERVED_VALUE_RANGE) if (rvw->reserved->type & DM_REPORT_FIELD_RESERVED_VALUE_RANGE)
fs->value->next->v.d = (((double *) reserved_value)[1]); fs->value->next->v.d = (((double *) rvw->value)[1]);
} else { } else {
fs->value->v.d = strtod(s, NULL); fs->value->v.d = strtod(s, NULL);
if (errno == ERANGE) { if (errno == ERANGE) {
@ -2707,10 +2728,10 @@ static struct field_selection *_create_field_selection(struct dm_report *rh,
dm_pool_free(rh->selection->mem, s); dm_pool_free(rh->selection->mem, s);
break; break;
case DM_REPORT_FIELD_TYPE_PERCENT: case DM_REPORT_FIELD_TYPE_PERCENT:
if (reserved_value) { if (rvw->value) {
fs->value->v.i = *(uint64_t *) reserved_value; fs->value->v.i = *(uint64_t *) rvw->value;
if (reserved->type & DM_REPORT_FIELD_RESERVED_VALUE_RANGE) if (rvw->reserved->type & DM_REPORT_FIELD_RESERVED_VALUE_RANGE)
fs->value->next->v.i = (((uint64_t *) reserved_value)[1]); fs->value->next->v.i = (((uint64_t *) rvw->value)[1]);
} else { } else {
fs->value->v.d = strtod(s, NULL); fs->value->v.d = strtod(s, NULL);
if ((errno == ERANGE) || (fs->value->v.d < 0) || (fs->value->v.d > 100)) { if ((errno == ERANGE) || (fs->value->v.d < 0) || (fs->value->v.d > 100)) {
@ -2868,7 +2889,7 @@ static struct selection_node *_parse_selection(struct dm_report *rh,
int implicit; int implicit;
const struct dm_report_field_type *ft; const struct dm_report_field_type *ft;
struct selection_str_list *str_list; struct selection_str_list *str_list;
const struct dm_report_reserved_value *reserved; struct reserved_value_wrapper rvw = {0};
uint64_t factor; uint64_t factor;
void *custom = NULL; void *custom = NULL;
char *tmp; char *tmp;
@ -2931,7 +2952,7 @@ static struct selection_node *_parse_selection(struct dm_report *rh,
/* comparison value */ /* comparison value */
if (flags & FLD_CMP_REGEX) { if (flags & FLD_CMP_REGEX) {
if (!(last = _tok_value_regex(rh, ft, last, &vs, &ve, &flags, &reserved))) if (!(last = _tok_value_regex(rh, ft, last, &vs, &ve, &flags, &rvw)))
goto_bad; goto_bad;
} else { } else {
if (ft->flags == DM_REPORT_FIELD_TYPE_SIZE || if (ft->flags == DM_REPORT_FIELD_TYPE_SIZE ||
@ -2944,14 +2965,14 @@ static struct selection_node *_parse_selection(struct dm_report *rh,
custom = NULL; custom = NULL;
if (!(last = _tok_value(rh, ft, field_num, implicit, if (!(last = _tok_value(rh, ft, field_num, implicit,
last, &vs, &ve, &flags, last, &vs, &ve, &flags,
&reserved, rh->selection->mem, custom))) &rvw, rh->selection->mem, custom)))
goto_bad; goto_bad;
} }
*next = _skip_space(last); *next = _skip_space(last);
/* create selection */ /* create selection */
if (!(fs = _create_field_selection(rh, field_num, implicit, vs, (size_t) (ve - vs), flags, reserved, custom))) if (!(fs = _create_field_selection(rh, field_num, implicit, vs, (size_t) (ve - vs), flags, &rvw, custom)))
return_NULL; return_NULL;
/* create selection node */ /* create selection node */