]> git.rkrishnan.org Git - functorrent.git/blobdiff - src/FuncTorrent/Utils.hs
new modules FileSystem and PieceManager
[functorrent.git] / src / FuncTorrent / Utils.hs
index 2a2797626527c8bdc7f3f983361bed31f127e129..50478323664a58ca2f6574479c04225201ef1770 100644 (file)
@@ -14,7 +14,7 @@ import qualified Crypto.Hash.SHA1 as SHA1 (hash)
 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(..))
+import System.IO (Handle, hSeek, SeekMode(..))
 import System.Directory (doesFileExist)
 
 splitN :: Int -> BC.ByteString -> [BC.ByteString]
@@ -36,16 +36,15 @@ createDummyFile path size = do
     return $ Right ()
 
 -- write into a file at a specific offet
-writeFileAtOffset :: FilePath -> Integer -> ByteString -> IO ()
-writeFileAtOffset path offset block =
-  withFile path ReadWriteMode (\h -> do
-                                  hSeek h AbsoluteSeek offset
-                                  hPut h block)
-readFileAtOffset :: FilePath -> Integer -> Integer -> IO ByteString
-readFileAtOffset path offset len =
-  withFile path ReadWriteMode (\h -> do
-                                  hSeek h AbsoluteSeek offset
-                                  hGet h (fromInteger len))
+writeFileAtOffset :: Handle -> Integer -> ByteString -> IO ()
+writeFileAtOffset h offset block = do
+  hSeek h AbsoluteSeek offset
+  hPut h block
+
+readFileAtOffset :: Handle -> Integer -> Integer -> IO ByteString
+readFileAtOffset h offset len = do
+  hSeek h AbsoluteSeek offset
+  hGet h (fromInteger len)
 
 verifyHash :: ByteString -> ByteString -> Bool
 verifyHash bs pieceHash =