flip catchDefaultIO

This commit is contained in:
Joey Hess 2012-09-17 00:18:07 -04:00
parent ba744c84a4
commit e8188ea611
18 changed files with 29 additions and 26 deletions

View file

@ -322,7 +322,7 @@ needUpdateIndex :: Git.Ref -> Annex Bool
needUpdateIndex branchref = do needUpdateIndex branchref = do
lock <- fromRepo gitAnnexIndexLock lock <- fromRepo gitAnnexIndexLock
lockref <- Git.Ref . firstLine <$> lockref <- Git.Ref . firstLine <$>
liftIO (catchDefaultIO (readFileStrict lock) "") liftIO (catchDefaultIO "" $ readFileStrict lock)
return (lockref /= branchref) return (lockref /= branchref)
{- Record that the branch's index has been updated to correspond to a {- Record that the branch's index has been updated to correspond to a

View file

@ -280,7 +280,7 @@ getKeysPresent :: Annex [Key]
getKeysPresent = liftIO . traverse (2 :: Int) =<< fromRepo gitAnnexObjectDir getKeysPresent = liftIO . traverse (2 :: Int) =<< fromRepo gitAnnexObjectDir
where where
traverse depth dir = do traverse depth dir = do
contents <- catchDefaultIO (dirContents dir) [] contents <- catchDefaultIO [] (dirContents dir)
if depth == 0 if depth == 0
then continue (mapMaybe (fileKey . takeFileName) contents) [] then continue (mapMaybe (fileKey . takeFileName) contents) []
else do else do

View file

@ -47,8 +47,8 @@ getJournalledFiles = map fileJournal <$> getJournalFiles
getJournalFiles :: Annex [FilePath] getJournalFiles :: Annex [FilePath]
getJournalFiles = do getJournalFiles = do
g <- gitRepo g <- gitRepo
fs <- liftIO $ fs <- liftIO $ catchDefaultIO [] $
catchDefaultIO (getDirectoryContents $ gitAnnexJournalDir g) [] getDirectoryContents $ gitAnnexJournalDir g
return $ filter (`notElem` [".", ".."]) fs return $ filter (`notElem` [".", ".."]) fs
{- Checks if there are changes in the journal. -} {- Checks if there are changes in the journal. -}

View file

@ -71,7 +71,7 @@ sshCleanup :: Annex ()
sshCleanup = do sshCleanup = do
dir <- fromRepo gitAnnexSshDir dir <- fromRepo gitAnnexSshDir
sockets <- filter (not . isLock) <$> sockets <- filter (not . isLock) <$>
liftIO (catchDefaultIO (dirContents dir) []) liftIO (catchDefaultIO [] $ dirContents dir)
forM_ sockets cleanup forM_ sockets cleanup
where where
cleanup socketfile = do cleanup socketfile = do

View file

@ -108,7 +108,7 @@ startDaemonStatus :: Annex DaemonStatusHandle
startDaemonStatus = do startDaemonStatus = do
file <- fromRepo gitAnnexDaemonStatusFile file <- fromRepo gitAnnexDaemonStatusFile
status <- liftIO $ status <- liftIO $
catchDefaultIO (readDaemonStatusFile file) =<< newDaemonStatus flip catchDefaultIO (readDaemonStatusFile file) =<< newDaemonStatus
transfers <- M.fromList <$> getTransfers transfers <- M.fromList <$> getTransfers
remotes <- calcKnownRemotes remotes <- calcKnownRemotes
liftIO $ atomically $ newTMVar status liftIO $ atomically $ newTMVar status

View file

@ -78,7 +78,8 @@ bestHostName :: PairMsg -> IO HostName
bestHostName msg = case remoteHostName $ pairMsgData msg of bestHostName msg = case remoteHostName $ pairMsgData msg of
Just h -> do Just h -> do
let localname = h ++ ".local" let localname = h ++ ".local"
addrs <- catchDefaultIO (getAddrInfo Nothing (Just localname) Nothing) [] addrs <- catchDefaultIO [] $
getAddrInfo Nothing (Just localname) Nothing
maybe fallback (const $ return localname) (headMaybe addrs) maybe fallback (const $ return localname) (headMaybe addrs)
Nothing -> fallback Nothing -> fallback
where where
@ -88,4 +89,5 @@ bestHostName msg = case remoteHostName $ pairMsgData msg of
IPv4Addr addr -> SockAddrInet (PortNum 0) addr IPv4Addr addr -> SockAddrInet (PortNum 0) addr
IPv6Addr addr -> SockAddrInet6 (PortNum 0) 0 addr 0 IPv6Addr addr -> SockAddrInet6 (PortNum 0) 0 addr 0
fromMaybe (showAddr a) fromMaybe (showAddr a)
<$> catchDefaultIO (fst <$> getNameInfo [] True False sockaddr) Nothing <$> catchDefaultIO Nothing
(fst <$> getNameInfo [] True False sockaddr)

View file

@ -49,7 +49,7 @@ isRoot = do
inDestDir :: FilePath -> IO FilePath inDestDir :: FilePath -> IO FilePath
inDestDir f = do inDestDir f = do
destdir <- catchDefaultIO (getEnv "DESTDIR") "" destdir <- catchDefaultIO "" (getEnv "DESTDIR")
return $ destdir </> f return $ destdir </> f
writeFDODesktop :: FilePath -> IO () writeFDODesktop :: FilePath -> IO ()

View file

@ -79,7 +79,7 @@ pipeNullSplit params repo =
reap :: IO () reap :: IO ()
reap = do reap = do
-- throws an exception when there are no child processes -- throws an exception when there are no child processes
catchDefaultIO (getAnyProcessStatus False True) Nothing catchDefaultIO Nothing (getAnyProcessStatus False True)
>>= maybe noop (const reap) >>= maybe noop (const reap)
{- Runs a git command as a coprocess. -} {- Runs a git command as a coprocess. -}

View file

@ -224,7 +224,7 @@ checkForRepo dir =
<&&> doesDirectoryExist (dir </> "objects") <&&> doesDirectoryExist (dir </> "objects")
gitDirFile = do gitDirFile = do
c <- firstLine <$> c <- firstLine <$>
catchDefaultIO (readFile $ dir </> ".git") "" catchDefaultIO "" (readFile $ dir </> ".git")
return $ if gitdirprefix `isPrefixOf` c return $ if gitdirprefix `isPrefixOf` c
then Just $ Local then Just $ Local
{ gitdir = drop (length gitdirprefix) c { gitdir = drop (length gitdirprefix) c

View file

@ -132,5 +132,5 @@ checkNotReadOnly cmd
checkEnv :: String -> IO () checkEnv :: String -> IO ()
checkEnv var = checkEnv var =
whenM (not . null <$> catchDefaultIO (getEnv var) "") $ whenM (not . null <$> catchDefaultIO "" (getEnv var)) $
error $ "Action blocked by " ++ var error $ "Action blocked by " ++ var

View file

@ -28,4 +28,4 @@ programFile = userConfigFile "program"
readProgramFile :: IO FilePath readProgramFile :: IO FilePath
readProgramFile = do readProgramFile = do
programfile <- programFile programfile <- programFile
catchDefaultIO (readFile programfile) "git-annex" catchDefaultIO "git-annex" $ readFile programfile

View file

@ -134,9 +134,8 @@ checkTransfer t = do
liftIO $ closeFd fd liftIO $ closeFd fd
case locked of case locked of
Nothing -> return Nothing Nothing -> return Nothing
Just (pid, _) -> liftIO $ Just (pid, _) -> liftIO $ catchDefaultIO Nothing $
flip catchDefaultIO Nothing $ readTransferInfoFile (Just pid) tfile
readTransferInfoFile (Just pid) tfile
{- Gets all currently running transfers. -} {- Gets all currently running transfers. -}
getTransfers :: Annex [(Transfer, TransferInfo)] getTransfers :: Annex [(Transfer, TransferInfo)]

View file

@ -180,7 +180,8 @@ writeLog1 :: FilePath -> [LogLine] -> IO ()
writeLog1 file ls = viaTmp writeFile file (showLog ls) writeLog1 file ls = viaTmp writeFile file (showLog ls)
readLog1 :: FilePath -> IO [LogLine] readLog1 :: FilePath -> IO [LogLine]
readLog1 file = catchDefaultIO (parseLog <$> readFileStrict file) [] readLog1 file = catchDefaultIO [] $
parseLog <$> readFileStrict file
lookupFile1 :: FilePath -> Annex (Maybe (Key, Backend)) lookupFile1 :: FilePath -> Annex (Maybe (Key, Backend))
lookupFile1 file = do lookupFile1 file = do

View file

@ -71,7 +71,7 @@ locationLogs = do
files <- mapM tryDirContents (concat levelb) files <- mapM tryDirContents (concat levelb)
return $ mapMaybe islogfile (concat files) return $ mapMaybe islogfile (concat files)
where where
tryDirContents d = catchDefaultIO (dirContents d) [] tryDirContents d = catchDefaultIO [] $ dirContents d
islogfile f = maybe Nothing (\k -> Just (k, f)) $ islogfile f = maybe Nothing (\k -> Just (k, f)) $
logFileKey $ takeFileName f logFileKey $ takeFileName f

View file

@ -43,7 +43,7 @@ dirContentsRecursive topdir = dirContentsRecursive' topdir [""]
dirContentsRecursive' :: FilePath -> [FilePath] -> IO [FilePath] dirContentsRecursive' :: FilePath -> [FilePath] -> IO [FilePath]
dirContentsRecursive' _ [] = return [] dirContentsRecursive' _ [] = return []
dirContentsRecursive' topdir (dir:dirs) = unsafeInterleaveIO $ do dirContentsRecursive' topdir (dir:dirs) = unsafeInterleaveIO $ do
(files, dirs') <- collect [] [] =<< catchDefaultIO (dirContents (topdir </> dir)) [] (files, dirs') <- collect [] [] =<< catchDefaultIO [] (dirContents (topdir </> dir))
files' <- dirContentsRecursive' topdir (dirs' ++ dirs) files' <- dirContentsRecursive' topdir (dirs' ++ dirs)
return (files ++ files') return (files ++ files')
where where

View file

@ -13,15 +13,15 @@ import Control.Applicative
{- Catches IO errors and returns a Bool -} {- Catches IO errors and returns a Bool -}
catchBoolIO :: IO Bool -> IO Bool catchBoolIO :: IO Bool -> IO Bool
catchBoolIO a = catchDefaultIO a False catchBoolIO a = catchDefaultIO False a
{- Catches IO errors and returns a Maybe -} {- Catches IO errors and returns a Maybe -}
catchMaybeIO :: IO a -> IO (Maybe a) catchMaybeIO :: IO a -> IO (Maybe a)
catchMaybeIO a = catchDefaultIO (Just <$> a) Nothing catchMaybeIO a = catchDefaultIO Nothing $ Just <$> a
{- Catches IO errors and returns a default value. -} {- Catches IO errors and returns a default value. -}
catchDefaultIO :: IO a -> a -> IO a catchDefaultIO :: a -> IO a -> IO a
catchDefaultIO a def = catchIO a (const $ return def) catchDefaultIO def a = catchIO a (const $ return def)
{- Catches IO errors and returns the error message. -} {- Catches IO errors and returns the error message. -}
catchMsgIO :: IO a -> IO (Either String a) catchMsgIO :: IO a -> IO (Either String a)

View file

@ -123,4 +123,5 @@ userDesktopDir = maybe fallback return =<< (parse <$> xdg_user_dir)
xdgEnvHome :: String -> String -> IO String xdgEnvHome :: String -> String -> IO String
xdgEnvHome envbase homedef = do xdgEnvHome envbase homedef = do
home <- myHomeDir home <- myHomeDir
catchDefaultIO (getEnv $ "XDG_" ++ envbase) (home </> homedef) catchDefaultIO (home </> homedef) $
getEnv $ "XDG_" ++ envbase

View file

@ -34,7 +34,7 @@ withTempFile :: Template -> (FilePath -> Handle -> IO a) -> IO a
withTempFile template a = bracket create remove use withTempFile template a = bracket create remove use
where where
create = do create = do
tmpdir <- catchDefaultIO getTemporaryDirectory "." tmpdir <- catchDefaultIO "." getTemporaryDirectory
openTempFile tmpdir template openTempFile tmpdir template
remove (name, handle) = do remove (name, handle) = do
hClose handle hClose handle
@ -48,7 +48,7 @@ withTempDir template = bracket create remove
where where
remove = removeDirectoryRecursive remove = removeDirectoryRecursive
create = do create = do
tmpdir <- catchDefaultIO getTemporaryDirectory "." tmpdir <- catchDefaultIO "." getTemporaryDirectory
createDirectoryIfMissing True tmpdir createDirectoryIfMissing True tmpdir
pid <- getProcessID pid <- getProcessID
makedir tmpdir (template ++ show pid) (0 :: Int) makedir tmpdir (template ++ show pid) (0 :: Int)