2020-03-24 15:49:18 +00:00
|
|
|
{-# LANGUAGE QuasiQuotes #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2020-07-13 09:52:34 +00:00
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
2020-03-24 15:49:18 +00:00
|
|
|
{-# LANGUAGE TemplateHaskell #-}
|
2020-01-11 20:15:05 +00:00
|
|
|
|
2020-07-21 23:08:58 +00:00
|
|
|
{-|
|
|
|
|
Module : GHCup.Utils.File
|
|
|
|
Description : File and unix APIs
|
|
|
|
Copyright : (c) Julian Ospald, 2020
|
2020-07-30 18:04:02 +00:00
|
|
|
License : LGPL-3.0
|
2020-07-21 23:08:58 +00:00
|
|
|
Maintainer : hasufell@hasufell.de
|
|
|
|
Stability : experimental
|
|
|
|
Portability : POSIX
|
|
|
|
|
|
|
|
This module handles file and executable handling.
|
|
|
|
Some of these functions use sophisticated logging.
|
|
|
|
-}
|
2020-01-11 20:15:05 +00:00
|
|
|
module GHCup.Utils.File where
|
|
|
|
|
|
|
|
import GHCup.Utils.Dirs
|
|
|
|
import GHCup.Utils.Prelude
|
2020-07-13 09:52:34 +00:00
|
|
|
import GHCup.Types
|
2020-01-11 20:15:05 +00:00
|
|
|
|
2020-03-24 15:49:18 +00:00
|
|
|
import Control.Concurrent
|
2020-07-21 20:43:09 +00:00
|
|
|
import Control.Concurrent.Async
|
2020-04-09 15:00:09 +00:00
|
|
|
import Control.Exception ( evaluate )
|
2020-01-11 20:15:05 +00:00
|
|
|
import Control.Exception.Safe
|
|
|
|
import Control.Monad
|
2020-07-13 09:52:34 +00:00
|
|
|
import Control.Monad.Reader
|
2020-07-15 13:00:59 +00:00
|
|
|
import Control.Monad.Trans.State.Strict
|
2020-03-24 15:49:18 +00:00
|
|
|
import Data.ByteString ( ByteString )
|
2020-01-11 20:15:05 +00:00
|
|
|
import Data.Foldable
|
|
|
|
import Data.Functor
|
2020-03-24 15:49:18 +00:00
|
|
|
import Data.IORef
|
2020-01-11 20:15:05 +00:00
|
|
|
import Data.Maybe
|
2020-07-15 13:00:59 +00:00
|
|
|
import Data.Sequence ( Seq, (|>) )
|
2020-04-25 10:06:41 +00:00
|
|
|
import Data.Text ( Text )
|
|
|
|
import Data.Void
|
2020-07-15 13:00:59 +00:00
|
|
|
import Data.Word8
|
2020-01-11 20:15:05 +00:00
|
|
|
import GHC.IO.Exception
|
|
|
|
import HPath
|
2020-07-15 13:00:59 +00:00
|
|
|
import HPath.IO hiding ( hideError )
|
|
|
|
import Optics hiding ((<|), (|>))
|
2020-03-24 15:49:18 +00:00
|
|
|
import System.Console.Pretty
|
|
|
|
import System.Console.Regions
|
|
|
|
import System.IO.Error
|
2020-01-11 20:15:05 +00:00
|
|
|
import System.Posix.Directory.ByteString
|
|
|
|
import System.Posix.FD as FD
|
|
|
|
import System.Posix.FilePath hiding ( (</>) )
|
|
|
|
import System.Posix.Foreign ( oExcl )
|
|
|
|
import "unix" System.Posix.IO.ByteString
|
|
|
|
hiding ( openFd )
|
|
|
|
import System.Posix.Process ( ProcessStatus(..) )
|
|
|
|
import System.Posix.Types
|
2020-04-25 10:06:41 +00:00
|
|
|
import Text.Regex.Posix
|
2020-01-11 20:15:05 +00:00
|
|
|
|
|
|
|
|
2020-03-24 15:49:18 +00:00
|
|
|
import qualified Control.Exception as EX
|
2020-07-15 13:00:59 +00:00
|
|
|
import qualified Data.Sequence as Sq
|
2020-03-24 15:49:18 +00:00
|
|
|
import qualified Data.Text as T
|
2020-04-25 10:06:41 +00:00
|
|
|
import qualified Data.Text.Encoding as E
|
2020-01-11 20:15:05 +00:00
|
|
|
import qualified System.Posix.Process.ByteString
|
|
|
|
as SPPB
|
|
|
|
import Streamly.External.Posix.DirStream
|
|
|
|
import qualified Streamly.Prelude as S
|
2020-04-25 10:06:41 +00:00
|
|
|
import qualified Text.Megaparsec as MP
|
2020-03-24 15:49:18 +00:00
|
|
|
import qualified Data.ByteString as BS
|
|
|
|
import qualified "unix-bytestring" System.Posix.IO.ByteString
|
|
|
|
as SPIB
|
|
|
|
|
|
|
|
|
2020-04-25 10:06:41 +00:00
|
|
|
|
2020-01-11 20:15:05 +00:00
|
|
|
data ProcessError = NonZeroExit Int ByteString [ByteString]
|
|
|
|
| PTerminated ByteString [ByteString]
|
|
|
|
| PStopped ByteString [ByteString]
|
|
|
|
| NoSuchPid ByteString [ByteString]
|
|
|
|
deriving Show
|
|
|
|
|
|
|
|
|
|
|
|
data CapturedProcess = CapturedProcess
|
|
|
|
{ _exitCode :: ExitCode
|
|
|
|
, _stdOut :: ByteString
|
|
|
|
, _stdErr :: ByteString
|
|
|
|
}
|
|
|
|
deriving (Eq, Show)
|
|
|
|
|
|
|
|
makeLenses ''CapturedProcess
|
|
|
|
|
|
|
|
|
|
|
|
-- | 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 :: Path Rel -> 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.
|
|
|
|
asum $ fmap (handleIO (\_ -> pure Nothing)) $ fmap
|
|
|
|
-- asum for short-circuiting behavior
|
|
|
|
(\s' -> (isExecutable (s' </> ex) >>= guard) $> (Just (s' </> ex)))
|
|
|
|
sPaths
|
|
|
|
|
|
|
|
|
|
|
|
-- | Execute the given command and collect the stdout, stderr and the exit code.
|
|
|
|
-- The command is run in a subprocess.
|
2020-03-24 15:49:18 +00:00
|
|
|
executeOut :: Path b -- ^ command as filename, e.g. 'ls'
|
2020-01-11 20:15:05 +00:00
|
|
|
-> [ByteString] -- ^ arguments to the command
|
|
|
|
-> Maybe (Path Abs) -- ^ chdir to this path
|
|
|
|
-> IO CapturedProcess
|
|
|
|
executeOut path args chdir = captureOutStreams $ do
|
|
|
|
maybe (pure ()) (changeWorkingDirectory . toFilePath) chdir
|
|
|
|
SPPB.executeFile (toFilePath path) True args Nothing
|
|
|
|
|
|
|
|
|
2020-07-13 09:52:34 +00:00
|
|
|
execLogged :: (MonadReader Settings m, MonadIO m, MonadThrow m)
|
|
|
|
=> ByteString -- ^ thing to execute
|
2020-01-11 20:15:05 +00:00
|
|
|
-> Bool -- ^ whether to search PATH for the thing
|
|
|
|
-> [ByteString] -- ^ args for the thing
|
|
|
|
-> Path Rel -- ^ log filename
|
|
|
|
-> Maybe (Path Abs) -- ^ optionally chdir into this
|
|
|
|
-> Maybe [(ByteString, ByteString)] -- ^ optional environment
|
2020-07-13 09:52:34 +00:00
|
|
|
-> m (Either ProcessError ())
|
2020-01-11 20:15:05 +00:00
|
|
|
execLogged exe spath args lfile chdir env = do
|
2020-07-15 13:00:59 +00:00
|
|
|
Settings {..} <- ask
|
|
|
|
ldir <- liftIO ghcupLogsDir
|
|
|
|
logfile <- (ldir </>) <$> parseRel (toFilePath lfile <> ".log")
|
|
|
|
liftIO $ bracket (createFile (toFilePath logfile) newFilePerms)
|
|
|
|
closeFd
|
|
|
|
(action verbose)
|
2020-01-11 20:15:05 +00:00
|
|
|
where
|
2020-07-13 09:52:34 +00:00
|
|
|
action verbose fd = do
|
2020-03-24 15:49:18 +00:00
|
|
|
actionWithPipes $ \(stdoutRead, stdoutWrite) -> do
|
2020-07-15 13:00:59 +00:00
|
|
|
-- start the thread that logs to stdout
|
|
|
|
pState <- newEmptyMVar
|
|
|
|
done <- newEmptyMVar
|
|
|
|
void
|
2020-07-21 20:43:09 +00:00
|
|
|
$ forkIO
|
2020-03-24 15:49:18 +00:00
|
|
|
$ EX.handle (\(_ :: IOException) -> pure ())
|
2020-07-21 20:43:09 +00:00
|
|
|
$ flip EX.finally (putMVar done ())
|
2020-07-15 13:00:59 +00:00
|
|
|
$ (if verbose
|
|
|
|
then tee fd stdoutRead
|
|
|
|
else printToRegion fd stdoutRead 6 pState
|
|
|
|
)
|
2020-03-24 15:49:18 +00:00
|
|
|
|
2020-07-15 13:00:59 +00:00
|
|
|
-- fork the subprocess
|
2020-03-24 15:49:18 +00:00
|
|
|
pid <- SPPB.forkProcess $ do
|
|
|
|
void $ dupTo stdoutWrite stdOutput
|
|
|
|
void $ dupTo stdoutWrite stdError
|
|
|
|
closeFd stdoutRead
|
2020-07-15 13:00:59 +00:00
|
|
|
closeFd stdoutWrite
|
2020-01-11 20:15:05 +00:00
|
|
|
|
2020-03-24 15:49:18 +00:00
|
|
|
-- execute the action
|
|
|
|
maybe (pure ()) (changeWorkingDirectory . toFilePath) chdir
|
2020-07-15 13:00:59 +00:00
|
|
|
void $ SPPB.executeFile exe spath args env
|
2020-03-24 15:49:18 +00:00
|
|
|
|
|
|
|
closeFd stdoutWrite
|
|
|
|
|
|
|
|
-- wait for the subprocess to finish
|
2020-07-15 13:00:59 +00:00
|
|
|
e <- toProcessError exe args <$!> SPPB.getProcessStatus True True pid
|
|
|
|
putMVar pState (either (const False) (const True) e)
|
2020-03-24 15:49:18 +00:00
|
|
|
|
2020-07-21 20:43:09 +00:00
|
|
|
void $ race (takeMVar done) (threadDelay (1000000 * 3))
|
2020-03-24 15:49:18 +00:00
|
|
|
closeFd stdoutRead
|
2020-07-15 13:00:59 +00:00
|
|
|
|
2020-03-24 15:49:18 +00:00
|
|
|
pure e
|
|
|
|
|
2020-07-15 13:00:59 +00:00
|
|
|
tee :: Fd -> Fd -> IO ()
|
|
|
|
tee fileFd fdIn = readTilEOF lineAction fdIn
|
2020-07-13 09:52:34 +00:00
|
|
|
|
|
|
|
where
|
2020-07-15 13:00:59 +00:00
|
|
|
lineAction :: ByteString -> IO ()
|
2020-07-13 09:52:34 +00:00
|
|
|
lineAction bs' = do
|
|
|
|
void $ SPIB.fdWrite fileFd (bs' <> "\n")
|
|
|
|
void $ SPIB.fdWrite stdOutput (bs' <> "\n")
|
|
|
|
|
2020-03-24 15:49:18 +00:00
|
|
|
-- Reads fdIn and logs the output in a continous scrolling area
|
|
|
|
-- of 'size' terminal lines. Also writes to a log file.
|
2020-07-15 13:00:59 +00:00
|
|
|
printToRegion :: Fd -> Fd -> Int -> MVar Bool -> IO ()
|
|
|
|
printToRegion fileFd fdIn size pState = do
|
|
|
|
void $ displayConsoleRegions $ do
|
|
|
|
rs <-
|
|
|
|
liftIO
|
|
|
|
. fmap Sq.fromList
|
|
|
|
. sequence
|
|
|
|
. replicate size
|
|
|
|
. openConsoleRegion
|
|
|
|
$ Linear
|
|
|
|
flip runStateT mempty
|
2020-03-24 15:49:18 +00:00
|
|
|
$ handle
|
2020-07-15 13:00:59 +00:00
|
|
|
(\(ex :: SomeException) -> do
|
|
|
|
ps <- liftIO $ takeMVar pState
|
|
|
|
when (ps == True) (forM_ rs (liftIO . closeConsoleRegion))
|
|
|
|
throw ex
|
2020-03-24 15:49:18 +00:00
|
|
|
)
|
2020-07-15 13:00:59 +00:00
|
|
|
$ readTilEOF (lineAction rs) fdIn
|
2020-03-24 15:49:18 +00:00
|
|
|
|
|
|
|
where
|
|
|
|
-- action to perform line by line
|
2020-07-13 09:52:34 +00:00
|
|
|
-- TODO: do this with vty for efficiency
|
2020-07-15 13:00:59 +00:00
|
|
|
lineAction :: (MonadMask m, MonadIO m)
|
|
|
|
=> Seq ConsoleRegion
|
|
|
|
-> ByteString
|
|
|
|
-> StateT (Seq ByteString) m ()
|
|
|
|
lineAction rs = \bs' -> do
|
|
|
|
void $ liftIO $ SPIB.fdWrite fileFd (bs' <> "\n")
|
|
|
|
modify (swapRegs bs')
|
|
|
|
regs <- get
|
|
|
|
liftIO $ forM_ (Sq.zip regs rs) $ \(bs, r) -> setConsoleRegion r $ do
|
|
|
|
w <- consoleWidth
|
|
|
|
return
|
|
|
|
. T.pack
|
|
|
|
. color Blue
|
|
|
|
. T.unpack
|
|
|
|
. decUTF8Safe
|
|
|
|
. trim w
|
|
|
|
. (\b -> "[ " <> toFilePath lfile <> " ] " <> b)
|
|
|
|
$ bs
|
|
|
|
|
|
|
|
swapRegs :: a -> Seq a -> Seq a
|
|
|
|
swapRegs bs = \regs -> if
|
|
|
|
| Sq.length regs < size -> regs |> bs
|
|
|
|
| otherwise -> Sq.drop 1 regs |> bs
|
2020-03-24 15:49:18 +00:00
|
|
|
|
|
|
|
-- trim output line to terminal width
|
2020-07-15 13:00:59 +00:00
|
|
|
trim :: Int -> ByteString -> ByteString
|
|
|
|
trim w = \bs -> if
|
|
|
|
| BS.length bs > w && w > 5 -> BS.take (w - 4) bs <> "..."
|
|
|
|
| otherwise -> bs
|
2020-03-24 15:49:18 +00:00
|
|
|
|
2020-07-21 20:43:09 +00:00
|
|
|
-- Consecutively read from Fd in 512 chunks until we hit
|
|
|
|
-- newline or EOF.
|
|
|
|
readLine :: MonadIO m
|
|
|
|
=> Fd -- ^ input file descriptor
|
|
|
|
-> ByteString -- ^ rest buffer (read across newline)
|
|
|
|
-> m (ByteString, ByteString, Bool) -- ^ (full line, rest, eof)
|
|
|
|
readLine fd = \inBs -> go inBs
|
2020-07-15 13:00:59 +00:00
|
|
|
where
|
|
|
|
go inBs = do
|
2020-07-21 20:43:09 +00:00
|
|
|
-- if buffer is not empty, process it first
|
|
|
|
mbs <- if BS.length inBs == 0
|
|
|
|
-- otherwise attempt read
|
|
|
|
then liftIO
|
|
|
|
$ handleIO (\e -> if isEOFError e then pure Nothing else ioError e)
|
|
|
|
$ fmap Just
|
|
|
|
$ SPIB.fdRead fd 512
|
|
|
|
else pure $ Just inBs
|
|
|
|
case mbs of
|
|
|
|
Nothing -> pure ("", "", True)
|
|
|
|
Just bs -> do
|
|
|
|
-- split on newline
|
|
|
|
let (line, rest) = BS.span (/= _lf) bs
|
|
|
|
if
|
|
|
|
| BS.length rest /= 0 -> pure (line, BS.tail rest, False)
|
|
|
|
-- if rest is empty, then there was no newline, process further
|
|
|
|
| otherwise -> (\(l, r, b) -> (line <> l, r, b)) <$!> go mempty
|
2020-07-15 13:00:59 +00:00
|
|
|
|
|
|
|
readTilEOF :: MonadIO m => (ByteString -> m a) -> Fd -> m ()
|
|
|
|
readTilEOF ~action' fd' = go mempty
|
|
|
|
where
|
|
|
|
go bs' = do
|
2020-07-21 20:43:09 +00:00
|
|
|
(bs, rest, eof) <- readLine fd' bs'
|
|
|
|
if eof
|
|
|
|
then liftIO $ ioError (mkIOError eofErrorType "" Nothing Nothing)
|
|
|
|
else (void $ action' bs) >> go rest
|
2020-01-11 20:15:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- | Capture the stdout and stderr of the given action, which
|
|
|
|
-- is run in a subprocess. Stdin is closed. You might want to
|
|
|
|
-- 'race' this to make sure it terminates.
|
|
|
|
captureOutStreams :: IO a
|
|
|
|
-- ^ the action to execute in a subprocess
|
|
|
|
-> IO CapturedProcess
|
2020-04-09 15:00:09 +00:00
|
|
|
captureOutStreams action = do
|
2020-01-11 20:15:05 +00:00
|
|
|
actionWithPipes $ \(parentStdoutRead, childStdoutWrite) ->
|
|
|
|
actionWithPipes $ \(parentStderrRead, childStderrWrite) -> do
|
|
|
|
pid <- SPPB.forkProcess $ do
|
|
|
|
-- dup stdout
|
|
|
|
void $ dupTo childStdoutWrite stdOutput
|
|
|
|
closeFd childStdoutWrite
|
|
|
|
closeFd parentStdoutRead
|
|
|
|
|
|
|
|
-- dup stderr
|
|
|
|
void $ dupTo childStderrWrite stdError
|
|
|
|
closeFd childStderrWrite
|
|
|
|
closeFd parentStderrRead
|
|
|
|
|
|
|
|
-- execute the action
|
2020-04-09 15:00:09 +00:00
|
|
|
a <- action
|
|
|
|
void $ evaluate a
|
2020-01-11 20:15:05 +00:00
|
|
|
|
|
|
|
-- close everything we don't need
|
|
|
|
closeFd childStdoutWrite
|
|
|
|
closeFd childStderrWrite
|
|
|
|
|
2020-04-09 15:00:09 +00:00
|
|
|
-- start thread that writes the output
|
|
|
|
refOut <- newIORef BS.empty
|
|
|
|
refErr <- newIORef BS.empty
|
|
|
|
done <- newEmptyMVar
|
2020-04-09 16:26:02 +00:00
|
|
|
_ <-
|
2020-04-09 15:00:09 +00:00
|
|
|
forkIO
|
|
|
|
$ EX.handle (\(_ :: IOException) -> pure ())
|
2020-07-21 20:43:09 +00:00
|
|
|
$ flip EX.finally (putMVar done ())
|
2020-04-09 15:00:09 +00:00
|
|
|
$ writeStds parentStdoutRead parentStderrRead refOut refErr
|
|
|
|
|
|
|
|
status <- SPPB.getProcessStatus True True pid
|
2020-07-21 20:43:09 +00:00
|
|
|
void $ race (takeMVar done) (threadDelay (1000000 * 3))
|
2020-04-09 15:00:09 +00:00
|
|
|
|
|
|
|
case status of
|
2020-01-11 20:15:05 +00:00
|
|
|
-- readFd will take care of closing the fd
|
|
|
|
Just (SPPB.Exited es) -> do
|
2020-04-09 15:00:09 +00:00
|
|
|
stdout' <- readIORef refOut
|
|
|
|
stderr' <- readIORef refErr
|
2020-01-11 20:15:05 +00:00
|
|
|
pure $ CapturedProcess { _exitCode = es
|
|
|
|
, _stdOut = stdout'
|
|
|
|
, _stdErr = stderr'
|
|
|
|
}
|
2020-04-09 15:00:09 +00:00
|
|
|
|
2020-01-11 20:15:05 +00:00
|
|
|
_ -> throwIO $ userError $ ("No such PID " ++ show pid)
|
|
|
|
|
2020-04-09 15:00:09 +00:00
|
|
|
where
|
|
|
|
writeStds pout perr rout rerr = do
|
|
|
|
doneOut <- newEmptyMVar
|
|
|
|
void
|
|
|
|
$ forkIO
|
2020-04-09 16:26:02 +00:00
|
|
|
$ hideError eofErrorType
|
2020-07-21 20:43:09 +00:00
|
|
|
$ flip EX.finally (putMVar doneOut ())
|
2020-04-09 15:00:09 +00:00
|
|
|
$ readTilEOF (\x -> modifyIORef' rout (<> x)) pout
|
|
|
|
doneErr <- newEmptyMVar
|
|
|
|
void
|
|
|
|
$ forkIO
|
2020-04-09 16:26:02 +00:00
|
|
|
$ hideError eofErrorType
|
2020-07-21 20:43:09 +00:00
|
|
|
$ flip EX.finally (putMVar doneErr ())
|
2020-04-09 15:00:09 +00:00
|
|
|
$ readTilEOF (\x -> modifyIORef' rerr (<> x)) perr
|
|
|
|
takeMVar doneOut
|
|
|
|
takeMVar doneErr
|
|
|
|
|
2020-04-09 16:26:02 +00:00
|
|
|
readTilEOF ~action' fd' = do
|
2020-04-09 15:00:09 +00:00
|
|
|
bs <- SPIB.fdRead fd' 512
|
2020-04-09 17:53:22 +00:00
|
|
|
void $ action' bs
|
2020-04-09 16:26:02 +00:00
|
|
|
readTilEOF action' fd'
|
2020-04-09 15:00:09 +00:00
|
|
|
|
2020-03-24 15:49:18 +00:00
|
|
|
|
|
|
|
actionWithPipes :: ((Fd, Fd) -> IO b) -> IO b
|
|
|
|
actionWithPipes a =
|
|
|
|
createPipe >>= \(p1, p2) -> (flip finally) (cleanup [p1, p2]) $ a (p1, p2)
|
|
|
|
|
|
|
|
cleanup :: [Fd] -> IO ()
|
|
|
|
cleanup fds = for_ fds $ \fd -> handleIO (\_ -> pure ()) $ closeFd fd
|
2020-01-11 20:15:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- | Create a new regular file in write-only mode. The file must not exist.
|
|
|
|
createRegularFileFd :: FileMode -> Path b -> IO Fd
|
|
|
|
createRegularFileFd fm dest =
|
|
|
|
FD.openFd (toFilePath dest) WriteOnly [oExcl] (Just fm)
|
|
|
|
|
|
|
|
|
|
|
|
-- | Thin wrapper around `executeFile`.
|
|
|
|
exec :: ByteString -- ^ thing to execute
|
|
|
|
-> Bool -- ^ whether to search PATH for the thing
|
|
|
|
-> [ByteString] -- ^ args for the thing
|
|
|
|
-> Maybe (Path Abs) -- ^ optionally chdir into this
|
|
|
|
-> Maybe [(ByteString, ByteString)] -- ^ optional environment
|
|
|
|
-> IO (Either ProcessError ())
|
|
|
|
exec exe spath args chdir env = do
|
|
|
|
pid <- SPPB.forkProcess $ do
|
|
|
|
maybe (pure ()) (changeWorkingDirectory . toFilePath) chdir
|
|
|
|
SPPB.executeFile exe spath args env
|
|
|
|
|
|
|
|
fmap (toProcessError exe args) $ SPPB.getProcessStatus True True pid
|
|
|
|
|
|
|
|
|
|
|
|
toProcessError :: ByteString
|
|
|
|
-> [ByteString]
|
|
|
|
-> Maybe ProcessStatus
|
|
|
|
-> Either ProcessError ()
|
|
|
|
toProcessError exe args mps = case mps of
|
|
|
|
Just (SPPB.Exited (ExitFailure i)) -> Left $ NonZeroExit i exe args
|
|
|
|
Just (SPPB.Exited ExitSuccess ) -> Right ()
|
|
|
|
Just (Terminated _ _ ) -> Left $ PTerminated exe args
|
|
|
|
Just (Stopped _ ) -> Left $ PStopped exe args
|
|
|
|
Nothing -> Left $ NoSuchPid exe args
|
|
|
|
|
|
|
|
|
|
|
|
-- | Search for a file in the search paths.
|
|
|
|
--
|
|
|
|
-- Catches `PermissionDenied` and `NoSuchThing` and returns `Nothing`.
|
|
|
|
searchPath :: [Path Abs] -> Path Rel -> IO (Maybe (Path Abs))
|
|
|
|
searchPath paths needle = go paths
|
|
|
|
where
|
|
|
|
go [] = pure Nothing
|
|
|
|
go (x : xs) =
|
2020-04-16 21:09:04 +00:00
|
|
|
hideErrorDefM [InappropriateType, PermissionDenied, NoSuchThing] (go xs)
|
2020-01-11 20:15:05 +00:00
|
|
|
$ do
|
|
|
|
dirStream <- openDirStream (toFilePath x)
|
|
|
|
S.findM (\(_, p) -> isMatch x p) (dirContentsStream dirStream)
|
|
|
|
>>= \case
|
|
|
|
Just _ -> pure $ Just (x </> needle)
|
|
|
|
Nothing -> go xs
|
|
|
|
isMatch basedir p = do
|
|
|
|
if p == toFilePath needle
|
|
|
|
then isExecutable (basedir </> needle)
|
|
|
|
else pure False
|
2020-04-25 10:06:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
findFiles :: Path Abs -> Regex -> IO [Path Rel]
|
|
|
|
findFiles path regex = do
|
|
|
|
dirStream <- openDirStream (toFilePath path)
|
|
|
|
f <-
|
|
|
|
(fmap . fmap) snd
|
|
|
|
. S.toList
|
|
|
|
. S.filter (\(_, p) -> match regex p)
|
|
|
|
$ dirContentsStream dirStream
|
|
|
|
pure $ join $ fmap parseRel f
|
|
|
|
|
|
|
|
|
|
|
|
findFiles' :: Path Abs -> MP.Parsec Void Text () -> IO [Path Rel]
|
|
|
|
findFiles' path parser = do
|
|
|
|
dirStream <- openDirStream (toFilePath path)
|
|
|
|
f <-
|
|
|
|
(fmap . fmap) snd
|
|
|
|
. S.toList
|
|
|
|
. S.filter (\(_, p) -> case E.decodeUtf8' p of
|
|
|
|
Left _ -> False
|
|
|
|
Right p' -> isJust $ MP.parseMaybe parser p')
|
|
|
|
$ dirContentsStream dirStream
|
|
|
|
pure $ join $ fmap parseRel f
|
2020-07-28 23:43:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
isBrokenSymlink :: Path Abs -> IO Bool
|
|
|
|
isBrokenSymlink p =
|
|
|
|
handleIO
|
|
|
|
(\e -> if ioeGetErrorType e == NoSuchThing then pure True else throwIO e)
|
|
|
|
$ do
|
|
|
|
_ <- canonicalizePath p
|
|
|
|
pure False
|