Skip to content

Commit 0afc004

Browse files
committed
Use optind to parse subcommand name
1 parent 1304c88 commit 0afc004

File tree

3 files changed

+128
-130
lines changed

3 files changed

+128
-130
lines changed

src/pg_probackup.c

Lines changed: 109 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ static char *target_inclusive;
6464
static TimeLineID target_tli;
6565
static bool target_immediate;
6666
static char *target_name = NULL;
67-
static char *target_action = NULL;;
67+
static char *target_action = NULL;
6868

6969
static pgRecoveryTarget *recovery_target_options = NULL;
7070

@@ -99,9 +99,10 @@ ShowFormat show_format = SHOW_PLAIN;
9999

100100
/* current settings */
101101
pgBackup current;
102-
ProbackupSubcmd backup_subcmd;
102+
ProbackupSubcmd backup_subcmd = NO_CMD;
103103

104-
bool help = false;
104+
static bool help_opt = false;
105+
static bool version_opt = false;
105106

106107
static void opt_backup_mode(pgut_option *opt, const char *arg);
107108
static void opt_log_level_console(pgut_option *opt, const char *arg);
@@ -114,7 +115,8 @@ static void compress_init(void);
114115
static pgut_option options[] =
115116
{
116117
/* directory options */
117-
{ 'b', 1, "help", &help, SOURCE_CMDLINE },
118+
{ 'b', 1, "help", &help_opt, SOURCE_CMDLINE },
119+
{ 'b', 'V', "version", &version_opt, SOURCE_CMDLINE },
118120
{ 's', 'D', "pgdata", &pgdata, SOURCE_CMDLINE },
119121
{ 's', 'B', "backup-path", &backup_path, SOURCE_CMDLINE },
120122
/* common options */
@@ -152,7 +154,7 @@ static pgut_option options[] =
152154
{ 'b', 131, "expired", &delete_expired, SOURCE_CMDLINE },
153155
{ 'b', 132, "all", &apply_to_all, SOURCE_CMDLINE },
154156
/* TODO not implemented yet */
155-
{ 'b', 133, "force", &force_delete, SOURCE_CMDLINE },
157+
{ 'b', 133, "force", &force_delete, SOURCE_CMDLINE },
156158
/* retention options */
157159
{ 'u', 134, "retention-redundancy", &retention_redundancy, SOURCE_CMDLINE },
158160
{ 'u', 135, "retention-window", &retention_window, SOURCE_CMDLINE },
@@ -198,6 +200,9 @@ main(int argc, char *argv[])
198200
/* Check if backup_path is directory. */
199201
struct stat stat_buf;
200202
int rc;
203+
int i,
204+
len = 0,
205+
allocated = 0;
201206

202207
/* initialize configuration */
203208
pgBackup_init(&current);
@@ -210,106 +215,94 @@ main(int argc, char *argv[])
210215
*/
211216
main_tid = pthread_self();
212217

213-
/* Parse subcommands and non-subcommand options */
214-
if (argc > 1)
215-
{
216-
if (strcmp(argv[1], "archive-push") == 0)
217-
backup_subcmd = ARCHIVE_PUSH;
218-
else if (strcmp(argv[1], "archive-get") == 0)
219-
backup_subcmd = ARCHIVE_GET;
220-
else if (strcmp(argv[1], "add-instance") == 0)
221-
backup_subcmd = ADD_INSTANCE;
222-
else if (strcmp(argv[1], "del-instance") == 0)
223-
backup_subcmd = DELETE_INSTANCE;
224-
else if (strcmp(argv[1], "init") == 0)
225-
backup_subcmd = INIT;
226-
else if (strcmp(argv[1], "backup") == 0)
227-
backup_subcmd = BACKUP;
228-
else if (strcmp(argv[1], "restore") == 0)
229-
backup_subcmd = RESTORE;
230-
else if (strcmp(argv[1], "validate") == 0)
231-
backup_subcmd = VALIDATE;
232-
else if (strcmp(argv[1], "show") == 0)
233-
backup_subcmd = SHOW;
234-
else if (strcmp(argv[1], "delete") == 0)
235-
backup_subcmd = DELETE_SUBCMD;
236-
else if (strcmp(argv[1], "set-config") == 0)
237-
backup_subcmd = SET_CONFIG;
238-
else if (strcmp(argv[1], "show-config") == 0)
239-
backup_subcmd = SHOW_CONFIG;
240-
else if (strcmp(argv[1], "--help") == 0
241-
|| strcmp(argv[1], "help") == 0
242-
|| strcmp(argv[1], "-?") == 0)
243-
{
244-
if (argc > 2)
245-
help_command(argv[2]);
246-
else
247-
help_pg_probackup();
248-
}
249-
else if (strcmp(argv[1], "--version") == 0
250-
|| strcmp(argv[1], "version") == 0
251-
|| strcmp(argv[1], "-V") == 0)
252-
{
253-
if (argc == 2)
254-
{
255-
#ifdef PGPRO_VERSION
256-
fprintf(stderr, "%s %s (Postgres Pro %s %s)\n",
257-
PROGRAM_NAME, PROGRAM_VERSION,
258-
PGPRO_VERSION, PGPRO_EDITION);
259-
#else
260-
fprintf(stderr, "%s %s (PostgreSQL %s)\n",
261-
PROGRAM_NAME, PROGRAM_VERSION, PG_VERSION);
262-
#endif
263-
exit(0);
264-
}
265-
else if (strcmp(argv[2], "--help") == 0)
266-
help_command(argv[1]);
267-
else
268-
elog(ERROR, "Invalid arguments for \"%s\" subcommand", argv[1]);
269-
}
270-
else
271-
elog(ERROR, "Unknown subcommand");
272-
}
273-
274218
/*
275219
* Make command string before getopt_long() will call. It permutes the
276220
* content of argv.
277221
*/
278-
if (backup_subcmd == BACKUP ||
279-
backup_subcmd == RESTORE ||
280-
backup_subcmd == VALIDATE ||
281-
backup_subcmd == DELETE_SUBCMD)
282-
{
283-
int i,
284-
len = 0,
285-
allocated = 0;
222+
allocated = sizeof(char) * MAXPGPATH;
223+
command = (char *) palloc(allocated);
286224

287-
allocated = sizeof(char) * MAXPGPATH;
288-
command = (char *) palloc(allocated);
225+
for (i = 0; i < argc; i++)
226+
{
227+
int arglen = strlen(argv[i]);
289228

290-
for (i = 0; i < argc; i++)
229+
if (arglen + len > allocated)
291230
{
292-
int arglen = strlen(argv[i]);
293-
294-
if (arglen + len > allocated)
295-
{
296-
allocated *= 2;
297-
command = repalloc(command, allocated);
298-
}
299-
300-
strncpy(command + len, argv[i], arglen);
301-
len += arglen;
302-
command[len++] = ' ';
231+
allocated *= 2;
232+
command = repalloc(command, allocated);
303233
}
304234

305-
command[len] = '\0';
235+
strncpy(command + len, argv[i], arglen);
236+
len += arglen;
237+
command[len++] = ' ';
306238
}
307239

240+
command[len] = '\0';
241+
308242
/* Parse command line arguments */
309243
pgut_getopt(argc, argv, options);
310244

311-
if (help)
312-
help_command(argv[2]);
245+
/* Process a command */
246+
if (optind < argc)
247+
{
248+
if (strcmp(argv[optind], "archive-push") == 0)
249+
backup_subcmd = ARCHIVE_PUSH_CMD;
250+
else if (strcmp(argv[optind], "archive-get") == 0)
251+
backup_subcmd = ARCHIVE_GET_CMD;
252+
else if (strcmp(argv[optind], "add-instance") == 0)
253+
backup_subcmd = ADD_INSTANCE_CMD;
254+
else if (strcmp(argv[optind], "del-instance") == 0)
255+
backup_subcmd = DELETE_INSTANCE_CMD;
256+
else if (strcmp(argv[optind], "init") == 0)
257+
backup_subcmd = INIT_CMD;
258+
else if (strcmp(argv[optind], "backup") == 0)
259+
backup_subcmd = BACKUP_CMD;
260+
else if (strcmp(argv[optind], "restore") == 0)
261+
backup_subcmd = RESTORE_CMD;
262+
else if (strcmp(argv[optind], "validate") == 0)
263+
backup_subcmd = VALIDATE_CMD;
264+
else if (strcmp(argv[optind], "show") == 0)
265+
backup_subcmd = SHOW_CMD;
266+
else if (strcmp(argv[optind], "delete") == 0)
267+
backup_subcmd = DELETE_CMD;
268+
else if (strcmp(argv[optind], "set-config") == 0)
269+
backup_subcmd = SET_CONFIG_CMD;
270+
else if (strcmp(argv[optind], "show-config") == 0)
271+
backup_subcmd = SHOW_CONFIG_CMD;
272+
else if (strcmp(argv[optind], "help") == 0)
273+
{
274+
if (argc - optind < 2)
275+
help_pg_probackup();
276+
else
277+
help_command(argv[optind + 1]);
278+
}
279+
else if (strcmp(argv[optind], "version") == 0)
280+
version_opt = true;
281+
else
282+
elog(ERROR, "Unknown subcommand \"%s\"", argv[optind]);
283+
}
284+
285+
if (help_opt)
286+
{
287+
if (backup_subcmd == NO_CMD || argc - optind < 1)
288+
help_pg_probackup();
289+
else
290+
help_command(argv[optind]);
291+
}
292+
else if (version_opt)
293+
{
294+
#ifdef PGPRO_VERSION
295+
fprintf(stderr, "%s %s (Postgres Pro %s %s)\n",
296+
PROGRAM_NAME, PROGRAM_VERSION,
297+
PGPRO_VERSION, PGPRO_EDITION);
298+
#else
299+
fprintf(stderr, "%s %s (PostgreSQL %s)\n",
300+
PROGRAM_NAME, PROGRAM_VERSION, PG_VERSION);
301+
#endif
302+
return 0;
303+
}
304+
else if (backup_subcmd == NO_CMD)
305+
elog(ERROR, "No subcommand specified");
313306

314307
/* backup_path is required for all pg_probackup commands except help */
315308
if (backup_path == NULL)
@@ -343,7 +336,7 @@ main(int argc, char *argv[])
343336
}
344337

345338
/* Option --instance is required for all commands except init and show */
346-
if (backup_subcmd != INIT && backup_subcmd != SHOW && backup_subcmd != VALIDATE)
339+
if (backup_subcmd != INIT_CMD && backup_subcmd != SHOW_CMD && backup_subcmd != VALIDATE_CMD)
347340
{
348341
if (instance_name == NULL)
349342
elog(ERROR, "required parameter not specified: --instance");
@@ -363,7 +356,7 @@ main(int argc, char *argv[])
363356
* for all commands except init, which doesn't take this parameter
364357
* and add-instance which creates new instance.
365358
*/
366-
if (backup_subcmd != INIT && backup_subcmd != ADD_INSTANCE)
359+
if (backup_subcmd != INIT_CMD && backup_subcmd != ADD_INSTANCE_CMD)
367360
{
368361
if (access(backup_instance_path, F_OK) != 0)
369362
elog(ERROR, "Instance '%s' does not exist in this backup catalog",
@@ -375,7 +368,7 @@ main(int argc, char *argv[])
375368
* Read options from env variables or from config file,
376369
* unless we're going to set them via set-config.
377370
*/
378-
if (instance_name && backup_subcmd != SET_CONFIG)
371+
if (instance_name && backup_subcmd != SET_CONFIG_CMD)
379372
{
380373
/* Read environment variables */
381374
pgut_getopt_env(options);
@@ -398,10 +391,10 @@ main(int argc, char *argv[])
398391
/* Sanity check of --backup-id option */
399392
if (backup_id_string_param != NULL)
400393
{
401-
if (backup_subcmd != RESTORE
402-
&& backup_subcmd != VALIDATE
403-
&& backup_subcmd != DELETE_SUBCMD
404-
&& backup_subcmd != SHOW)
394+
if (backup_subcmd != RESTORE_CMD
395+
&& backup_subcmd != VALIDATE_CMD
396+
&& backup_subcmd != DELETE_CMD
397+
&& backup_subcmd != SHOW_CMD)
405398
elog(ERROR, "Cannot use -i (--backup-id) option together with the '%s' command",
406399
argv[1]);
407400

@@ -429,7 +422,7 @@ main(int argc, char *argv[])
429422
pgdata_exclude_dir[i] = "pg_log";
430423
}
431424

432-
if (backup_subcmd == VALIDATE || backup_subcmd == RESTORE)
425+
if (backup_subcmd == VALIDATE_CMD || backup_subcmd == RESTORE_CMD)
433426
{
434427
/* parse all recovery target options into recovery_target_options structure */
435428
recovery_target_options = parseRecoveryTargetOptions(target_time, target_xid,
@@ -445,17 +438,17 @@ main(int argc, char *argv[])
445438
/* do actual operation */
446439
switch (backup_subcmd)
447440
{
448-
case ARCHIVE_PUSH:
441+
case ARCHIVE_PUSH_CMD:
449442
return do_archive_push(wal_file_path, wal_file_name, file_overwrite);
450-
case ARCHIVE_GET:
443+
case ARCHIVE_GET_CMD:
451444
return do_archive_get(wal_file_path, wal_file_name);
452-
case ADD_INSTANCE:
445+
case ADD_INSTANCE_CMD:
453446
return do_add_instance();
454-
case DELETE_INSTANCE:
447+
case DELETE_INSTANCE_CMD:
455448
return do_delete_instance();
456-
case INIT:
449+
case INIT_CMD:
457450
return do_init();
458-
case BACKUP:
451+
case BACKUP_CMD:
459452
{
460453
const char *backup_mode;
461454
time_t start_time;
@@ -470,20 +463,20 @@ main(int argc, char *argv[])
470463

471464
return do_backup(start_time);
472465
}
473-
case RESTORE:
466+
case RESTORE_CMD:
474467
return do_restore_or_validate(current.backup_id,
475468
recovery_target_options,
476469
true);
477-
case VALIDATE:
470+
case VALIDATE_CMD:
478471
if (current.backup_id == 0 && target_time == 0 && target_xid == 0)
479472
return do_validate_all();
480473
else
481474
return do_restore_or_validate(current.backup_id,
482475
recovery_target_options,
483476
false);
484-
case SHOW:
477+
case SHOW_CMD:
485478
return do_show(current.backup_id);
486-
case DELETE_SUBCMD:
479+
case DELETE_CMD:
487480
if (delete_expired && backup_id_string_param)
488481
elog(ERROR, "You cannot specify --delete-expired and --backup-id options together");
489482
if (!delete_expired && !delete_wal && !backup_id_string_param)
@@ -494,10 +487,13 @@ main(int argc, char *argv[])
494487
return do_retention_purge();
495488
else
496489
return do_delete(current.backup_id);
497-
case SHOW_CONFIG:
490+
case SHOW_CONFIG_CMD:
498491
return do_configure(true);
499-
case SET_CONFIG:
492+
case SET_CONFIG_CMD:
500493
return do_configure(false);
494+
case NO_CMD:
495+
/* Should not happen */
496+
elog(ERROR, "Unknown subcommand");
501497
}
502498

503499
return 0;
@@ -561,7 +557,7 @@ compress_init(void)
561557
if (compress_shortcut)
562558
compress_alg = ZLIB_COMPRESS;
563559

564-
if (backup_subcmd != SET_CONFIG)
560+
if (backup_subcmd != SET_CONFIG_CMD)
565561
{
566562
if (compress_level != DEFAULT_COMPRESS_LEVEL
567563
&& compress_alg == NOT_DEFINED_COMPRESS)
@@ -574,7 +570,7 @@ compress_init(void)
574570
if (compress_level == 0)
575571
compress_alg = NOT_DEFINED_COMPRESS;
576572

577-
if (backup_subcmd == BACKUP || backup_subcmd == ARCHIVE_PUSH)
573+
if (backup_subcmd == BACKUP_CMD || backup_subcmd == ARCHIVE_PUSH_CMD)
578574
{
579575
#ifndef HAVE_LIBZ
580576
if (compress_alg == ZLIB_COMPRESS)

0 commit comments

Comments
 (0)