From 968ccbf7652c3718fca087d3c2610cd56f6cf2b4 Mon Sep 17 00:00:00 2001
From: Ramakrishnan Muthukrishnan <ram@rkrishnan.org>
Date: Sat, 25 Jul 2015 12:12:03 +0530
Subject: [PATCH] bugfix: calculate proper file 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 845c14a..baf9ce8 100644
--- a/src/FuncTorrent/Peer.hs
+++ b/src/FuncTorrent/Peer.hs
@@ -218,7 +218,8 @@ msgLoop pState pieceStatus | not (meInterested pState) && heChoking pState = do
                                    then
                                    putStrLn $ "Hash mismatch: " ++ show (hash (pieceStatus ! workPiece)) ++ " vs " ++ show (take 20 (SHA1.hash pBS))
                                    else do
-                                   let fileOffset = if workPiece == 0 then 0 else (len (pieceStatus ! (workPiece - 1)))
+                                   let fileOffset = if workPiece == 0 then 0 else workPiece * len (pieceStatus ! (workPiece - 1))
+                                   putStrLn $ "Write into file at offset: " ++ show fileOffset
                                    writeFileAtOffset "/tmp/download.file" fileOffset pBS
                                    msgLoop pState (adjust (\pieceData -> pieceData { state = Have }) workPiece pieceStatus)
                           | otherwise = do
-- 
2.45.2