]> git.rkrishnan.org Git - functorrent.git/commitdiff
fix hlint suggestions
authorRamakrishnan Muthukrishnan <ram@rkrishnan.org>
Fri, 18 Sep 2015 13:10:41 +0000 (18:40 +0530)
committerRamakrishnan Muthukrishnan <ram@rkrishnan.org>
Fri, 18 Sep 2015 13:10:41 +0000 (18:40 +0530)
src/FuncTorrent/Peer.hs
src/FuncTorrent/Tracker.hs
src/FuncTorrent/Utils.hs

index e4a53495d0949ddda016beaa175914abd79c1a41..6131a6bb58750d6cf2548c9680b81b822ef5f5ab 100644 (file)
@@ -60,18 +60,18 @@ initPieceMap pieceHash fileLen pieceLen = fromList kvs
                          , len = pLen })
           | (i, h, pLen) <- zip3 [0..numPieces] hashes pLengths]
     hashes = splitN 20 pieceHash
                          , 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 -> IO PieceMap
-pieceMapFromFile filePath pieceMap = do
+pieceMapFromFile filePath pieceMap =
   traverseWithKey f 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 =
 
 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
   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
   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
     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
           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
             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
         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
           msgLoop pieceStatus file
-        PortMsg _ -> do
+        PortMsg _ ->
           msgLoop pieceStatus file
         -- handle RequestMsg, HaveMsg. No need to handle PieceMsg here.
         -- also BitFieldMsg
           msgLoop pieceStatus file
         -- handle RequestMsg, HaveMsg. No need to handle PieceMsg here.
         -- also BitFieldMsg
index ce630808ee8517a672eb0d91c04f4211159c789d..ab14b09e31920bb4da579e2a2c1a43d1693b80e9 100644 (file)
@@ -65,7 +65,7 @@ mkTrackerResponse resp =
 
 -- | Connect to a tracker and get peer info
 tracker :: PortNumber -> String -> Metainfo -> IO ByteString
 
 -- | 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)
   get (head . announceList $ m) $ mkArgs port peer_id m
 
 getTrackerResponse :: PortNumber -> String -> Metainfo -> IO (Either ByteString TrackerResponse)
index 48ebe67710d81258b9600c874dec3de414d52cf5..b1db8d0defcbf9d6b1c9f46ec520448369b90b19 100644 (file)
@@ -11,6 +11,7 @@ module FuncTorrent.Utils
 import Prelude hiding (writeFile, take)
 
 import qualified Crypto.Hash.SHA1 as SHA1 (hash)
 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(..))
 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
 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
     writeFile path (BC.replicate size '\0')
 
 -- write into a file at a specific offet