]> git.rkrishnan.org Git - functorrent.git/commitdiff
refactor file operations into Fileops module
authorRamakrishnan Muthukrishnan <ram@rkrishnan.org>
Sun, 26 Jul 2015 06:38:53 +0000 (12:08 +0530)
committerRamakrishnan Muthukrishnan <ram@rkrishnan.org>
Sun, 26 Jul 2015 06:38:53 +0000 (12:08 +0530)
src/FuncTorrent/Fileops.hs [new file with mode: 0644]
src/FuncTorrent/Peer.hs

diff --git a/src/FuncTorrent/Fileops.hs b/src/FuncTorrent/Fileops.hs
new file mode 100644 (file)
index 0000000..f1bbefe
--- /dev/null
@@ -0,0 +1,21 @@
+module FuncTorrent.Fileops
+       (createDummyFile,
+        writeFileAtOffset
+       ) where
+
+import Prelude hiding (writeFile)
+
+import System.IO (withFile, hSeek, IOMode(..), SeekMode(..))
+import Data.ByteString (ByteString, writeFile, hPut)
+import qualified Data.ByteString.Char8 as BC (replicate)
+
+createDummyFile :: FilePath -> Int -> IO ()
+createDummyFile path size =
+  writeFile path (BC.replicate size '\0')
+
+-- 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)
index baf9ce8d7fce2f381bd26d8446647e0bd859b726..f7ccfcfcee4889bb36c674eff015b9cbc664dd9a 100644 (file)
@@ -4,10 +4,10 @@ module FuncTorrent.Peer
      handlePeerMsgs
     ) where
 
      handlePeerMsgs
     ) where
 
-import Prelude hiding (lookup, concat, replicate, splitAt, writeFile, take)
+import Prelude hiding (lookup, concat, replicate, splitAt, take)
 
 
-import System.IO (Handle, BufferMode(..), IOMode(..), SeekMode(..), withFile, hSeek, hSetBuffering)
-import Data.ByteString (ByteString, pack, unpack, concat, hGet, hPut, singleton, writeFile, take, empty)
+import System.IO (Handle, BufferMode(..), hSetBuffering)
+import Data.ByteString (ByteString, pack, unpack, concat, hGet, hPut, singleton, take, empty)
 import Data.ByteString.Lazy (fromStrict, fromChunks, toStrict)
 import qualified Data.ByteString.Char8 as BC (replicate, pack, length)
 import Network (connectTo, PortID(..))
 import Data.ByteString.Lazy (fromStrict, fromChunks, toStrict)
 import qualified Data.ByteString.Char8 as BC (replicate, pack, length)
 import Network (connectTo, PortID(..))
@@ -23,6 +23,7 @@ import qualified Crypto.Hash.SHA1 as SHA1 (hash)
 
 import FuncTorrent.Metainfo (Info(..), Metainfo(..))
 import FuncTorrent.Utils (splitN, splitNum)
 
 import FuncTorrent.Metainfo (Info(..), Metainfo(..))
 import FuncTorrent.Utils (splitN, splitNum)
+import FuncTorrent.Fileops (createDummyFile, writeFileAtOffset)
 
 type ID = String
 type IP = String
 
 type ID = String
 type IP = String
@@ -185,17 +186,6 @@ bitfieldToList bs = go bs 0
           in
            setBits ++ go bs' (pos + 1)
 
           in
            setBits ++ go bs' (pos + 1)
 
-createDummyFile :: FilePath -> Int -> IO ()
-createDummyFile path size =
-  writeFile path (BC.replicate size '\0')
-
--- 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)
-
 -- recvMsg :: Peer -> Handle -> Msg
 msgLoop :: PeerState -> PieceMap -> IO ()
 msgLoop pState pieceStatus | not (meInterested pState) && heChoking pState = do
 -- recvMsg :: Peer -> Handle -> Msg
 msgLoop :: PeerState -> PieceMap -> IO ()
 msgLoop pState pieceStatus | not (meInterested pState) && heChoking pState = do