1 module FuncTorrent.Utils
10 import Prelude hiding (writeFile)
12 import System.IO (withFile, hSeek, IOMode(..), SeekMode(..))
13 import System.Directory (doesFileExist)
14 import Data.ByteString (ByteString, writeFile, hPut, hGet)
15 import qualified Data.ByteString.Char8 as BC
16 import qualified Data.ByteString.Char8 as BC (replicate)
18 splitN :: Int -> BC.ByteString -> [BC.ByteString]
19 splitN n bs | BC.null bs = []
20 | otherwise = BC.take n bs : splitN n (BC.drop n bs)
22 splitNum :: Integer -> Integer -> [Integer]
23 splitNum n d | n == 0 = []
25 | otherwise = d : splitNum (n - d) d
27 createDummyFile :: FilePath -> Int -> IO ()
28 createDummyFile path size = do
29 dfe <- doesFileExist path
33 writeFile path (BC.replicate size '\0')
35 -- write into a file at a specific offet
36 writeFileAtOffset :: FilePath -> Integer -> ByteString -> IO ()
37 writeFileAtOffset path offset block =
38 withFile path ReadWriteMode (\h -> do
39 hSeek h AbsoluteSeek offset
41 readFileAtOffset :: FilePath -> Integer -> Integer -> IO ByteString
42 readFileAtOffset path offset len =
43 withFile path ReadWriteMode (\h -> do
44 hSeek h AbsoluteSeek offset
45 hGet h (fromInteger len))