Last active
July 31, 2019 19:33
-
-
Save fuzz/2a4c0fdcad1413d992563ae86650252b to your computer and use it in GitHub Desktop.
Test if a FilePath--not its referent if a symbolic link--exists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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