For PR351:
[oota-llvm.git] / lib / System / Unix / Path.cpp
index 84c2d2d23d5cf79152b8f65d4678195950d00c31..99923335c5aab2a9f289a0775fdec0da30d65637 100644 (file)
 //===          is guaranteed to work on *all* UNIX variants.
 //===----------------------------------------------------------------------===//
 
-#include <llvm/Config/config.h>
-#include <llvm/Config/alloca.h>
+#include "llvm/Config/alloca.h"
 #include "Unix.h"
+#if HAVE_SYS_STAT_H
 #include <sys/stat.h>
+#endif
+#if HAVE_FCNTL_H
 #include <fcntl.h>
+#endif
+#if HAVE_UTIME_H
 #include <utime.h>
-#include <dirent.h>
+#endif
+#if HAVE_TIME_H
+#include <time.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
+
 
 namespace llvm {
 using namespace sys;
 
-Path::Path(const std::string& unverified_path) 
-  : path(unverified_path)
-{
+Path::Path(const std::string& unverified_path) : path(unverified_path) {
   if (unverified_path.empty())
     return;
   if (this->isValid()) 
@@ -39,6 +61,21 @@ Path::Path(const std::string& unverified_path)
   ThrowErrno(unverified_path + ": path is not valid");
 }
 
+bool 
+Path::isValid() const {
+  if (path.empty()) 
+    return false;
+  else if (path.length() >= MAXPATHLEN)
+    return false;
+#if defined(HAVE_REALPATH)
+  char pathname[MAXPATHLEN];
+  if (0 == realpath(path.c_str(), pathname))
+    if (errno != EACCES && errno != EIO && errno != ENOENT && errno != ENOTDIR)
+      return false;
+#endif
+  return true;
+}
+
 Path
 Path::GetRootDirectory() {
   Path result;
@@ -46,6 +83,79 @@ Path::GetRootDirectory() {
   return result;
 }
 
+Path
+Path::GetTemporaryDirectory() {
+#if defined(HAVE_MKDTEMP)
+  // The best way is with mkdtemp but that's not available on many systems, 
+  // Linux and FreeBSD have it. Others probably won't.
+  char pathname[MAXPATHLEN];
+  strcpy(pathname,"/tmp/llvm_XXXXXX");
+  if (0 == mkdtemp(pathname))
+    ThrowErrno(std::string(pathname) + ": Can't create temporary directory");
+  Path result;
+  result.setDirectory(pathname);
+  assert(result.isValid() && "mkdtemp didn't create a valid pathname!");
+  return result;
+#elif defined(HAVE_MKSTEMP)
+  // If no mkdtemp is available, mkstemp can be used to create a temporary file
+  // which is then removed and created as a directory. We prefer this over
+  // mktemp because of mktemp's inherent security and threading risks. We still
+  // have a slight race condition from the time the temporary file is created to
+  // the time it is re-created as a directoy. 
+  char pathname[MAXPATHLEN];
+  strcpy(pathname, "/tmp/llvm_XXXXXX");
+  int fd = 0;
+  if (-1 == (fd = mkstemp(pathname)))
+    ThrowErrno(std::string(pathname) + ": Can't create temporary directory");
+  ::close(fd);
+  ::unlink(pathname); // start race condition, ignore errors
+  if (-1 == ::mkdir(pathname, S_IRWXU)) // end race condition
+    ThrowErrno(std::string(pathname) + ": Can't create temporary directory");
+  Path result;
+  result.setDirectory(pathname);
+  assert(result.isValid() && "mkstemp didn't create a valid pathname!");
+  return result;
+#elif defined(HAVE_MKTEMP)
+  // If a system doesn't have mkdtemp(3) or mkstemp(3) but it does have
+  // mktemp(3) then we'll assume that system (e.g. AIX) has a reasonable
+  // implementation of mktemp(3) and doesn't follow BSD 4.3's lead of replacing
+  // the XXXXXX with the pid of the process and a letter. That leads to only
+  // twenty six temporary files that can be generated.
+  char pathname[MAXPATHLEN];
+  strcpy(pathname, "/tmp/llvm_XXXXXX");
+  char *TmpName = ::mktemp(pathname);
+  if (TmpName == 0)
+    throw std::string(TmpName) + ": Can't create unique directory name";
+  if (-1 == ::mkdir(TmpName, S_IRWXU))
+    ThrowErrno(std::string(TmpName) + ": Can't create temporary directory");
+  Path result;
+  result.setDirectory(TmpName);
+  assert(result.isValid() && "mktemp didn't create a valid pathname!");
+  return result;
+#else
+  // This is the worst case implementation. tempnam(3) leaks memory unless its
+  // on an SVID2 (or later) system. On BSD 4.3 it leaks. tmpnam(3) has thread
+  // issues. The mktemp(3) function doesn't have enough variability in the
+  // temporary name generated. So, we provide our own implementation that 
+  // increments an integer from a random number seeded by the current time. This
+  // should be sufficiently unique that we don't have many collisions between
+  // processes. Generally LLVM processes don't run very long and don't use very
+  // many temporary files so this shouldn't be a big issue for LLVM.
+  static time_t num = ::time(0);
+  char pathname[MAXPATHLEN];
+  do {
+    num++;
+    sprintf(pathname, "/tmp/llvm_%010u", unsigned(num));
+  } while ( 0 == access(pathname, F_OK ) );
+  if (-1 == ::mkdir(pathname, S_IRWXU))
+    ThrowErrno(std::string(pathname) + ": Can't create temporary directory");
+  Path result;
+  result.setDirectory(pathname);
+  assert(result.isValid() && "mkstemp didn't create a valid pathname!");
+  return result;
+#endif
+}
+
 static void getPathList(const char*path, std::vector<sys::Path>& Paths) {
   const char* at = path;
   const char* delim = strchr(at, ':');
@@ -64,6 +174,7 @@ static void getPathList(const char*path, std::vector<sys::Path>& Paths) {
         Paths.push_back(tmpPath);
 
 }
+
 void 
 Path::GetSystemLibraryPaths(std::vector<sys::Path>& Paths) {
 #ifdef LTDL_SHLIBPATH_VAR
@@ -85,13 +196,6 @@ Path::GetBytecodeLibraryPaths(std::vector<sys::Path>& Paths) {
   if (env_var != 0) {
     getPathList(env_var,Paths);
   }
-#ifdef LLVMGCCDIR
-  {
-    Path tmpPath(std::string(LLVMGCCDIR) + "lib/");
-    if (tmpPath.readable())
-      Paths.push_back(tmpPath);
-  }
-#endif
 #ifdef LLVM_LIBDIR
   {
     Path tmpPath;
@@ -499,7 +603,7 @@ Path::createTemporaryFile(bool reuse_current) {
 }
 
 bool
-Path::destroyDirectory(bool remove_contents) {
+Path::destroyDirectory(bool remove_contents) const {
   // Make sure we're dealing with a directory
   if (!isDirectory()) return false;
 
@@ -527,7 +631,7 @@ Path::destroyDirectory(bool remove_contents) {
 }
 
 bool
-Path::destroyFile() {
+Path::destroyFile() const {
   if (!isFile()) return false;
   if (0 != unlink(path.c_str()))
     ThrowErrno(path + ": Can't destroy file");
@@ -557,7 +661,7 @@ Path::setStatusInfo(const StatusInfo& si) const {
 }
 
 void 
-CopyFile(const sys::Path &Dest, const sys::Path &Src) {
+sys::CopyFile(const sys::Path &Dest, const sys::Path &Src) {
   int inFile = -1;
   int outFile = -1;
   try {