summaryrefslogtreecommitdiff
path: root/src/Propellor/Property/Chroot/Util.hs
blob: ff227f52a3a95f63d7f7c252c9d9898828b15896 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
module Propellor.Property.Chroot.Util where

import Propellor.Property.Mount

import Utility.Exception
import Utility.Env
import Utility.Directory

import System.Directory
import Control.Applicative
import Prelude

-- | When chrooting, it's useful to ensure that PATH has all the standard
-- directories in it. This adds those directories to whatever PATH is
-- already set.
standardPathEnv :: IO [(String, String)]
standardPathEnv = do
	path <- getEnvDefault "PATH" "/bin"
	addEntry "PATH" (path ++ stdPATH)
		<$> getEnvironment

stdPATH :: String
stdPATH = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

-- | Removes the contents of a chroot. First, unmounts any filesystems
-- mounted within it.
removeChroot :: FilePath -> IO ()
removeChroot c = do
	unmountBelow c
	removeDirectoryRecursive c

-- | Returns true if a chroot directory is empty.
unpopulated :: FilePath -> IO Bool
unpopulated d = null <$> catchDefaultIO [] (dirContents d)