+{-
+ - Copyright (C) 2015-2016 Ramakrishnan Muthukrishnan <ram@rkrishnan.org>
+ -
+ - This file is part of FuncTorrent.
+ -
+ - FuncTorrent is free software; you can redistribute it and/or modify
+ - it under the terms of the GNU General Public License as published by
+ - the Free Software Foundation; either version 3 of the License, or
+ - (at your option) any later version.
+ -
+ - FuncTorrent is distributed in the hope that it will be useful,
+ - but WITHOUT ANY WARRANTY; without even the implied warranty of
+ - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ - GNU General Public License for more details.
+ -
+ - You should have received a copy of the GNU General Public License
+ - along with FuncTorrent; if not, see <http://www.gnu.org/licenses/>
+ -}
+
module FuncTorrent.Utils
(createDummyFile,
writeFileAtOffset,
import Prelude hiding (writeFile, take)
import qualified Crypto.Hash.SHA1 as SHA1 (hash)
+import Control.Exception.Base (IOException, try)
import Data.ByteString (ByteString, writeFile, hPut, hGet, take)
import qualified Data.ByteString.Char8 as BC
-import System.IO (withFile, hSeek, IOMode(..), SeekMode(..))
+import System.IO (Handle, hSeek, SeekMode(..))
import System.Directory (doesFileExist)
splitN :: Int -> BC.ByteString -> [BC.ByteString]
| n < d = [n]
| otherwise = d : splitNum (n - d) d
-createDummyFile :: FilePath -> Int -> IO ()
+createDummyFile :: FilePath -> Int -> IO (Either IOException ())
createDummyFile path size = do
dfe <- doesFileExist path
- if dfe
- then return ()
+ if not dfe
+ then do
+ try $ writeFile path (BC.replicate size '\0')
else
- writeFile path (BC.replicate size '\0')
+ return $ Right ()
-- write into a file at a specific offet
-writeFileAtOffset :: FilePath -> Integer -> ByteString -> IO ()
-writeFileAtOffset path offset block =
- withFile path ReadWriteMode (\h -> do
- hSeek h AbsoluteSeek offset
- hPut h block)
-readFileAtOffset :: FilePath -> Integer -> Integer -> IO ByteString
-readFileAtOffset path offset len =
- withFile path ReadWriteMode (\h -> do
- hSeek h AbsoluteSeek offset
- hGet h (fromInteger len))
+writeFileAtOffset :: Handle -> Integer -> ByteString -> IO ()
+writeFileAtOffset h offset block = do
+ hSeek h AbsoluteSeek offset
+ hPut h block
+
+readFileAtOffset :: Handle -> Integer -> Integer -> IO ByteString
+readFileAtOffset h offset len = do
+ hSeek h AbsoluteSeek offset
+ hGet h (fromInteger len)
verifyHash :: ByteString -> ByteString -> Bool
verifyHash bs pieceHash =