summaryrefslogtreecommitdiff
path: root/Utility
diff options
context:
space:
mode:
authorGravatar Joey Hess <joeyh@joeyh.name>2015-06-01 13:52:23 -0400
committerGravatar Joey Hess <joeyh@joeyh.name>2015-06-01 13:52:23 -0400
commit39c13f91004b41166b786785083e78b34df0c48f (patch)
treebf5ea0acf27c9075a63d3b23b970c3ea44ecee64 /Utility
parent3a9ca30fa8740a57ea477243498339f9738102d6 (diff)
remove Params constructor from Utility.SafeCommand
This removes a bit of complexity, and should make things faster (avoids tokenizing Params string), and probably involve less garbage collection. In a few places, it was useful to use Params to avoid needing a list, but that is easily avoided. Problems noticed while doing this conversion: * Some uses of Params "oneword" which was entirely unnecessary overhead. * A few places that built up a list of parameters with ++ and then used Params to split it! Test suite passes.
Diffstat (limited to 'Utility')
-rw-r--r--Utility/Gpg.hs40
-rw-r--r--Utility/Quvi.hs19
-rw-r--r--Utility/Rsync.hs3
-rw-r--r--Utility/SafeCommand.hs14
-rw-r--r--Utility/Url.hs12
5 files changed, 46 insertions, 42 deletions
diff --git a/Utility/Gpg.hs b/Utility/Gpg.hs
index a1b782d97..0a0b04a03 100644
--- a/Utility/Gpg.hs
+++ b/Utility/Gpg.hs
@@ -60,17 +60,20 @@ stdParams params = do
{- Usual options for symmetric / public-key encryption. -}
stdEncryptionParams :: Bool -> [CommandParam]
-stdEncryptionParams symmetric =
- [ enc symmetric
- , Param "--force-mdc"
+stdEncryptionParams symmetric = enc symmetric ++
+ [ Param "--force-mdc"
, Param "--no-textmode"
]
where
- enc True = Param "--symmetric"
+ enc True = [ Param "--symmetric" ]
-- Force gpg to only encrypt to the specified recipients, not
-- configured defaults. Recipients are assumed to be specified in
-- elsewhere.
- enc False = Params "--encrypt --no-encrypt-to --no-default-recipient"
+ enc False =
+ [ Param "--encrypt"
+ , Param "--no-encrypt-to"
+ , Param "--no-default-recipient"
+ ]
{- Runs gpg with some params and returns its stdout, strictly. -}
readStrict :: [CommandParam] -> IO String
@@ -152,7 +155,7 @@ pipeLazy params feeder reader = do
findPubKeys :: String -> IO KeyIds
findPubKeys for = KeyIds . parse . lines <$> readStrict params
where
- params = [Params "--with-colons --list-public-keys", Param for]
+ params = [Param "--with-colons", Param "--list-public-keys", Param for]
parse = mapMaybe (keyIdField . split ":")
keyIdField ("pub":_:_:_:f:_) = Just f
keyIdField _ = Nothing
@@ -165,7 +168,7 @@ secretKeys :: IO (M.Map KeyId UserId)
secretKeys = catchDefaultIO M.empty makemap
where
makemap = M.fromList . parse . lines <$> readStrict params
- params = [Params "--with-colons --list-secret-keys --fixed-list-mode"]
+ params = [Param "--with-colons", Param "--list-secret-keys", Param "--fixed-list-mode"]
parse = extract [] Nothing . map (split ":")
extract c (Just keyid) (("uid":_:_:_:_:_:_:_:_:userid:_):rest) =
extract ((keyid, decode_c userid):c) Nothing rest
@@ -215,13 +218,14 @@ genSecretKey keytype passphrase userid keysize =
- It is armored, to avoid newlines, since gpg only reads ciphers up to the
- first newline. -}
genRandom :: Bool -> Size -> IO String
-genRandom highQuality size = checksize <$> readStrict
- [ Params params
- , Param $ show randomquality
- , Param $ show size
- ]
+genRandom highQuality size = checksize <$> readStrict params
where
- params = "--gen-random --armor"
+ params =
+ [ Param "--gen-random"
+ , Param "--armor"
+ , Param $ show randomquality
+ , Param $ show size
+ ]
-- See http://www.gnupg.org/documentation/manuals/gcrypt/Quality-of-random-numbers.html
-- for the meaning of random quality levels.
@@ -242,7 +246,7 @@ genRandom highQuality size = checksize <$> readStrict
else shortread len
shortread got = error $ unwords
- [ "Not enough bytes returned from gpg", params
+ [ "Not enough bytes returned from gpg", show params
, "(got", show got, "; expected", show expectedlength, ")"
]
@@ -335,8 +339,8 @@ testHarness a = do
dir <- mktmpdir $ base </> "gpgtmpXXXXXX"
setEnv var dir True
-- For some reason, recent gpg needs a trustdb to be set up.
- _ <- pipeStrict [Params "--trust-model auto --update-trustdb"] []
- _ <- pipeStrict [Params "--import -q"] $ unlines
+ _ <- pipeStrict [Param "--trust-model auto", Param "--update-trustdb"] []
+ _ <- pipeStrict [Param "--import", Param "-q"] $ unlines
[testSecretKey, testKey]
return dir
@@ -356,13 +360,13 @@ checkEncryptionFile :: FilePath -> Maybe KeyIds -> IO Bool
checkEncryptionFile filename keys =
checkGpgPackets keys =<< readStrict params
where
- params = [Params "--list-packets --list-only", File filename]
+ params = [Param "--list-packets", Param "--list-only", File filename]
checkEncryptionStream :: String -> Maybe KeyIds -> IO Bool
checkEncryptionStream stream keys =
checkGpgPackets keys =<< pipeStrict params stream
where
- params = [Params "--list-packets --list-only"]
+ params = [Param "--list-packets", Param "--list-only"]
{- Parses an OpenPGP packet list, and checks whether data is
- symmetrically encrypted (keys is Nothing), or encrypted to some
diff --git a/Utility/Quvi.hs b/Utility/Quvi.hs
index 0412116a1..8d37b1c8f 100644
--- a/Utility/Quvi.hs
+++ b/Utility/Quvi.hs
@@ -108,7 +108,8 @@ check v ps url = maybe False (not . null . pageLinks) <$> query v ps url
supported :: QuviVersion -> URLString -> IO Bool
supported NoQuvi _ = return False
supported Quvi04 url = boolSystem "quvi"
- [ Params "--verbosity mute --support"
+ [ Param "--verbosity mute"
+ , Param "--support"
, Param url
]
{- Use quvi-info to see if the url's domain is supported.
@@ -134,18 +135,18 @@ listdomains Quvi09 = concatMap (split ",")
(toCommand [Param "info", Param "-p", Param "domains"])
listdomains _ = return []
-type QuviParam = QuviVersion -> CommandParam
+type QuviParams = QuviVersion -> [CommandParam]
{- Disables progress, but not information output. -}
-quiet :: QuviParam
+quiet :: QuviParams
-- Cannot use quiet as it now disables informational output.
-- No way to disable progress.
-quiet Quvi09 = Params "--verbosity verbose"
-quiet Quvi04 = Params "--verbosity quiet"
-quiet NoQuvi = Params ""
+quiet Quvi09 = [Param "--verbosity", Param "verbose"]
+quiet Quvi04 = [Param "--verbosity", Param "quiet"]
+quiet NoQuvi = []
{- Only return http results, not streaming protocols. -}
-httponly :: QuviParam
+httponly :: QuviParams
-- No way to do it with 0.9?
-httponly Quvi04 = Params "-c http"
-httponly _ = Params "" -- No way to do it with 0.9?
+httponly Quvi04 = [Param "-c", Param "http"]
+httponly _ = [] -- No way to do it with 0.9?
diff --git a/Utility/Rsync.hs b/Utility/Rsync.hs
index 4f4c4eb5d..3aaf9281b 100644
--- a/Utility/Rsync.hs
+++ b/Utility/Rsync.hs
@@ -44,7 +44,8 @@ rsyncServerParams =
-- allow resuming of transfers of big files
, Param "--inplace"
-- other options rsync normally uses in server mode
- , Params "-e.Lsf ."
+ , Param "-e.Lsf"
+ , Param "."
]
rsyncUseDestinationPermissions :: CommandParam
diff --git a/Utility/SafeCommand.hs b/Utility/SafeCommand.hs
index 82e35049a..9102b7267 100644
--- a/Utility/SafeCommand.hs
+++ b/Utility/SafeCommand.hs
@@ -19,25 +19,23 @@ import Prelude
-- | Parameters that can be passed to a shell command.
data CommandParam
- = Params String -- ^ Contains multiple parameters, separated by whitespace
- | Param String -- ^ A single parameter
+ = Param String -- ^ A parameter
| File FilePath -- ^ The name of a file
deriving (Eq, Show, Ord)
-- | Used to pass a list of CommandParams to a function that runs
-- a command and expects Strings. -}
toCommand :: [CommandParam] -> [String]
-toCommand = concatMap unwrap
+toCommand = map unwrap
where
- unwrap (Param s) = [s]
- unwrap (Params s) = filter (not . null) (split " " s)
+ unwrap (Param s) = s
-- 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]
- unwrap (File s) = [s]
+ | isAlphaNum h || h `elem` pathseps = s
+ | otherwise = "./" ++ s
+ unwrap (File s) = s
-- '/' is explicitly included because it's an alternative
-- path separator on Windows.
pathseps = pathSeparator:"./"
diff --git a/Utility/Url.hs b/Utility/Url.hs
index 2ef1167e5..81a9a1b05 100644
--- a/Utility/Url.hs
+++ b/Utility/Url.hs
@@ -228,14 +228,14 @@ download' quiet url file uo = do
- a less cluttered download display.
-}
#ifndef __ANDROID__
- wgetparams = catMaybes
+ wgetparams = concat
[ if Build.SysConfig.wgetquietprogress && not quiet
- then Just $ Params "-q --show-progress"
- else Nothing
- , Just $ Params "--clobber -c -O"
+ then [Param "-q", Param "--show-progress"]
+ else []
+ , [ Param "--clobber", Param "-c", Param "-O"]
]
#else
- wgetparams = [Params "-c -O"]
+ wgetparams = [Param "-c", Param "-O"]
#endif
{- Uses the -# progress display, because the normal
- one is very confusing when resuming, showing
@@ -247,7 +247,7 @@ download' quiet url file uo = do
-- if the url happens to be empty, so pre-create.
writeFile file ""
go "curl" $ headerparams ++ quietopt "-s" ++
- [Params "-f -L -C - -# -o"]
+ [Param "-f", Param "-L", Param "-C", Param "-", Param "-#", Param "-o"]
{- Run wget in a temp directory because it has been buggy
- and overwritten files in the current directory, even though