From 78b510e82a2e9c3ccc2b3586979fdd0348b62337 Mon Sep 17 00:00:00 2001 From: Arthur Zakirov Date: Wed, 22 Nov 2017 19:15:05 +0300 Subject: [PATCH] PGPRO-584: Save unit name within configuration file --- src/configure.c | 21 +++++++++-- src/utils/pgut.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++++ src/utils/pgut.h | 5 +++ 3 files changed, 115 insertions(+), 2 deletions(-) diff --git a/src/configure.c b/src/configure.c index 9faa0b41..786b4277 100644 --- a/src/configure.c +++ b/src/configure.c @@ -109,6 +109,9 @@ pgBackupConfigInit(pgBackupConfig *config) void writeBackupCatalogConfig(FILE *out, pgBackupConfig *config) { + uint64 res; + const char *unit; + fprintf(out, "#Backup instance info\n"); fprintf(out, "PGDATA = %s\n", config->pgdata); fprintf(out, "system-identifier = %li\n", config->system_identifier); @@ -146,10 +149,24 @@ writeBackupCatalogConfig(FILE *out, pgBackupConfig *config) fprintf(out, "error-log-filename = %s\n", config->error_log_filename); if (config->log_directory) fprintf(out, "log-directory = %s\n", config->log_directory); + + /* + * Convert values from base unit + */ if (config->log_rotation_size) - fprintf(out, "log-rotation-size = %d\n", config->log_rotation_size); + { + convert_from_base_unit_u(config->log_rotation_size, OPTION_UNIT_KB, + &res, &unit); + fprintf(out, "log-rotation-size = " UINT64_FORMAT "%s\n", + res, unit); + } if (config->log_rotation_age) - fprintf(out, "log-rotation-age = %d\n", config->log_rotation_age); + { + convert_from_base_unit_u(config->log_rotation_age, OPTION_UNIT_S, + &res, &unit); + fprintf(out, "log-rotation-age = " UINT64_FORMAT "%s\n", + res, unit); + } fprintf(out, "#Retention parameters:\n"); if (config->retention_redundancy) diff --git a/src/utils/pgut.c b/src/utils/pgut.c index e68d7007..02a5aaf2 100644 --- a/src/utils/pgut.c +++ b/src/utils/pgut.c @@ -338,6 +338,97 @@ convert_to_base_unit_u(uint64 value, const char *unit, return false; } +/* + * Convert a value in some base unit to a human-friendly unit. The output + * unit is chosen so that it's the greatest unit that can represent the value + * without loss. For example, if the base unit is GUC_UNIT_KB, 1024 is + * converted to 1 MB, but 1025 is represented as 1025 kB. + */ +void +convert_from_base_unit(int64 base_value, int base_unit, + int64 *value, const char **unit) +{ + const unit_conversion *table; + int i; + + *unit = NULL; + + if (base_unit & OPTION_UNIT_MEMORY) + table = memory_unit_conversion_table; + else + table = time_unit_conversion_table; + + for (i = 0; *table[i].unit; i++) + { + if (base_unit == table[i].base_unit) + { + /* + * Accept the first conversion that divides the value evenly. We + * assume that the conversions for each base unit are ordered from + * greatest unit to the smallest! + */ + if (table[i].multiplier < 0) + { + *value = base_value * (-table[i].multiplier); + *unit = table[i].unit; + break; + } + else if (base_value % table[i].multiplier == 0) + { + *value = base_value / table[i].multiplier; + *unit = table[i].unit; + break; + } + } + } + + Assert(*unit != NULL); +} + +/* + * Unsigned variant of convert_from_base_unit() + */ +void +convert_from_base_unit_u(uint64 base_value, int base_unit, + uint64 *value, const char **unit) +{ + const unit_conversion *table; + int i; + + *unit = NULL; + + if (base_unit & OPTION_UNIT_MEMORY) + table = memory_unit_conversion_table; + else + table = time_unit_conversion_table; + + for (i = 0; *table[i].unit; i++) + { + if (base_unit == table[i].base_unit) + { + /* + * Accept the first conversion that divides the value evenly. We + * assume that the conversions for each base unit are ordered from + * greatest unit to the smallest! + */ + if (table[i].multiplier < 0) + { + *value = base_value * (-table[i].multiplier); + *unit = table[i].unit; + break; + } + else if (base_value % table[i].multiplier == 0) + { + *value = base_value / table[i].multiplier; + *unit = table[i].unit; + break; + } + } + } + + Assert(*unit != NULL); +} + static bool parse_unit(char *unit_str, int flags, int64 value, int64 *base_value) { diff --git a/src/utils/pgut.h b/src/utils/pgut.h index bb918bb1..1ad0c3f6 100644 --- a/src/utils/pgut.h +++ b/src/utils/pgut.h @@ -200,6 +200,11 @@ extern bool parse_time(const char *value, time_t *result); extern bool parse_int(const char *value, int *result, int flags, const char **hintmsg); +extern void convert_from_base_unit(int64 base_value, int base_unit, + int64 *value, const char **unit); +extern void convert_from_base_unit_u(uint64 base_value, int base_unit, + uint64 *value, const char **unit); + #define IsSpace(c) (isspace((unsigned char)(c))) #define IsAlpha(c) (isalpha((unsigned char)(c))) #define IsAlnum(c) (isalnum((unsigned char)(c)))