tree-wide: extend verbs functions with extra per-verb data parameter
We often have a pattern where the same verb function is used for multiple actions. This leads to an antipattern where we figure out what action needs to be taken from argv[0] multiple times: often once in parse_argv() to figure out what options are allowed, then once again implicitly in dispatch_verb(), and then again in the action verb itself. Let's allow passing a parameter into the verb to simplify this. This matches a pattern we have in conf-parser.h, where we have both void *userdata (more global) and void *data (per-config item). Here, I opted for uintptr_t userdata. It seems that most of the time we'll want to just pass an enum value. This works OK with no casts. I also tried a void* and union. In both cases, much more boilerplate is needed: either a cast or a macro to help avoid compiler warnings. uintptr_t seems generic enough to cover foreseeable usecases with no fuss. This is a noop refactoring. See next commit for an example.
This commit is contained in:
committed by
Luca Boccassi
parent
9e8babc8ab
commit
31ddd87da0
@@ -2586,7 +2586,7 @@ static int discover_key(const char *key_file, const char *volume, TokenType toke
|
||||
return r;
|
||||
}
|
||||
|
||||
static int verb_attach(int argc, char *argv[], void *userdata) {
|
||||
static int verb_attach(int argc, char *argv[], uintptr_t _data, void *userdata) {
|
||||
_cleanup_(crypt_freep) struct crypt_device *cd = NULL;
|
||||
_unused_ _cleanup_(remove_and_erasep) const char *destroy_key_file = NULL;
|
||||
crypt_status_info status;
|
||||
@@ -2826,7 +2826,7 @@ static int verb_attach(int argc, char *argv[], void *userdata) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int verb_detach(int argc, char *argv[], void *userdata) {
|
||||
static int verb_detach(int argc, char *argv[], uintptr_t _data, void *userdata) {
|
||||
_cleanup_(crypt_freep) struct crypt_device *cd = NULL;
|
||||
const char *volume = ASSERT_PTR(argv[1]);
|
||||
int r;
|
||||
|
||||
Reference in New Issue
Block a user