[llvm-commits] [llvm] r122879 - in /llvm/trunk: lib/Support/Unix/PathV2.inc unittests/Support/Path.cpp

Michael J. Spencer bigcheesegs at gmail.com
Wed Jan 5 08:38:57 PST 2011


Author: mspencer
Date: Wed Jan  5 10:38:57 2011
New Revision: 122879

URL: http://llvm.org/viewvc/llvm-project?rev=122879&view=rev
Log:
Support/PathV2: Implement directory iteration on POSIX.

Modified:
    llvm/trunk/lib/Support/Unix/PathV2.inc
    llvm/trunk/unittests/Support/Path.cpp

Modified: llvm/trunk/lib/Support/Unix/PathV2.inc
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Unix/PathV2.inc?rev=122879&r1=122878&r2=122879&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Unix/PathV2.inc (original)
+++ llvm/trunk/lib/Support/Unix/PathV2.inc Wed Jan  5 10:38:57 2011
@@ -23,6 +23,22 @@
 #if HAVE_FCNTL_H
 #include <fcntl.h>
 #endif
+#if HAVE_DIRENT_H
+# include <dirent.h>
+# define NAMLEN(dirent) strlen((dirent)->d_name)
+#else
+# define dirent direct
+# define NAMLEN(dirent) (dirent)->d_namlen
+# if HAVE_SYS_NDIR_H
+#  include <sys/ndir.h>
+# endif
+# if HAVE_SYS_DIR_H
+#  include <sys/dir.h>
+# endif
+# if HAVE_NDIR_H
+#  include <ndir.h>
+# endif
+#endif
 #if HAVE_STDIO_H
 #include <stdio.h>
 #endif
@@ -408,6 +424,44 @@
   return success;
 }
 
+error_code directory_iterator_construct(directory_iterator &it, StringRef path){
+  SmallString<128> path_null(path);
+  DIR *directory = ::opendir(path_null.c_str());
+  if (directory == 0)
+    return error_code(errno, system_category());
+
+  it.IterationHandle = reinterpret_cast<intptr_t>(directory);
+  // Add something for replace_filename to replace.
+  path::append(path_null, ".");
+  it.CurrentEntry = directory_entry(path_null.str());
+  return directory_iterator_increment(it);
+}
+
+error_code directory_iterator_destruct(directory_iterator& it) {
+  if (it.IterationHandle)
+    ::closedir(reinterpret_cast<DIR *>(it.IterationHandle));
+  it.IterationHandle = 0;
+  it.CurrentEntry = directory_entry();
+  return success;
+}
+
+error_code directory_iterator_increment(directory_iterator& it) {
+  errno = 0;
+  dirent *cur_dir = ::readdir(reinterpret_cast<DIR *>(it.IterationHandle));
+  if (cur_dir == 0 && errno != 0) {
+    return error_code(errno, system_category());
+  } else if (cur_dir != 0) {
+    StringRef name(cur_dir->d_name, NAMLEN(cur_dir));
+    if ((name.size() == 1 && name[0] == '.') ||
+        (name.size() == 2 && name[0] == '.' && name[1] == '.'))
+      return directory_iterator_increment(it);
+    it.CurrentEntry.replace_filename(name);
+  } else
+    return directory_iterator_destruct(it);
+
+  return success;
+}
+
 } // end namespace fs
 } // end namespace sys
 } // end namespace llvm

Modified: llvm/trunk/unittests/Support/Path.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/Path.cpp?rev=122879&r1=122878&r2=122879&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/Path.cpp (original)
+++ llvm/trunk/unittests/Support/Path.cpp Wed Jan  5 10:38:57 2011
@@ -176,8 +176,6 @@
   ASSERT_NO_ERROR(fs::exists(Twine(TempPath), TempFileExists));
   EXPECT_FALSE(TempFileExists);
 
-  // I've yet to do directory iteration on Unix.
-#ifdef LLVM_ON_WIN32
   error_code ec;
   for (fs::directory_iterator i(".", ec), e; i != e; i.increment(ec)) {
     if (ec) {
@@ -186,7 +184,6 @@
       report_fatal_error("Directory iteration failed!");
     }
   }
-#endif
 }
 
 } // anonymous namespace





More information about the llvm-commits mailing list