chiark / gitweb /
systemctl: append default suffix only if none present
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 28 Nov 2014 16:28:10 +0000 (11:28 -0500)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 28 Nov 2014 16:54:37 +0000 (11:54 -0500)
Simplify unit_name_mangle() and unit_name_mangle_with_suffix() to
always behave the same, and only append a suffix if there is no
type suffix. If a user says 'isolate blah.device' it is better to
return an error that the type cannot be isolated, than to try to
isolate blah.device.target.

src/shared/unit-name.c
src/shared/unit-name.h
src/systemctl/systemctl.c

index 6c6d7f4f83cd8a7512d09977b24eab90ce00dba1..21b66913c9d1dae539f17db31dc2a8bd3f83a99e 100644 (file)
@@ -502,13 +502,18 @@ 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.
+ *  Convert a string to a unit name. /dev/blah is converted to dev-blah.device,
+ *  /blah/blah is converted to blah-blah.mount, anything else is left alone,
+ *  except that @suffix is appended if a valid unit suffix is not present.
+ *
+ *  If @allow_globs, globs characters are preserved. Otherwise they are escaped.
  */
-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) {
         char *r, *t;
 
         assert(name);
+        assert(suffix);
+        assert(suffix[0] == '.');
 
         if (is_device_path(name))
                 return unit_name_from_path(name, ".device");
@@ -516,38 +521,13 @@ char *unit_name_mangle(const char *name, enum unit_name_mangle allow_globs) {
         if (path_is_absolute(name))
                 return unit_name_from_path(name, ".mount");
 
-        r = new(char, strlen(name) * 4 + strlen(".service") + 1);
-        if (!r)
-                return NULL;
-
-        t = do_escape_mangle(name, allow_globs, r);
-
-        if (unit_name_to_type(name) < 0)
-                strcpy(t, ".service");
-        else
-                *t = 0;
-
-        return r;
-}
-
-/**
- *  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, enum unit_name_mangle allow_globs, const char *suffix) {
-        char *r, *t;
-
-        assert(name);
-        assert(suffix);
-        assert(suffix[0] == '.');
-
         r = new(char, strlen(name) * 4 + strlen(suffix) + 1);
         if (!r)
                 return NULL;
 
         t = do_escape_mangle(name, allow_globs, r);
 
-        if (!endswith(name, suffix))
+        if (unit_name_to_type(name) < 0)
                 strcpy(t, suffix);
         else
                 *t = 0;
index daeb56a860d8b0cd1b4c883a118a42347e15da75..6f139cc4c4101a18cd4105c663112408f5339baf 100644 (file)
@@ -156,8 +156,10 @@ enum unit_name_mangle {
         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);
+static inline char *unit_name_mangle(const char *name, enum unit_name_mangle allow_globs) {
+        return unit_name_mangle_with_suffix(name, allow_globs, ".service");
+}
 
 int build_subslice(const char *slice, const char*name, char **subslice);
 
index 4299429fbde5195516464cf827c21badf22ee5b3..263755fec70baf3b6dd23a82b423eae98a90a823 100644 (file)
@@ -2701,10 +2701,7 @@ static int expand_names(sd_bus *bus, char **names, const char* suffix, char ***r
         STRV_FOREACH(name, names) {
                 char *t;
 
-                if (suffix)
-                        t = unit_name_mangle_with_suffix(*name, MANGLE_GLOB, suffix);
-                else
-                        t = unit_name_mangle(*name, MANGLE_GLOB);
+                t = unit_name_mangle_with_suffix(*name, MANGLE_GLOB, suffix);
                 if (!t)
                         return log_oom();