2011-08-22 20:14:12 +00:00
|
|
|
{- safely running shell commands
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2010-2013 Joey Hess <id@joeyh.name>
|
2011-08-22 20:14:12 +00:00
|
|
|
-
|
2014-05-10 14:01:27 +00:00
|
|
|
- License: BSD-2-clause
|
2011-08-22 20:14:12 +00:00
|
|
|
-}
|
|
|
|
|
|
|
|
module Utility.SafeCommand where
|
|
|
|
|
|
|
|
import System.Exit
|
2012-07-19 04:43:36 +00:00
|
|
|
import Utility.Process
|
2011-08-22 20:14:12 +00:00
|
|
|
import Data.String.Utils
|
2011-11-09 21:28:35 +00:00
|
|
|
import Control.Applicative
|
2013-08-01 19:15:49 +00:00
|
|
|
import System.FilePath
|
|
|
|
import Data.Char
|
2011-08-22 20:14:12 +00:00
|
|
|
|
|
|
|
{- A type for parameters passed to a shell command. A command can
|
|
|
|
- be passed either some Params (multiple parameters can be included,
|
|
|
|
- whitespace-separated, or a single Param (for when parameters contain
|
|
|
|
- whitespace), or a File.
|
|
|
|
-}
|
|
|
|
data CommandParam = Params String | Param String | File FilePath
|
|
|
|
deriving (Eq, Show, Ord)
|
|
|
|
|
|
|
|
{- Used to pass a list of CommandParams to a function that runs
|
|
|
|
- a command and expects Strings. -}
|
|
|
|
toCommand :: [CommandParam] -> [String]
|
2013-08-01 19:15:49 +00:00
|
|
|
toCommand = concatMap unwrap
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
unwrap (Param s) = [s]
|
|
|
|
unwrap (Params s) = filter (not . null) (split " " s)
|
2013-08-01 19:15:49 +00:00
|
|
|
-- Files that start with a non-alphanumeric that is not a path
|
|
|
|
-- separator are modified to avoid the command interpreting them as
|
|
|
|
-- options or other special constructs.
|
|
|
|
unwrap (File s@(h:_))
|
|
|
|
| isAlphaNum h || h `elem` pathseps = [s]
|
|
|
|
| otherwise = ["./" ++ s]
|
2012-12-13 04:24:19 +00:00
|
|
|
unwrap (File s) = [s]
|
2013-08-02 16:37:45 +00:00
|
|
|
-- '/' is explicitly included because it's an alternative
|
|
|
|
-- path separator on Windows.
|
2013-08-02 16:42:14 +00:00
|
|
|
pathseps = pathSeparator:"./"
|
2011-08-22 20:14:12 +00:00
|
|
|
|
|
|
|
{- Run a system command, and returns True or False
|
|
|
|
- if it succeeded or failed.
|
|
|
|
-}
|
|
|
|
boolSystem :: FilePath -> [CommandParam] -> IO Bool
|
|
|
|
boolSystem command params = boolSystemEnv command params Nothing
|
|
|
|
|
|
|
|
boolSystemEnv :: FilePath -> [CommandParam] -> Maybe [(String, String)] -> IO Bool
|
2012-07-18 19:30:26 +00:00
|
|
|
boolSystemEnv command params environ = dispatch <$> safeSystemEnv command params environ
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
dispatch ExitSuccess = True
|
|
|
|
dispatch _ = False
|
2011-11-09 21:28:35 +00:00
|
|
|
|
|
|
|
{- Runs a system command, returning the exit status. -}
|
|
|
|
safeSystem :: FilePath -> [CommandParam] -> IO ExitCode
|
|
|
|
safeSystem command params = safeSystemEnv command params Nothing
|
|
|
|
|
|
|
|
safeSystemEnv :: FilePath -> [CommandParam] -> Maybe [(String, String)] -> IO ExitCode
|
2012-07-18 19:30:26 +00:00
|
|
|
safeSystemEnv command params environ = do
|
|
|
|
(_, _, _, pid) <- createProcess (proc command $ toCommand params)
|
|
|
|
{ env = environ }
|
|
|
|
waitForProcess pid
|
2011-08-22 20:14:12 +00:00
|
|
|
|
2013-03-29 17:09:30 +00:00
|
|
|
{- Wraps a shell command line inside sh -c, allowing it to be run in a
|
|
|
|
- login shell that may not support POSIX shell, eg csh. -}
|
|
|
|
shellWrap :: String -> String
|
|
|
|
shellWrap cmdline = "sh -c " ++ shellEscape cmdline
|
|
|
|
|
2011-08-22 20:14:12 +00:00
|
|
|
{- Escapes a filename or other parameter to be safely able to be exposed to
|
2013-03-29 17:09:30 +00:00
|
|
|
- the shell.
|
|
|
|
-
|
|
|
|
- This method works for POSIX shells, as well as other shells like csh.
|
|
|
|
-}
|
2011-08-22 20:14:12 +00:00
|
|
|
shellEscape :: String -> String
|
|
|
|
shellEscape f = "'" ++ escaped ++ "'"
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
-- replace ' with '"'"'
|
|
|
|
escaped = join "'\"'\"'" $ split "'" f
|
2011-08-22 20:14:12 +00:00
|
|
|
|
|
|
|
{- Unescapes a set of shellEscaped words or filenames. -}
|
|
|
|
shellUnEscape :: String -> [String]
|
|
|
|
shellUnEscape [] = []
|
|
|
|
shellUnEscape s = word : shellUnEscape rest
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
(word, rest) = findword "" s
|
|
|
|
findword w [] = (w, "")
|
|
|
|
findword w (c:cs)
|
|
|
|
| c == ' ' = (w, cs)
|
|
|
|
| c == '\'' = inquote c w cs
|
|
|
|
| c == '"' = inquote c w cs
|
|
|
|
| otherwise = findword (w++[c]) cs
|
|
|
|
inquote _ w [] = (w, "")
|
|
|
|
inquote q w (c:cs)
|
|
|
|
| c == q = findword w cs
|
|
|
|
| otherwise = inquote q (w++[c]) cs
|
2011-08-22 20:14:12 +00:00
|
|
|
|
|
|
|
{- For quickcheck. -}
|
|
|
|
prop_idempotent_shellEscape :: String -> Bool
|
|
|
|
prop_idempotent_shellEscape s = [s] == (shellUnEscape . shellEscape) s
|
|
|
|
prop_idempotent_shellEscape_multiword :: [String] -> Bool
|
|
|
|
prop_idempotent_shellEscape_multiword s = s == (shellUnEscape . unwords . map shellEscape) s
|
2013-02-15 17:05:19 +00:00
|
|
|
|
|
|
|
{- Segements a list of filenames into groups that are all below the manximum
|
|
|
|
- command-line length limit. Does not preserve order. -}
|
|
|
|
segmentXargs :: [FilePath] -> [[FilePath]]
|
|
|
|
segmentXargs l = go l [] 0 []
|
|
|
|
where
|
|
|
|
go [] c _ r = c:r
|
|
|
|
go (f:fs) c accumlen r
|
|
|
|
| len < maxlen && newlen > maxlen = go (f:fs) [] 0 (c:r)
|
|
|
|
| otherwise = go fs (f:c) newlen r
|
|
|
|
where
|
|
|
|
len = length f
|
|
|
|
newlen = accumlen + len
|
|
|
|
|
|
|
|
{- 10k of filenames per command, well under Linux's 20k limit;
|
|
|
|
- allows room for other parameters etc. -}
|
|
|
|
maxlen = 10240
|