From 40c4dccba2b180faa3857fddb0a2b8968785fa90 Mon Sep 17 00:00:00 2001
From: Ramakrishnan Muthukrishnan <ram@rkrishnan.org>
Date: Sat, 25 Jul 2015 06:59:20 +0530
Subject: [PATCH] bugfix: use the previous piece length times number of pieces
 to calculate offset

---
 src/FuncTorrent/Peer.hs | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/src/FuncTorrent/Peer.hs b/src/FuncTorrent/Peer.hs
index 0bce055..9984a2a 100644
--- a/src/FuncTorrent/Peer.hs
+++ b/src/FuncTorrent/Peer.hs
@@ -220,7 +220,8 @@ msgLoop pState pieceStatus | meInterested pState == False &&
                                    then
                                    putStrLn $ "Hash mismatch: " ++ show (hash (pieceStatus ! workPiece)) ++ " vs " ++ show (take 20 (SHA1.hash pBS))
                                    else do
-                                   writeFileAtOffset "/tmp/download.file" (workPiece * pLen) pBS
+                                   let fileOffset = if workPiece == 0 then 0 else (len (pieceStatus ! (workPiece - 1)))
+                                   writeFileAtOffset "/tmp/download.file" fileOffset pBS
                                    msgLoop pState (adjust (\pieceData -> pieceData { state = Have }) workPiece pieceStatus)
                           | otherwise = do
                               msg <- getMsg (handle pState)
-- 
2.45.2