]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-settings, config: Add "unlimited" value for uint/size and "infinite" for time...
authorTimo Sirainen <timo.sirainen@open-xchange.com>
Sun, 4 Jun 2023 21:48:57 +0000 (00:48 +0300)
committerAki Tuomi <aki.tuomi@open-xchange.com>
Wed, 12 Feb 2025 10:34:09 +0000 (12:34 +0200)
This is converted to the maximum size of the number.

src/config/config-parser.c
src/config/config-request.c
src/lib-settings/settings-parser.c
src/lib-settings/settings-parser.h

index 490fa7c5931448759c59ff5bbc618c52ff6cc9eb..a2a9ea2f719b46962b52f1bfba5ff129c257e0a2 100644 (file)
@@ -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;
index 901e51aa6ba510e7bb05b963bd7da3d6481e141d..80e7c995dbab9e90352682bf35d92878362ad8d0 100644 (file)
@@ -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: {
index 748fc1bda96e8d918cdb5cc5f3a5cff999eb4c5f..1dd11f487c4ccaf502b688e904ec8db9b5f149de 100644 (file)
@@ -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;
index 9cccaf19012eab3ecf3635ad8cd733589169c1d2..bf3a39161d2942d0f256ab5c7f77161bdcc600f3 100644 (file)
@@ -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);