//===- llvm/Support/Unix/Path.cpp - Unix Path Implementation -----*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements the Unix specific portion of the Path class.
//
//===----------------------------------------------------------------------===//
//===----------------------------------------------------------------------===//
//=== WARNING: Implementation here must contain only generic UNIX code that
//=== is guaranteed to work on *all* UNIX variants.
//===----------------------------------------------------------------------===//
#include "Unix.h"
#if HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#if HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_SYS_MMAN_H
#include <sys/mman.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#if HAVE_UTIME_H
#include <utime.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
#if HAVE_DLFCN_H
#include <dlfcn.h>
#endif
#ifdef __APPLE__
#include <mach-o/dyld.h>
#endif
// For GNU Hurd
#if defined(__GNU__) && !defined(MAXPATHLEN)
# define MAXPATHLEN 4096
#endif
// Put in a hack for Cygwin which falsely reports that the mkdtemp function
// is available when it is not.
#ifdef __CYGWIN__
# undef HAVE_MKDTEMP
#endif
namespace {
inline bool lastIsSlash(const std::string& path) {
return !path.empty() && path[path.length() - 1] == '/';
}
}
namespace llvm {
using namespace sys;
const char sys::PathSeparator = ':';
StringRef Path::GetEXESuffix() {
return StringRef();
}
Path::Path(StringRef p)
: path(p) {}
Path::Path(const char *StrStart, unsigned StrLen)
: path(StrStart, StrLen) {}
Path&
Path::operator=(StringRef that) {
path.assign(that.data(), that.size());
return *this;
}
bool
Path::isValid() const {
// Empty paths are considered invalid here.
// This code doesn't check MAXPATHLEN because there's no need. Nothing in
// LLVM manipulates Paths with fixed-sizes arrays, and if the OS can't
// handle names longer than some limit, it'll report this on demand using
// ENAMETOLONG.
return !path.empty();
}
bool
Path::isAbsolute(const char *NameStart, unsigned NameLen) {
assert(NameStart);
if (NameLen == 0)
return false;
return NameStart[0] == '/';
}
bool
Path::isAbsolute() const {
if (path.empty())
return false;
return path[0] == '/';
}
Path
Path::GetRootDirectory() {
Path result;
result.set("/");
return result;
}
Path
Path::GetTemporaryDirectory(std::string *ErrMsg) {
#if defined(__native_client__)
return Path("");
#elif 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[] = "/tmp/llvm_XXXXXX";
if (0 == mkdtemp(pathname)) {
MakeErrMsg(ErrMsg,
std::string(pathname) + ": can't create temporary directory");
return Path();
}
return Path(pathname);
#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[] = "/tmp/llvm_XXXXXX";
int fd = 0;
if (-1 == (fd = mkstemp(pathname))) {
MakeErrMsg(ErrMsg,
std::string(pathname) + ": can't create temporary directory");
return Path();
}
::close(fd);
::unlink(pathname); // start race condition, ignore errors
if (-1 == ::mkdir(pathname, S_IRWXU)) { // end race condition
MakeErrMsg(ErrMsg,
std::string(pathname) + ": can't create temporary directory");
return Path();
}
return Path(pathname);
#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[] = "/tmp/llvm_XXXXXX";
char