Skip to content

Pbckp 216 float locale #506

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/pg_probackup.c
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,10 @@ main(int argc, char *argv[])
set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_probackup"));
PROGRAM_FULL_PATH = palloc0(MAXPGPATH);

// Setting C locale for numeric values in order to impose dot-based floating-point representation
memorize_environment_locale();
setlocale(LC_NUMERIC, "C");

/* Get current time */
current_time = time(NULL);

Expand Down Expand Up @@ -1024,6 +1028,8 @@ main(int argc, char *argv[])
break;
}

free_environment_locale();

return 0;
}

Expand Down
2 changes: 2 additions & 0 deletions src/pg_probackup.h
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,8 @@ extern InstanceConfig *readInstanceConfigFile(InstanceState *instanceState);
/* in show.c */
extern int do_show(CatalogState *catalogState, InstanceState *instanceState,
time_t requested_backup_id, bool show_archive);
extern void memorize_environment_locale(void);
extern void free_environment_locale(void);

/* in delete.c */
extern void do_delete(InstanceState *instanceState, time_t backup_id);
Expand Down
53 changes: 51 additions & 2 deletions src/show.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* show.c: show backup information.
*
* Portions Copyright (c) 2009-2011, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
* Portions Copyright (c) 2015-2019, Postgres Professional
* Portions Copyright (c) 2015-2022, Postgres Professional
*
*-------------------------------------------------------------------------
*/
Expand All @@ -12,6 +12,7 @@

#include <time.h>
#include <dirent.h>
#include <locale.h>
#include <sys/stat.h>

#include "utils/json.h"
Expand Down Expand Up @@ -71,6 +72,43 @@ static PQExpBufferData show_buf;
static bool first_instance = true;
static int32 json_level = 0;

static const char* lc_env_locale;
typedef enum {
LOCALE_C, // Used for formatting output to unify the dot-based floating point representation
LOCALE_ENV // Default environment locale
} output_numeric_locale;

#ifdef HAVE_USELOCALE
static locale_t env_locale, c_locale;
#endif
void memorize_environment_locale() {
lc_env_locale = (const char *)getenv("LC_NUMERIC");
lc_env_locale = lc_env_locale != NULL ? lc_env_locale : "C";
#ifdef HAVE_USELOCALE
env_locale = newlocale(LC_NUMERIC_MASK, lc_env_locale, (locale_t)0);
c_locale = newlocale(LC_NUMERIC_MASK, "C", (locale_t)0);
#else
#ifdef HAVE__CONFIGTHREADLOCALE
_configthreadlocale(_ENABLE_PER_THREAD_LOCALE);
#endif
#endif
}

void free_environment_locale() {
#ifdef HAVE_USELOCALE
freelocale(env_locale);
freelocale(c_locale);
#endif
}

static void set_output_numeric_locale(output_numeric_locale loc) {
#ifdef HAVE_USELOCALE
uselocale(loc == LOCALE_C ? c_locale : env_locale);
#else
setlocale(LC_NUMERIC, loc == LOCALE_C ? "C" : lc_env_locale);
#endif
}

/*
* Entry point of pg_probackup SHOW subcommand.
*/
Expand Down Expand Up @@ -513,6 +551,9 @@ show_instance_plain(const char *instance_name, parray *backup_list, bool show_na
ShowBackendRow *rows;
TimeLineID parent_tli = 0;

// Since we've been printing a table, set LC_NUMERIC to its default environment value
set_output_numeric_locale(LOCALE_ENV);

for (i = 0; i < SHOW_FIELDS_COUNT; i++)
widths[i] = strlen(names[i]);

Expand Down Expand Up @@ -726,6 +767,8 @@ show_instance_plain(const char *instance_name, parray *backup_list, bool show_na
}

pfree(rows);
// Restore the C locale
set_output_numeric_locale(LOCALE_C);
}

/*
Expand Down Expand Up @@ -806,6 +849,9 @@ show_archive_plain(const char *instance_name, uint32 xlog_seg_size,
uint32 widths_sum = 0;
ShowArchiveRow *rows;

// Since we've been printing a table, set LC_NUMERIC to its default environment value
set_output_numeric_locale(LOCALE_ENV);

for (i = 0; i < SHOW_ARCHIVE_FIELDS_COUNT; i++)
widths[i] = strlen(names[i]);

Expand Down Expand Up @@ -973,6 +1019,8 @@ show_archive_plain(const char *instance_name, uint32 xlog_seg_size,
}

pfree(rows);
// Restore the C locale
set_output_numeric_locale(LOCALE_C);
//TODO: free timelines
}

Expand Down Expand Up @@ -1045,8 +1093,9 @@ show_archive_json(const char *instance_name, uint32 xlog_seg_size,
appendPQExpBuffer(buf, "%lu", tlinfo->size);

json_add_key(buf, "zratio", json_level);

if (tlinfo->size != 0)
zratio = ((float)xlog_seg_size*tlinfo->n_xlog_files) / tlinfo->size;
zratio = ((float) xlog_seg_size * tlinfo->n_xlog_files) / tlinfo->size;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Мне кажется, это к сути тикета не относится.

Copy link
Member Author

@kulaginm kulaginm Jul 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Этот кусочек вроде я приводил к единому стилю. Можно оставить, а можно выпилить.

appendPQExpBuffer(buf, "%.2f", zratio);

if (tlinfo->closest_backup != NULL)
Expand Down
3 changes: 3 additions & 0 deletions src/utils/configuration.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

#include "getopt_long.h"

#ifndef WIN32
#include <pwd.h>
#endif
#include <time.h>

#define MAXPG_LSNCOMPONENT 8
Expand Down