]> git.proxmox.com Git - pve-common.git/blobdiff - src/PVE/Systemd.pm
systemd: add un-/escape_unit helpers
[pve-common.git] / src / PVE / Systemd.pm
index 46fc90fb3404580b179359104851da723e24182b..85b35a34d7e877a9fd1910daf4e7cd937aa4bca8 100644 (file)
@@ -7,6 +7,31 @@ use Net::DBus qw(dbus_uint32 dbus_uint64);
 use Net::DBus::Callback;
 use Net::DBus::Reactor;
 
+sub escape_unit {
+    my ($val, $is_path) = @_;
+
+    # NOTE: this is not complete, but enough for our needs. normally all
+    # characters which are not alpha-numerical, '.' or '_' would need escaping
+    $val =~ s/\-/\\x2d/g;
+
+    if ($is_path) {
+       $val =~ s/^\///g;
+       $val =~ s/\/$//g;
+    }
+    $val =~ s/\//-/g;
+
+    return $val;
+}
+
+sub unescape_unit {
+    my ($val) = @_;
+
+    $val =~ s/-/\//g;
+    $val =~ s/\\x([a-fA-F0-9]{2})/chr(hex($1))/eg;
+
+    return $val;
+}
+
 # $code should take the parameters ($interface, $reactor, $finish_callback).
 #
 # $finish_callback can be used by dbus-signal-handlers to stop the reactor.