dangling symbolic links

I think I've exhausted my options without catching exceptions. If I have an invalid symbolic link, how can I identify that it exists? (Sorry about the line wrap.) tmp$ ls -l # no tricks up my sleeve, empty directory tmp$ touch foo tmp$ ln -s foo bar tmp$ ls -l total 8 lrwxr-xr-x 1 nfrisby nfrisby 3 Aug 27 23:29 bar -> foo -rw-r--r-- 1 nfrisby nfrisby 0 Aug 27 23:29 foo tmp$ ghc -e 'System.Directory.doesFileExist "bar"' True tmp$ rm foo tmp$ ls -l total 8 lrwxr-xr-x 1 nfrisby nfrisby 3 Aug 27 23:29 bar -> foo tmp$ ghc -e 'System.Directory.doesFileExist "bar"' # it follows the broken link False tmp$ ghc -e 'System.Posix.Files.fileExist "bar"' # the POSIX API also follows False tmp$ ghc -e 'System.Posix.Files.isSymbolicLink `fmap` System.Posix.Files.getFileStatus "bar"' # so does this one POSIX API also follows *** Exception: bar: getFileStatus: does not exist (No such file or directory) tmp$ ghc -e 'System.Posix.Files.isSymbolicLink `fmap` System.Posix.Files.getSymbolicLinkStatus "bar"' # the most successful so far True tmp$ rm bar # but it isn't an existence check... tmp$ ls -l tmp$ ghc -e 'System.Posix.Files.isSymbolicLink `fmap` System.Posix.Files.getSymbolicLinkStatus "bar"' *** Exception: bar: getSymbolicLinkStatus: does not exist (No such file or directory) Is there a way to check for the existence of a symbolic link without testing if getSymbolicLinkStatus raises an exception? This is with Darwin Kernel Version 8.11.1, GHC 6.8.2, directory-1.0.0.0, and unix-2.3.0.0. Thanks.

Ah the magic of using a mailing list... I just realized that using
getDirectoryContents lists the entry.
Still, a "doesLinkExist" function might be nice...
On Wed, Aug 27, 2008 at 11:46 PM, Nicolas Frisby
I think I've exhausted my options without catching exceptions.
If I have an invalid symbolic link, how can I identify that it exists?
(Sorry about the line wrap.)
tmp$ ls -l # no tricks up my sleeve, empty directory tmp$ touch foo tmp$ ln -s foo bar tmp$ ls -l total 8 lrwxr-xr-x 1 nfrisby nfrisby 3 Aug 27 23:29 bar -> foo -rw-r--r-- 1 nfrisby nfrisby 0 Aug 27 23:29 foo tmp$ ghc -e 'System.Directory.doesFileExist "bar"' True tmp$ rm foo tmp$ ls -l total 8 lrwxr-xr-x 1 nfrisby nfrisby 3 Aug 27 23:29 bar -> foo tmp$ ghc -e 'System.Directory.doesFileExist "bar"' # it follows the broken link False tmp$ ghc -e 'System.Posix.Files.fileExist "bar"' # the POSIX API also follows False tmp$ ghc -e 'System.Posix.Files.isSymbolicLink `fmap` System.Posix.Files.getFileStatus "bar"' # so does this one POSIX API also follows *** Exception: bar: getFileStatus: does not exist (No such file or directory) tmp$ ghc -e 'System.Posix.Files.isSymbolicLink `fmap` System.Posix.Files.getSymbolicLinkStatus "bar"' # the most successful so far True tmp$ rm bar # but it isn't an existence check... tmp$ ls -l tmp$ ghc -e 'System.Posix.Files.isSymbolicLink `fmap` System.Posix.Files.getSymbolicLinkStatus "bar"' *** Exception: bar: getSymbolicLinkStatus: does not exist (No such file or directory)
Is there a way to check for the existence of a symbolic link without testing if getSymbolicLinkStatus raises an exception?
This is with Darwin Kernel Version 8.11.1, GHC 6.8.2, directory-1.0.0.0, and unix-2.3.0.0.
Thanks.

On 2008 Aug 28, at 0:46, Nicolas Frisby wrote:
I think I've exhausted my options without catching exceptions.
If I have an invalid symbolic link, how can I identify that it exists?
getSymbolicLinkStatus vs. getFileStatus? The former will return if the link exists, the latter if its target does. (Where not returning means throwing an exception in IO.) -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH
participants (2)
-
Brandon S. Allbery KF8NH
-
Nicolas Frisby