From ebc045715374d3418a0c1466f6ae95252603899c Mon Sep 17 00:00:00 2001 From: Ramakrishnan Muthukrishnan Date: Fri, 18 Sep 2015 18:40:41 +0530 Subject: [PATCH] fix hlint suggestions --- src/FuncTorrent/Peer.hs | 28 ++++++++++++++-------------- src/FuncTorrent/Tracker.hs | 2 +- src/FuncTorrent/Utils.hs | 5 ++--- 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/src/FuncTorrent/Peer.hs b/src/FuncTorrent/Peer.hs index e4a5349..6131a6b 100644 --- a/src/FuncTorrent/Peer.hs +++ b/src/FuncTorrent/Peer.hs @@ -60,18 +60,18 @@ initPieceMap pieceHash fileLen pieceLen = fromList kvs , len = pLen }) | (i, h, pLen) <- zip3 [0..numPieces] hashes pLengths] hashes = splitN 20 pieceHash - pLengths = (splitNum fileLen pieceLen) + pLengths = splitNum fileLen pieceLen pieceMapFromFile :: FilePath -> PieceMap -> IO PieceMap -pieceMapFromFile filePath pieceMap = do +pieceMapFromFile filePath pieceMap = traverseWithKey f pieceMap - where - f k v = do - let offset = if k == 0 then 0 else k * len (pieceMap ! (k - 1)) - isHashValid <- (flip verifyHash) (hash v) <$> (readFileAtOffset filePath offset (len v)) - if isHashValid - then return $ v { dlstate = Have } - else return $ v + where + f k v = do + let offset = if k == 0 then 0 else k * len (pieceMap ! (k - 1)) + isHashValid <- flip verifyHash (hash v) <$> readFileAtOffset filePath offset (len v) + if isHashValid + then return $ v { dlstate = Have } + else return v havePiece :: PieceMap -> Integer -> Bool havePiece pm index = @@ -93,13 +93,13 @@ doHandshake True h peer infoHash peerid = do return () doHandshake False h peer infoHash peerid = do let hs = genHandshakeMsg infoHash peerid - putStrLn $ "waiting for a handshake" + putStrLn "waiting for a handshake" hsMsg <- hGet h (length (unpack hs)) putStrLn $ "<-- handshake from peer: " ++ show peer let rxInfoHash = take 20 $ drop 28 hsMsg if rxInfoHash /= infoHash then do - putStrLn $ "infoHashes does not match" + putStrLn "infoHashes does not match" hClose h return () else do @@ -175,7 +175,7 @@ msgLoop pieceStatus file = do pBS <- liftIO $ downloadPiece h workPiece pLen if not $ verifyHash pBS (hash (pieceStatus ! workPiece)) then - liftIO $ putStrLn $ "Hash mismatch" + liftIO $ putStrLn "Hash mismatch" else do let fileOffset = if workPiece == 0 then 0 else workPiece * len (pieceStatus ! (workPiece - 1)) liftIO $ putStrLn $ "Write into file at offset: " ++ show fileOffset @@ -210,9 +210,9 @@ msgLoop pieceStatus file = do NotInterestedMsg -> do modify (\st -> st {heInterested = False}) msgLoop pieceStatus file - CancelMsg _ _ _ -> do -- check if valid index, begin, length + CancelMsg _ _ _ -> -- check if valid index, begin, length msgLoop pieceStatus file - PortMsg _ -> do + PortMsg _ -> msgLoop pieceStatus file -- handle RequestMsg, HaveMsg. No need to handle PieceMsg here. -- also BitFieldMsg diff --git a/src/FuncTorrent/Tracker.hs b/src/FuncTorrent/Tracker.hs index ce63080..ab14b09 100644 --- a/src/FuncTorrent/Tracker.hs +++ b/src/FuncTorrent/Tracker.hs @@ -65,7 +65,7 @@ mkTrackerResponse resp = -- | Connect to a tracker and get peer info tracker :: PortNumber -> String -> Metainfo -> IO ByteString -tracker port peer_id m = do +tracker port peer_id m = get (head . announceList $ m) $ mkArgs port peer_id m getTrackerResponse :: PortNumber -> String -> Metainfo -> IO (Either ByteString TrackerResponse) diff --git a/src/FuncTorrent/Utils.hs b/src/FuncTorrent/Utils.hs index 48ebe67..b1db8d0 100644 --- a/src/FuncTorrent/Utils.hs +++ b/src/FuncTorrent/Utils.hs @@ -11,6 +11,7 @@ module FuncTorrent.Utils import Prelude hiding (writeFile, take) import qualified Crypto.Hash.SHA1 as SHA1 (hash) +import Control.Monad (unless) import Data.ByteString (ByteString, writeFile, hPut, hGet, take) import qualified Data.ByteString.Char8 as BC import System.IO (withFile, hSeek, IOMode(..), SeekMode(..)) @@ -28,9 +29,7 @@ splitNum n d | n == 0 = [] createDummyFile :: FilePath -> Int -> IO () createDummyFile path size = do dfe <- doesFileExist path - if dfe - then return () - else + unless dfe $ writeFile path (BC.replicate size '\0') -- write into a file at a specific offet -- 2.37.2