diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2014-01-05 18:40:12 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2014-01-05 18:40:12 +0400 |
commit | b925e91c5e53a4300e5b0511e8811c5f1af9e803 (patch) | |
tree | a8ea60a53bc37d2287b67d269f962438030145e1 /src/System | |
parent | 5b122b1fab7f2de4942db6100a7363ccdc478a71 (diff) |
Add totalPieces function
Diffstat (limited to 'src/System')
-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 | ||