]> git.proxmox.com Git - pve-common.git/commitdiff
systemd: add un-/escape_unit helpers
authorThomas Lamprecht <t.lamprecht@proxmox.com>
Fri, 31 Jan 2020 09:24:24 +0000 (10:24 +0100)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Fri, 31 Jan 2020 12:45:56 +0000 (13:45 +0100)
moved from pve-storage, we need to reuse it in another module there,
as it's a general method lets just move it here already.

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
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.