1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-23 21:35:11 +03:00

exec-util: modernize exec_command_flags_{to,from}_strv

- Rename ret params following our coding style
- Use assertion where appropriate
- Use BIT_FOREACH()
This commit is contained in:
Mike Yuan 2024-07-10 22:23:37 +02:00
parent 09a8a0d023
commit 05c754bc7f
No known key found for this signature in database
GPG Key ID: 417471C0A40F58B3
3 changed files with 32 additions and 43 deletions

View File

@ -8,6 +8,7 @@
#include <stdio.h> #include <stdio.h>
#include "alloc-util.h" #include "alloc-util.h"
#include "bitfield.h"
#include "conf-files.h" #include "conf-files.h"
#include "env-file.h" #include "env-file.h"
#include "env-util.h" #include "env-util.h"
@ -425,46 +426,42 @@ static int gather_environment_consume(int fd, void *arg) {
return r; return r;
} }
int exec_command_flags_from_strv(char **ex_opts, ExecCommandFlags *flags) { int exec_command_flags_from_strv(char * const *ex_opts, ExecCommandFlags *ret) {
ExecCommandFlags ex_flag, ret_flags = 0; ExecCommandFlags flags = 0;
assert(flags); assert(ret);
STRV_FOREACH(opt, ex_opts) { STRV_FOREACH(opt, ex_opts) {
ex_flag = exec_command_flags_from_string(*opt); ExecCommandFlags fl = exec_command_flags_from_string(*opt);
if (ex_flag < 0) if (fl < 0)
return ex_flag; return fl;
ret_flags |= ex_flag;
flags |= fl;
} }
*flags = ret_flags; *ret = flags;
return 0; return 0;
} }
int exec_command_flags_to_strv(ExecCommandFlags flags, char ***ex_opts) { int exec_command_flags_to_strv(ExecCommandFlags flags, char ***ret) {
_cleanup_strv_free_ char **ret_opts = NULL; _cleanup_strv_free_ char **opts = NULL;
ExecCommandFlags it = flags;
const char *str;
int r; int r;
assert(ex_opts); assert(flags >= 0);
assert(ret);
if (flags < 0) BIT_FOREACH(i, flags) {
return flags; const char *s = exec_command_flags_to_string(1 << i);
if (!s)
return -EINVAL;
for (unsigned i = 0; it != 0; it &= ~(1 << i), i++) r = strv_extend(&opts, s);
if (FLAGS_SET(flags, (1 << i))) { if (r < 0)
str = exec_command_flags_to_string(1 << i); return r;
if (!str) }
return -EINVAL;
r = strv_extend(&ret_opts, str); *ret = TAKE_PTR(opts);
if (r < 0)
return r;
}
*ex_opts = TAKE_PTR(ret_opts);
return 0; return 0;
} }

View File

@ -51,8 +51,8 @@ int execute_directories(
char *envp[], char *envp[],
ExecDirFlags flags); ExecDirFlags flags);
int exec_command_flags_from_strv(char **ex_opts, ExecCommandFlags *flags); int exec_command_flags_from_strv(char * const *ex_opts, ExecCommandFlags *ret);
int exec_command_flags_to_strv(ExecCommandFlags flags, char ***ex_opts); int exec_command_flags_to_strv(ExecCommandFlags flags, char ***ret);
extern const gather_stdout_callback_t gather_environment[_STDOUT_CONSUME_MAX]; extern const gather_stdout_callback_t gather_environment[_STDOUT_CONSUME_MAX];

View File

@ -429,27 +429,19 @@ TEST(exec_command_flags_from_strv) {
} }
TEST(exec_command_flags_to_strv) { TEST(exec_command_flags_to_strv) {
_cleanup_strv_free_ char **opts = NULL, **empty_opts = NULL, **invalid_opts = NULL; _cleanup_strv_free_ char **opts = NULL;
ExecCommandFlags flags = 0;
int r;
flags |= (EXEC_COMMAND_AMBIENT_MAGIC|EXEC_COMMAND_NO_ENV_EXPAND|EXEC_COMMAND_IGNORE_FAILURE); ASSERT_OK(exec_command_flags_to_strv(EXEC_COMMAND_AMBIENT_MAGIC|EXEC_COMMAND_NO_ENV_EXPAND|EXEC_COMMAND_IGNORE_FAILURE, &opts));
r = exec_command_flags_to_strv(flags, &opts);
assert_se(r == 0);
assert_se(strv_equal(opts, STRV_MAKE("ignore-failure", "ambient", "no-env-expand"))); assert_se(strv_equal(opts, STRV_MAKE("ignore-failure", "ambient", "no-env-expand")));
r = exec_command_flags_to_strv(0, &empty_opts); opts = strv_free(opts);
assert_se(r == 0); ASSERT_OK(exec_command_flags_to_strv(0, &opts));
assert_se(strv_equal(empty_opts, STRV_MAKE_EMPTY)); assert_se(strv_isempty(opts));
flags = _EXEC_COMMAND_FLAGS_INVALID; opts = strv_free(opts);
r = exec_command_flags_to_strv(flags, &invalid_opts); ASSERT_ERROR(exec_command_flags_to_strv(1U << 20, &opts), EINVAL);
assert_se(r == -EINVAL);
} }
DEFINE_TEST_MAIN(LOG_DEBUG); DEFINE_TEST_MAIN(LOG_DEBUG);