generalize withNullHandle to MonadIO

This commit is contained in:
Joey Hess 2020-06-03 15:18:48 -04:00
parent e683207123
commit 31d53587d5
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38
3 changed files with 13 additions and 8 deletions

View file

@ -100,18 +100,20 @@ startDaemon assistant foreground startdelay cannotrun listenhost startbrowser =
let flag = "GIT_ANNEX_OUTPUT_REDIR"
createAnnexDirectory (parentDir logfile)
ifM (liftIO $ isNothing <$> getEnv flag)
( liftIO $ withFile devNull WriteMode $ \nullh -> do
( liftIO $ withNullHandle $ \nullh -> do
loghandle <- openLog logfile
e <- getEnvironment
cmd <- programPath
ps <- getArgs
(_, _, _, pid) <- createProcess (proc cmd ps)
let p = (proc cmd ps)
{ env = Just (addEntry flag "1" e)
, std_in = UseHandle nullh
, std_out = UseHandle loghandle
, std_err = UseHandle loghandle
}
exitWith =<< waitForProcess pid
exitcode <- withCreateProcess p $ \_ _ _ pid ->
waitForProcess pid
exitWith exitcode
, start (Utility.Daemon.foreground (Just pidfile)) $
case startbrowser of
Nothing -> Nothing

View file

@ -137,8 +137,8 @@ openFileBrowser = do
ifM (liftIO $ inPath cmd)
( do
let run = void $ liftIO $ forkIO $ do
(Nothing, Nothing, Nothing, pid) <- createProcess p
void $ waitForProcess pid
withCreateProcess p $ \_ _ _ pid -> void $
waitForProcess pid
run
#ifdef mingw32_HOST_OS
{- On windows, if the file browser is not

View file

@ -50,6 +50,7 @@ import Utility.Exception
import System.Exit
import System.IO
import System.Log.Logger
import Control.Monad.IO.Class
import Control.Concurrent
import qualified Control.Exception as E
import Control.Monad
@ -207,8 +208,10 @@ withOEHandles creator p a = creator p' $ a . oeHandles
, std_err = CreatePipe
}
withNullHandle :: (Handle -> IO a) -> IO a
withNullHandle = withFile devNull WriteMode
withNullHandle :: (MonadIO m, MonadMask m) => (Handle -> m a) -> m a
withNullHandle = bracket
(liftIO $ openFile devNull WriteMode)
(liftIO . hClose)
-- | Forces the CreateProcessRunner to run quietly;
-- both stdout and stderr are discarded.
@ -230,7 +233,7 @@ feedWithQuietOutput
-> CreateProcess
-> (Handle -> IO a)
-> IO a
feedWithQuietOutput creator p a = withFile devNull WriteMode $ \nullh -> do
feedWithQuietOutput creator p a = withNullHandle $ \nullh -> do
let p' = p
{ std_in = CreatePipe
, std_out = UseHandle nullh