summaryrefslogtreecommitdiff
path: root/src/System/Torrent/Storage.hs
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2014-01-05 22:58:25 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2014-01-05 22:58:25 +0400
commit29036b62a5da2505862c904bc069c7b0b94129e4 (patch)
treee34734c822f525c2afbee720609ab5070146d2ac /src/System/Torrent/Storage.hs
parent8d194406c7d23e610bf5227f52ee8e04a555e85d (diff)
Implement getBitfield and genPieceInfo functions
Diffstat (limited to 'src/System/Torrent/Storage.hs')
-rw-r--r--src/System/Torrent/Storage.hs29
1 files changed, 24 insertions, 5 deletions
diff --git a/src/System/Torrent/Storage.hs b/src/System/Torrent/Storage.hs
index 8aa1aa99..a8b0bdc6 100644
--- a/src/System/Torrent/Storage.hs
+++ b/src/System/Torrent/Storage.hs
@@ -44,9 +44,12 @@ module System.Torrent.Storage
44 44
45import Control.Applicative 45import Control.Applicative
46import Control.Exception 46import Control.Exception
47import Control.Monad as M
47import Control.Monad.Trans 48import Control.Monad.Trans
48import Data.ByteString.Lazy as BL 49import Data.ByteString.Lazy as BL
49import Data.Conduit 50import Data.Conduit as C
51import Data.Conduit.Binary as C
52import Data.Conduit.List as C
50import Data.Typeable 53import Data.Typeable
51 54
52import Data.Torrent.Bitfield as BF 55import Data.Torrent.Bitfield as BF
@@ -156,10 +159,26 @@ sinkStorage s = do
156 awaitForever $ \ piece -> 159 awaitForever $ \ piece ->
157 liftIO $ writePiece piece s 160 liftIO $ writePiece piece s
158 161
159-- | TODO examples of use 162-- | This function can be used to generate 'InfoDict' from a set of
163-- opened files.
160genPieceInfo :: Storage -> IO PieceInfo 164genPieceInfo :: Storage -> IO PieceInfo
161genPieceInfo = undefined 165genPieceInfo s = do
166 hashes <- sourceStorage s $= C.map hashPiece $$ C.sinkLbs
167 return $ PieceInfo (pieceLen s) (HashList (BL.toStrict hashes))
162 168
163-- | TODO examples of use 169-- | Verify storage.
170--
171-- Throws 'InvalidSize' if piece info size do not match with storage
172-- piece size.
173--
164getBitfield :: Storage -> PieceInfo -> IO Bitfield 174getBitfield :: Storage -> PieceInfo -> IO Bitfield
165getBitfield = undefined \ No newline at end of file 175getBitfield s @ Storage {..} pinfo @ PieceInfo {..}
176 | pieceLen /= piPieceLength = throwIO (InvalidSize piPieceLength)
177 | otherwise = M.foldM verifyPiece (BF.haveNone total) [0..total - 1]
178 where
179 total = totalPieces s
180
181 verifyPiece :: Bitfield -> PieceIx -> IO Bitfield
182 verifyPiece bf pix = do
183 valid <- checkPieceLazy pinfo <$> readPiece pix s
184 return $ if valid then BF.insert pix bf else bf