zfs: get: only accept whole column for -o, not col[=whatever]

Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Ahelenia Ziemiańska <nabijaczleweli@nabijaczleweli.xyz>
Closes #12996
This commit is contained in:
наб 2022-01-22 23:24:14 +01:00 committed by Brian Behlendorf
parent c79787845d
commit 7c17e82cbe
1 changed files with 26 additions and 34 deletions

View File

@ -2038,11 +2038,17 @@ zfs_do_get(int argc, char **argv)
* the structure to give us a blank slate. * the structure to give us a blank slate.
*/ */
memset(&cb.cb_columns, 0, sizeof (cb.cb_columns)); memset(&cb.cb_columns, 0, sizeof (cb.cb_columns));
i = 0; i = 0;
while (*optarg != '\0') { for (char *tok; (tok = strsep(&optarg, ",")); ) {
static char *col_subopts[] = static const char *const col_subopts[] =
{ "name", "property", "value", "received", { "name", "property", "value",
"source", "all", NULL }; "received", "source", "all" };
static const zfs_get_column_t col_subopt_col[] =
{ GET_COL_NAME, GET_COL_PROPERTY, GET_COL_VALUE,
GET_COL_RECVD, GET_COL_SOURCE };
static const int col_subopt_flags[] =
{ 0, 0, 0, ZFS_ITER_RECVD_PROPS, 0 };
if (i == ZFS_GET_NCOLS) { if (i == ZFS_GET_NCOLS) {
(void) fprintf(stderr, gettext("too " (void) fprintf(stderr, gettext("too "
@ -2051,25 +2057,16 @@ zfs_do_get(int argc, char **argv)
usage(B_FALSE); usage(B_FALSE);
} }
switch (getsubopt(&optarg, col_subopts, for (c = 0; c < ARRAY_SIZE(col_subopts); ++c)
&value)) { if (strcmp(tok, col_subopts[c]) == 0)
case 0: goto found;
cb.cb_columns[i++] = GET_COL_NAME;
break; (void) fprintf(stderr,
case 1: gettext("invalid column name '%s'\n"), tok);
cb.cb_columns[i++] = GET_COL_PROPERTY; usage(B_FALSE);
break;
case 2: found:
cb.cb_columns[i++] = GET_COL_VALUE; if (c >= 5) {
break;
case 3:
cb.cb_columns[i++] = GET_COL_RECVD;
flags |= ZFS_ITER_RECVD_PROPS;
break;
case 4:
cb.cb_columns[i++] = GET_COL_SOURCE;
break;
case 5:
if (i > 0) { if (i > 0) {
(void) fprintf(stderr, (void) fprintf(stderr,
gettext("\"all\" conflicts " gettext("\"all\" conflicts "
@ -2077,19 +2074,14 @@ zfs_do_get(int argc, char **argv)
"given to -o option\n")); "given to -o option\n"));
usage(B_FALSE); usage(B_FALSE);
} }
cb.cb_columns[0] = GET_COL_NAME;
cb.cb_columns[1] = GET_COL_PROPERTY; memcpy(cb.cb_columns, col_subopt_col,
cb.cb_columns[2] = GET_COL_VALUE; sizeof (col_subopt_col));
cb.cb_columns[3] = GET_COL_RECVD;
cb.cb_columns[4] = GET_COL_SOURCE;
flags |= ZFS_ITER_RECVD_PROPS; flags |= ZFS_ITER_RECVD_PROPS;
i = ZFS_GET_NCOLS; i = ZFS_GET_NCOLS;
break; } else {
default: cb.cb_columns[i++] = col_subopt_col[c];
(void) fprintf(stderr, flags |= col_subopt_flags[c];
gettext("invalid column name "
"'%s'\n"), value);
usage(B_FALSE);
} }
} }
break; break;