Skip to content

Commit

Permalink
Break command args into chunks under system limit like what xargs does (
Browse files Browse the repository at this point in the history
#63)

Closes #62

We removed the depedency on xargs to for better windows compatibility.
However, xargs was mainly introduced to break shell call into multiple
ones to avoid hitting the system limit when we have tens of thousands
files. We need to simulate this behavior.
  • Loading branch information
watashi authored Mar 22, 2024
1 parent dddb6cf commit 13235e1
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions Retrie/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
--
{-# LANGUAGE ApplicativeDo #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TupleSections #-}
module Retrie.Options
Expand Down Expand Up @@ -487,13 +488,28 @@ runGrepChain targetDir verbosity GrepCommands{..} = foldM (commandStep targetDir

-- | run a command with a list of files as quoted arguments
commandStep :: FilePath -> Verbosity -> [FilePath]-> CommandLine -> IO [FilePath]
commandStep targetDir verbosity files cmd = doCmd targetDir verbosity (cmd <> formatPaths files)
where
formatPaths [] = ""
formatPaths xs = " " <> unwords (map quotePath xs)
commandStep targetDir verbosity files cmd =
fmap concat $ forM (chunkifyArgs $ map quotePath files) $ \args ->
doCmd targetDir verbosity $ cmd <> " " <> unwords args

quotePath :: FilePath -> FilePath
quotePath x = "'" <> x <> "'"

-- Split arguments into chunks whose size does not exceed 'argMax'.
-- Note that @chunkifyArgs []@ returns @[[]]@ intentionally.
chunkifyArgs :: [String] -> [[String]]
chunkifyArgs = go 0 []
where
go _ acc [] = [reverse acc]
go n acc (arg: args)
| n <= 0 || n + len <= argMax = go (n + len) (arg: acc) args
| otherwise = reverse acc: go len [arg] args
where
len = length arg + 1

-- | Hardcoded default @--max-args=max-args@ used by @xargs@.
argMax :: Int
argMax = 128 * 1024

doCmd :: FilePath -> Verbosity -> String -> IO [FilePath]
doCmd targetDir verbosity shellCmd = do
Expand Down

0 comments on commit 13235e1

Please sign in to comment.