2014-03-30 03:10:52 +00:00
|
|
|
{- safely running shell commands
|
|
|
|
-
|
2015-05-07 16:08:26 +00:00
|
|
|
- Copyright 2010-2015 Joey Hess <id@joeyh.name>
|
2014-03-30 03:10:52 +00:00
|
|
|
-
|
2014-05-10 14:05:28 +00:00
|
|
|
- License: BSD-2-clause
|
2014-03-30 03:10:52 +00:00
|
|
|
-}
|
|
|
|
|
2015-05-27 16:18:42 +00:00
|
|
|
{-# OPTIONS_GHC -fno-warn-tabs #-}
|
|
|
|
|
2014-03-30 03:10:52 +00:00
|
|
|
module Utility.SafeCommand where
|
|
|
|
|
|
|
|
import System.Exit
|
|
|
|
import Utility.Process
|
|
|
|
import Data.String.Utils
|
|
|
|
import System.FilePath
|
|
|
|
import Data.Char
|
2015-05-27 16:18:42 +00:00
|
|
|
import Control.Applicative
|
|
|
|
import Prelude
|
2014-03-30 03:10:52 +00:00
|
|
|
|
2015-05-27 16:38:45 +00:00
|
|
|
-- | Parameters that can be passed to a shell command.
|
|
|
|
data CommandParam
|
2015-06-01 17:56:30 +00:00
|
|
|
= Param String -- ^ A parameter
|
2015-05-27 16:38:45 +00:00
|
|
|
| File FilePath -- ^ The name of a file
|
2014-03-30 03:10:52 +00:00
|
|
|
deriving (Eq, Show, Ord)
|
|
|
|
|
2015-05-27 16:38:45 +00:00
|
|
|
-- | Used to pass a list of CommandParams to a function that runs
|
|
|
|
-- a command and expects Strings. -}
|
2014-03-30 03:10:52 +00:00
|
|
|
toCommand :: [CommandParam] -> [String]
|
2015-06-01 17:56:30 +00:00
|
|
|
toCommand = map unwrap
|
2014-03-30 03:10:52 +00:00
|
|
|
where
|
2015-06-01 17:56:30 +00:00
|
|
|
unwrap (Param s) = s
|
2014-03-30 03:10:52 +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:_))
|
2015-06-01 17:56:30 +00:00
|
|
|
| isAlphaNum h || h `elem` pathseps = s
|
|
|
|
| otherwise = "./" ++ s
|
|
|
|
unwrap (File s) = s
|
2014-03-30 03:10:52 +00:00
|
|
|
-- '/' is explicitly included because it's an alternative
|
|
|
|
-- path separator on Windows.
|
|
|
|
pathseps = pathSeparator:"./"
|
|
|
|
|
2015-05-27 16:38:45 +00:00
|
|
|
-- | Run a system command, and returns True or False if it succeeded or failed.
|
|
|
|
--
|
|
|
|
-- This and other command running functions in this module log the commands
|
|
|
|
-- run at debug level, using System.Log.Logger.
|
2014-03-30 03:10:52 +00:00
|
|
|
boolSystem :: FilePath -> [CommandParam] -> IO Bool
|
2015-05-07 16:08:26 +00:00
|
|
|
boolSystem command params = boolSystem' command params id
|
2014-03-30 03:10:52 +00:00
|
|
|
|
2015-05-07 16:08:26 +00:00
|
|
|
boolSystem' :: FilePath -> [CommandParam] -> (CreateProcess -> CreateProcess) -> IO Bool
|
|
|
|
boolSystem' command params mkprocess = dispatch <$> safeSystem' command params mkprocess
|
2014-03-30 03:10:52 +00:00
|
|
|
where
|
|
|
|
dispatch ExitSuccess = True
|
|
|
|
dispatch _ = False
|
|
|
|
|
2015-05-07 16:08:26 +00:00
|
|
|
boolSystemEnv :: FilePath -> [CommandParam] -> Maybe [(String, String)] -> IO Bool
|
|
|
|
boolSystemEnv command params environ = boolSystem' command params $
|
|
|
|
\p -> p { env = environ }
|
|
|
|
|
2015-05-27 16:38:45 +00:00
|
|
|
-- | Runs a system command, returning the exit status.
|
2014-03-30 03:10:52 +00:00
|
|
|
safeSystem :: FilePath -> [CommandParam] -> IO ExitCode
|
2015-05-07 16:08:26 +00:00
|
|
|
safeSystem command params = safeSystem' command params id
|
2014-03-30 03:10:52 +00:00
|
|
|
|
2015-05-07 16:08:26 +00:00
|
|
|
safeSystem' :: FilePath -> [CommandParam] -> (CreateProcess -> CreateProcess) -> IO ExitCode
|
|
|
|
safeSystem' command params mkprocess = do
|
|
|
|
(_, _, _, pid) <- createProcess p
|
2014-03-30 03:10:52 +00:00
|
|
|
waitForProcess pid
|
2015-05-07 16:08:26 +00:00
|
|
|
where
|
|
|
|
p = mkprocess $ proc command (toCommand params)
|
|
|
|
|
|
|
|
safeSystemEnv :: FilePath -> [CommandParam] -> Maybe [(String, String)] -> IO ExitCode
|
|
|
|
safeSystemEnv command params environ = safeSystem' command params $
|
|
|
|
\p -> p { env = environ }
|
2014-03-30 03:10:52 +00:00
|
|
|
|
2015-05-27 16:38:45 +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.
|
2014-03-30 03:10:52 +00:00
|
|
|
shellWrap :: String -> String
|
|
|
|
shellWrap cmdline = "sh -c " ++ shellEscape cmdline
|
|
|
|
|
2015-05-27 16:38:45 +00:00
|
|
|
-- | Escapes a filename or other parameter to be safely able to be exposed to
|
|
|
|
-- the shell.
|
|
|
|
--
|
|
|
|
-- This method works for POSIX shells, as well as other shells like csh.
|
2014-03-30 03:10:52 +00:00
|
|
|
shellEscape :: String -> String
|
|
|
|
shellEscape f = "'" ++ escaped ++ "'"
|
|
|
|
where
|
|
|
|
-- replace ' with '"'"'
|
|
|
|
escaped = join "'\"'\"'" $ split "'" f
|
|
|
|
|
2015-05-27 16:38:45 +00:00
|
|
|
-- | Unescapes a set of shellEscaped words or filenames.
|
2014-03-30 03:10:52 +00:00
|
|
|
shellUnEscape :: String -> [String]
|
|
|
|
shellUnEscape [] = []
|
|
|
|
shellUnEscape s = word : shellUnEscape rest
|
|
|
|
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
|
|
|
|
|
2015-05-27 16:38:45 +00:00
|
|
|
-- | For quickcheck.
|
2014-03-30 03:10:52 +00:00
|
|
|
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
|
|
|
|
|
2015-05-27 16:38:45 +00:00
|
|
|
-- | Segments a list of filenames into groups that are all below the maximum
|
|
|
|
-- command-line length limit.
|
2015-04-29 18:26:13 +00:00
|
|
|
segmentXargsOrdered :: [FilePath] -> [[FilePath]]
|
|
|
|
segmentXargsOrdered = reverse . map reverse . segmentXargsUnordered
|
|
|
|
|
2015-05-27 16:38:45 +00:00
|
|
|
-- | Not preserving order is a little faster, and streams better when
|
|
|
|
-- there are a great many filenames.
|
2015-04-29 18:26:13 +00:00
|
|
|
segmentXargsUnordered :: [FilePath] -> [[FilePath]]
|
|
|
|
segmentXargsUnordered l = go l [] 0 []
|
2014-03-30 03:10:52 +00:00
|
|
|
where
|
2015-04-29 18:26:13 +00:00
|
|
|
go [] c _ r = (c:r)
|
2014-03-30 03:10:52 +00:00
|
|
|
go (f:fs) c accumlen r
|
2015-04-29 18:26:13 +00:00
|
|
|
| newlen > maxlen && len < maxlen = go (f:fs) [] 0 (c:r)
|
2014-03-30 03:10:52 +00:00
|
|
|
| otherwise = go fs (f:c) newlen r
|
|
|
|
where
|
|
|
|
len = length f
|
|
|
|
newlen = accumlen + len
|
|
|
|
|
2015-04-29 18:26:13 +00:00
|
|
|
{- 10k of filenames per command, well under 100k limit
|
|
|
|
- of Linux (and OSX has a similar limit);
|
|
|
|
- allows room for other parameters etc. Also allows for
|
|
|
|
- eg, multibyte characters. -}
|
2014-03-30 03:10:52 +00:00
|
|
|
maxlen = 10240
|