mount: use string_to_bitmask()

Signed-off-by: Vaclav Dolezal <vdolezal@redhat.com>
This commit is contained in:
Vaclav Dolezal 2018-02-14 14:03:41 +01:00
parent 50ed551992
commit 9730aa40cf
1 changed files with 32 additions and 29 deletions

View File

@ -475,25 +475,39 @@ struct flag_str {
char *str; char *str;
}; };
static int flag_from_string(const struct flag_str *flags, const char *str) static int omode2mask(const char *str)
{ {
for (; flags->str; flags++) { size_t i;
if (!strcmp(str, flags->str))
return flags->value; static const struct flag_str flags[] = {
{ MNT_OMODE_IGNORE, "ignore" },
{ MNT_OMODE_APPEND, "append" },
{ MNT_OMODE_PREPEND, "prepend" },
{ MNT_OMODE_REPLACE, "replace" },
};
for (i = 0; i < ARRAY_SIZE(flags); i++) {
if (!strcmp(str, flags[i].str))
return flags[i].value;
} }
return -EINVAL; return -EINVAL;
} }
static int flags_from_string(const struct flag_str *flags, char *str) static long osrc2mask(const char *str, size_t len)
{ {
int ret = 0, tmp; size_t i;
for (str = strtok(str, ","); str; str = strtok(NULL, ",")) {
tmp = flag_from_string(flags, str); static const struct flag_str flags[] = {
if (tmp < 0) { MNT_OMODE_FSTAB, "fstab" },
return tmp; { MNT_OMODE_MTAB, "mtab" },
ret |= tmp; { MNT_OMODE_NOTAB, "disable" },
};
for (i = 0; i < ARRAY_SIZE(flags); i++) {
if (!strncmp(str, flags[i].str, len) && !flags[i].str[len])
return flags[i].value;
} }
return ret; return -EINVAL;
} }
int main(int argc, char **argv) int main(int argc, char **argv)
@ -570,20 +584,6 @@ int main(int argc, char **argv)
}; };
int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT; int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
static const struct flag_str opt_optmodes[] = {
{ MNT_OMODE_IGNORE, "ignore" },
{ MNT_OMODE_APPEND, "append" },
{ MNT_OMODE_PREPEND, "prepend" },
{ MNT_OMODE_REPLACE, "replace" },
{ 0, NULL }
};
static const struct flag_str opt_optsources[] = {
{ MNT_OMODE_FSTAB, "fstab" },
{ MNT_OMODE_MTAB, "mtab" },
{ MNT_OMODE_NOTAB, "disable" },
{ 0, NULL }
};
sanitize_env(); sanitize_env();
setlocale(LC_ALL, ""); setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR); bindtextdomain(PACKAGE, LOCALEDIR);
@ -728,19 +728,22 @@ int main(int argc, char **argv)
mnt_context_set_source(cxt, optarg); mnt_context_set_source(cxt, optarg);
break; break;
case MOUNT_OPT_OPTMODE: case MOUNT_OPT_OPTMODE:
optmode_mode = flag_from_string(opt_optmodes, optarg); optmode_mode = omode2mask(optarg);
if (optmode_mode == -EINVAL) { if (optmode_mode == -EINVAL) {
warnx(_("bad usage")); warnx(_("bad usage"));
errtryhelp(MNT_EX_USAGE); errtryhelp(MNT_EX_USAGE);
} }
break; break;
case MOUNT_OPT_OPTSRC: case MOUNT_OPT_OPTSRC:
optmode_src = flags_from_string(opt_optsources, optarg); {
if (optmode_src == -EINVAL) { unsigned long tmp = 0;
if (string_to_bitmask(optarg, &tmp, osrc2mask)) {
warnx(_("bad usage")); warnx(_("bad usage"));
errtryhelp(MNT_EX_USAGE); errtryhelp(MNT_EX_USAGE);
} }
optmode_src = tmp;
break; break;
}
case MOUNT_OPT_OPTSRC_FORCE: case MOUNT_OPT_OPTSRC_FORCE:
optmode |= MNT_OMODE_FORCE; optmode |= MNT_OMODE_FORCE;
break; break;