From: Ramakrishnan Muthukrishnan Date: Fri, 27 Mar 2015 05:50:41 +0000 (+0530) Subject: error out if the input string is not a valid filepath X-Git-Url: https://git.rkrishnan.org/%5B/%5D%20/uri/htmlfontify-example.html?a=commitdiff_plain;h=fa56dc9388e22edc554414e875076ca1c4ad05a1;p=functorrent.git error out if the input string is not a valid filepath --- diff --git a/functorrent.cabal b/functorrent.cabal index fc4cfb3..b56110b 100644 --- a/functorrent.cabal +++ b/functorrent.cabal @@ -52,7 +52,8 @@ executable functorrent cryptohash, HTTP, network-uri, - parsec + parsec, + directory test-suite functorrent-test type: exitcode-stdio-1.0 diff --git a/src/Main.hs b/src/Main.hs index 17bd053..d081265 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -5,6 +5,7 @@ import Prelude hiding (length, readFile, writeFile) import Data.ByteString.Char8 (ByteString, readFile, writeFile, length) import System.Environment (getArgs) import System.Exit (exitSuccess) +import System.Directory (doesFileExist) import Text.ParserCombinators.Parsec (ParseError) import FuncTorrent.Bencode (decode, BVal(..)) @@ -27,7 +28,11 @@ usage = putStrLn "usage: functorrent torrent-file" parse :: [String] -> IO ByteString parse [] = usage >> exit -parse [a] = readFile a +parse [a] = do + fileExist <- doesFileExist a + if fileExist + then readFile a + else error "file does not exist" parse _ = exit main :: IO ()