From: Ramakrishnan Muthukrishnan Date: Sun, 26 Jul 2015 06:38:53 +0000 (+0530) Subject: refactor file operations into Fileops module X-Git-Url: https://git.rkrishnan.org/vdrive/components/com_hotproperty/configuration.txt?a=commitdiff_plain;h=5b606c972c65d9b7df3e415c2cfc0f0c3585e71c;p=functorrent.git refactor file operations into Fileops module --- diff --git a/src/FuncTorrent/Fileops.hs b/src/FuncTorrent/Fileops.hs new file mode 100644 index 0000000..f1bbefe --- /dev/null +++ b/src/FuncTorrent/Fileops.hs @@ -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) diff --git a/src/FuncTorrent/Peer.hs b/src/FuncTorrent/Peer.hs index baf9ce8..f7ccfcf 100644 --- a/src/FuncTorrent/Peer.hs +++ b/src/FuncTorrent/Peer.hs @@ -4,10 +4,10 @@ module FuncTorrent.Peer 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(..)) @@ -23,6 +23,7 @@ import qualified Crypto.Hash.SHA1 as SHA1 (hash) import FuncTorrent.Metainfo (Info(..), Metainfo(..)) import FuncTorrent.Utils (splitN, splitNum) +import FuncTorrent.Fileops (createDummyFile, writeFileAtOffset) type ID = String type IP = String @@ -185,17 +186,6 @@ bitfieldToList bs = go bs 0 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