summaryrefslogtreecommitdiff
path: root/src/System
diff options
context:
space:
mode:
Diffstat (limited to 'src/System')
-rw-r--r--src/System/Torrent/Storage.hs11
1 files changed, 9 insertions, 2 deletions
diff --git a/src/System/Torrent/Storage.hs b/src/System/Torrent/Storage.hs
index bf44d7bf..214998a9 100644
--- a/src/System/Torrent/Storage.hs
+++ b/src/System/Torrent/Storage.hs
@@ -34,6 +34,7 @@ module System.Torrent.Storage
34 -- * Modification 34 -- * Modification
35 , writePiece 35 , writePiece
36 , readPiece 36 , readPiece
37 , hintRead
37 , unsafeReadPiece 38 , unsafeReadPiece
38 ) where 39 ) where
39 40
@@ -45,7 +46,7 @@ import Data.Typeable
45import Data.Torrent.Bitfield 46import Data.Torrent.Bitfield
46import Data.Torrent.Layout 47import Data.Torrent.Layout
47import Data.Torrent.Piece 48import Data.Torrent.Piece
48import System.Torrent.FileMap 49import System.Torrent.FileMap as FM
49 50
50 51
51data StorageFailure 52data StorageFailure
@@ -82,7 +83,9 @@ withStorage :: Mode -> PieceSize -> FileLayout FileSize
82withStorage m s l = bracket (open m s l) close 83withStorage m s l = bracket (open m s l) close
83 84
84isValidIx :: PieceIx -> Storage -> Bool 85isValidIx :: PieceIx -> Storage -> Bool
85isValidIx i s = 0 <= i && i < undefined s 86isValidIx i Storage {..} = 0 <= i && i < pcount
87 where
88 pcount = FM.size fileMap `sizeInBase` pieceLen
86 89
87writePiece :: Piece BL.ByteString -> Storage -> IO () 90writePiece :: Piece BL.ByteString -> Storage -> IO ()
88writePiece p @ Piece {..} s @ Storage {..} 91writePiece p @ Piece {..} s @ Storage {..}
@@ -101,6 +104,10 @@ readPiece pix s @ Storage {..}
101 offset = fromIntegral pix * fromIntegral pieceLen 104 offset = fromIntegral pix * fromIntegral pieceLen
102 sz = fromIntegral pieceLen 105 sz = fromIntegral pieceLen
103 106
107-- | Hint about the coming 'readPiece'.
108hintRead :: PieceIx -> Storage -> IO ()
109hintRead _pix Storage {..} = return ()
110
104unsafeReadPiece :: PieceIx -> Storage -> IO (Piece BL.ByteString) 111unsafeReadPiece :: PieceIx -> Storage -> IO (Piece BL.ByteString)
105unsafeReadPiece pix s @ Storage {..} 112unsafeReadPiece pix s @ Storage {..}
106 | not (isValidIx pix s) = throwIO (InvalidIndex pix) 113 | not (isValidIx pix s) = throwIO (InvalidIndex pix)