]> git.rkrishnan.org Git - functorrent.git/blobdiff - src/FuncTorrent/Utils.hs
rename tracker response function, Utils, catch exceptions.
[functorrent.git] / src / FuncTorrent / Utils.hs
index b1db8d0defcbf9d6b1c9f46ec520448369b90b19..2a2797626527c8bdc7f3f983361bed31f127e129 100644 (file)
@@ -11,7 +11,7 @@ module FuncTorrent.Utils
 import Prelude hiding (writeFile, take)
 
 import qualified Crypto.Hash.SHA1 as SHA1 (hash)
-import Control.Monad (unless)
+import Control.Exception.Base (IOException, try)
 import Data.ByteString (ByteString, writeFile, hPut, hGet, take)
 import qualified Data.ByteString.Char8 as BC
 import System.IO (withFile, hSeek, IOMode(..), SeekMode(..))
@@ -26,11 +26,14 @@ splitNum n d | n == 0 = []
              | n < d = [n]
              | otherwise = d : splitNum (n - d) d
 
-createDummyFile :: FilePath -> Int -> IO ()
+createDummyFile :: FilePath -> Int -> IO (Either IOException ())
 createDummyFile path size = do
   dfe <- doesFileExist path
-  unless dfe $
-    writeFile path (BC.replicate size '\0')
+  if not dfe
+    then do
+    try $ writeFile path (BC.replicate size '\0')
+    else
+    return $ Right ()
 
 -- write into a file at a specific offet
 writeFileAtOffset :: FilePath -> Integer -> ByteString -> IO ()