From b18ae944cbd65351ea922342cf9187178e18ee43 Mon Sep 17 00:00:00 2001 From: Eric Bollengier Date: Wed, 24 Nov 2010 21:44:00 +0100 Subject: [PATCH] Add store_speed() in configuration parser --- bacula/src/lib/edit.c | 33 ++++++++++++++++++++++++------- bacula/src/lib/parse_conf.c | 39 ++++++++++++++++++++++++++++--------- bacula/src/lib/parse_conf.h | 1 + bacula/src/lib/protos.h | 1 + 4 files changed, 58 insertions(+), 16 deletions(-) diff --git a/bacula/src/lib/edit.c b/bacula/src/lib/edit.c index 6561dc2a5e..d77d8bfaff 100644 --- a/bacula/src/lib/edit.c +++ b/bacula/src/lib/edit.c @@ -332,18 +332,13 @@ char *edit_utime(utime_t val, char *buf, int buf_len) return buf; } -/* - * Convert a size in bytes to uint64_t - * Returns false: if error - true: if OK, and value stored in value - */ -bool size_to_uint64(char *str, int str_len, uint64_t *value) +static bool strunit_to_uint64(char *str, int str_len, uint64_t *value, + const char **mod) { int i, mod_len; double val; char mod_str[20]; char num_str[50]; - static const char *mod[] = {"*", "k", "kb", "m", "mb", "g", "gb", NULL}; /* first item * not used */ const int64_t mult[] = {1, /* byte */ 1024, /* kilobyte */ 1000, /* kb kilobyte */ @@ -379,6 +374,30 @@ bool size_to_uint64(char *str, int str_len, uint64_t *value) return true; } +/* + * Convert a size in bytes to uint64_t + * Returns false: if error + true: if OK, and value stored in value + */ +bool size_to_uint64(char *str, int str_len, uint64_t *value) +{ + /* first item * not used */ + static const char *mod[] = {"*", "k", "kb", "m", "mb", "g", "gb", NULL}; + return strunit_to_uint64(str, str_len, value, mod); +} + +/* + * Convert a speed in bytes/s to uint64_t + * Returns false: if error + true: if OK, and value stored in value + */ +bool speed_to_uint64(char *str, int str_len, uint64_t *value) +{ + /* first item * not used */ + static const char *mod[] = {"*", "k/s", "kb/s", "m/s", "mb/s", NULL}; + return strunit_to_uint64(str, str_len, value, mod); +} + /* * Check if specified string is a number or not. * Taken from SQLite, cool, thanks. diff --git a/bacula/src/lib/parse_conf.c b/bacula/src/lib/parse_conf.c index 09f4d1c756..9e856f4a93 100644 --- a/bacula/src/lib/parse_conf.c +++ b/bacula/src/lib/parse_conf.c @@ -213,6 +213,8 @@ static void init_resource(CONFIG *config, int type, RES_ITEM *items, int pass) *(int64_t *)(items[i].value) = items[i].default_value; } else if (items[i].handler == store_size64) { *(uint64_t *)(items[i].value) = (uint64_t)items[i].default_value; + } else if (items[i].handler == store_speed) { + *(uint64_t *)(items[i].value) = (uint64_t)items[i].default_value; } else if (items[i].handler == store_time) { *(utime_t *)(items[i].value) = (utime_t)items[i].default_value; } else if (pass == 1 && items[i].handler == store_addresses) { @@ -627,14 +629,20 @@ void store_int64(LEX *lc, RES_ITEM *item, int index, int pass) set_bit(index, res_all.hdr.item_present); } +enum store_unit_type { + STORE_SIZE, + STORE_SPEED +} ; + /* Store a size in bytes */ -static void store_size(LEX *lc, RES_ITEM *item, int index, int pass, bool size32) +static void store_int_unit(LEX *lc, RES_ITEM *item, int index, int pass, + bool size32, enum store_unit_type type) { int token; uint64_t uvalue; char bsize[500]; - Dmsg0(900, "Enter store_size\n"); + Dmsg0(900, "Enter store_unit\n"); token = lex_get_token(lc, T_SKIP_EOL); errno = 0; switch (token) { @@ -653,9 +661,16 @@ static void store_size(LEX *lc, RES_ITEM *item, int index, int pass, bool size32 break; } } - if (!size_to_uint64(bsize, strlen(bsize), &uvalue)) { - scan_err1(lc, _("expected a size number, got: %s"), lc->str); - return; + if (type == STORE_SIZE) { + if (!size_to_uint64(bsize, strlen(bsize), &uvalue)) { + scan_err1(lc, _("expected a size number, got: %s"), lc->str); + return; + } + } else { + if (!speed_to_uint64(bsize, strlen(bsize), &uvalue)) { + scan_err1(lc, _("expected a speed number, got: %s"), lc->str); + return; + } } if (size32) { *(uint32_t *)(item->value) = (uint32_t)uvalue; @@ -664,28 +679,34 @@ static void store_size(LEX *lc, RES_ITEM *item, int index, int pass, bool size32 } break; default: - scan_err1(lc, _("expected a size, got: %s"), lc->str); + scan_err2(lc, _("expected a %s, got: %s"), + (type == STORE_SIZE)?_("size"):_("speed"), lc->str); return; } if (token != T_EOL) { scan_to_eol(lc); } set_bit(index, res_all.hdr.item_present); - Dmsg0(900, "Leave store_size\n"); + Dmsg0(900, "Leave store_unit\n"); } /* Store a size in bytes */ void store_size32(LEX *lc, RES_ITEM *item, int index, int pass) { - store_size(lc, item, index, pass, true /* 32 bit */); + store_int_unit(lc, item, index, pass, true /* 32 bit */, STORE_SIZE); } /* Store a size in bytes */ void store_size64(LEX *lc, RES_ITEM *item, int index, int pass) { - store_size(lc, item, index, pass, false /* not 32 bit */); + store_int_unit(lc, item, index, pass, false /* not 32 bit */, STORE_SIZE); } +/* Store a speed in bytes/s */ +void store_speed(LEX *lc, RES_ITEM *item, int index, int pass) +{ + store_int_unit(lc, item, index, pass, false /* 64 bit */, STORE_SPEED); +} /* Store a time period in seconds */ void store_time(LEX *lc, RES_ITEM *item, int index, int pass) diff --git a/bacula/src/lib/parse_conf.h b/bacula/src/lib/parse_conf.h index efa79f3c2b..f075df7154 100644 --- a/bacula/src/lib/parse_conf.h +++ b/bacula/src/lib/parse_conf.h @@ -244,6 +244,7 @@ void store_bool(LEX *lc, RES_ITEM *item, int index, int pass); void store_time(LEX *lc, RES_ITEM *item, int index, int pass); void store_size64(LEX *lc, RES_ITEM *item, int index, int pass); void store_size32(LEX *lc, RES_ITEM *item, int index, int pass); +void store_speed(LEX *lc, RES_ITEM *item, int index, int pass); void store_defs(LEX *lc, RES_ITEM *item, int index, int pass); void store_label(LEX *lc, RES_ITEM *item, int index, int pass); diff --git a/bacula/src/lib/protos.h b/bacula/src/lib/protos.h index b63f5382a2..8a18b45908 100644 --- a/bacula/src/lib/protos.h +++ b/bacula/src/lib/protos.h @@ -185,6 +185,7 @@ char * edit_int64 (int64_t val, char *buf); char * edit_int64_with_commas (int64_t val, char *buf); bool duration_to_utime (char *str, utime_t *value); bool size_to_uint64(char *str, int str_len, uint64_t *rtn_value); +bool speed_to_uint64(char *str, int str_len, uint64_t *rtn_value); char *edit_utime (utime_t val, char *buf, int buf_len); bool is_a_number (const char *num); bool is_a_number_list (const char *n); -- 2.39.2