mirror of
https://github.com/postgres/postgres.git
synced 2025-10-28 11:55:03 +03:00
Unify parsing logic for command-line integer options
Most of the integer options for command-line binaries now make use of a single routine able to do the job, fixing issues with the detection of sloppy values caused for example by the use of atoi(), that fails on strings beginning with numerical characters with junk trailing characters. This commit cuts down the number of strings requiring translation by 26 per my count, switching the code to have two error types for invalid and out-of-range values instead. Much more could be done here, with float or even int64 options, but int32 was the most appealing case as it is possible to rely on strtol() to do the job reliably. Note that there are some exceptions for now, like pg_ctl or pg_upgrade that use their own logging logic. A couple of negative TAP tests required some adjustments for the new errors generated. pg_dump and pg_restore tracked the maximum number of parallel jobs within the option parsing. The code is refactored a bit to track that in the code dedicated to parallelism instead. Author: Kyotaro Horiguchi, Michael Paquier Reviewed-by: David Rowley, Álvaro Herrera Discussion: https://postgr.es/m/CALj2ACXqdG9WhqVoJ9zYf-iZt7sgK7Szv5USs=he6NnWQ2ofTA@mail.gmail.com
This commit is contained in:
@@ -63,6 +63,7 @@
|
||||
#include "common/username.h"
|
||||
#include "fe_utils/cancel.h"
|
||||
#include "fe_utils/conditional.h"
|
||||
#include "fe_utils/option_utils.h"
|
||||
#include "fe_utils/string_utils.h"
|
||||
#include "getopt_long.h"
|
||||
#include "libpq-fe.h"
|
||||
@@ -5887,10 +5888,9 @@ main(int argc, char **argv)
|
||||
break;
|
||||
case 'c':
|
||||
benchmarking_option_set = true;
|
||||
nclients = atoi(optarg);
|
||||
if (nclients <= 0)
|
||||
if (!option_parse_int(optarg, "-c/--clients", 1, INT_MAX,
|
||||
&nclients))
|
||||
{
|
||||
pg_log_fatal("invalid number of clients: \"%s\"", optarg);
|
||||
exit(1);
|
||||
}
|
||||
#ifdef HAVE_GETRLIMIT
|
||||
@@ -5914,10 +5914,9 @@ main(int argc, char **argv)
|
||||
break;
|
||||
case 'j': /* jobs */
|
||||
benchmarking_option_set = true;
|
||||
nthreads = atoi(optarg);
|
||||
if (nthreads <= 0)
|
||||
if (!option_parse_int(optarg, "-j/--jobs", 1, INT_MAX,
|
||||
&nthreads))
|
||||
{
|
||||
pg_log_fatal("invalid number of threads: \"%s\"", optarg);
|
||||
exit(1);
|
||||
}
|
||||
#ifndef ENABLE_THREAD_SAFETY
|
||||
@@ -5938,30 +5937,21 @@ main(int argc, char **argv)
|
||||
break;
|
||||
case 's':
|
||||
scale_given = true;
|
||||
scale = atoi(optarg);
|
||||
if (scale <= 0)
|
||||
{
|
||||
pg_log_fatal("invalid scaling factor: \"%s\"", optarg);
|
||||
if (!option_parse_int(optarg, "-s/--scale", 1, INT_MAX,
|
||||
&scale))
|
||||
exit(1);
|
||||
}
|
||||
break;
|
||||
case 't':
|
||||
benchmarking_option_set = true;
|
||||
nxacts = atoi(optarg);
|
||||
if (nxacts <= 0)
|
||||
{
|
||||
pg_log_fatal("invalid number of transactions: \"%s\"", optarg);
|
||||
if (!option_parse_int(optarg, "-t/--transactions", 1, INT_MAX,
|
||||
&nxacts))
|
||||
exit(1);
|
||||
}
|
||||
break;
|
||||
case 'T':
|
||||
benchmarking_option_set = true;
|
||||
duration = atoi(optarg);
|
||||
if (duration <= 0)
|
||||
{
|
||||
pg_log_fatal("invalid duration: \"%s\"", optarg);
|
||||
if (!option_parse_int(optarg, "-T/--time", 1, INT_MAX,
|
||||
&duration))
|
||||
exit(1);
|
||||
}
|
||||
break;
|
||||
case 'U':
|
||||
username = pg_strdup(optarg);
|
||||
@@ -6019,12 +6009,9 @@ main(int argc, char **argv)
|
||||
break;
|
||||
case 'F':
|
||||
initialization_option_set = true;
|
||||
fillfactor = atoi(optarg);
|
||||
if (fillfactor < 10 || fillfactor > 100)
|
||||
{
|
||||
pg_log_fatal("invalid fillfactor: \"%s\"", optarg);
|
||||
if (!option_parse_int(optarg, "-F/--fillfactor", 10, 100,
|
||||
&fillfactor))
|
||||
exit(1);
|
||||
}
|
||||
break;
|
||||
case 'M':
|
||||
benchmarking_option_set = true;
|
||||
@@ -6039,12 +6026,9 @@ main(int argc, char **argv)
|
||||
break;
|
||||
case 'P':
|
||||
benchmarking_option_set = true;
|
||||
progress = atoi(optarg);
|
||||
if (progress <= 0)
|
||||
{
|
||||
pg_log_fatal("invalid thread progress delay: \"%s\"", optarg);
|
||||
if (!option_parse_int(optarg, "-P/--progress", 1, INT_MAX,
|
||||
&progress))
|
||||
exit(1);
|
||||
}
|
||||
break;
|
||||
case 'R':
|
||||
{
|
||||
@@ -6098,12 +6082,9 @@ main(int argc, char **argv)
|
||||
break;
|
||||
case 5: /* aggregate-interval */
|
||||
benchmarking_option_set = true;
|
||||
agg_interval = atoi(optarg);
|
||||
if (agg_interval <= 0)
|
||||
{
|
||||
pg_log_fatal("invalid number of seconds for aggregation: \"%s\"", optarg);
|
||||
if (!option_parse_int(optarg, "--aggregate-interval", 1, INT_MAX,
|
||||
&agg_interval))
|
||||
exit(1);
|
||||
}
|
||||
break;
|
||||
case 6: /* progress-timestamp */
|
||||
progress_timestamp = true;
|
||||
@@ -6135,12 +6116,9 @@ main(int argc, char **argv)
|
||||
break;
|
||||
case 11: /* partitions */
|
||||
initialization_option_set = true;
|
||||
partitions = atoi(optarg);
|
||||
if (partitions < 0)
|
||||
{
|
||||
pg_log_fatal("invalid number of partitions: \"%s\"", optarg);
|
||||
if (!option_parse_int(optarg, "--partitions", 1, INT_MAX,
|
||||
&partitions))
|
||||
exit(1);
|
||||
}
|
||||
break;
|
||||
case 12: /* partition-method */
|
||||
initialization_option_set = true;
|
||||
|
||||
Reference in New Issue
Block a user