diff options
Diffstat (limited to 'src/System')
-rw-r--r-- | src/System/Torrent/Storage.hs | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/src/System/Torrent/Storage.hs b/src/System/Torrent/Storage.hs index bf44d7bf..214998a9 100644 --- a/src/System/Torrent/Storage.hs +++ b/src/System/Torrent/Storage.hs | |||
@@ -34,6 +34,7 @@ module System.Torrent.Storage | |||
34 | -- * Modification | 34 | -- * Modification |
35 | , writePiece | 35 | , writePiece |
36 | , readPiece | 36 | , readPiece |
37 | , hintRead | ||
37 | , unsafeReadPiece | 38 | , unsafeReadPiece |
38 | ) where | 39 | ) where |
39 | 40 | ||
@@ -45,7 +46,7 @@ import Data.Typeable | |||
45 | import Data.Torrent.Bitfield | 46 | import Data.Torrent.Bitfield |
46 | import Data.Torrent.Layout | 47 | import Data.Torrent.Layout |
47 | import Data.Torrent.Piece | 48 | import Data.Torrent.Piece |
48 | import System.Torrent.FileMap | 49 | import System.Torrent.FileMap as FM |
49 | 50 | ||
50 | 51 | ||
51 | data StorageFailure | 52 | data StorageFailure |
@@ -82,7 +83,9 @@ withStorage :: Mode -> PieceSize -> FileLayout FileSize | |||
82 | withStorage m s l = bracket (open m s l) close | 83 | withStorage m s l = bracket (open m s l) close |
83 | 84 | ||
84 | isValidIx :: PieceIx -> Storage -> Bool | 85 | isValidIx :: PieceIx -> Storage -> Bool |
85 | isValidIx i s = 0 <= i && i < undefined s | 86 | isValidIx i Storage {..} = 0 <= i && i < pcount |
87 | where | ||
88 | pcount = FM.size fileMap `sizeInBase` pieceLen | ||
86 | 89 | ||
87 | writePiece :: Piece BL.ByteString -> Storage -> IO () | 90 | writePiece :: Piece BL.ByteString -> Storage -> IO () |
88 | writePiece p @ Piece {..} s @ Storage {..} | 91 | writePiece p @ Piece {..} s @ Storage {..} |
@@ -101,6 +104,10 @@ readPiece pix s @ Storage {..} | |||
101 | offset = fromIntegral pix * fromIntegral pieceLen | 104 | offset = fromIntegral pix * fromIntegral pieceLen |
102 | sz = fromIntegral pieceLen | 105 | sz = fromIntegral pieceLen |
103 | 106 | ||
107 | -- | Hint about the coming 'readPiece'. | ||
108 | hintRead :: PieceIx -> Storage -> IO () | ||
109 | hintRead _pix Storage {..} = return () | ||
110 | |||
104 | unsafeReadPiece :: PieceIx -> Storage -> IO (Piece BL.ByteString) | 111 | unsafeReadPiece :: PieceIx -> Storage -> IO (Piece BL.ByteString) |
105 | unsafeReadPiece pix s @ Storage {..} | 112 | unsafeReadPiece pix s @ Storage {..} |
106 | | not (isValidIx pix s) = throwIO (InvalidIndex pix) | 113 | | not (isValidIx pix s) = throwIO (InvalidIndex pix) |