aboutsummaryrefslogtreecommitdiff
path: root/Handlers.hs
diff options
context:
space:
mode:
authorRaúl Benencia <rul@kalgan.cc>2013-09-03 15:39:46 -0300
committerRaúl Benencia <rul@kalgan.cc>2013-09-03 15:39:46 -0300
commitc91af8d1f85f876eb7119ce8406385cf570d3886 (patch)
treeed8fd1e3ed0c33cb07bc79862dfed4424b3eb4af /Handlers.hs
parentde8bde7be0a8ab99dbbf5cdbf6e5fc7fc582da01 (diff)
Fixed file descriptor leak
Diffstat (limited to 'Handlers.hs')
-rw-r--r--Handlers.hs9
1 files changed, 7 insertions, 2 deletions
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
nihil fit ex nihilo