Skip to content

Instantly share code, notes, and snippets.

@fuzz
Last active July 31, 2019 19:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fuzz/2a4c0fdcad1413d992563ae86650252b to your computer and use it in GitHub Desktop.
Save fuzz/2a4c0fdcad1413d992563ae86650252b to your computer and use it in GitHub Desktop.
Test if a FilePath--not its referent if a symbolic link--exists
import Control.Exception
import System.Posix.Files (FileStatus, getSymbolicLinkStatus)
filePathExist :: FilePath -> IO Bool
filePathExist fp = do
x <- try (getSymbolicLinkStatus fp) :: IO (Either IOError FileStatus)
case x of
Left _ -> return False
Right _ -> return True
{-
Existence is tested from the perspective of the $USER
hence any error in obtaining file status returns False.
This allows safe handling of unreferenced symlinks--unreferenced
symlinks are not "broken" and should not be treated as
exceptions--but is insufficient to make determinations about
read/write access or potential error conditions.
Use other functions to check access, etc, or just handle
the exception from the next operation on the FilePath.
-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment