summaryrefslogtreecommitdiff
path: root/src/System/Torrent/Storage.hs
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2014-01-05 18:40:12 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2014-01-05 18:40:12 +0400
commitb925e91c5e53a4300e5b0511e8811c5f1af9e803 (patch)
treea8ea60a53bc37d2287b67d269f962438030145e1 /src/System/Torrent/Storage.hs
parent5b122b1fab7f2de4942db6100a7363ccdc478a71 (diff)
Add totalPieces function
Diffstat (limited to 'src/System/Torrent/Storage.hs')
-rw-r--r--src/System/Torrent/Storage.hs14
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
43import Data.ByteString.Lazy as BL 43import Data.ByteString.Lazy as BL
44import Data.Typeable 44import Data.Typeable
45 45
46import Data.Torrent.Bitfield 46import Data.Torrent.Bitfield as BF
47import Data.Torrent.Layout 47import Data.Torrent.Layout
48import Data.Torrent.Piece 48import Data.Torrent.Piece
49import System.Torrent.FileMap as FM 49import System.Torrent.FileMap as FM
@@ -82,10 +82,11 @@ withStorage :: Mode -> PieceSize -> FileLayout FileSize
82 -> (Storage -> IO ()) -> IO () 82 -> (Storage -> IO ()) -> IO ()
83withStorage m s l = bracket (open m s l) close 83withStorage m s l = bracket (open m s l) close
84 84
85totalPieces :: Storage -> PieceCount
86totalPieces Storage {..} = FM.size fileMap `sizeInBase` pieceLen
87
85isValidIx :: PieceIx -> Storage -> Bool 88isValidIx :: PieceIx -> Storage -> Bool
86isValidIx i Storage {..} = 0 <= i && i < pcount 89isValidIx i s = 0 <= i && i < totalPieces s
87 where
88 pcount = FM.size fileMap `sizeInBase` pieceLen
89 90
90writePiece :: Piece BL.ByteString -> Storage -> IO () 91writePiece :: Piece BL.ByteString -> Storage -> IO ()
91writePiece p @ Piece {..} s @ Storage {..} 92writePiece 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--
108hintRead :: PieceIx -> Storage -> IO () 112hintRead :: PieceIx -> Storage -> IO ()
109hintRead _pix Storage {..} = return () 113hintRead _pix Storage {..} = return ()
110 114