summaryrefslogtreecommitdiff
path: root/src/Propellor
diff options
context:
space:
mode:
authorJoey Hess2018-08-19 21:53:52 -0400
committerJoey Hess2018-08-19 21:53:52 -0400
commit58f28b2a19ed193d68ce0b209f6f7f87726719de (patch)
tree7e29cdb3b2074fea6d089849cef512b6bac4e8b0 /src/Propellor
parent5f5d99824a2b24d11ccbf4969f934c94e58a9a90 (diff)
Systemd.escapePath function
Useful when creating mount units.
Diffstat (limited to 'src/Propellor')
-rw-r--r--src/Propellor/Property/Systemd.hs15
1 files changed, 15 insertions, 0 deletions
diff --git a/src/Propellor/Property/Systemd.hs b/src/Propellor/Property/Systemd.hs
index 39b4bd84..b983d9d8 100644
--- a/src/Propellor/Property/Systemd.hs
+++ b/src/Propellor/Property/Systemd.hs
@@ -13,6 +13,7 @@ module Propellor.Property.Systemd (
networkd,
journald,
logind,
+ escapePath,
-- * Configuration
installed,
Option,
@@ -58,7 +59,9 @@ import Utility.FileMode
import Utility.Split
import Data.List
+import Data.Char
import qualified Data.Map as M
+import Text.Printf
type ServiceName = String
@@ -459,3 +462,15 @@ bind p = containerCfg $ "--bind=" ++ toBind p
-- | Read-only mind mount.
bindRo :: Bindable p => p -> RevertableProperty (HasInfo + Linux) (HasInfo + Linux)
bindRo p = containerCfg $ "--bind-ro=" ++ toBind p
+
+-- | Escapes a path for inclusion in a systemd unit name,
+-- the same as systemd-escape does.
+escapePath :: FilePath -> String
+escapePath = concatMap escape
+ . dropWhile (== '/')
+ . reverse . dropWhile (== '/') . reverse
+ where
+ escape '/' = "-"
+ escape c
+ | (isAscii c || c == '_') = [c]
+ | otherwise = '\\' : printf "%x" c