summaryrefslogtreecommitdiff
path: root/src/System
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2014-01-05 20:19:01 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2014-01-05 20:19:01 +0400
commitf1d79ea2180e46545f442985a85e5bb1cce90436 (patch)
tree1fc753798ee3fc228c60b94c286a337b4972eed9 /src/System
parent3eaa3003d9ece714595dffbf07202ee90336d05f (diff)
Check size of last piece properly (in writePiece)
Diffstat (limited to 'src/System')
-rw-r--r--src/System/Torrent/Storage.hs20
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
97writePiece :: Piece BL.ByteString -> Storage -> IO () 97writePiece :: Piece BL.ByteString -> Storage -> IO ()
98writePiece p @ Piece {..} s @ Storage {..} 98writePiece 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
106readPiece :: PieceIx -> Storage -> IO (Piece BL.ByteString) 118readPiece :: PieceIx -> Storage -> IO (Piece BL.ByteString)