]> git.rkrishnan.org Git - functorrent.git/commitdiff
Fileops merged into Utils module
authorRamakrishnan Muthukrishnan <ram@rkrishnan.org>
Wed, 16 Sep 2015 15:59:23 +0000 (21:29 +0530)
committerRamakrishnan Muthukrishnan <ram@rkrishnan.org>
Wed, 16 Sep 2015 15:59:23 +0000 (21:29 +0530)
functorrent.cabal
src/FuncTorrent/Fileops.hs [deleted file]
src/FuncTorrent/Peer.hs
src/FuncTorrent/Utils.hs

index b9374728331df159fb8ee57c061ab1040c02dce8..66bbb164f952c3ff6bd2819c470ed959184f3a81 100644 (file)
@@ -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 (file)
index cab73c7..0000000
+++ /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))
index 2b9ff5ffd2264d26c311407d697ed4a0f7e6418e..13c07e3a8e2ab8824670a6af416c824321924e05 100644 (file)
@@ -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
index cf6c2844f2745152b91926c272fab1c664bfabea..de2a4561c36f070c76841f944f0d134616943d19 100644 (file)
@@ -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))