2020-01-14 21:55:34 +00:00
|
|
|
module GHCup.File where
|
|
|
|
|
2020-01-16 22:27:38 +00:00
|
|
|
import Data.ByteString
|
2020-01-14 21:55:34 +00:00
|
|
|
import qualified Data.ByteString.Lazy as L
|
|
|
|
import Data.Char
|
|
|
|
import Data.Maybe
|
|
|
|
import HPath
|
|
|
|
import HPath.IO
|
|
|
|
import Streamly.ByteString
|
|
|
|
import Streamly
|
|
|
|
import System.Posix.FilePath hiding ( (</>) )
|
|
|
|
import Data.Foldable
|
|
|
|
import Control.Monad
|
|
|
|
import Control.Exception.Safe
|
|
|
|
import Data.Functor
|
|
|
|
import System.Posix.Files.ByteString
|
2020-01-16 22:27:38 +00:00
|
|
|
import System.IO
|
|
|
|
import System.Posix.IO.ByteString
|
|
|
|
hiding ( openFd )
|
2020-01-14 21:55:34 +00:00
|
|
|
import qualified System.Posix.Process.ByteString
|
|
|
|
as SPPB
|
|
|
|
import System.Posix.Types
|
|
|
|
|
|
|
|
import qualified Streamly.Internal.Memory.ArrayStream
|
|
|
|
as AS
|
|
|
|
import qualified Streamly.FileSystem.Handle as FH
|
|
|
|
import qualified Streamly.Internal.Data.Unfold as SU
|
|
|
|
import qualified Streamly.Prelude as S
|
|
|
|
import System.Exit
|
|
|
|
import qualified Streamly.Data.Fold as FL
|
|
|
|
import Data.ByteString.Builder
|
|
|
|
import Foreign.C.Error
|
|
|
|
import GHCup.Prelude
|
|
|
|
|
|
|
|
|
|
|
|
-- |Checks whether a file is executable. Follows symlinks.
|
|
|
|
--
|
|
|
|
-- Only eACCES, eROFS, eTXTBSY, ePERM are catched (and return False).
|
|
|
|
--
|
|
|
|
-- Throws:
|
|
|
|
--
|
|
|
|
-- - `NoSuchThing` if the file does not exist
|
|
|
|
--
|
|
|
|
-- Note: calls `access`
|
|
|
|
isExecutable :: Path b -> IO Bool
|
|
|
|
isExecutable p = fileAccess (toFilePath p) False False True
|
|
|
|
|
|
|
|
|
|
|
|
readFd :: Fd -> IO L.ByteString
|
|
|
|
readFd fd = do
|
|
|
|
handle' <- fdToHandle fd
|
|
|
|
let stream =
|
|
|
|
(S.unfold (SU.finally hClose FH.readChunks) handle')
|
|
|
|
>>= arrayToByteString
|
|
|
|
toLazyByteString <$> S.fold FL.mconcat (fmap byteString stream)
|
|
|
|
|
|
|
|
|
|
|
|
-- | Read the lines of a file into a stream. The stream holds
|
|
|
|
-- a file handle as a resource and will close it once the stream
|
|
|
|
-- terminates (either through exception or because it's drained).
|
|
|
|
readFileLines :: Path b -> IO (SerialT IO ByteString)
|
|
|
|
readFileLines p = do
|
2020-01-16 22:27:38 +00:00
|
|
|
stream <- readFileStream p
|
2020-01-14 21:55:34 +00:00
|
|
|
pure
|
|
|
|
. (>>= arrayToByteString)
|
|
|
|
. AS.splitOn (fromIntegral $ ord '\n')
|
|
|
|
. (>>= byteStringToArray)
|
|
|
|
$ stream
|
|
|
|
|
|
|
|
|
|
|
|
-- | Find the given executable by searching all *absolute* PATH components.
|
|
|
|
-- Relative paths in PATH are ignored.
|
|
|
|
--
|
|
|
|
-- This shouldn't throw IO exceptions, unless getting the environment variable
|
|
|
|
-- PATH does.
|
|
|
|
findExecutable :: RelC r => Path r -> IO (Maybe (Path Abs))
|
|
|
|
findExecutable ex = do
|
|
|
|
sPaths <- fmap catMaybes . (fmap . fmap) parseAbs $ getSearchPath
|
|
|
|
-- We don't want exceptions to mess up our result. If we can't
|
|
|
|
-- figure out if a file exists, then treat it as a negative result.
|
2020-01-16 22:27:38 +00:00
|
|
|
asum $ fmap (handleIO (\_ -> pure Nothing)) $ fmap
|
2020-01-14 21:55:34 +00:00
|
|
|
-- asum for short-circuiting behavior
|
|
|
|
(\s -> (isExecutable (s </> ex) >>= guard) $> (Just (s </> ex)))
|
|
|
|
sPaths
|
|
|
|
|
|
|
|
|
|
|
|
-- | Execute the given command and collect the stdout and the exit code.
|
|
|
|
executeOut :: Path Fn -- ^ command as filename, e.g. 'ls'
|
|
|
|
-> [ByteString] -- ^ arguments to the command
|
|
|
|
-> IO (Maybe (L.ByteString, ExitCode)) -- ^ fst of the tuple is the stdout
|
|
|
|
executeOut path args = withFnPath path $ \fp -> do
|
2020-01-16 22:27:38 +00:00
|
|
|
(parentRead, childWrite) <- createPipe
|
|
|
|
pid <- SPPB.forkProcess $ do
|
2020-01-14 21:55:34 +00:00
|
|
|
whileM_
|
2020-01-16 22:27:38 +00:00
|
|
|
(dupTo childWrite stdOutput)
|
2020-01-14 21:55:34 +00:00
|
|
|
(\r ->
|
|
|
|
getErrno >>= \e -> pure $ r == fromIntegral (-1 :: Int) && e == eINTR
|
|
|
|
)
|
2020-01-16 22:27:38 +00:00
|
|
|
closeFd childWrite
|
|
|
|
closeFd parentRead
|
|
|
|
closeFd stdInput
|
2020-01-14 21:55:34 +00:00
|
|
|
SPPB.executeFile fp True args Nothing
|
|
|
|
|
2020-01-16 22:27:38 +00:00
|
|
|
closeFd childWrite
|
2020-01-14 21:55:34 +00:00
|
|
|
|
|
|
|
SPPB.getProcessStatus True True pid >>= \case
|
2020-01-16 22:27:38 +00:00
|
|
|
-- readE will take care of closing the fd
|
|
|
|
Just (SPPB.Exited es) -> readFd parentRead <&> Just . (, es)
|
|
|
|
_ -> closeFd parentRead $> Nothing
|