Ignore dangling symlinks in getDirectoryContents()
authorMisha Brukman <brukman+llvm@gmail.com>
Wed, 20 Apr 2005 04:04:07 +0000 (04:04 +0000)
committerMisha Brukman <brukman+llvm@gmail.com>
Wed, 20 Apr 2005 04:04:07 +0000 (04:04 +0000)
Thanks to Markus Oberhumer for the patch!

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@21370 91177308-0d34-0410-b5e6-96231b3b80d8

lib/System/Unix/Path.inc

index e37741bc2684113dacd3c8554d147a7aed1eb6f8..e55d9edcf2c8ebeebc5e7f25dd3d94f0143d8128 100644 (file)
@@ -398,17 +398,22 @@ Path::getDirectoryContents(std::set<Path>& result) const {
 
   result.clear();
   struct dirent* de = ::readdir(direntries);
-  while (de != 0) {
+  for ( ; de != 0; de = ::readdir(direntries)) {
     if (de->d_name[0] != '.') {
       Path aPath(path + (const char*)de->d_name);
       struct stat buf;
-      if (0 != stat(aPath.path.c_str(), &buf))
+      if (0 != stat(aPath.path.c_str(), &buf)) {
+        int saved_errno = errno;
+        struct stat st;
+        if (0 == lstat(aPath.path.c_str(), &st) && S_ISLNK(st.st_mode))
+          continue; // dangling symlink -- ignore
+        errno = saved_errno;
         ThrowErrno(aPath.path + ": can't get status");
+      }
       if (S_ISDIR(buf.st_mode))
         aPath.path += "/";
       result.insert(aPath);
     }
-    de = ::readdir(direntries);
   }
   
   closedir(direntries);