summaryrefslogtreecommitdiffhomepage
path: root/Plugins/MBox.hs
diff options
context:
space:
mode:
authorJose A Ortega Ruiz <jao@gnu.org>2010-02-14 18:56:47 +0100
committerJose A Ortega Ruiz <jao@gnu.org>2010-02-14 18:56:47 +0100
commit47a5cdddfb46734e5d3449863c9f124dce2f7fbd (patch)
tree513603d121796c2ed25e88e34f8df95c56933511 /Plugins/MBox.hs
parent75e97337a0f68caacbfa7ddadcf040c6f3f889df (diff)
downloadxmobar-47a5cdddfb46734e5d3449863c9f124dce2f7fbd.tar.gz
xmobar-47a5cdddfb46734e5d3449863c9f124dce2f7fbd.tar.bz2
Protect against IO failres in MBox
Ignore-this: cd42eea2c51ae7fa959319be6463da14 darcs-hash:20100214175647-748be-5b24058b89f149b5d4a454eea8ff5933469f9482.gz
Diffstat (limited to 'Plugins/MBox.hs')
-rw-r--r--Plugins/MBox.hs17
1 files changed, 9 insertions, 8 deletions
diff --git a/Plugins/MBox.hs b/Plugins/MBox.hs
index 276d50a..8ca0e76 100644
--- a/Plugins/MBox.hs
+++ b/Plugins/MBox.hs
@@ -15,11 +15,11 @@
module Plugins.MBox (MBox(..)) where
import Prelude hiding (catch)
-import System.IO
import Plugins
import Control.Monad
import Control.Concurrent.STM
+import Control.Exception (SomeException, handle, evaluate)
import System.Directory
import System.FilePath
@@ -45,9 +45,9 @@ instance Exec MBox where
ev = [Modify, Create]
i <- initINotify
- zipWithM_ (\f v -> addWatch i ev f (handle v)) fs vs
+ zipWithM_ (\f v -> addWatch i ev f (handleNotification v)) fs vs
- forM (zip fs vs) $ \(f, v) -> do
+ forM_ (zip fs vs) $ \(f, v) -> do
exists <- doesFileExist f
n <- if exists then countMails f else return 0
atomically $ writeTVar v (f, n)
@@ -61,12 +61,13 @@ showC m n c =
where msg = m ++ show n
countMails :: FilePath -> IO Int
-countMails f = do
- txt <- readFileSafe f
- return $ length . filter (isPrefixOf "From ") . lines $ txt
+countMails f =
+ handle ((\_ -> evaluate 0) :: SomeException -> IO Int)
+ (do txt <- readFileSafe f
+ evaluate $! length . filter (isPrefixOf "From ") . lines $ txt)
-handle :: TVar (FilePath, Int) -> Event -> IO ()
-handle v _ = do
+handleNotification :: TVar (FilePath, Int) -> Event -> IO ()
+handleNotification v _ = do
(p, _) <- atomically $ readTVar v
n <- countMails p
atomically $ writeTVar v (p, n)