chiark / gitweb /
Use enums to make it obvious what boolean params mean
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 26 Dec 2013 20:30:22 +0000 (15:30 -0500)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 26 Dec 2013 20:49:54 +0000 (15:49 -0500)
Suggested-by: Russ Allbery <rra@debian.org>
15 files changed:
src/core/dbus-unit.c
src/core/device.c
src/core/load-fragment.c
src/core/manager.c
src/core/snapshot.c
src/core/unit.c
src/dbus1-generator/dbus1-generator.c
src/journal/journalctl.c
src/run/run.c
src/shared/cgroup-util.c
src/shared/install.c
src/shared/unit-name.c
src/shared/unit-name.h
src/systemctl/systemctl.c
src/test/test-unit-name.c

index 4d3e3cc9ec643e8a1c4b4e262f929e88398d99c2..37c1e8cbf822b6542a06e945313c39ad1d8d1c29 100644 (file)
@@ -813,7 +813,7 @@ static int bus_unit_set_transient_property(
                 if (r < 0)
                         return r;
 
-                if (!unit_name_is_valid(s, false) || !endswith(s, ".slice"))
+                if (!unit_name_is_valid(s, TEMPLATE_INVALID) || !endswith(s, ".slice"))
                         return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid slice name %s", s);
 
                 if (isempty(s)) {
@@ -865,7 +865,7 @@ static int bus_unit_set_transient_property(
                         return r;
 
                 while ((r = sd_bus_message_read(message, "s", &other)) > 0) {
-                        if (!unit_name_is_valid(other, false))
+                        if (!unit_name_is_valid(other, TEMPLATE_INVALID))
                                 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid unit name %s", other);
 
                         if (mode != UNIT_CHECK) {
index d3976c9d47314ebf475d0e7c10a4395252ed9ad1..c7bc1e25e7bd5225a9ce089f79fb3244baf19be1 100644 (file)
@@ -268,7 +268,7 @@ static int device_update_unit(Manager *m, struct udev_device *dev, const char *p
                                 memcpy(e, w, l);
                                 e[l] = 0;
 
-                                n = unit_name_mangle(e, false);
+                                n = unit_name_mangle(e, MANGLE_NOGLOB);
                                 if (!n) {
                                         r = -ENOMEM;
                                         goto fail;
index 183c43d58fff73176960ae4f7f3f0199a9ea83f3..7a2d32ddbd6c3a80c2f4acb9bdaceee85354361c 100644 (file)
@@ -2433,7 +2433,7 @@ static int open_follow(char **filename, FILE **_f, Set *names, char **_final) {
                  * unit name. */
                 name = basename(*filename);
 
-                if (unit_name_is_valid(name, true)) {
+                if (unit_name_is_valid(name, TEMPLATE_VALID)) {
 
                         id = set_get(names, name);
                         if (!id) {
index 6b33c1828341562b8848ec03157d0b356a32f49b..f69ae079df6f0e432dc2a0d4f248974f3eb35e41 100644 (file)
@@ -1115,7 +1115,7 @@ int manager_load_unit_prepare(
 
         t = unit_name_to_type(name);
 
-        if (t == _UNIT_TYPE_INVALID || !unit_name_is_valid(name, false))
+        if (t == _UNIT_TYPE_INVALID || !unit_name_is_valid(name, TEMPLATE_INVALID))
                 return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS, "Unit name %s is not valid.", name);
 
         ret = manager_get_unit(m, name);
index 21e89ac9964a7f07ed7caee2c962b3563909f928..d914af20e7c0651f1301fa8be1d7874c249c1119 100644 (file)
@@ -200,7 +200,7 @@ int snapshot_create(Manager *m, const char *name, bool cleanup, sd_bus_error *e,
         assert(_s);
 
         if (name) {
-                if (!unit_name_is_valid(name, false))
+                if (!unit_name_is_valid(name, TEMPLATE_INVALID))
                         return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS, "Unit name %s is not valid.", name);
 
                 if (unit_name_to_type(name) != UNIT_SNAPSHOT)
index 160a346d8652a5639d3d3c7dfe6f80c33ba4fc7a..e3b620603cf2c489ba10e5063a7e923a8bd5d2ee 100644 (file)
@@ -119,7 +119,7 @@ int unit_add_name(Unit *u, const char *text) {
         if (!s)
                 return -ENOMEM;
 
-        if (!unit_name_is_valid(s, false)) {
+        if (!unit_name_is_valid(s, TEMPLATE_INVALID)) {
                 r = -EINVAL;
                 goto fail;
         }
index d7ecd7f9a4b5e9c14822f0f9518d5b5dee460c53..5470bc94c6a2351840b94ce8064f79dd248eb32c 100644 (file)
@@ -190,7 +190,7 @@ static int add_dbus(const char *path, const char *fname, const char *type) {
         }
 
         if (service) {
-                if (!unit_name_is_valid(service, false)) {
+                if (!unit_name_is_valid(service, TEMPLATE_INVALID)) {
                         log_warning("Unit name %s is not valid, ignoring.", service);
                         return 0;
                 }
index fdee9d4e64c4d4663313f0680cc46477af3737c3..42c16f67ec5f558101af8d0d60f4f629a059991e 100644 (file)
@@ -991,7 +991,7 @@ static int add_units(sd_journal *j) {
         assert(j);
 
         STRV_FOREACH(i, arg_system_units) {
-                u = unit_name_mangle(*i, false);
+                u = unit_name_mangle(*i, MANGLE_NOGLOB);
                 if (!u)
                         return log_oom();
                 r = add_matches_for_unit(j, u);
@@ -1003,7 +1003,7 @@ static int add_units(sd_journal *j) {
         }
 
         STRV_FOREACH(i, arg_user_units) {
-                u = unit_name_mangle(*i, false);
+                u = unit_name_mangle(*i, MANGLE_NOGLOB);
                 if (!u)
                         return log_oom();
 
index ef2015fe3c3e753d7cc5f622c63b164550c43819..1b14e40e4d2e7ed6dd38f545ed19042150e34df6 100644 (file)
@@ -208,7 +208,7 @@ static int message_start_transient_unit_new(sd_bus *bus, const char *name, sd_bu
         if (!isempty(arg_slice)) {
                 _cleanup_free_ char *slice;
 
-                slice = unit_name_mangle_with_suffix(arg_slice, false, ".slice");
+                slice = unit_name_mangle_with_suffix(arg_slice, MANGLE_NOGLOB, ".slice");
                 if (!slice)
                         return -ENOMEM;
 
@@ -255,7 +255,7 @@ static int start_transient_service(
         int r;
 
         if (arg_unit)
-                name = unit_name_mangle_with_suffix(arg_unit, false, ".service");
+                name = unit_name_mangle_with_suffix(arg_unit, MANGLE_NOGLOB, ".service");
         else
                 asprintf(&name, "run-%lu.service", (unsigned long) getpid());
         if (!name)
@@ -342,7 +342,7 @@ static int start_transient_scope(
         assert(bus);
 
         if (arg_unit)
-                name = unit_name_mangle_with_suffix(arg_unit, false, ".scope");
+                name = unit_name_mangle_with_suffix(arg_unit, MANGLE_NOGLOB, ".scope");
         else
                 asprintf(&name, "run-%lu.scope", (unsigned long) getpid());
         if (!name)
index 27eee8eacddae52da0f30baab8320f2bd6fb5f3a..f2af8dcfda57ccde25b92c77fff40f91efa9f6b4 100644 (file)
@@ -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);
index 233d64b6e95c082f45deb0a19932f999f9255262..62151d99d0cfbf1041cf0ffaaa0461f1cf74d049 100644 (file)
@@ -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))
index 832b9268133f2e1b06a5c3c499cb1be850df9500..408323b8fb8d9b708e4564d6ceffa733d63ccd3d 100644 (file)
@@ -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;
 
index 362ff0c00c007d1db8994e5d9f0822798d4d18dd..d06d2b23537708bd394751b217afbb5443fdbff1 100644 (file)
@@ -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);
index 03b9dd9c20cc486c6ba7f329bca9f99077370941..3a9bca90cec1aff20b1eba59189acac5f4d3d65d 100644 (file)
@@ -1375,7 +1375,7 @@ static int list_dependencies(sd_bus *bus, char **args) {
         assert(bus);
 
         if (args[1]) {
-                unit = unit_name_mangle(args[1], false);
+                unit = unit_name_mangle(args[1], MANGLE_NOGLOB);
                 if (!unit)
                         return log_oom();
                 u = unit;
@@ -1475,7 +1475,7 @@ static int set_default(sd_bus *bus, char **args) {
         unsigned n_changes = 0;
         int r;
 
-        unit = unit_name_mangle_with_suffix(args[1], false, ".target");
+        unit = unit_name_mangle_with_suffix(args[1], MANGLE_NOGLOB, ".target");
         if (!unit)
                 return log_oom();
 
@@ -1924,7 +1924,7 @@ static int check_one_unit(sd_bus *bus, const char *name, const char *good_states
 
         assert(name);
 
-        n = unit_name_mangle(name, false);
+        n = unit_name_mangle(name, MANGLE_NOGLOB);
         if (!n)
                 return log_oom();
 
@@ -1981,7 +1981,7 @@ static int check_triggering_units(
         char **i;
         int r;
 
-        n = unit_name_mangle(name, false);
+        n = unit_name_mangle(name, MANGLE_NOGLOB);
         if (!n)
                 return log_oom();
 
@@ -2111,9 +2111,9 @@ static int expand_names(sd_bus *bus, char **names, const char* suffix, char ***r
                 char *t;
 
                 if (suffix)
-                        t = unit_name_mangle_with_suffix(*name, true, suffix);
+                        t = unit_name_mangle_with_suffix(*name, MANGLE_GLOB, suffix);
                 else
-                        t = unit_name_mangle(*name, true);
+                        t = unit_name_mangle(*name, MANGLE_GLOB);
                 if (!t)
                         return log_oom();
 
@@ -4117,7 +4117,7 @@ static int set_property(sd_bus *bus, char **args) {
         if (r < 0)
                 return bus_log_create_error(r);
 
-        n = unit_name_mangle(args[1], false);
+        n = unit_name_mangle(args[1], MANGLE_NOGLOB);
         if (!n)
                 return log_oom();
 
@@ -4164,7 +4164,7 @@ static int snapshot(sd_bus *bus, char **args) {
         int r;
 
         if (strv_length(args) > 1)
-                n = unit_name_mangle_with_suffix(args[1], false, ".snapshot");
+                n = unit_name_mangle_with_suffix(args[1], MANGLE_NOGLOB, ".snapshot");
         else
                 n = strdup("");
         if (!n)
@@ -4619,7 +4619,7 @@ static int mangle_names(char **original_names, char ***mangled_names) {
                 if (is_path(*name))
                         *i = strdup(*name);
                 else
-                        *i = unit_name_mangle(*name, false);
+                        *i = unit_name_mangle(*name, MANGLE_NOGLOB);
 
                 if (!*i) {
                         strv_free(l);
index ad664bc545c4468dc4ff8bafeb9ac55095935084..c10ec608af5e60801d08e5a2581a15f77d93e83e 100644 (file)
@@ -63,7 +63,7 @@ static void test_replacements(void) {
                 puts(t);                                           \
                 k = unit_name_to_path(t);                          \
                 puts(k);                                           \
-                assert(streq(k, expected ? expected : path));     \
+                assert(streq(k, expected ? expected : path));      \
         }
 
         expect("/waldo", ".mount", NULL);
@@ -74,12 +74,12 @@ static void test_replacements(void) {
 
         puts("-------------------------------------------------");
 #undef expect
-#define expect(pattern, path, suffix, expected)                         \
-        {                                                               \
-                _cleanup_free_ char *t =                                \
+#define expect(pattern, path, suffix, expected)                              \
+        {                                                                    \
+                _cleanup_free_ char *t =                                     \
                         unit_name_from_path_instance(pattern, path, suffix); \
-                puts(t);                                                \
-                assert(streq(t, expected));                             \
+                puts(t);                                                     \
+                assert(streq(t, expected));                                  \
         }
 
         expect("waldo", "/waldo", ".mount", "waldo@waldo.mount");
@@ -89,13 +89,13 @@ static void test_replacements(void) {
 
         puts("-------------------------------------------------");
 #undef expect
-#define expect(pattern)                                                 \
-        {                                                               \
-                _cleanup_free_ char *k, *t;                             \
-                assert_se(t = unit_name_mangle(pattern, false));        \
-                assert_se(k = unit_name_mangle(t, false));              \
-                puts(t);                                                \
-                assert_se(streq(t, k));                                 \
+#define expect(pattern)                                                     \
+        {                                                                   \
+                _cleanup_free_ char *k, *t;                                 \
+                assert_se(t = unit_name_mangle(pattern, MANGLE_NOGLOB));    \
+                assert_se(k = unit_name_mangle(t, MANGLE_NOGLOB));          \
+                puts(t);                                                    \
+                assert_se(streq(t, k));                                     \
         }
 
         expect("/home");