1module Network.HTTP2.Arch.File where
2
3import System.IO
4
5import Imports
6import Network.HPACK
7
8-- | Offset for file.
9type FileOffset = Int64
10-- | How many bytes to read
11type ByteCount = Int64
12
13-- | Position read for files.
14type PositionRead = FileOffset -> ByteCount -> Buffer -> IO ByteCount
15
16-- | Manipulating a file resource.
17data Sentinel =
18    -- | Closing a file resource. Its refresher is automatiaclly generated by
19    --   the internal timer.
20    Closer (IO ())
21    -- | Refreshing a file resource while reading.
22    --   Closing the file must be done by its own timer or something.
23  | Refresher (IO ())
24
25-- | Making a position read and its closer.
26type PositionReadMaker = FilePath -> IO (PositionRead, Sentinel)
27-- | Position read based on 'Handle'.
28defaultPositionReadMaker :: PositionReadMaker
29defaultPositionReadMaker file = do
30    hdl <- openBinaryFile file ReadMode
31    return (pread hdl, Closer $ hClose hdl)
32  where
33    pread :: Handle -> PositionRead
34    pread hdl off bytes buf = do
35        hSeek hdl AbsoluteSeek $ fromIntegral off
36        fromIntegral <$> hGetBufSome hdl buf (fromIntegral bytes)
37