summaryrefslogtreecommitdiff
path: root/src/System/Torrent
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2014-01-06 00:01:49 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2014-01-06 00:01:49 +0400
commita467181b6aa21b1e41d56e7772d481cbf0c37f39 (patch)
tree2f0ad59b193b1f790d38d94110dc9b83a6864901 /src/System/Torrent
parent29036b62a5da2505862c904bc069c7b0b94129e4 (diff)
Add check command to mktorrent utility
Diffstat (limited to 'src/System/Torrent')
-rw-r--r--src/System/Torrent/Storage.hs14
1 files changed, 10 insertions, 4 deletions
diff --git a/src/System/Torrent/Storage.hs b/src/System/Torrent/Storage.hs
index a8b0bdc6..b5092b2e 100644
--- a/src/System/Torrent/Storage.hs
+++ b/src/System/Torrent/Storage.hs
@@ -28,6 +28,8 @@ module System.Torrent.Storage
28 , withStorage 28 , withStorage
29 29
30 -- * Query 30 -- * Query
31 , totalPieces
32 , verifyPiece
31 , genPieceInfo 33 , genPieceInfo
32 , getBitfield 34 , getBitfield
33 35
@@ -166,6 +168,10 @@ genPieceInfo s = do
166 hashes <- sourceStorage s $= C.map hashPiece $$ C.sinkLbs 168 hashes <- sourceStorage s $= C.map hashPiece $$ C.sinkLbs
167 return $ PieceInfo (pieceLen s) (HashList (BL.toStrict hashes)) 169 return $ PieceInfo (pieceLen s) (HashList (BL.toStrict hashes))
168 170
171-- | Verify specific piece using infodict hash list.
172verifyPiece :: Storage -> PieceInfo -> PieceIx -> IO Bool
173verifyPiece s pinfo pix = checkPieceLazy pinfo <$> readPiece pix s
174
169-- | Verify storage. 175-- | Verify storage.
170-- 176--
171-- Throws 'InvalidSize' if piece info size do not match with storage 177-- Throws 'InvalidSize' if piece info size do not match with storage
@@ -174,11 +180,11 @@ genPieceInfo s = do
174getBitfield :: Storage -> PieceInfo -> IO Bitfield 180getBitfield :: Storage -> PieceInfo -> IO Bitfield
175getBitfield s @ Storage {..} pinfo @ PieceInfo {..} 181getBitfield s @ Storage {..} pinfo @ PieceInfo {..}
176 | pieceLen /= piPieceLength = throwIO (InvalidSize piPieceLength) 182 | pieceLen /= piPieceLength = throwIO (InvalidSize piPieceLength)
177 | otherwise = M.foldM verifyPiece (BF.haveNone total) [0..total - 1] 183 | otherwise = M.foldM checkPiece (BF.haveNone total) [0..total - 1]
178 where 184 where
179 total = totalPieces s 185 total = totalPieces s
180 186
181 verifyPiece :: Bitfield -> PieceIx -> IO Bitfield 187 checkPiece :: Bitfield -> PieceIx -> IO Bitfield
182 verifyPiece bf pix = do 188 checkPiece bf pix = do
183 valid <- checkPieceLazy pinfo <$> readPiece pix s 189 valid <- verifyPiece s pinfo pix
184 return $ if valid then BF.insert pix bf else bf 190 return $ if valid then BF.insert pix bf else bf