From: Timo Sirainen Date: Sun, 4 Jun 2023 21:48:57 +0000 (+0300) Subject: lib-settings, config: Add "unlimited" value for uint/size and "infinite" for time... X-Git-Tag: 2.4.1~1573 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b16b97bfa79bbabecab0dcd8a0605b664eb75772;p=thirdparty%2Fdovecot%2Fcore.git lib-settings, config: Add "unlimited" value for uint/size and "infinite" for time/time_msecs This is converted to the maximum size of the number. --- diff --git a/src/config/config-parser.c b/src/config/config-parser.c index 490fa7c593..a2a9ea2f71 100644 --- a/src/config/config-parser.c +++ b/src/config/config-parser.c @@ -306,6 +306,8 @@ settings_value_check(struct config_parser_context *ctx, if (strchr(value, '%') != NULL) break; + if (settings_value_is_unlimited(value)) + break; if (str_to_uint(value, &num) < 0) { ctx->error = p_strdup_printf(ctx->pool, "Invalid number %s: %s", value, @@ -318,6 +320,8 @@ settings_value_check(struct config_parser_context *ctx, unsigned int interval; if (strchr(value, '%') != NULL) break; + if (settings_value_is_unlimited(value)) + break; if (str_parse_get_interval(value, &interval, &error) < 0) { ctx->error = p_strdup(ctx->pool, error); return -1; @@ -328,6 +332,8 @@ settings_value_check(struct config_parser_context *ctx, unsigned int interval; if (strchr(value, '%') != NULL) break; + if (settings_value_is_unlimited(value)) + break; if (str_parse_get_interval_msecs(value, &interval, &error) < 0) { ctx->error = p_strdup(ctx->pool, error); return -1; @@ -338,6 +344,8 @@ settings_value_check(struct config_parser_context *ctx, uoff_t size; if (strchr(value, '%') != NULL) break; + if (settings_value_is_unlimited(value)) + break; if (str_parse_get_size(value, &size, &error) < 0) { ctx->error = p_strdup(ctx->pool, error); return -1; diff --git a/src/config/config-request.c b/src/config/config-request.c index 901e51aa6b..80e7c995db 100644 --- a/src/config/config-request.c +++ b/src/config/config-request.c @@ -36,6 +36,10 @@ static void config_export_size(string_t *str, uoff_t size) str_append_c(str, '0'); return; } + if (size == SET_SIZE_UNLIMITED) { + str_append(str, SET_VALUE_UNLIMITED); + return; + } for (i = 1; i < N_ELEMENTS(suffixes) && (size % 1024) == 0; i++) { suffix = suffixes[i]; size /= 1024; @@ -51,6 +55,10 @@ static void config_export_time(string_t *str, unsigned int stamp) str_append_c(str, '0'); return; } + if (stamp == SET_TIME_INFINITE) { + str_append(str, SET_VALUE_INFINITE); + return; + } if (stamp % 60 == 0) { stamp /= 60; @@ -74,7 +82,9 @@ static void config_export_time(string_t *str, unsigned int stamp) static void config_export_time_msecs(string_t *str, unsigned int stamp_msecs) { - if ((stamp_msecs % 1000) == 0) + if (stamp_msecs == SET_TIME_MSECS_INFINITE) + str_append(str, SET_VALUE_INFINITE); + else if ((stamp_msecs % 1000) == 0) config_export_time(str, stamp_msecs/1000); else str_printfa(str, "%u ms", stamp_msecs); @@ -97,25 +107,26 @@ bool config_export_type(string_t *str, const void *value, break; } case SET_UINT: - case SET_UINT_OCT: + case SET_UINT_OCT: { + const unsigned int *val = value; + if (*val == SET_UINT_UNLIMITED) { + str_append(str, SET_VALUE_UNLIMITED); + break; + } + if (type == SET_UINT_OCT) + str_printfa(str, "0%o", *val); + else + str_printfa(str, "%u", *val); + break; + } case SET_TIME: case SET_TIME_MSECS: { const unsigned int *val = value; - switch (type) { - case SET_UINT_OCT: - str_printfa(str, "0%o", *val); - break; - case SET_TIME: + if (type == SET_TIME) config_export_time(str, *val); - break; - case SET_TIME_MSECS: + else config_export_time_msecs(str, *val); - break; - default: - str_printfa(str, "%u", *val); - break; - } break; } case SET_IN_PORT: { diff --git a/src/lib-settings/settings-parser.c b/src/lib-settings/settings-parser.c index 748fc1bda9..1dd11f487c 100644 --- a/src/lib-settings/settings-parser.c +++ b/src/lib-settings/settings-parser.c @@ -190,6 +190,10 @@ static int get_uint(struct setting_parser_context *ctx, const char *value, unsigned int *result_r) { + if (settings_value_is_unlimited(value)) { + *result_r = SET_UINT_UNLIMITED; + return 0; + } if (str_to_uint(value, result_r) < 0) { settings_parser_set_error(ctx, t_strdup_printf( "Invalid number %s: %s", value, @@ -319,18 +323,30 @@ settings_parse(struct setting_parser_context *ctx, return -1; break; case SET_TIME: + if (settings_value_is_unlimited(value)) { + *(unsigned int *)ptr = SET_TIME_INFINITE; + return 0; + } if (str_parse_get_interval(value, (unsigned int *)ptr, &error) < 0) { settings_parser_set_error(ctx, error); return -1; } break; case SET_TIME_MSECS: + if (settings_value_is_unlimited(value)) { + *(unsigned int *)ptr = SET_TIME_MSECS_INFINITE; + return 0; + } if (str_parse_get_interval_msecs(value, (unsigned int *)ptr, &error) < 0) { settings_parser_set_error(ctx, error); return -1; } break; case SET_SIZE: + if (settings_value_is_unlimited(value)) { + *(uoff_t *)ptr = SET_SIZE_UNLIMITED; + return 0; + } if (str_parse_get_size(value, (uoff_t *)ptr, &error) < 0) { settings_parser_set_error(ctx, error); return -1; diff --git a/src/lib-settings/settings-parser.h b/src/lib-settings/settings-parser.h index 9cccaf1901..bf3a39161d 100644 --- a/src/lib-settings/settings-parser.h +++ b/src/lib-settings/settings-parser.h @@ -9,6 +9,16 @@ struct var_expand_func_table; #define SETTINGS_SEPARATOR '/' #define SETTINGS_SEPARATOR_S "/" +/* These values are shown as "unlimited" */ +#define SET_VALUE_UNLIMITED "unlimited" +#define SET_UINT_UNLIMITED UINT_MAX +#define SET_SIZE_UNLIMITED UOFF_T_MAX + +/* These values are shown as "infinite" */ +#define SET_VALUE_INFINITE "infinite" +#define SET_TIME_INFINITE UINT_MAX +#define SET_TIME_MSECS_INFINITE UINT_MAX + enum setting_type { SET_BOOL, SET_UINT, @@ -189,6 +199,13 @@ bool settings_check(struct event *event, const struct setting_parser_info *info, const char *settings_section_escape(const char *name); const char *settings_section_unescape(const char *name); +static inline bool settings_value_is_unlimited(const char *value) +{ + /* allow both as input for all types */ + return strcmp(value, SET_VALUE_UNLIMITED) == 0 || + strcmp(value, SET_VALUE_INFINITE) == 0; +} + void set_config_binary(bool value); bool is_config_binary(void);