diff options
Diffstat (limited to 'src/System/Torrent/Storage.hs')
-rw-r--r-- | src/System/Torrent/Storage.hs | 14 |
1 files changed, 9 insertions, 5 deletions
diff --git a/src/System/Torrent/Storage.hs b/src/System/Torrent/Storage.hs index 214998a9..1c84bf69 100644 --- a/src/System/Torrent/Storage.hs +++ b/src/System/Torrent/Storage.hs | |||
@@ -43,7 +43,7 @@ import Control.Exception | |||
43 | import Data.ByteString.Lazy as BL | 43 | import Data.ByteString.Lazy as BL |
44 | import Data.Typeable | 44 | import Data.Typeable |
45 | 45 | ||
46 | import Data.Torrent.Bitfield | 46 | import Data.Torrent.Bitfield as BF |
47 | import Data.Torrent.Layout | 47 | import Data.Torrent.Layout |
48 | import Data.Torrent.Piece | 48 | import Data.Torrent.Piece |
49 | import System.Torrent.FileMap as FM | 49 | import System.Torrent.FileMap as FM |
@@ -82,10 +82,11 @@ withStorage :: Mode -> PieceSize -> FileLayout FileSize | |||
82 | -> (Storage -> IO ()) -> IO () | 82 | -> (Storage -> IO ()) -> IO () |
83 | withStorage m s l = bracket (open m s l) close | 83 | withStorage m s l = bracket (open m s l) close |
84 | 84 | ||
85 | totalPieces :: Storage -> PieceCount | ||
86 | totalPieces Storage {..} = FM.size fileMap `sizeInBase` pieceLen | ||
87 | |||
85 | isValidIx :: PieceIx -> Storage -> Bool | 88 | isValidIx :: PieceIx -> Storage -> Bool |
86 | isValidIx i Storage {..} = 0 <= i && i < pcount | 89 | isValidIx i s = 0 <= i && i < totalPieces s |
87 | where | ||
88 | pcount = FM.size fileMap `sizeInBase` pieceLen | ||
89 | 90 | ||
90 | writePiece :: Piece BL.ByteString -> Storage -> IO () | 91 | writePiece :: Piece BL.ByteString -> Storage -> IO () |
91 | writePiece p @ Piece {..} s @ Storage {..} | 92 | writePiece p @ Piece {..} s @ Storage {..} |
@@ -104,7 +105,10 @@ readPiece pix s @ Storage {..} | |||
104 | offset = fromIntegral pix * fromIntegral pieceLen | 105 | offset = fromIntegral pix * fromIntegral pieceLen |
105 | sz = fromIntegral pieceLen | 106 | sz = fromIntegral pieceLen |
106 | 107 | ||
107 | -- | Hint about the coming 'readPiece'. | 108 | -- | Hint about the coming 'readPiece'. Ignores invalid indexes, for e.g.: |
109 | -- | ||
110 | -- @forall s. hindRead (-1) s == return ()@ | ||
111 | -- | ||
108 | hintRead :: PieceIx -> Storage -> IO () | 112 | hintRead :: PieceIx -> Storage -> IO () |
109 | hintRead _pix Storage {..} = return () | 113 | hintRead _pix Storage {..} = return () |
110 | 114 | ||