chiark / gitweb /
unit-name: add new unit_name_build_from_type() helper
authorLennart Poettering <lennart@poettering.net>
Thu, 22 Feb 2018 17:24:57 +0000 (18:24 +0100)
committerSven Eden <yamakuzure@gmx.net>
Fri, 24 Aug 2018 14:47:08 +0000 (16:47 +0200)
The new helper is much like unit_name_build() but expects a UnitType
value instead of a suffix.

src/basic/unit-name.c
src/basic/unit-name.h

index 3d5b0372f1ba2183cd20e23fa3235eed73176b83..35e63c17e3e6d7871b88b370c8adc074133e1531 100644 (file)
@@ -245,25 +245,45 @@ int unit_name_change_suffix(const char *n, const char *suffix, char **ret) {
 #endif // 0
 
 int unit_name_build(const char *prefix, const char *instance, const char *suffix, char **ret) {
-        char *s;
+        UnitType type;
 
         assert(prefix);
         assert(suffix);
         assert(ret);
 
+        if (isempty(suffix))
+                return -EINVAL;
+        if (suffix[0] != '.')
+                return -EINVAL;
+
+        type = unit_type_from_string(suffix + 1);
+        if (type < 0)
+                return -EINVAL;
+
+        return unit_name_build_from_type(prefix, instance, type, ret);
+}
+
+int unit_name_build_from_type(const char *prefix, const char *instance, UnitType type, char **ret) {
+        const char *ut;
+        char *s;
+
+        assert(prefix);
+        assert(type >= 0);
+        assert(type < _UNIT_TYPE_MAX);
+        assert(ret);
+
         if (!unit_prefix_is_valid(prefix))
                 return -EINVAL;
 
         if (instance && !unit_instance_is_valid(instance))
                 return -EINVAL;
 
-        if (!unit_suffix_is_valid(suffix))
-                return -EINVAL;
+        ut = unit_type_to_string(type);
 
         if (!instance)
-                s = strappend(prefix, suffix);
+                s = strjoin(prefix, ".", ut);
         else
-                s = strjoin(prefix, "@", instance, suffix);
+                s = strjoin(prefix, "@", instance, ".", ut);
         if (!s)
                 return -ENOMEM;
 
index 3515fad4ce3fb513ca2b92eccee11c19d08c4f41..07229769a499f85b2b71e88b77cbb5db976116cc 100644 (file)
@@ -46,6 +46,7 @@ int unit_name_change_suffix(const char *n, const char *suffix, char **ret);
 #endif // 0
 
 int unit_name_build(const char *prefix, const char *instance, const char *suffix, char **ret);
+int unit_name_build_from_type(const char *prefix, const char *instance, UnitType, char **ret);
 
 #if 0 /// UNNEEDED by elogind
 char *unit_name_escape(const char *f);