-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathconfiguration.c
1589 lines (1378 loc) · 33.2 KB
/
configuration.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-------------------------------------------------------------------------
*
* configuration.c: - function implementations to work with pg_probackup
* configurations.
*
* Copyright (c) 2017-2019, Postgres Professional
*
*-------------------------------------------------------------------------
*/
#include "pg_probackup.h"
#include "configuration.h"
#include "logger.h"
#include "pgut.h"
#include "file.h"
#include "datatype/timestamp.h"
#include "getopt_long.h"
#ifndef WIN32
#include <pwd.h>
#endif
#include <time.h>
#define MAXPG_LSNCOMPONENT 8
/*
* Unit conversion tables.
*
* Copied from guc.c.
*/
#define MAX_UNIT_LEN 3 /* length of longest recognized unit string */
typedef struct
{
char unit[MAX_UNIT_LEN + 1]; /* unit, as a string, like "kB" or
* "min" */
int base_unit; /* OPTION_UNIT_XXX */
int multiplier; /* If positive, multiply the value with this
* for unit -> base_unit conversion. If
* negative, divide (with the absolute value) */
} unit_conversion;
static const char *memory_units_hint = "Valid units for this parameter are \"kB\", \"MB\", \"GB\", and \"TB\".";
static const unit_conversion memory_unit_conversion_table[] =
{
{"TB", OPTION_UNIT_KB, 1024 * 1024 * 1024},
{"GB", OPTION_UNIT_KB, 1024 * 1024},
{"MB", OPTION_UNIT_KB, 1024},
{"KB", OPTION_UNIT_KB, 1},
{"kB", OPTION_UNIT_KB, 1},
{"TB", OPTION_UNIT_BLOCKS, (1024 * 1024 * 1024) / (BLCKSZ / 1024)},
{"GB", OPTION_UNIT_BLOCKS, (1024 * 1024) / (BLCKSZ / 1024)},
{"MB", OPTION_UNIT_BLOCKS, 1024 / (BLCKSZ / 1024)},
{"kB", OPTION_UNIT_BLOCKS, -(BLCKSZ / 1024)},
{"TB", OPTION_UNIT_XBLOCKS, (1024 * 1024 * 1024) / (XLOG_BLCKSZ / 1024)},
{"GB", OPTION_UNIT_XBLOCKS, (1024 * 1024) / (XLOG_BLCKSZ / 1024)},
{"MB", OPTION_UNIT_XBLOCKS, 1024 / (XLOG_BLCKSZ / 1024)},
{"kB", OPTION_UNIT_XBLOCKS, -(XLOG_BLCKSZ / 1024)},
{""} /* end of table marker */
};
static const char *time_units_hint = "Valid units for this parameter are \"ms\", \"s\", \"min\", \"h\", and \"d\".";
static const unit_conversion time_unit_conversion_table[] =
{
{"d", OPTION_UNIT_MS, 1000 * 60 * 60 * 24},
{"h", OPTION_UNIT_MS, 1000 * 60 * 60},
{"min", OPTION_UNIT_MS, 1000 * 60},
{"s", OPTION_UNIT_MS, 1000},
{"ms", OPTION_UNIT_MS, 1},
{"d", OPTION_UNIT_S, 60 * 60 * 24},
{"h", OPTION_UNIT_S, 60 * 60},
{"min", OPTION_UNIT_S, 60},
{"s", OPTION_UNIT_S, 1},
{"ms", OPTION_UNIT_S, -1000},
{"d", OPTION_UNIT_MIN, 60 * 24},
{"h", OPTION_UNIT_MIN, 60},
{"min", OPTION_UNIT_MIN, 1},
{"s", OPTION_UNIT_MIN, -60},
{"ms", OPTION_UNIT_MIN, -1000 * 60},
{""} /* end of table marker */
};
/* Order is important, keep it in sync with utils/configuration.h:enum ProbackupSubcmd declaration */
static char const * const subcmd_names[] =
{
"NO_CMD",
"init",
"add-instance",
"del-instance",
"archive-push",
"archive-get",
"backup",
"restore",
"validate",
"delete",
"merge",
"show",
"set-config",
"set-backup",
"show-config",
"checkdb",
"ssh",
"agent",
"help",
"version",
"catchup",
};
ProbackupSubcmd
parse_subcmd(char const * const subcmd_str)
{
struct {
ProbackupSubcmd id;
char *name;
}
static const subcmd_additional_names[] = {
{ HELP_CMD, "--help" },
{ HELP_CMD, "-?" },
{ VERSION_CMD, "--version" },
{ VERSION_CMD, "-V" },
};
int i;
for(i = (int)NO_CMD + 1; i < sizeof(subcmd_names) / sizeof(subcmd_names[0]); ++i)
if(strcmp(subcmd_str, subcmd_names[i]) == 0)
return (ProbackupSubcmd)i;
for(i = 0; i < sizeof(subcmd_additional_names) / sizeof(subcmd_additional_names[0]); ++i)
if(strcmp(subcmd_str, subcmd_additional_names[i].name) == 0)
return subcmd_additional_names[i].id;
return NO_CMD;
}
char const *
get_subcmd_name(ProbackupSubcmd const subcmd)
{
Assert((int)subcmd < sizeof(subcmd_names) / sizeof(subcmd_names[0]));
return subcmd_names[(int)subcmd];
}
/*
* Reading functions.
*/
static uint32
option_length(const ConfigOption opts[])
{
uint32 len;
for (len = 0; opts && opts[len].type; len++) { }
return len;
}
static int
option_has_arg(char type)
{
switch (type)
{
case 'b':
case 'B':
return no_argument;//optional_argument;
default:
return required_argument;
}
}
static void
option_copy(struct option dst[], const ConfigOption opts[], size_t len)
{
size_t i;
for (i = 0; i < len; i++)
{
dst[i].name = opts[i].lname;
dst[i].has_arg = option_has_arg(opts[i].type);
dst[i].flag = NULL;
dst[i].val = opts[i].sname;
}
}
static ConfigOption *
option_find(int c, ConfigOption opts1[])
{
size_t i;
for (i = 0; opts1 && opts1[i].type; i++)
if (opts1[i].sname == c)
return &opts1[i];
return NULL; /* not found */
}
static char *
longopts_to_optstring(const struct option opts[], const size_t len)
{
size_t i;
char *result;
char *s;
result = pgut_malloc(len * 2 + 1);
s = result;
for (i = 0; i < len; i++)
{
if (!isprint(opts[i].val))
continue;
*s++ = opts[i].val;
if (opts[i].has_arg != no_argument)
*s++ = ':';
}
*s = '\0';
return result;
}
/*
* Compare two strings ignore cases and ignore.
*/
static bool
key_equals(const char *lhs, const char *rhs)
{
for (; *lhs && *rhs; lhs++, rhs++)
{
if (strchr("-_ ", *lhs))
{
if (!strchr("-_ ", *rhs))
return false;
}
else if (ToLower(*lhs) != ToLower(*rhs))
return false;
}
return *lhs == '\0' && *rhs == '\0';
}
static void
assign_option(ConfigOption *opt, const char *optarg, OptionSource src)
{
const char *message;
if (opt == NULL)
elog(ERROR, "Option is not found. Try \"%s --help\" for more information.\n",
PROGRAM_NAME);
if (opt->source > src)
{
/* high prior value has been set already. */
return;
}
/* Allow duplicate entries for function option */
else if (src >= SOURCE_CMD && opt->source >= src && opt->type != 'f')
{
message = "specified only once";
}
else
{
OptionSource orig_source = opt->source;
/* can be overwritten if non-command line source */
opt->source = src;
switch (opt->type)
{
case 'b':
case 'B':
if (optarg == NULL)
{
*((bool *) opt->var) = (opt->type == 'b');
return;
}
else if (parse_bool(optarg, (bool *) opt->var))
{
return;
}
message = "a boolean";
break;
case 'f':
((option_assign_fn) opt->var)(opt, optarg);
return;
case 'i':
if (parse_int32(optarg, opt->var, opt->flags))
return;
message = "a 32bit signed integer";
break;
case 'u':
if (parse_uint32(optarg, opt->var, opt->flags))
return;
message = "a 32bit unsigned integer";
break;
case 'I':
if (parse_int64(optarg, opt->var, opt->flags))
return;
message = "a 64bit signed integer";
break;
case 'U':
if (parse_uint64(optarg, opt->var, opt->flags))
return;
message = "a 64bit unsigned integer";
break;
case 's':
if (orig_source != SOURCE_DEFAULT)
free(*(char **) opt->var);
/* 'none' and 'off' are always disable the string parameter */
//if (optarg && (pg_strcasecmp(optarg, "none") == 0))
//{
// *(char **) opt->var = "none";
// return;
//}
*(char **) opt->var = pgut_strdup(optarg);
if (strcmp(optarg,"") != 0)
return;
message = "a valid string";
break;
case 't':
if (parse_time(optarg, opt->var,
opt->source == SOURCE_FILE))
return;
message = "a time";
break;
default:
elog(ERROR, "Invalid option type: %c", opt->type);
return; /* keep compiler quiet */
}
}
if (optarg)
{
if (isprint(opt->sname))
elog(ERROR, "Option -%c, --%s should be %s: '%s'",
opt->sname, opt->lname, message, optarg);
else
elog(ERROR, "Option --%s should be %s: '%s'",
opt->lname, message, optarg);
}
else
{
if (isprint(opt->sname))
elog(ERROR, "Option -%c, --%s should be %s",
opt->sname, opt->lname, message);
else
elog(ERROR, "Option --%s should be %s",
opt->lname, message);
}
}
static const char *
skip_space(const char *str, const char *line)
{
while (IsSpace(*str)) { str++; }
return str;
}
static const char *
get_next_token(const char *src, char *dst, const char *line)
{
const char *s;
int i;
int j;
if ((s = skip_space(src, line)) == NULL)
return NULL;
/* parse quoted string */
if (*s == '\'')
{
s++;
for (i = 0, j = 0; s[i] != '\0'; i++)
{
if (s[i] == '\'')
{
i++;
/* doubled quote becomes just one quote */
if (s[i] == '\'')
dst[j] = s[i];
else
break;
}
else
dst[j] = s[i];
j++;
}
}
else
{
i = j = strcspn(s, "#\n\r\t\v");
memcpy(dst, s, j);
}
dst[j] = '\0';
return s + i;
}
static bool
parse_pair(const char buffer[], char key[], char value[])
{
const char *start;
const char *end;
key[0] = value[0] = '\0';
/*
* parse key
*/
start = buffer;
if ((start = skip_space(start, buffer)) == NULL)
return false;
end = start + strcspn(start, "=# \n\r\t\v");
/* skip blank buffer */
if (end - start <= 0)
{
if (*start == '=')
elog(ERROR, "Syntax error in \"%s\"", buffer);
return false;
}
/* key found */
strncpy(key, start, end - start);
key[end - start] = '\0';
/* find key and value split char */
if ((start = skip_space(end, buffer)) == NULL)
return false;
if (*start != '=')
{
elog(ERROR, "Syntax error in \"%s\"", buffer);
return false;
}
start++;
/*
* parse value
*/
if ((end = get_next_token(start, value, buffer)) == NULL)
return false;
if ((start = skip_space(end, buffer)) == NULL)
return false;
if (*start != '\0' && *start != '#')
{
elog(ERROR, "Syntax error in \"%s\"", buffer);
return false;
}
return true;
}
/*
* Returns the current user name.
*/
static const char *
get_username(void)
{
const char *ret;
#ifndef WIN32
struct passwd *pw;
pw = getpwuid(geteuid());
ret = (pw ? pw->pw_name : NULL);
#else
static char username[128]; /* remains after function exit */
DWORD len = sizeof(username) - 1;
if (GetUserName(username, &len))
ret = username;
else
{
_dosmaperr(GetLastError());
ret = NULL;
}
#endif
if (ret == NULL)
elog(ERROR, "Could not get current user name: %s", strerror(errno));
return ret;
}
/*
* Process options passed from command line.
* TODO: currectly argument parsing treat missing argument for options
* as invalid option
*/
int
config_get_opt(int argc, char **argv, ConfigOption cmd_options[],
ConfigOption options[])
{
int c;
int optindex = 0;
char *optstring;
struct option *longopts;
uint32 cmd_len,
len;
cmd_len = option_length(cmd_options);
len = option_length(options);
longopts = pgut_newarray(struct option,
cmd_len + len + 1 /* zero/end option */);
/* Concatenate two options */
option_copy(longopts, cmd_options, cmd_len);
option_copy(longopts + cmd_len, options, len + 1);
optstring = longopts_to_optstring(longopts, cmd_len + len);
/* Assign named options */
while ((c = getopt_long(argc, argv, optstring, longopts, &optindex)) != -1)
{
ConfigOption *opt;
opt = option_find(c, cmd_options);
if (opt == NULL)
opt = option_find(c, options);
if (opt
&& !remote_agent
&& opt->allowed < SOURCE_CMD && opt->allowed != SOURCE_CMD_STRICT)
elog(ERROR, "Option %s cannot be specified in command line",
opt->lname);
/* Check 'opt == NULL' is performed in assign_option() */
assign_option(opt, optarg, SOURCE_CMD);
}
return optind;
}
/*
* Get configuration from configuration file.
* Return number of parsed options.
*/
int
config_read_opt(const char *path, ConfigOption options[], int elevel,
bool strict, bool missing_ok)
{
FILE *fp;
char buf[4096];
char key[1024];
char value[2048];
int parsed_options = 0;
if (!options)
return parsed_options;
if ((fp = pgut_fopen(path, "rt", missing_ok)) == NULL)
return parsed_options;
while (fgets(buf, lengthof(buf), fp))
{
size_t i;
for (i = strlen(buf); i > 0 && IsSpace(buf[i - 1]); i--)
buf[i - 1] = '\0';
if (parse_pair(buf, key, value))
{
for (i = 0; options[i].type; i++)
{
ConfigOption *opt = &options[i];
if (key_equals(key, opt->lname))
{
if (opt->allowed < SOURCE_FILE &&
opt->allowed != SOURCE_FILE_STRICT)
elog(elevel, "Option %s cannot be specified in file",
opt->lname);
else if (opt->source <= SOURCE_FILE)
{
assign_option(opt, value, SOURCE_FILE);
parsed_options++;
}
break;
}
}
if (strict && !options[i].type)
elog(elevel, "Invalid option \"%s\" in file \"%s\"", key, path);
}
}
if (ferror(fp))
elog(ERROR, "Failed to read from file: \"%s\"", path);
fio_close_stream(fp);
return parsed_options;
}
/*
* Process options passed as environment variables.
*/
void
config_get_opt_env(ConfigOption options[])
{
size_t i;
for (i = 0; options && options[i].type; i++)
{
ConfigOption *opt = &options[i];
const char *value = NULL;
/* If option was already set do not check env */
if (opt->source > SOURCE_ENV || opt->allowed < SOURCE_ENV)
continue;
if (strcmp(opt->lname, "pgdata") == 0)
value = getenv("PGDATA");
if (strcmp(opt->lname, "port") == 0)
value = getenv("PGPORT");
if (strcmp(opt->lname, "host") == 0)
value = getenv("PGHOST");
if (strcmp(opt->lname, "username") == 0)
value = getenv("PGUSER");
if (strcmp(opt->lname, "pgdatabase") == 0)
{
value = getenv("PGDATABASE");
if (value == NULL)
value = getenv("PGUSER");
if (value == NULL)
value = get_username();
}
if (value)
assign_option(opt, value, SOURCE_ENV);
}
}
/*
* Manually set source of the option. Find it by the pointer var.
*/
void
config_set_opt(ConfigOption options[], void *var, OptionSource source)
{
int i;
for (i = 0; options[i].type; i++)
{
ConfigOption *opt = &options[i];
if (opt->var == var)
{
if ((opt->allowed == SOURCE_FILE_STRICT && source != SOURCE_FILE) ||
(opt->allowed == SOURCE_CMD_STRICT && source != SOURCE_CMD) ||
(opt->allowed < source && opt->allowed >= SOURCE_ENV))
elog(ERROR, "Invalid option source %d for %s",
source, opt->lname);
opt->source = source;
break;
}
}
}
/*
* Return value of the function in the string representation. Result is
* allocated string.
*/
char *
option_get_value(ConfigOption *opt)
{
int64 value = 0;
uint64 value_u = 0;
const char *unit = NULL;
/*
* If it is defined a unit for the option get readable value from base with
* unit name.
*/
if (opt->flags & OPTION_UNIT)
{
if (opt->type == 'i')
convert_from_base_unit(*((int32 *) opt->var),
opt->flags & OPTION_UNIT, &value, &unit);
else if (opt->type == 'i')
convert_from_base_unit(*((int64 *) opt->var),
opt->flags & OPTION_UNIT, &value, &unit);
else if (opt->type == 'u')
convert_from_base_unit_u(*((uint32 *) opt->var),
opt->flags & OPTION_UNIT, &value_u, &unit);
else if (opt->type == 'U')
convert_from_base_unit_u(*((uint64 *) opt->var),
opt->flags & OPTION_UNIT, &value_u, &unit);
}
/* Get string representation itself */
switch (opt->type)
{
case 'b':
case 'B':
return psprintf("%s", *((bool *) opt->var) ? "true" : "false");
case 'i':
if (opt->flags & OPTION_UNIT)
return psprintf(INT64_FORMAT "%s", value, unit);
else
return psprintf("%d", *((int32 *) opt->var));
case 'u':
if (opt->flags & OPTION_UNIT)
return psprintf(UINT64_FORMAT "%s", value_u, unit);
else
return psprintf("%u", *((uint32 *) opt->var));
case 'I':
if (opt->flags & OPTION_UNIT)
return psprintf(INT64_FORMAT "%s", value, unit);
else
return psprintf(INT64_FORMAT, *((int64 *) opt->var));
case 'U':
if (opt->flags & OPTION_UNIT)
return psprintf(UINT64_FORMAT "%s", value_u, unit);
else
return psprintf(UINT64_FORMAT, *((uint64 *) opt->var));
case 's':
if (*((char **) opt->var) == NULL)
return NULL;
/* 'none' and 'off' are always disable the string parameter */
//if ((pg_strcasecmp(*((char **) opt->var), "none") == 0) ||
// (pg_strcasecmp(*((char **) opt->var), "off") == 0))
// return NULL;
return pstrdup(*((char **) opt->var));
case 't':
{
char *timestamp;
time_t t = *((time_t *) opt->var);
if (t > 0)
{
timestamp = palloc(100);
time2iso(timestamp, 100, t, false);
}
else
timestamp = palloc0(1 /* just null termination */);
return timestamp;
}
default:
elog(ERROR, "Invalid option type: %c", opt->type);
return NULL; /* keep compiler quiet */
}
}
/*
* Parsing functions
*/
/*
* Convert a value from one of the human-friendly units ("kB", "min" etc.)
* to the given base unit. 'value' and 'unit' are the input value and unit
* to convert from. The converted value is stored in *base_value.
*
* Returns true on success, false if the input unit is not recognized.
*/
static bool
convert_to_base_unit(int64 value, const char *unit,
int base_unit, int64 *base_value)
{
const unit_conversion *table;
int i;
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 &&
strcmp(unit, table[i].unit) == 0)
{
if (table[i].multiplier < 0)
*base_value = value / (-table[i].multiplier);
else
{
/* Check for integer overflow first */
if (value > PG_INT64_MAX / table[i].multiplier)
return false;
*base_value = value * table[i].multiplier;
}
return true;
}
}
return false;
}
/*
* Unsigned variant of convert_to_base_unit()
*/
static bool
convert_to_base_unit_u(uint64 value, const char *unit,
int base_unit, uint64 *base_value)
{
const unit_conversion *table;
int i;
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 &&
strcmp(unit, table[i].unit) == 0)
{
if (table[i].multiplier < 0)
*base_value = value / (-table[i].multiplier);
else
{
/* Check for integer overflow first */
if (value > PG_UINT64_MAX / table[i].multiplier)
return false;
*base_value = value * table[i].multiplier;
}
return true;
}
}
return false;
}
static bool
parse_unit(char *unit_str, int flags, int64 value, int64 *base_value)
{
/* allow whitespace between integer and unit */
while (isspace((unsigned char) *unit_str))
unit_str++;
/* Handle possible unit */
if (*unit_str != '\0')
{
char unit[MAX_UNIT_LEN + 1];
int unitlen;
bool converted = false;
if ((flags & OPTION_UNIT) == 0)
return false; /* this setting does not accept a unit */
unitlen = 0;
while (*unit_str != '\0' && !isspace((unsigned char) *unit_str) &&
unitlen < MAX_UNIT_LEN)
unit[unitlen++] = *(unit_str++);
unit[unitlen] = '\0';
/* allow whitespace after unit */
while (isspace((unsigned char) *unit_str))
unit_str++;
if (*unit_str == '\0')
converted = convert_to_base_unit(value, unit, (flags & OPTION_UNIT),
base_value);
if (!converted)
return false;
}
return true;
}
/*
* Unsigned variant of parse_unit()
*/
static bool
parse_unit_u(char *unit_str, int flags, uint64 value, uint64 *base_value)
{
/* allow whitespace between integer and unit */
while (isspace((unsigned char) *unit_str))
unit_str++;
/* Handle possible unit */
if (*unit_str != '\0')
{
char unit[MAX_UNIT_LEN + 1];
int unitlen;
bool converted = false;
if ((flags & OPTION_UNIT) == 0)
return false; /* this setting does not accept a unit */
unitlen = 0;
while (*unit_str != '\0' && !isspace((unsigned char) *unit_str) &&
unitlen < MAX_UNIT_LEN)
unit[unitlen++] = *(unit_str++);
unit[unitlen] = '\0';
/* allow whitespace after unit */
while (isspace((unsigned char) *unit_str))
unit_str++;
if (*unit_str == '\0')
converted = convert_to_base_unit_u(value, unit,
(flags & OPTION_UNIT),
base_value);
if (!converted)
return false;
}
return true;
}
/*
* Try to interpret value as boolean value. Valid values are: true,
* false, yes, no, on, off, 1, 0; as well as unique prefixes thereof.
* If the string parses okay, return true, else false.
* If okay and result is not NULL, return the value in *result.
*/
bool
parse_bool(const char *value, bool *result)
{
return parse_bool_with_len(value, strlen(value), result);
}
bool
parse_bool_with_len(const char *value, size_t len, bool *result)
{
switch (*value)
{
case 't':
case 'T':
if (pg_strncasecmp(value, "true", len) == 0)
{
if (result)
*result = true;
return true;
}
break;
case 'f':
case 'F':
if (pg_strncasecmp(value, "false", len) == 0)
{
if (result)
*result = false;
return true;
}
break;
case 'y':
case 'Y':
if (pg_strncasecmp(value, "yes", len) == 0)
{
if (result)
*result = true;
return true;
}
break;
case 'n':
case 'N':
if (pg_strncasecmp(value, "no", len) == 0)
{
if (result)
*result = false;
return true;
}
break;
case 'o':
case 'O':
/* 'o' is not unique enough */
if (pg_strncasecmp(value, "on", (len > 2 ? len : 2)) == 0)
{
if (result)
*result = true;
return true;
}
else if (pg_strncasecmp(value, "off", (len > 2 ? len : 2)) == 0)
{
if (result)
*result = false;
return true;
}
break;
case '1':
if (len == 1)
{
if (result)
*result = true;
return true;
}
break;
case '0':
if (len == 1)
{
if (result)
*result = false;
return true;
}
break;
default:
break;
}
if (result)
*result = false; /* suppress compiler warning */