From 94251de91e8900c06e01fe48760bd7746f1a5cff Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Wed, 20 Nov 2013 13:41:13 -0400 Subject: [PATCH] add readFileStrictAnyEncoding --- Assistant/WebApp/Control.hs | 6 +----- Command/ImportFeed.hs | 2 +- Utility/Misc.hs | 10 ++++++++++ 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/Assistant/WebApp/Control.hs b/Assistant/WebApp/Control.hs index 7684bc5b7d..268bdad222 100644 --- a/Assistant/WebApp/Control.hs +++ b/Assistant/WebApp/Control.hs @@ -67,9 +67,5 @@ getLogR :: Handler Html getLogR = page "Logs" Nothing $ do logfile <- liftAnnex $ fromRepo gitAnnexLogFile logs <- liftIO $ listLogs logfile - logcontent <- liftIO $ concat <$> mapM readlog logs + logcontent <- liftIO $ concat <$> mapM readFileStrictAnyEncoding logs $(widgetFile "control/log") - where - readlog f = withFile f ReadMode $ \h -> do - fileEncoding h -- log may contain invalid utf-8 - hClose h `after` hGetContentsStrict h diff --git a/Command/ImportFeed.hs b/Command/ImportFeed.hs index 7f54643c97..45a0d3b7ee 100644 --- a/Command/ImportFeed.hs +++ b/Command/ImportFeed.hs @@ -106,7 +106,7 @@ downloadFeed url = do liftIO $ withTmpFile "feed" $ \f h -> do fileEncoding h ifM (Url.download url [] [] f ua) - ( liftIO $ parseFeedString <$> hGetContentsStrict h + ( parseFeedString <$> hGetContentsStrict h , return Nothing ) diff --git a/Utility/Misc.hs b/Utility/Misc.hs index 4b0e9a1e44..68199c8283 100644 --- a/Utility/Misc.hs +++ b/Utility/Misc.hs @@ -21,6 +21,9 @@ import System.Posix.Process (getAnyProcessStatus) import Utility.Exception #endif +import Utility.FileSystemEncoding +import Utility.Monad + {- A version of hgetContents that is not lazy. Ensures file is - all read before it gets closed. -} hGetContentsStrict :: Handle -> IO String @@ -30,6 +33,13 @@ hGetContentsStrict = hGetContents >=> \s -> length s `seq` return s readFileStrict :: FilePath -> IO String readFileStrict = readFile >=> \s -> length s `seq` return s +{- Reads a file strictly, and using the FileSystemEncofing, so it will + - never crash on a badly encoded file. -} +readFileStrictAnyEncoding :: FilePath -> IO String +readFileStrictAnyEncoding f = withFile f ReadMode $ \h -> do + fileEncoding h + hClose h `after` hGetContentsStrict h + {- Like break, but the item matching the condition is not included - in the second result list. -