From aed979c1918b479dabac7ff0403a20868679c4c2 Mon Sep 17 00:00:00 2001 From: Ramakrishnan Muthukrishnan Date: Wed, 16 Sep 2015 21:29:23 +0530 Subject: [PATCH] Fileops merged into Utils module --- functorrent.cabal | 1 - src/FuncTorrent/Fileops.hs | 32 -------------------------------- src/FuncTorrent/Peer.hs | 3 +-- src/FuncTorrent/Utils.hs | 35 ++++++++++++++++++++++++++++++++++- 4 files changed, 35 insertions(+), 36 deletions(-) delete mode 100644 src/FuncTorrent/Fileops.hs diff --git a/functorrent.cabal b/functorrent.cabal index b937472..66bbb16 100644 --- a/functorrent.cabal +++ b/functorrent.cabal @@ -17,7 +17,6 @@ cabal-version: >=1.18 library exposed-modules: FuncTorrent.Bencode, - FuncTorrent.Fileops, FuncTorrent.Logger, FuncTorrent.Metainfo, FuncTorrent.Network diff --git a/src/FuncTorrent/Fileops.hs b/src/FuncTorrent/Fileops.hs deleted file mode 100644 index cab73c7..0000000 --- a/src/FuncTorrent/Fileops.hs +++ /dev/null @@ -1,32 +0,0 @@ -module FuncTorrent.Fileops - (createDummyFile, - writeFileAtOffset, - readFileAtOffset - ) where - -import Prelude hiding (writeFile) - -import System.IO (withFile, hSeek, IOMode(..), SeekMode(..)) -import System.Directory (doesFileExist) -import Data.ByteString (ByteString, writeFile, hPut, hGet) -import qualified Data.ByteString.Char8 as BC (replicate) - -createDummyFile :: FilePath -> Int -> IO () -createDummyFile path size = do - dfe <- doesFileExist path - if dfe - then return () - else - 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) -readFileAtOffset :: FilePath -> Integer -> Integer -> IO ByteString -readFileAtOffset path offset len = - withFile path ReadWriteMode (\h -> do - hSeek h AbsoluteSeek offset - hGet h (fromInteger len)) diff --git a/src/FuncTorrent/Peer.hs b/src/FuncTorrent/Peer.hs index 2b9ff5f..13c07e3 100644 --- a/src/FuncTorrent/Peer.hs +++ b/src/FuncTorrent/Peer.hs @@ -20,8 +20,7 @@ import qualified Crypto.Hash.SHA1 as SHA1 (hash) import Safe (headMay) import FuncTorrent.Metainfo (Info(..), Metainfo(..)) -import FuncTorrent.Utils (splitN, splitNum) -import FuncTorrent.Fileops (createDummyFile, writeFileAtOffset, readFileAtOffset) +import FuncTorrent.Utils (splitN, splitNum, createDummyFile, writeFileAtOffset, readFileAtOffset) import FuncTorrent.PeerMsgs (Peer(..), PeerMsg(..), sendMsg, getMsg, genHandshakeMsg) data PState = PState { handle :: Handle diff --git a/src/FuncTorrent/Utils.hs b/src/FuncTorrent/Utils.hs index cf6c284..de2a456 100644 --- a/src/FuncTorrent/Utils.hs +++ b/src/FuncTorrent/Utils.hs @@ -1,6 +1,19 @@ -module FuncTorrent.Utils where +module FuncTorrent.Utils + (createDummyFile, + writeFileAtOffset, + readFileAtOffset, + splitNum, + splitN + ) + where +import Prelude hiding (writeFile) + +import System.IO (withFile, hSeek, IOMode(..), SeekMode(..)) +import System.Directory (doesFileExist) +import Data.ByteString (ByteString, writeFile, hPut, hGet) import qualified Data.ByteString.Char8 as BC +import qualified Data.ByteString.Char8 as BC (replicate) splitN :: Int -> BC.ByteString -> [BC.ByteString] splitN n bs | BC.null bs = [] @@ -10,3 +23,23 @@ splitNum :: Integer -> Integer -> [Integer] splitNum n d | n == 0 = [] | n < d = [n] | otherwise = d : splitNum (n - d) d + +createDummyFile :: FilePath -> Int -> IO () +createDummyFile path size = do + dfe <- doesFileExist path + if dfe + then return () + else + 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) +readFileAtOffset :: FilePath -> Integer -> Integer -> IO ByteString +readFileAtOffset path offset len = + withFile path ReadWriteMode (\h -> do + hSeek h AbsoluteSeek offset + hGet h (fromInteger len)) -- 2.37.2