Skip to content

Commit 01bc03a

Browse files
committed
Simplify scrub options parser
Signed-off-by: Mariusz Zaborski <[email protected]>
1 parent 545d662 commit 01bc03a

File tree

1 file changed

+53
-43
lines changed

1 file changed

+53
-43
lines changed

cmd/zpool/zpool_main.c

Lines changed: 53 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8446,6 +8446,11 @@ date_string_to_sec(const char *timestr, boolean_t rounding)
84468446
return (mktime(&tm) + adjustment);
84478447
}
84488448

8449+
struct zpool_scrub_option {
8450+
char name;
8451+
boolean_t enabled;
8452+
};
8453+
84498454
/*
84508455
* zpool scrub [-e | -s | -p | -C | -E | -S] [-w] [-a | <pool> ...]
84518456
*
@@ -8463,27 +8468,27 @@ zpool_do_scrub(int argc, char **argv)
84638468
{
84648469
int c;
84658470
scrub_cbdata_t cb;
8466-
boolean_t wait = B_FALSE;
84678471
int error;
84688472

84698473
cb.cb_type = POOL_SCAN_SCRUB;
84708474
cb.cb_scrub_cmd = POOL_SCRUB_NORMAL;
84718475
cb.cb_date_start = cb.cb_date_end = 0;
84728476

8473-
boolean_t is_error_scrub = B_FALSE;
8474-
boolean_t is_pause = B_FALSE;
8475-
boolean_t is_stop = B_FALSE;
8476-
boolean_t is_txg_continue = B_FALSE;
8477-
boolean_t scrub_all = B_FALSE;
8477+
struct zpool_scrub_option wait = {'w', B_FALSE};
8478+
struct zpool_scrub_option is_error_scrub = {'e', B_FALSE};
8479+
struct zpool_scrub_option is_pause = {'p', B_FALSE};
8480+
struct zpool_scrub_option is_stop = {'s', B_FALSE};
8481+
struct zpool_scrub_option is_txg_continue = {'C', B_FALSE};
8482+
struct zpool_scrub_option scrub_all = {'a', B_FALSE};
84788483

84798484
/* check options */
84808485
while ((c = getopt(argc, argv, "aspweCE:S:")) != -1) {
84818486
switch (c) {
84828487
case 'a':
8483-
scrub_all = B_TRUE;
8488+
scrub_all.enabled = B_TRUE;
84848489
break;
84858490
case 'e':
8486-
is_error_scrub = B_TRUE;
8491+
is_error_scrub.enabled = B_TRUE;
84878492
break;
84888493
case 'E':
84898494
/*
@@ -8493,19 +8498,19 @@ zpool_do_scrub(int argc, char **argv)
84938498
cb.cb_date_end = date_string_to_sec(optarg, B_TRUE);
84948499
break;
84958500
case 's':
8496-
is_stop = B_TRUE;
8501+
is_stop.enabled = B_TRUE;
84978502
break;
84988503
case 'S':
84998504
cb.cb_date_start = date_string_to_sec(optarg, B_FALSE);
85008505
break;
85018506
case 'p':
8502-
is_pause = B_TRUE;
8507+
is_pause.enabled = B_TRUE;
85038508
break;
85048509
case 'w':
8505-
wait = B_TRUE;
8510+
wait.enabled = B_TRUE;
85068511
break;
85078512
case 'C':
8508-
is_txg_continue = B_TRUE;
8513+
is_txg_continue.enabled = B_TRUE;
85098514
break;
85108515
case '?':
85118516
(void) fprintf(stderr, gettext("invalid option '%c'\n"),
@@ -8514,37 +8519,42 @@ zpool_do_scrub(int argc, char **argv)
85148519
}
85158520
}
85168521

8517-
if (is_pause && is_stop) {
8518-
(void) fprintf(stderr, gettext("invalid option "
8519-
"combination: -s and -p are mutually exclusive\n"));
8520-
usage(B_FALSE);
8521-
} else if (is_pause && is_txg_continue) {
8522-
(void) fprintf(stderr, gettext("invalid option "
8523-
"combination: -p and -C are mutually exclusive\n"));
8524-
usage(B_FALSE);
8525-
} else if (is_stop && is_txg_continue) {
8526-
(void) fprintf(stderr, gettext("invalid option "
8527-
"combination: -s and -C are mutually exclusive\n"));
8528-
usage(B_FALSE);
8529-
} else if (is_error_scrub && is_txg_continue) {
8530-
(void) fprintf(stderr, gettext("invalid option "
8531-
"combination: -e and -C are mutually exclusive\n"));
8532-
usage(B_FALSE);
8533-
} else {
8534-
if (is_error_scrub)
8535-
cb.cb_type = POOL_SCAN_ERRORSCRUB;
8536-
8537-
if (is_pause) {
8538-
cb.cb_scrub_cmd = POOL_SCRUB_PAUSE;
8539-
} else if (is_stop) {
8540-
cb.cb_type = POOL_SCAN_NONE;
8541-
} else if (is_txg_continue) {
8542-
cb.cb_scrub_cmd = POOL_SCRUB_FROM_LAST_TXG;
8543-
} else {
8544-
cb.cb_scrub_cmd = POOL_SCRUB_NORMAL;
8522+
struct {
8523+
struct zpool_scrub_option *op1;
8524+
struct zpool_scrub_option *op2;
8525+
} scrub_exclusive_options[] = {
8526+
{&is_stop, &is_pause},
8527+
{&is_stop, &is_txg_continue},
8528+
{&is_pause, &is_txg_continue},
8529+
{&is_error_scrub, &is_txg_continue},
8530+
};
8531+
8532+
for (int i = 0; i < sizeof(scrub_exclusive_options) /
8533+
sizeof(scrub_exclusive_options[0]); i++) {
8534+
if (scrub_exclusive_options[i].op1->enabled &&
8535+
scrub_exclusive_options[i].op2->enabled) {
8536+
(void) fprintf(stderr, gettext("invalid option "
8537+
"combination: -%c and -%c are mutually "
8538+
"exclusive\n"),
8539+
scrub_exclusive_options[i].op1->name,
8540+
scrub_exclusive_options[i].op2->name);
8541+
usage(B_FALSE);
85458542
}
85468543
}
85478544

8545+
if (is_error_scrub.enabled)
8546+
cb.cb_type = POOL_SCAN_ERRORSCRUB;
8547+
8548+
if (is_pause.enabled) {
8549+
cb.cb_scrub_cmd = POOL_SCRUB_PAUSE;
8550+
} else if (is_stop.enabled) {
8551+
cb.cb_type = POOL_SCAN_NONE;
8552+
} else if (is_txg_continue.enabled) {
8553+
cb.cb_scrub_cmd = POOL_SCRUB_FROM_LAST_TXG;
8554+
} else {
8555+
cb.cb_scrub_cmd = POOL_SCRUB_NORMAL;
8556+
}
8557+
85488558
if ((cb.cb_date_start != 0 || cb.cb_date_end != 0) &&
85498559
cb.cb_scrub_cmd != POOL_SCRUB_NORMAL) {
85508560
(void) fprintf(stderr, gettext("invalid option combination: "
@@ -8558,7 +8568,7 @@ zpool_do_scrub(int argc, char **argv)
85588568
usage(B_FALSE);
85598569
}
85608570

8561-
if (wait && (cb.cb_type == POOL_SCAN_NONE ||
8571+
if (wait.enabled && (cb.cb_type == POOL_SCAN_NONE ||
85628572
cb.cb_scrub_cmd == POOL_SCRUB_PAUSE)) {
85638573
(void) fprintf(stderr, gettext("invalid option combination: "
85648574
"-w cannot be used with -p or -s\n"));
@@ -8568,15 +8578,15 @@ zpool_do_scrub(int argc, char **argv)
85688578
argc -= optind;
85698579
argv += optind;
85708580

8571-
if (argc < 1 && !scrub_all) {
8581+
if (argc < 1 && !scrub_all.enabled) {
85728582
(void) fprintf(stderr, gettext("missing pool name argument\n"));
85738583
usage(B_FALSE);
85748584
}
85758585

85768586
error = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL,
85778587
B_FALSE, scrub_callback, &cb);
85788588

8579-
if (wait && !error) {
8589+
if (wait.enabled && !error) {
85808590
zpool_wait_activity_t act = ZPOOL_WAIT_SCRUB;
85818591
error = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL,
85828592
B_FALSE, wait_callback, &act);

0 commit comments

Comments
 (0)