mirror of
https://gitlab.com/cryptsetup/cryptsetup.git
synced 2025-12-18 14:20:09 +01:00
Introduce new function to parse command line arguments.
To be used later in all tools.
This commit is contained in:
committed by
Milan Broz
parent
c4b7bf8635
commit
aebe14a6e9
@@ -6,6 +6,7 @@ cryptsetup_SOURCES = \
|
|||||||
lib/utils_loop.c \
|
lib/utils_loop.c \
|
||||||
lib/utils_io.c \
|
lib/utils_io.c \
|
||||||
lib/utils_blkid.c \
|
lib/utils_blkid.c \
|
||||||
|
src/utils_args.c \
|
||||||
src/utils_tools.c \
|
src/utils_tools.c \
|
||||||
src/utils_password.c \
|
src/utils_password.c \
|
||||||
src/utils_luks2.c \
|
src/utils_luks2.c \
|
||||||
|
|||||||
@@ -24,6 +24,7 @@
|
|||||||
#ifndef CRYPTSETUP_H
|
#ifndef CRYPTSETUP_H
|
||||||
#define CRYPTSETUP_H
|
#define CRYPTSETUP_H
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
@@ -55,6 +56,11 @@
|
|||||||
#define ROUND_SECTOR(x) (((x) + SECTOR_SIZE - 1) / SECTOR_SIZE)
|
#define ROUND_SECTOR(x) (((x) + SECTOR_SIZE - 1) / SECTOR_SIZE)
|
||||||
|
|
||||||
#define DEFAULT_WIPE_BLOCK 1048576 /* 1 MiB */
|
#define DEFAULT_WIPE_BLOCK 1048576 /* 1 MiB */
|
||||||
|
#define MAX_ACTIONS 16
|
||||||
|
|
||||||
|
#ifndef ARRAY_SIZE
|
||||||
|
# define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
|
||||||
|
#endif
|
||||||
|
|
||||||
extern int opt_debug;
|
extern int opt_debug;
|
||||||
extern int opt_debug_json;
|
extern int opt_debug_json;
|
||||||
@@ -127,4 +133,33 @@ void tools_cleanup(void);
|
|||||||
#define log_verbose(x...) clogger(NULL, CRYPT_LOG_VERBOSE, __FILE__, __LINE__, x)
|
#define log_verbose(x...) clogger(NULL, CRYPT_LOG_VERBOSE, __FILE__, __LINE__, x)
|
||||||
#define log_err(x...) clogger(NULL, CRYPT_LOG_ERROR, __FILE__, __LINE__, x)
|
#define log_err(x...) clogger(NULL, CRYPT_LOG_ERROR, __FILE__, __LINE__, x)
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
CRYPT_ARG_BOOL = 0,
|
||||||
|
CRYPT_ARG_STRING,
|
||||||
|
CRYPT_ARG_INT32,
|
||||||
|
CRYPT_ARG_UINT32,
|
||||||
|
CRYPT_ARG_INT64,
|
||||||
|
CRYPT_ARG_UINT64
|
||||||
|
} crypt_arg_type_info;
|
||||||
|
|
||||||
|
struct tools_arg {
|
||||||
|
const char *name;
|
||||||
|
bool set;
|
||||||
|
crypt_arg_type_info type;
|
||||||
|
union {
|
||||||
|
char *str_value;
|
||||||
|
uint64_t u64_value;
|
||||||
|
uint32_t u32_value;
|
||||||
|
int32_t i32_value;
|
||||||
|
int64_t i64_value;
|
||||||
|
} u;
|
||||||
|
const char *actions_array[MAX_ACTIONS];
|
||||||
|
};
|
||||||
|
|
||||||
|
void tools_parse_arg_value(poptContext popt_context, crypt_arg_type_info type, struct tools_arg *arg, const char *popt_arg, int popt_val, bool(*needs_size_conv_fn)(unsigned arg_id));
|
||||||
|
|
||||||
|
void tools_args_free(struct tools_arg *args, size_t args_count);
|
||||||
|
|
||||||
|
void tools_check_args(const char *action, const struct tools_arg *args, size_t args_size, poptContext popt_context);
|
||||||
|
|
||||||
#endif /* CRYPTSETUP_H */
|
#endif /* CRYPTSETUP_H */
|
||||||
|
|||||||
127
src/utils_args.c
Normal file
127
src/utils_args.c
Normal file
@@ -0,0 +1,127 @@
|
|||||||
|
/*
|
||||||
|
* Cryptsetup command line arguments list
|
||||||
|
*
|
||||||
|
* Copyright (C) 2020 Red Hat, Inc. All rights reserved.
|
||||||
|
* Copyright (C) 2020 Ondrej Kozina
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU General Public License
|
||||||
|
* as published by the Free Software Foundation; either version 2
|
||||||
|
* of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "cryptsetup.h"
|
||||||
|
|
||||||
|
void tools_parse_arg_value(poptContext popt_context, crypt_arg_type_info type, struct tools_arg *arg, const char *popt_arg, int popt_val, bool(*needs_size_conv_fn)(unsigned arg_id))
|
||||||
|
{
|
||||||
|
char *end, msg[128];
|
||||||
|
long int l;
|
||||||
|
long long int ll;
|
||||||
|
long unsigned int ul;
|
||||||
|
long long unsigned int ull;
|
||||||
|
|
||||||
|
errno = 0;
|
||||||
|
|
||||||
|
switch (type) {
|
||||||
|
case CRYPT_ARG_BOOL:
|
||||||
|
break;
|
||||||
|
case CRYPT_ARG_STRING:
|
||||||
|
if (arg->set)
|
||||||
|
free(arg->u.str_value);
|
||||||
|
arg->u.str_value = poptGetOptArg(popt_context);
|
||||||
|
break;
|
||||||
|
case CRYPT_ARG_INT32:
|
||||||
|
l = strtol(popt_arg, &end, 10);
|
||||||
|
if (*end || !*popt_arg || l > INT32_MAX || l < INT32_MIN || errno == ERANGE)
|
||||||
|
usage(popt_context, EXIT_FAILURE, poptStrerror(POPT_ERROR_BADNUMBER),
|
||||||
|
poptGetInvocationName(popt_context));
|
||||||
|
arg->u.i32_value = l;
|
||||||
|
break;
|
||||||
|
case CRYPT_ARG_UINT32:
|
||||||
|
ul = strtoul(popt_arg, &end, 10);
|
||||||
|
if (*end || !*popt_arg || ul > UINT32_MAX || errno == ERANGE)
|
||||||
|
usage(popt_context, EXIT_FAILURE, poptStrerror(POPT_ERROR_BADNUMBER),
|
||||||
|
poptGetInvocationName(popt_context));
|
||||||
|
arg->u.u32_value = ul;
|
||||||
|
break;
|
||||||
|
case CRYPT_ARG_INT64:
|
||||||
|
ll = strtoll(popt_arg, &end, 10);
|
||||||
|
if (*end || !*popt_arg || errno == ERANGE)
|
||||||
|
usage(popt_context, EXIT_FAILURE, poptStrerror(POPT_ERROR_BADNUMBER),
|
||||||
|
poptGetInvocationName(popt_context));
|
||||||
|
arg->u.i64_value = ll;
|
||||||
|
break;
|
||||||
|
case CRYPT_ARG_UINT64:
|
||||||
|
/* special size strings with units converted to integers */
|
||||||
|
if (needs_size_conv_fn && needs_size_conv_fn(popt_val)) {
|
||||||
|
if (tools_string_to_size(NULL, popt_arg, &arg->u.u64_value)) {
|
||||||
|
snprintf(msg, sizeof(msg), _("Invalid size specification in parameter --%s."), arg->name);
|
||||||
|
usage(popt_context, EXIT_FAILURE, msg,
|
||||||
|
poptGetInvocationName(popt_context));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ull = strtoull(popt_arg, &end, 10);
|
||||||
|
if (*end || !*popt_arg || errno == ERANGE)
|
||||||
|
usage(popt_context, EXIT_FAILURE, poptStrerror(POPT_ERROR_BADNUMBER),
|
||||||
|
poptGetInvocationName(popt_context));
|
||||||
|
arg->u.u64_value = ull;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
/* this signals internal tools coding mistake */
|
||||||
|
abort();
|
||||||
|
}
|
||||||
|
|
||||||
|
arg->set = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void tools_args_free(struct tools_arg *args, size_t args_size)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
for (i = 0; i < args_size; i++) {
|
||||||
|
if (args[i].set && args[i].type == CRYPT_ARG_STRING)
|
||||||
|
free(args[i].u.str_value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool action_allowed(const char *action, const char * const* list, size_t list_size)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
if (!list[0])
|
||||||
|
return true;
|
||||||
|
|
||||||
|
for (i = 0; i < list_size && list[i]; i++) {
|
||||||
|
if (!strcmp(action, list[i]))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void tools_check_args(const char *action, const struct tools_arg *args, size_t args_size, poptContext popt_context)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
char msg[256];
|
||||||
|
|
||||||
|
for (i = 1; i < args_size; i++) {
|
||||||
|
if (args[i].set) {
|
||||||
|
if (action_allowed(action, args[i].actions_array, MAX_ACTIONS)) {
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
(void)snprintf(msg, sizeof(msg), _("Option --%s is not allowed with %s action."), args[i].name, action);
|
||||||
|
usage(popt_context, EXIT_FAILURE, msg, poptGetInvocationName(popt_context));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user