From f78e6385dc4cee0a1f399c4c89ebf823c108d447 Mon Sep 17 00:00:00 2001 From: Zbigniew Jędrzejewski-Szmek Date: Thu, 26 Dec 2013 15:30:22 -0500 Subject: Use enums to make it obvious what boolean params mean Suggested-by: Russ Allbery --- src/shared/cgroup-util.c | 6 +++--- src/shared/install.c | 18 +++++++++--------- src/shared/unit-name.c | 14 ++++++++------ src/shared/unit-name.h | 16 +++++++++++++--- 4 files changed, 33 insertions(+), 21 deletions(-) (limited to 'src/shared') diff --git a/src/shared/cgroup-util.c b/src/shared/cgroup-util.c index 27eee8eacd..f2af8dcfda 100644 --- a/src/shared/cgroup-util.c +++ b/src/shared/cgroup-util.c @@ -1152,7 +1152,7 @@ int cg_path_decode_unit(const char *cgroup, char **unit){ c = strndupa(cgroup, e - cgroup); c = cg_unescape(c); - if (!unit_name_is_valid(c, false)) + if (!unit_name_is_valid(c, TEMPLATE_INVALID)) return -EINVAL; s = strdup(c); @@ -1566,7 +1566,7 @@ int cg_slice_to_path(const char *unit, char **ret) { assert(unit); assert(ret); - if (!unit_name_is_valid(unit, false)) + if (!unit_name_is_valid(unit, TEMPLATE_INVALID)) return -EINVAL; if (!endswith(unit, ".slice")) @@ -1583,7 +1583,7 @@ int cg_slice_to_path(const char *unit, char **ret) { strcpy(stpncpy(n, p, dash - p), ".slice"); - if (!unit_name_is_valid(n, false)) + if (!unit_name_is_valid(n, TEMPLATE_INVALID)) return -EINVAL; escaped = cg_escape(n); diff --git a/src/shared/install.c b/src/shared/install.c index 233d64b6e9..62151d99d0 100644 --- a/src/shared/install.c +++ b/src/shared/install.c @@ -575,7 +575,7 @@ int unit_file_mask( STRV_FOREACH(i, files) { _cleanup_free_ char *path = NULL; - if (!unit_name_is_valid(*i, true)) { + if (!unit_name_is_valid(*i, TEMPLATE_VALID)) { if (r == 0) r = -EINVAL; continue; @@ -643,7 +643,7 @@ int unit_file_unmask( STRV_FOREACH(i, files) { char *path; - if (!unit_name_is_valid(*i, true)) { + if (!unit_name_is_valid(*i, TEMPLATE_VALID)) { if (r == 0) r = -EINVAL; continue; @@ -719,7 +719,7 @@ int unit_file_link( fn = basename(*i); if (!path_is_absolute(*i) || - !unit_name_is_valid(fn, true)) { + !unit_name_is_valid(fn, TEMPLATE_VALID)) { if (r == 0) r = -EINVAL; continue; @@ -861,7 +861,7 @@ static int install_info_add( if (!name) name = basename(path); - if (!unit_name_is_valid(name, true)) + if (!unit_name_is_valid(name, TEMPLATE_VALID)) return -EINVAL; if (hashmap_get(c->have_installed, name) || @@ -1235,7 +1235,7 @@ static int install_info_symlink_wants( if (q < 0) return q; - if (!unit_name_is_valid(dst, true)) { + if (!unit_name_is_valid(dst, TEMPLATE_VALID)) { r = -EINVAL; continue; } @@ -1272,7 +1272,7 @@ static int install_info_symlink_requires( if (q < 0) return q; - if (!unit_name_is_valid(dst, true)) { + if (!unit_name_is_valid(dst, TEMPLATE_VALID)) { r = -EINVAL; continue; } @@ -1675,7 +1675,7 @@ UnitFileState unit_file_get_state( if (root_dir && scope != UNIT_FILE_SYSTEM) return -EINVAL; - if (!unit_name_is_valid(name, true)) + if (!unit_name_is_valid(name, TEMPLATE_VALID)) return -EINVAL; r = lookup_paths_init_from_scope(&paths, scope); @@ -1845,7 +1845,7 @@ int unit_file_preset( STRV_FOREACH(i, files) { - if (!unit_name_is_valid(*i, true)) + if (!unit_name_is_valid(*i, TEMPLATE_VALID)) return -EINVAL; r = unit_file_query_preset(scope, *i); @@ -1949,7 +1949,7 @@ int unit_file_get_list( if (ignore_file(de->d_name)) continue; - if (!unit_name_is_valid(de->d_name, true)) + if (!unit_name_is_valid(de->d_name, TEMPLATE_VALID)) continue; if (hashmap_get(h, de->d_name)) diff --git a/src/shared/unit-name.c b/src/shared/unit-name.c index 832b926813..408323b8fb 100644 --- a/src/shared/unit-name.c +++ b/src/shared/unit-name.c @@ -62,7 +62,7 @@ static const char* const unit_load_state_table[_UNIT_LOAD_STATE_MAX] = { DEFINE_STRING_TABLE_LOOKUP(unit_load_state, UnitLoadState); -bool unit_name_is_valid(const char *n, bool template_ok) { +bool unit_name_is_valid(const char *n, enum template_valid template_ok) { const char *e, *i, *at; /* Valid formats: @@ -72,6 +72,7 @@ bool unit_name_is_valid(const char *n, bool template_ok) { */ assert(n); + assert(IN_SET(template_ok, TEMPLATE_VALID, TEMPLATE_INVALID)); if (strlen(n) >= UNIT_NAME_MAX) return false; @@ -96,7 +97,7 @@ bool unit_name_is_valid(const char *n, bool template_ok) { if (at == n) return false; - if (!template_ok && at+1 == e) + if (!template_ok == TEMPLATE_VALID && at+1 == e) return false; } @@ -186,7 +187,7 @@ char *unit_name_change_suffix(const char *n, const char *suffix) { size_t a, b; assert(n); - assert(unit_name_is_valid(n, true)); + assert(unit_name_is_valid(n, TEMPLATE_VALID)); assert(suffix); assert(suffix[0] == '.'); @@ -486,12 +487,13 @@ int unit_name_from_dbus_path(const char *path, char **name) { * Try to turn a string that might not be a unit name into a * sensible unit name. */ -char *unit_name_mangle(const char *name, bool allow_globs) { +char *unit_name_mangle(const char *name, enum unit_name_mangle allow_globs) { char *r, *t; const char *f; - const char* valid_chars = allow_globs ? "@" VALID_CHARS "[]!-*?" : "@" VALID_CHARS; + const char* valid_chars = allow_globs == MANGLE_GLOB ? "@" VALID_CHARS "[]!-*?" : "@" VALID_CHARS; assert(name); + assert(IN_SET(allow_globs, MANGLE_GLOB, MANGLE_NOGLOB)); if (is_device_path(name)) return unit_name_from_path(name, ".device"); @@ -528,7 +530,7 @@ char *unit_name_mangle(const char *name, bool allow_globs) { * Similar to unit_name_mangle(), but is called when we know * that this is about a specific unit type. */ -char *unit_name_mangle_with_suffix(const char *name, bool allow_globs, const char *suffix) { +char *unit_name_mangle_with_suffix(const char *name, enum unit_name_mangle allow_globs, const char *suffix) { char *r, *t; const char *f; diff --git a/src/shared/unit-name.h b/src/shared/unit-name.h index 362ff0c00c..d06d2b2353 100644 --- a/src/shared/unit-name.h +++ b/src/shared/unit-name.h @@ -69,7 +69,12 @@ int unit_name_to_instance(const char *n, char **instance); char* unit_name_to_prefix(const char *n); char* unit_name_to_prefix_and_instance(const char *n); -bool unit_name_is_valid(const char *n, bool template_ok) _pure_; +enum template_valid { + TEMPLATE_INVALID, + TEMPLATE_VALID, +}; + +bool unit_name_is_valid(const char *n, enum template_valid template_ok) _pure_; bool unit_prefix_is_valid(const char *p) _pure_; bool unit_instance_is_valid(const char *i) _pure_; @@ -98,7 +103,12 @@ char *unit_name_to_path(const char *name); char *unit_dbus_path_from_name(const char *name); int unit_name_from_dbus_path(const char *path, char **name); -char *unit_name_mangle(const char *name, bool allow_globs); -char *unit_name_mangle_with_suffix(const char *name, bool allow_globs, const char *suffix); +enum unit_name_mangle { + MANGLE_NOGLOB, + MANGLE_GLOB, +}; + +char *unit_name_mangle(const char *name, enum unit_name_mangle allow_globs); +char *unit_name_mangle_with_suffix(const char *name, enum unit_name_mangle allow_globs, const char *suffix); int build_subslice(const char *slice, const char*name, char **subslice); -- cgit v1.2.3-54-g00ecf