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

View file

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

View file

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