From b4f88e889f06a9b7f7993775223bdb987e6a2db9 Mon Sep 17 00:00:00 2001 From: Thomas Lamprecht Date: Fri, 31 Jan 2020 10:24:24 +0100 Subject: [PATCH] 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 --- src/PVE/Systemd.pm | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) 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. -- 2.39.2