diff options
Diffstat (limited to 'src/System/Torrent/Storage.hs')
-rw-r--r-- | src/System/Torrent/Storage.hs | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/src/System/Torrent/Storage.hs b/src/System/Torrent/Storage.hs index 3bb229a4..8aa1aa99 100644 --- a/src/System/Torrent/Storage.hs +++ b/src/System/Torrent/Storage.hs | |||
@@ -96,11 +96,23 @@ isValidIx i s = 0 <= i && i < totalPieces s | |||
96 | 96 | ||
97 | writePiece :: Piece BL.ByteString -> Storage -> IO () | 97 | writePiece :: Piece BL.ByteString -> Storage -> IO () |
98 | writePiece p @ Piece {..} s @ Storage {..} | 98 | writePiece p @ Piece {..} s @ Storage {..} |
99 | | mode == ReadOnly = throwIO StorageIsRO | 99 | | mode == ReadOnly = throwIO StorageIsRO |
100 | | pieceSize p /= pieceLen = throwIO (InvalidSize (pieceSize p)) | 100 | | isNotValidIx pieceIndex = throwIO (InvalidIndex pieceIndex) |
101 | | not (isValidIx pieceIndex s) = throwIO (InvalidIndex pieceIndex) | 101 | | isNotValidSize pieceIndex (pieceSize p) |
102 | | otherwise = writeBytes offset pieceData fileMap | 102 | = throwIO (InvalidSize (pieceSize p)) |
103 | | otherwise = writeBytes offset pieceData fileMap | ||
103 | where | 104 | where |
105 | isNotValidSize pix psize | ||
106 | | succ pix == pcount = psize /= lastPieceLen -- last piece may be shorter | ||
107 | | otherwise = psize /= pieceLen | ||
108 | where | ||
109 | lastPieceLen = fromIntegral (FM.size fileMap `rem` fromIntegral pieceLen) | ||
110 | {-# INLINE isNotValidSize #-} | ||
111 | |||
112 | isNotValidIx i = i < 0 || i >= pcount | ||
113 | {-# INLINE isNotValidIx #-} | ||
114 | |||
115 | pcount = totalPieces s | ||
104 | offset = fromIntegral pieceIndex * fromIntegral pieceLen | 116 | offset = fromIntegral pieceIndex * fromIntegral pieceLen |
105 | 117 | ||
106 | readPiece :: PieceIx -> Storage -> IO (Piece BL.ByteString) | 118 | readPiece :: PieceIx -> Storage -> IO (Piece BL.ByteString) |