chiark / gitweb /
bus: reuse more code
[elogind.git] / src / shared / unit-name.c
index d6391228f2118686d4cae9cd79ca230a3d8ffc20..06bbfacb0081d06c83441f92ab9a3f83a07f29b0 100644 (file)
@@ -33,7 +33,7 @@
         "ABCDEFGHIJKLMNOPQRSTUVWXYZ"            \
         ":-_.\\"
 
-static const char* const unit_type_table[_UNIT_TYPE_MAX] = {
+const char* const unit_type_table[_UNIT_TYPE_MAX] = {
         [UNIT_SERVICE] = "service",
         [UNIT_SOCKET] = "socket",
         [UNIT_TARGET] = "target",
@@ -48,7 +48,7 @@ static const char* const unit_type_table[_UNIT_TYPE_MAX] = {
 
 DEFINE_STRING_TABLE_LOOKUP(unit_type, UnitType);
 
-static const char* const unit_load_state_table[_UNIT_LOAD_STATE_MAX] = {
+const char* const unit_load_state_table[_UNIT_LOAD_STATE_MAX] = {
         [UNIT_STUB] = "stub",
         [UNIT_LOADED] = "loaded",
         [UNIT_ERROR] = "error",
@@ -470,15 +470,13 @@ char *unit_dbus_path_from_name(const char *name) {
 char *unit_name_mangle(const char *name) {
         char *r, *t;
         const char *f;
-        bool dot = false;
 
         assert(name);
 
         /* Try to turn a string that might not be a unit name into a
          * sensible unit name. */
 
-        if (path_startswith(name, "/dev/") ||
-            path_startswith(name, "/sys/"))
+        if (is_device_path(name))
                 return unit_name_from_path(name, ".device");
 
         if (path_is_absolute(name))
@@ -492,10 +490,6 @@ char *unit_name_mangle(const char *name) {
                 return NULL;
 
         for (f = name, t = r; *f; f++) {
-
-                if (*f == '.')
-                        dot = true;
-
                 if (*f == '/')
                         *(t++) = '-';
                 else if (!strchr("@" VALID_CHARS, *f))
@@ -504,7 +498,7 @@ char *unit_name_mangle(const char *name) {
                         *(t++) = *f;
         }
 
-        if (!dot)
+        if (unit_name_to_type(name) < 0)
                 strcpy(t, ".service");
         else
                 *t = 0;
@@ -512,6 +506,36 @@ char *unit_name_mangle(const char *name) {
         return r;
 }
 
+char *snapshot_name_mangle(const char *name) {
+        char *r, *t;
+        const char *f;
+
+        assert(name);
+
+        /* Similar to unit_name_mangle(), but is called when we know
+         * that this is about snapshot units. */
+
+        r = new(char, strlen(name) * 4 + 1 + sizeof(".snapshot")-1);
+        if (!r)
+                return NULL;
+
+        for (f = name, t = r; *f; f++) {
+                if (*f == '/')
+                        *(t++) = '-';
+                else if (!strchr(VALID_CHARS, *f))
+                        t = do_escape_char(*f, t);
+                else
+                        *(t++) = *f;
+        }
+
+        if (!endswith(name, ".snapshot"))
+                strcpy(t, ".snapshot");
+        else
+                *t = 0;
+
+        return r;
+}
+
 UnitType unit_name_to_type(const char *n) {
         const char *e;