[Haskell-cafe] dangling symbolic links

Nicolas Frisby nicolas.frisby at gmail.com
Thu Aug 28 00:46:02 EDT 2008


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.


More information about the Haskell-Cafe mailing list