From c91af8d1f85f876eb7119ce8406385cf570d3886 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Benencia?= Date: Tue, 3 Sep 2013 15:39:46 -0300 Subject: Fixed file descriptor leak --- Handlers.hs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'Handlers.hs') diff --git a/Handlers.hs b/Handlers.hs index 644d643..da886b3 100644 --- a/Handlers.hs +++ b/Handlers.hs @@ -13,6 +13,7 @@ import Control.Exception(evaluate) import Control.Monad.State import Data.List(intercalate, stripPrefix, sort) import System.FilePath(FilePath, takeFileName, dropTrailingPathSeparator) +import System.IO(openFile, IOMode(..), hClose) import System.Locale(rfc822DateFormat) import Data.DateTime(parseDateTime, startOfTime, formatDateTime) import qualified System.IO.UTF8 as UTF8 @@ -45,6 +46,7 @@ changeMode IndexMode = do changeMode MaildirMode = do st <- get unsortedEmails <- liftIO $ do + freeOldHandlers st let md = (selectedMD . maildirState) $ st emails <- getMaildirEmails md mapM toEmail emails @@ -60,11 +62,14 @@ changeMode MaildirMode = do where toEmail fp = do - msg <- readFile fp + handle <- openFile fp ReadMode + msg <- UTF8.hGetContents handle let value = parseMIMEMessage msg let headers = mime_val_headers value let date = maybe startOfTime id $ parseDateTime rfc822DateFormat $ takeWhile (/= '(') $ lookupField "date" headers - return (Email value date fp) + return (Email value date fp handle) + +freeOldHandlers st = mapM (hClose . emailHandle) $ selectedEmails . indexState $ st {- Boilerplate code -} incSelectedRow IndexMode = do -- cgit v1.2.3