diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2014-01-05 22:58:25 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2014-01-05 22:58:25 +0400 |
commit | 29036b62a5da2505862c904bc069c7b0b94129e4 (patch) | |
tree | e34734c822f525c2afbee720609ab5070146d2ac /src/System/Torrent | |
parent | 8d194406c7d23e610bf5227f52ee8e04a555e85d (diff) |
Implement getBitfield and genPieceInfo functions
Diffstat (limited to 'src/System/Torrent')
-rw-r--r-- | src/System/Torrent/Storage.hs | 29 |
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 | ||
45 | import Control.Applicative | 45 | import Control.Applicative |
46 | import Control.Exception | 46 | import Control.Exception |
47 | import Control.Monad as M | ||
47 | import Control.Monad.Trans | 48 | import Control.Monad.Trans |
48 | import Data.ByteString.Lazy as BL | 49 | import Data.ByteString.Lazy as BL |
49 | import Data.Conduit | 50 | import Data.Conduit as C |
51 | import Data.Conduit.Binary as C | ||
52 | import Data.Conduit.List as C | ||
50 | import Data.Typeable | 53 | import Data.Typeable |
51 | 54 | ||
52 | import Data.Torrent.Bitfield as BF | 55 | import 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. | ||
160 | genPieceInfo :: Storage -> IO PieceInfo | 164 | genPieceInfo :: Storage -> IO PieceInfo |
161 | genPieceInfo = undefined | 165 | genPieceInfo 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 | -- | ||
164 | getBitfield :: Storage -> PieceInfo -> IO Bitfield | 174 | getBitfield :: Storage -> PieceInfo -> IO Bitfield |
165 | getBitfield = undefined \ No newline at end of file | 175 | getBitfield 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 | ||