From fa56dc9388e22edc554414e875076ca1c4ad05a1 Mon Sep 17 00:00:00 2001
From: Ramakrishnan Muthukrishnan <ram@rkrishnan.org>
Date: Fri, 27 Mar 2015 11:20:41 +0530
Subject: [PATCH] error out if the input string is not a valid filepath

---
 functorrent.cabal | 3 ++-
 src/Main.hs       | 7 ++++++-
 2 files changed, 8 insertions(+), 2 deletions(-)

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 ()
-- 
2.45.2