]> git.proxmox.com Git - proxmox.git/commitdiff
sys: make escape_unit() more flexible, add unescape_unit_path
authorWolfgang Bumiller <w.bumiller@proxmox.com>
Thu, 7 Jul 2022 09:45:26 +0000 (11:45 +0200)
committerWolfgang Bumiller <w.bumiller@proxmox.com>
Thu, 7 Jul 2022 09:45:26 +0000 (11:45 +0200)
This adds the ability to use these functions with non-utf8
strings as well.

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
proxmox-sys/src/systemd.rs

index 4575a2044aae3c70197bc14ee65b6db989d3044b..6fa459e41fcb5fdc1dc0e7fe18a598e2ed777e9f 100644 (file)
@@ -1,3 +1,7 @@
+use std::ffi::OsString;
+use std::os::unix::ffi::OsStringExt;
+use std::path::PathBuf;
+
 use anyhow::{bail, Error};
 
 #[allow(clippy::manual_range_contains)]
@@ -16,16 +20,21 @@ fn parse_hex_digit(d: u8) -> Result<u8, Error> {
 }
 
 /// Escape strings for usage in systemd unit names
-pub fn escape_unit(mut unit: &str, is_path: bool) -> String {
+pub fn escape_unit<P: AsRef<[u8]>>(unit: P, is_path: bool) -> String {
+    escape_unit_bytes(unit.as_ref(), is_path)
+}
+
+fn escape_unit_bytes(mut unit: &[u8], is_path: bool) -> String {
     if is_path {
-        unit = unit.trim_matches('/');
+        while !unit.is_empty() && unit[0] == b'/' {
+            unit = &unit[1..];
+        }
+
         if unit.is_empty() {
             return String::from("-");
         }
     }
 
-    let unit = unit.as_bytes();
-
     let mut escaped = String::new();
 
     for (i, c) in unit.iter().enumerate() {
@@ -50,6 +59,16 @@ pub fn escape_unit(mut unit: &str, is_path: bool) -> String {
 
 /// Unescape strings used in systemd unit names
 pub fn unescape_unit(text: &str) -> Result<String, Error> {
+    Ok(String::from_utf8(unescape_unit_do(text)?)?)
+}
+
+/// Unescape strings used in systemd unit names
+pub fn unescape_unit_path(text: &str) -> Result<PathBuf, Error> {
+    Ok(OsString::from_vec(unescape_unit_do(text)?).into())
+}
+
+/// Unescape strings used in systemd unit names
+fn unescape_unit_do(text: &str) -> Result<Vec<u8>, Error> {
     let mut i = text.as_bytes();
 
     let mut data: Vec<u8> = Vec::new();
@@ -79,7 +98,5 @@ pub fn unescape_unit(text: &str) -> Result<String, Error> {
         }
     }
 
-    let text = String::from_utf8(data)?;
-
-    Ok(text)
+    Ok(data)
 }