From b925e91c5e53a4300e5b0511e8811c5f1af9e803 Mon Sep 17 00:00:00 2001 From: Sam Truzjan Date: Sun, 5 Jan 2014 18:40:12 +0400 Subject: Add totalPieces function --- src/System/Torrent/Storage.hs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'src/System/Torrent/Storage.hs') 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 import Data.ByteString.Lazy as BL import Data.Typeable -import Data.Torrent.Bitfield +import Data.Torrent.Bitfield as BF import Data.Torrent.Layout import Data.Torrent.Piece import System.Torrent.FileMap as FM @@ -82,10 +82,11 @@ withStorage :: Mode -> PieceSize -> FileLayout FileSize -> (Storage -> IO ()) -> IO () withStorage m s l = bracket (open m s l) close +totalPieces :: Storage -> PieceCount +totalPieces Storage {..} = FM.size fileMap `sizeInBase` pieceLen + isValidIx :: PieceIx -> Storage -> Bool -isValidIx i Storage {..} = 0 <= i && i < pcount - where - pcount = FM.size fileMap `sizeInBase` pieceLen +isValidIx i s = 0 <= i && i < totalPieces s writePiece :: Piece BL.ByteString -> Storage -> IO () writePiece p @ Piece {..} s @ Storage {..} @@ -104,7 +105,10 @@ readPiece pix s @ Storage {..} offset = fromIntegral pix * fromIntegral pieceLen sz = fromIntegral pieceLen --- | Hint about the coming 'readPiece'. +-- | Hint about the coming 'readPiece'. Ignores invalid indexes, for e.g.: +-- +-- @forall s. hindRead (-1) s == return ()@ +-- hintRead :: PieceIx -> Storage -> IO () hintRead _pix Storage {..} = return () -- cgit v1.2.3