summaryrefslogtreecommitdiff
path: root/fsmgr.hs
diff options
context:
space:
mode:
authorAndrew Cady <d@jerkface.net>2019-10-04 13:48:36 -0400
committerAndrew Cady <d@jerkface.net>2019-10-04 13:51:57 -0400
commitcd7dc6266b415cf3c937fc3b3aa14fce999c6f0d (patch)
treefe89beb18d89d29cee49f2e04326fc9152638bbf /fsmgr.hs
parenta9aab3eddf957e1ac4ef29a1dc6374567827c343 (diff)
factor out string functions
most of these were copied from MissingH
Diffstat (limited to 'fsmgr.hs')
-rw-r--r--fsmgr.hs42
1 files changed, 3 insertions, 39 deletions
diff --git a/fsmgr.hs b/fsmgr.hs
index 66bcaa8..770c8bc 100644
--- a/fsmgr.hs
+++ b/fsmgr.hs
@@ -27,56 +27,20 @@ import System.Directory (getCurrentDirectory, createDirectoryIfMissing)
27import System.Posix.Process (getProcessID) 27import System.Posix.Process (getProcessID)
28import System.Posix.Types (CUid (..)) 28import System.Posix.Types (CUid (..))
29import System.Posix.User (getEffectiveUserID) 29import System.Posix.User (getEffectiveUserID)
30import String
30 31
31noParent :: BaseImageSpecification -> Bool 32noParent :: BaseImageSpecification -> Bool
32noParent (EmptyImageOfBytes _) = True 33noParent (EmptyImageOfBytes _) = True
33noParent (ParentImageConfigFile _) = False 34noParent (ParentImageConfigFile _) = False
34 35
35split :: Eq a => [a] -> [a] -> [[a]] 36dynamicNames :: FilePath -> FilePath
36split _ [] = [] 37dynamicNames = replace "$(karch)" uname
37split delim str =
38 let (firstline, remainder) = breakList (startswith delim) str
39 in
40 firstline : case remainder of
41 [] -> []
42 x -> if x == delim
43 then [] : []
44 else split delim
45 (drop (length delim) x)
46
47spanList :: ([a] -> Bool) -> [a] -> ([a], [a])
48spanList _ [] = ([],[])
49spanList func list@(x:xs) =
50 if func list
51 then (x:ys,zs)
52 else ([],list)
53 where (ys,zs) = spanList func xs
54
55breakList :: ([a] -> Bool) -> [a] -> ([a], [a])
56breakList func = spanList (not . func)
57
58startswith :: Eq a => [a] -> [a] -> Bool
59startswith = isPrefixOf
60
61join :: [a] -> [[a]] -> [a]
62join delim l = concat (intersperse delim l)
63
64replace :: Eq a => [a] -> [a] -> [a] -> [a]
65replace old new l = join new . split old $ l
66
67wordsBy c s = words (rep <$> s)
68 where
69 rep x | x == c = ' '
70 | otherwise = x
71 38
72uname :: String 39uname :: String
73uname = unsafePerformIO $ do 40uname = unsafePerformIO $ do
74 Stdout out <- cmd "uname -r" 41 Stdout out <- cmd "uname -r"
75 return $ last . wordsBy '-' . head . lines $ out 42 return $ last . wordsBy '-' . head . lines $ out
76 43
77dynamicNames :: FilePath -> FilePath
78dynamicNames = replace "$(karch)" uname
79
80buildRoot :: DiskImageConfig -> FilePath -> Action () 44buildRoot :: DiskImageConfig -> FilePath -> Action ()
81buildRoot config@DiskImageConfig{..} finalOut = do 45buildRoot config@DiskImageConfig{..} finalOut = do
82 let out = finalOut <.> "tmp" 46 let out = finalOut <.> "tmp"