From: Thomas Lamprecht Date: Fri, 31 Jan 2020 09:24:24 +0000 (+0100) Subject: systemd: add un-/escape_unit helpers X-Git-Url: https://git.proxmox.com/?p=pve-common.git;a=commitdiff_plain;h=b4f88e889f06a9b7f7993775223bdb987e6a2db9 systemd: add un-/escape_unit helpers 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 --- diff --git a/src/PVE/Systemd.pm b/src/PVE/Systemd.pm index 46fc90f..85b35a3 100644 --- a/src/PVE/Systemd.pm +++ b/src/PVE/Systemd.pm @@ -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.