]> git.rkrishnan.org Git - functorrent.git/blob - src/FuncTorrent/Fileops.hs
Resume download from where it was left off last time
[functorrent.git] / src / FuncTorrent / Fileops.hs
1 module FuncTorrent.Fileops
2        (createDummyFile,
3         writeFileAtOffset,
4         readFileAtOffset
5        ) where
6
7 import Prelude hiding (writeFile)
8
9 import System.IO (withFile, hSeek, IOMode(..), SeekMode(..))
10 import System.Directory (doesFileExist)
11 import Data.ByteString (ByteString, writeFile, hPut, hGet)
12 import qualified Data.ByteString.Char8 as BC (replicate)
13
14 createDummyFile :: FilePath -> Int -> IO ()
15 createDummyFile path size = do
16   dfe <- doesFileExist path
17   if dfe
18     then return ()
19     else
20     writeFile path (BC.replicate size '\0')
21
22 -- write into a file at a specific offet
23 writeFileAtOffset :: FilePath -> Integer -> ByteString -> IO ()
24 writeFileAtOffset path offset block =
25   withFile path ReadWriteMode (\h -> do
26                                   hSeek h AbsoluteSeek offset
27                                   hPut h block)
28 readFileAtOffset :: FilePath -> Integer -> Integer -> IO ByteString
29 readFileAtOffset path offset len =
30   withFile path ReadWriteMode (\h -> do
31                                   hSeek h AbsoluteSeek offset
32                                   hGet h (fromInteger len))