2010-11-29 20:44:50 +01:00
|
|
|
//===- llvm/Support/Unix/Path.cpp - Unix Path Implementation -----*- C++ -*-===//
|
2009-02-15 04:20:03 +01:00
|
|
|
//
|
2004-08-25 08:20:07 +02:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 21:36:04 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2009-02-15 04:20:03 +01:00
|
|
|
//
|
2004-08-25 08:20:07 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the Unix specific portion of the Path class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//=== WARNING: Implementation here must contain only generic UNIX code that
|
2004-08-29 07:24:01 +02:00
|
|
|
//=== is guaranteed to work on *all* UNIX variants.
|
2004-08-25 08:20:07 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Unix.h"
|
2004-12-27 07:17:15 +01:00
|
|
|
#if HAVE_SYS_STAT_H
|
2004-08-25 08:20:07 +02:00
|
|
|
#include <sys/stat.h>
|
2004-12-27 07:17:15 +01:00
|
|
|
#endif
|
|
|
|
#if HAVE_FCNTL_H
|
2004-08-25 08:20:07 +02:00
|
|
|
#include <fcntl.h>
|
2004-12-27 07:17:15 +01:00
|
|
|
#endif
|
2008-04-01 08:25:23 +02:00
|
|
|
#ifdef HAVE_SYS_MMAN_H
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_STAT_H
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
2004-12-27 07:17:15 +01:00
|
|
|
#if HAVE_UTIME_H
|
2004-11-14 23:08:36 +01:00
|
|
|
#include <utime.h>
|
2004-12-27 07:17:15 +01:00
|
|
|
#endif
|
|
|
|
#if HAVE_TIME_H
|
2004-12-24 07:29:42 +01:00
|
|
|
#include <time.h>
|
2004-12-27 07:17:15 +01:00
|
|
|
#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
|
|
|
|
|
2008-03-03 03:55:43 +01:00
|
|
|
#if HAVE_DLFCN_H
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#endif
|
|
|
|
|
2009-09-09 14:09:08 +02:00
|
|
|
#ifdef __APPLE__
|
|
|
|
#include <mach-o/dyld.h>
|
|
|
|
#endif
|
|
|
|
|
2005-06-02 07:38:20 +02:00
|
|
|
// 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
|
2004-08-29 07:24:01 +02:00
|
|
|
|
2005-07-28 18:25:57 +02:00
|
|
|
namespace {
|
|
|
|
inline bool lastIsSlash(const std::string& path) {
|
|
|
|
return !path.empty() && path[path.length() - 1] == '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2004-08-29 07:24:01 +02:00
|
|
|
namespace llvm {
|
|
|
|
using namespace sys;
|
|
|
|
|
2009-12-09 04:26:33 +01:00
|
|
|
const char sys::PathSeparator = ':';
|
2008-02-27 07:17:10 +01:00
|
|
|
|
2010-11-02 21:32:26 +01:00
|
|
|
StringRef Path::GetEXESuffix() {
|
2010-11-02 21:52:47 +01:00
|
|
|
return StringRef();
|
2010-11-02 21:32:26 +01:00
|
|
|
}
|
|
|
|
|
2009-12-17 22:02:39 +01:00
|
|
|
Path::Path(StringRef p)
|
2008-05-11 19:37:40 +02:00
|
|
|
: path(p) {}
|
|
|
|
|
|
|
|
Path::Path(const char *StrStart, unsigned StrLen)
|
|
|
|
: path(StrStart, StrLen) {}
|
|
|
|
|
2008-08-12 01:39:47 +02:00
|
|
|
Path&
|
2009-12-17 22:02:39 +01:00
|
|
|
Path::operator=(StringRef that) {
|
|
|
|
path.assign(that.data(), that.size());
|
2008-08-12 01:39:47 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2009-02-15 04:20:03 +01:00
|
|
|
bool
|
2004-12-24 07:29:42 +01:00
|
|
|
Path::isValid() const {
|
2010-11-03 00:19:55 +01:00
|
|
|
// 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();
|
2004-12-24 07:29:42 +01:00
|
|
|
}
|
|
|
|
|
2009-06-15 06:17:07 +02:00
|
|
|
bool
|
|
|
|
Path::isAbsolute(const char *NameStart, unsigned NameLen) {
|
|
|
|
assert(NameStart);
|
|
|
|
if (NameLen == 0)
|
|
|
|
return false;
|
|
|
|
return NameStart[0] == '/';
|
|
|
|
}
|
|
|
|
|
2009-02-15 04:20:03 +01:00
|
|
|
bool
|
2007-03-29 18:43:20 +02:00
|
|
|
Path::isAbsolute() const {
|
|
|
|
if (path.empty())
|
|
|
|
return false;
|
|
|
|
return path[0] == '/';
|
2009-02-15 04:20:03 +01:00
|
|
|
}
|
2009-07-12 22:23:56 +02:00
|
|
|
|
2004-08-29 07:24:01 +02:00
|
|
|
Path
|
|
|
|
Path::GetRootDirectory() {
|
|
|
|
Path result;
|
2005-07-08 01:21:43 +02:00
|
|
|
result.set("/");
|
2004-08-29 07:24:01 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2004-12-24 07:29:42 +01:00
|
|
|
Path
|
2009-02-19 06:34:35 +01:00
|
|
|
Path::GetTemporaryDirectory(std::string *ErrMsg) {
|
2004-12-24 07:29:42 +01:00
|
|
|
#if defined(HAVE_MKDTEMP)
|
2009-02-15 04:20:03 +01:00
|
|
|
// The best way is with mkdtemp but that's not available on many systems,
|
2004-12-24 07:29:42 +01:00
|
|
|
// Linux and FreeBSD have it. Others probably won't.
|
2010-11-02 23:50:10 +01:00
|
|
|
char pathname[] = "/tmp/llvm_XXXXXX";
|
2006-08-22 21:01:30 +02:00
|
|
|
if (0 == mkdtemp(pathname)) {
|
2010-11-02 23:56:51 +01:00
|
|
|
MakeErrMsg(ErrMsg,
|
|
|
|
std::string(pathname) + ": can't create temporary directory");
|
2006-08-22 21:01:30 +02:00
|
|
|
return Path();
|
|
|
|
}
|
2010-11-03 01:01:23 +01:00
|
|
|
return Path(pathname);
|
2004-12-24 07:29:42 +01:00
|
|
|
#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
|
2009-02-15 04:20:03 +01:00
|
|
|
// the time it is re-created as a directoy.
|
2010-11-02 23:50:10 +01:00
|
|
|
char pathname[] = "/tmp/llvm_XXXXXX";
|
2004-12-24 07:29:42 +01:00
|
|
|
int fd = 0;
|
2006-08-22 21:01:30 +02:00
|
|
|
if (-1 == (fd = mkstemp(pathname))) {
|
2009-02-15 04:20:03 +01:00
|
|
|
MakeErrMsg(ErrMsg,
|
2006-08-22 21:01:30 +02:00
|
|
|
std::string(pathname) + ": can't create temporary directory");
|
|
|
|
return Path();
|
|
|
|
}
|
2004-12-24 07:29:42 +01:00
|
|
|
::close(fd);
|
|
|
|
::unlink(pathname); // start race condition, ignore errors
|
2006-08-22 21:01:30 +02:00
|
|
|
if (-1 == ::mkdir(pathname, S_IRWXU)) { // end race condition
|
2009-02-15 04:20:03 +01:00
|
|
|
MakeErrMsg(ErrMsg,
|
2006-08-22 21:01:30 +02:00
|
|
|
std::string(pathname) + ": can't create temporary directory");
|
|
|
|
return Path();
|
|
|
|
}
|
2010-11-03 01:01:23 +01:00
|
|
|
return Path(pathname);
|
2004-12-24 07:29:42 +01:00
|
|
|
#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.
|
2010-11-02 23:50:10 +01:00
|
|
|
char pathname[] = "/tmp/llvm_XXXXXX";
|
2004-12-24 07:29:42 +01:00
|
|
|
char *TmpName = ::mktemp(pathname);
|
2006-08-22 21:01:30 +02:00
|
|
|
if (TmpName == 0) {
|
2009-02-15 04:20:03 +01:00
|
|
|
MakeErrMsg(ErrMsg,
|
2006-08-22 21:01:30 +02:00
|
|
|
std::string(TmpName) + ": can't create unique directory name");
|
|
|
|
return Path();
|
|
|
|
}
|
|
|
|
if (-1 == ::mkdir(TmpName, S_IRWXU)) {
|
2009-02-15 04:20:03 +01:00
|
|
|
MakeErrMsg(ErrMsg,
|
2006-08-22 21:01:30 +02:00
|
|
|
std::string(TmpName) + ": can't create temporary directory");
|
|
|
|
return Path();
|
|
|
|
}
|
2010-11-03 01:01:23 +01:00
|
|
|
return Path(TmpName);
|
2004-12-24 07:29:42 +01:00
|
|
|
#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
|
2009-02-15 04:20:03 +01:00
|
|
|
// temporary name generated. So, we provide our own implementation that
|
2004-12-24 07:29:42 +01:00
|
|
|
// 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 ) );
|
2006-08-22 21:01:30 +02:00
|
|
|
if (-1 == ::mkdir(pathname, S_IRWXU)) {
|
2009-02-15 04:20:03 +01:00
|
|
|
MakeErrMsg(ErrMsg,
|
2006-08-22 21:01:30 +02:00
|
|
|
std::string(pathname) + ": can't create temporary directory");
|
|
|
|
return Path();
|
2006-08-23 22:34:57 +02:00
|
|
|
}
|
2010-11-03 01:01:23 +01:00
|
|
|
return Path(pathname);
|
2004-12-24 07:29:42 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-02-15 04:20:03 +01:00
|
|
|
void
|
2004-12-13 04:00:51 +01:00
|
|
|
Path::GetSystemLibraryPaths(std::vector<sys::Path>& Paths) {
|
|
|
|
#ifdef LTDL_SHLIBPATH_VAR
|
|
|
|
char* env_var = getenv(LTDL_SHLIBPATH_VAR);
|
|
|
|
if (env_var != 0) {
|
|
|
|
getPathList(env_var,Paths);
|
2004-09-14 02:16:39 +02:00
|
|
|
}
|
2004-12-13 04:00:51 +01:00
|
|
|
#endif
|
|
|
|
// FIXME: Should this look at LD_LIBRARY_PATH too?
|
|
|
|
Paths.push_back(sys::Path("/usr/local/lib/"));
|
|
|
|
Paths.push_back(sys::Path("/usr/X11R6/lib/"));
|
|
|
|
Paths.push_back(sys::Path("/usr/lib/"));
|
|
|
|
Paths.push_back(sys::Path("/lib/"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-07-05 19:07:56 +02:00
|
|
|
Path::GetBitcodeLibraryPaths(std::vector<sys::Path>& Paths) {
|
2004-12-13 04:00:51 +01:00
|
|
|
char * env_var = getenv("LLVM_LIB_SEARCH_PATH");
|
|
|
|
if (env_var != 0) {
|
|
|
|
getPathList(env_var,Paths);
|
|
|
|
}
|
|
|
|
#ifdef LLVM_LIBDIR
|
|
|
|
{
|
|
|
|
Path tmpPath;
|
2005-07-08 01:21:43 +02:00
|
|
|
if (tmpPath.set(LLVM_LIBDIR))
|
2005-07-07 20:21:42 +02:00
|
|
|
if (tmpPath.canRead())
|
2004-12-13 04:00:51 +01:00
|
|
|
Paths.push_back(tmpPath);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
GetSystemLibraryPaths(Paths);
|
2004-08-29 07:24:01 +02:00
|
|
|
}
|
|
|
|
|
2009-02-15 04:20:03 +01:00
|
|
|
Path
|
2004-08-29 07:24:01 +02:00
|
|
|
Path::GetLLVMDefaultConfigDir() {
|
|
|
|
return Path("/etc/llvm/");
|
|
|
|
}
|
|
|
|
|
|
|
|
Path
|
|
|
|
Path::GetUserHomeDirectory() {
|
|
|
|
const char* home = getenv("HOME");
|
2010-12-27 04:21:41 +01:00
|
|
|
Path result;
|
|
|
|
if (home && result.set(home))
|
|
|
|
return result;
|
|
|
|
result.set("/");
|
|
|
|
return result;
|
2004-08-29 07:24:01 +02:00
|
|
|
}
|
2004-08-25 08:20:07 +02:00
|
|
|
|
2007-12-18 23:07:33 +01:00
|
|
|
Path
|
|
|
|
Path::GetCurrentDirectory() {
|
|
|
|
char pathname[MAXPATHLEN];
|
|
|
|
if (!getcwd(pathname,MAXPATHLEN)) {
|
|
|
|
assert (false && "Could not query current working directory.");
|
2010-08-04 03:39:08 +02:00
|
|
|
return Path();
|
2007-12-18 23:07:33 +01:00
|
|
|
}
|
2009-02-15 04:20:03 +01:00
|
|
|
|
2007-12-18 23:07:33 +01:00
|
|
|
return Path(pathname);
|
|
|
|
}
|
2005-07-08 05:08:58 +02:00
|
|
|
|
2011-02-18 18:04:56 +01:00
|
|
|
#if defined(__FreeBSD__) || defined (__NetBSD__) || \
|
|
|
|
defined(__OpenBSD__) || defined(__minix)
|
2009-03-02 23:17:15 +01:00
|
|
|
static int
|
|
|
|
test_dir(char buf[PATH_MAX], char ret[PATH_MAX],
|
|
|
|
const char *dir, const char *bin)
|
|
|
|
{
|
2009-05-30 03:09:53 +02:00
|
|
|
struct stat sb;
|
2009-03-02 23:17:15 +01:00
|
|
|
|
2010-09-02 20:24:46 +02:00
|
|
|
snprintf(buf, PATH_MAX, "%s/%s", dir, bin);
|
2009-05-30 03:09:53 +02:00
|
|
|
if (realpath(buf, ret) == NULL)
|
|
|
|
return (1);
|
|
|
|
if (stat(buf, &sb) != 0)
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
return (0);
|
2009-03-02 23:17:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
getprogpath(char ret[PATH_MAX], const char *bin)
|
|
|
|
{
|
2009-05-30 03:09:53 +02:00
|
|
|
char *pv, *s, *t, buf[PATH_MAX];
|
|
|
|
|
|
|
|
/* First approach: absolute path. */
|
|
|
|
if (bin[0] == '/') {
|
|
|
|
if (test_dir(buf, ret, "/", bin) == 0)
|
|
|
|
return (ret);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Second approach: relative path. */
|
|
|
|
if (strchr(bin, '/') != NULL) {
|
|
|
|
if (getcwd(buf, PATH_MAX) == NULL)
|
|
|
|
return (NULL);
|
|
|
|
if (test_dir(buf, ret, buf, bin) == 0)
|
|
|
|
return (ret);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Third approach: $PATH */
|
|
|
|
if ((pv = getenv("PATH")) == NULL)
|
|
|
|
return (NULL);
|
|
|
|
s = pv = strdup(pv);
|
|
|
|
if (pv == NULL)
|
|
|
|
return (NULL);
|
|
|
|
while ((t = strsep(&s, ":")) != NULL) {
|
|
|
|
if (test_dir(buf, ret, t, bin) == 0) {
|
|
|
|
free(pv);
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(pv);
|
|
|
|
return (NULL);
|
2009-03-02 23:17:15 +01:00
|
|
|
}
|
2010-09-01 00:38:00 +02:00
|
|
|
#endif // __FreeBSD__ || __NetBSD__
|
2009-03-02 23:17:15 +01:00
|
|
|
|
2008-03-03 03:55:43 +01:00
|
|
|
/// GetMainExecutable - Return the path to the main executable, given the
|
|
|
|
/// value of argv[0] from program startup.
|
|
|
|
Path Path::GetMainExecutable(const char *argv0, void *MainAddr) {
|
2009-09-09 14:09:08 +02:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
// On OS X the executable path is saved to the stack by dyld. Reading it
|
|
|
|
// from there is much faster than calling dladdr, especially for large
|
|
|
|
// binaries with symbols.
|
|
|
|
char exe_path[MAXPATHLEN];
|
|
|
|
uint32_t size = sizeof(exe_path);
|
|
|
|
if (_NSGetExecutablePath(exe_path, &size) == 0) {
|
|
|
|
char link_path[MAXPATHLEN];
|
2009-11-29 18:19:48 +01:00
|
|
|
if (realpath(exe_path, link_path))
|
2010-11-02 23:07:47 +01:00
|
|
|
return Path(link_path);
|
2009-09-09 14:09:08 +02:00
|
|
|
}
|
2011-02-18 18:04:56 +01:00
|
|
|
#elif defined(__FreeBSD__) || defined (__NetBSD__) || \
|
|
|
|
defined(__OpenBSD__) || defined(__minix)
|
2009-03-02 23:17:15 +01:00
|
|
|
char exe_path[PATH_MAX];
|
|
|
|
|
|
|
|
if (getprogpath(exe_path, argv0) != NULL)
|
2010-11-02 23:07:47 +01:00
|
|
|
return Path(exe_path);
|
2009-03-02 23:17:15 +01:00
|
|
|
#elif defined(__linux__) || defined(__CYGWIN__)
|
2008-03-13 06:22:05 +01:00
|
|
|
char exe_path[MAXPATHLEN];
|
2008-06-28 00:55:30 +02:00
|
|
|
ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path));
|
2009-08-05 22:16:55 +02:00
|
|
|
if (len >= 0)
|
2010-11-02 23:07:47 +01:00
|
|
|
return Path(StringRef(exe_path, len));
|
2008-03-13 06:22:05 +01:00
|
|
|
#elif defined(HAVE_DLFCN_H)
|
2008-03-03 03:55:43 +01:00
|
|
|
// Use dladdr to get executable path if available.
|
|
|
|
Dl_info DLInfo;
|
|
|
|
int err = dladdr(MainAddr, &DLInfo);
|
2009-02-19 06:34:35 +01:00
|
|
|
if (err == 0)
|
|
|
|
return Path();
|
2009-05-30 03:09:53 +02:00
|
|
|
|
2009-02-19 06:34:35 +01:00
|
|
|
// If the filename is a symlink, we need to resolve and return the location of
|
|
|
|
// the actual executable.
|
|
|
|
char link_path[MAXPATHLEN];
|
2009-11-29 18:19:48 +01:00
|
|
|
if (realpath(DLInfo.dli_fname, link_path))
|
2010-11-02 23:07:47 +01:00
|
|
|
return Path(link_path);
|
2010-09-07 20:26:49 +02:00
|
|
|
#else
|
|
|
|
#error GetMainExecutable is not implemented on this host yet.
|
2008-03-03 03:55:43 +01:00
|
|
|
#endif
|
|
|
|
return Path();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-17 22:02:39 +01:00
|
|
|
StringRef Path::getDirname() const {
|
|
|
|
return getDirnameCharSep(path, "/");
|
2008-04-08 00:01:32 +02:00
|
|
|
}
|
2008-04-07 23:53:57 +02:00
|
|
|
|
2009-12-17 22:02:39 +01:00
|
|
|
StringRef
|
2004-11-05 23:15:36 +01:00
|
|
|
Path::getBasename() const {
|
2004-09-11 06:55:08 +02:00
|
|
|
// Find the last slash
|
2008-04-07 23:53:57 +02:00
|
|
|
std::string::size_type slash = path.rfind('/');
|
2004-09-11 06:55:08 +02:00
|
|
|
if (slash == std::string::npos)
|
|
|
|
slash = 0;
|
|
|
|
else
|
|
|
|
slash++;
|
|
|
|
|
2008-04-07 23:53:57 +02:00
|
|
|
std::string::size_type dot = path.rfind('.');
|
2005-07-09 20:42:02 +02:00
|
|
|
if (dot == std::string::npos || dot < slash)
|
2009-12-17 22:02:39 +01:00
|
|
|
return StringRef(path).substr(slash);
|
2005-07-09 20:42:02 +02:00
|
|
|
else
|
2009-12-17 22:02:39 +01:00
|
|
|
return StringRef(path).substr(slash, dot - slash);
|
2004-09-11 06:55:08 +02:00
|
|
|
}
|
|
|
|
|
2009-12-17 22:02:39 +01:00
|
|
|
StringRef
|
2008-06-15 17:15:19 +02:00
|
|
|
Path::getSuffix() const {
|
|
|
|
// Find the last slash
|
|
|
|
std::string::size_type slash = path.rfind('/');
|
|
|
|
if (slash == std::string::npos)
|
|
|
|
slash = 0;
|
|
|
|
else
|
|
|
|
slash++;
|
|
|
|
|
|
|
|
std::string::size_type dot = path.rfind('.');
|
|
|
|
if (dot == std::string::npos || dot < slash)
|
2010-08-04 03:39:08 +02:00
|
|
|
return StringRef();
|
2008-06-15 17:15:19 +02:00
|
|
|
else
|
2009-12-17 22:02:39 +01:00
|
|
|
return StringRef(path).substr(dot + 1);
|
2008-06-15 17:15:19 +02:00
|
|
|
}
|
|
|
|
|
2009-12-16 09:35:54 +01:00
|
|
|
bool Path::getMagicNumber(std::string &Magic, unsigned len) const {
|
2004-11-16 18:14:08 +01:00
|
|
|
assert(len < 1024 && "Request for magic string too long");
|
2009-12-16 09:35:54 +01:00
|
|
|
char Buf[1025];
|
2006-08-01 19:51:09 +02:00
|
|
|
int fd = ::open(path.c_str(), O_RDONLY);
|
2004-11-16 18:14:08 +01:00
|
|
|
if (fd < 0)
|
|
|
|
return false;
|
2009-12-16 09:35:54 +01:00
|
|
|
ssize_t bytes_read = ::read(fd, Buf, len);
|
2004-12-02 10:09:48 +01:00
|
|
|
::close(fd);
|
2010-05-27 19:14:10 +02:00
|
|
|
if (ssize_t(len) != bytes_read)
|
2004-11-16 18:14:08 +01:00
|
|
|
return false;
|
2009-12-16 09:35:54 +01:00
|
|
|
Magic.assign(Buf, len);
|
2004-11-14 23:08:36 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-08-25 08:20:07 +02:00
|
|
|
bool
|
2004-08-29 07:24:01 +02:00
|
|
|
Path::exists() const {
|
|
|
|
return 0 == access(path.c_str(), F_OK );
|
2004-08-25 08:20:07 +02:00
|
|
|
}
|
|
|
|
|
2007-12-18 20:46:22 +01:00
|
|
|
bool
|
|
|
|
Path::isDirectory() const {
|
|
|
|
struct stat buf;
|
|
|
|
if (0 != stat(path.c_str(), &buf))
|
|
|
|
return false;
|
2010-10-08 00:05:57 +02:00
|
|
|
return ((buf.st_mode & S_IFMT) == S_IFDIR) ? true : false;
|
2007-12-18 20:46:22 +01:00
|
|
|
}
|
|
|
|
|
2010-11-07 05:36:50 +01:00
|
|
|
bool
|
|
|
|
Path::isSymLink() const {
|
|
|
|
struct stat buf;
|
|
|
|
if (0 != lstat(path.c_str(), &buf))
|
|
|
|
return false;
|
|
|
|
return S_ISLNK(buf.st_mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-08-25 08:20:07 +02:00
|
|
|
bool
|
2005-07-07 20:21:42 +02:00
|
|
|
Path::canRead() const {
|
2009-07-29 01:22:01 +02:00
|
|
|
return 0 == access(path.c_str(), R_OK);
|
2004-08-29 07:24:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2005-07-07 20:21:42 +02:00
|
|
|
Path::canWrite() const {
|
2009-07-29 01:22:01 +02:00
|
|
|
return 0 == access(path.c_str(), W_OK);
|
2004-08-25 08:20:07 +02:00
|
|
|
}
|
|
|
|
|
2009-11-24 16:19:10 +01:00
|
|
|
bool
|
2009-11-25 07:32:19 +01:00
|
|
|
Path::isRegularFile() const {
|
2010-03-30 22:04:57 +02:00
|
|
|
// Get the status so we can determine if it's a file or directory
|
2009-11-24 16:19:10 +01:00
|
|
|
struct stat buf;
|
|
|
|
|
2009-11-24 20:03:33 +01:00
|
|
|
if (0 != stat(path.c_str(), &buf))
|
2009-11-24 16:19:10 +01:00
|
|
|
return false;
|
|
|
|
|
2009-11-25 07:32:19 +01:00
|
|
|
if (S_ISREG(buf.st_mode))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
2009-11-24 16:19:10 +01:00
|
|
|
}
|
|
|
|
|
2004-08-29 07:24:01 +02:00
|
|
|
bool
|
2005-07-07 20:21:42 +02:00
|
|
|
Path::canExecute() const {
|
2005-07-08 19:46:10 +02:00
|
|
|
if (0 != access(path.c_str(), R_OK | X_OK ))
|
|
|
|
return false;
|
2007-04-07 20:52:17 +02:00
|
|
|
struct stat buf;
|
|
|
|
if (0 != stat(path.c_str(), &buf))
|
|
|
|
return false;
|
|
|
|
if (!S_ISREG(buf.st_mode))
|
2005-04-20 17:33:22 +02:00
|
|
|
return false;
|
2005-07-08 19:46:10 +02:00
|
|
|
return true;
|
2004-08-29 07:24:01 +02:00
|
|
|
}
|
|
|
|
|
2009-12-17 22:02:39 +01:00
|
|
|
StringRef
|
2004-08-29 07:24:01 +02:00
|
|
|
Path::getLast() const {
|
|
|
|
// Find the last slash
|
|
|
|
size_t pos = path.rfind('/');
|
|
|
|
|
|
|
|
// Handle the corner cases
|
|
|
|
if (pos == std::string::npos)
|
|
|
|
return path;
|
|
|
|
|
|
|
|
// If the last character is a slash
|
|
|
|
if (pos == path.length()-1) {
|
|
|
|
// Find the second to last slash
|
|
|
|
size_t pos2 = path.rfind('/', pos-1);
|
|
|
|
if (pos2 == std::string::npos)
|
2009-12-17 22:02:39 +01:00
|
|
|
return StringRef(path).substr(0,pos);
|
2004-08-29 07:24:01 +02:00
|
|
|
else
|
2009-12-17 22:02:39 +01:00
|
|
|
return StringRef(path).substr(pos2+1,pos-pos2-1);
|
2004-08-25 08:20:07 +02:00
|
|
|
}
|
2004-08-29 07:24:01 +02:00
|
|
|
// Return everything after the last slash
|
2009-12-17 22:02:39 +01:00
|
|
|
return StringRef(path).substr(pos+1);
|
2004-08-25 08:20:07 +02:00
|
|
|
}
|
|
|
|
|
2007-04-07 20:52:17 +02:00
|
|
|
const FileStatus *
|
|
|
|
PathWithStatus::getFileStatus(bool update, std::string *ErrStr) const {
|
|
|
|
if (!fsIsValid || update) {
|
2007-03-29 18:43:20 +02:00
|
|
|
struct stat buf;
|
2007-03-29 21:05:44 +02:00
|
|
|
if (0 != stat(path.c_str(), &buf)) {
|
|
|
|
MakeErrMsg(ErrStr, path + ": can't get status of file");
|
|
|
|
return 0;
|
|
|
|
}
|
2007-04-07 20:52:17 +02:00
|
|
|
status.fileSize = buf.st_size;
|
|
|
|
status.modTime.fromEpochTime(buf.st_mtime);
|
|
|
|
status.mode = buf.st_mode;
|
|
|
|
status.user = buf.st_uid;
|
|
|
|
status.group = buf.st_gid;
|
|
|
|
status.uniqueID = uint64_t(buf.st_ino);
|
|
|
|
status.isDir = S_ISDIR(buf.st_mode);
|
|
|
|
status.isFile = S_ISREG(buf.st_mode);
|
|
|
|
fsIsValid = true;
|
2007-03-29 18:43:20 +02:00
|
|
|
}
|
2007-04-07 20:52:17 +02:00
|
|
|
return &status;
|
2004-11-14 23:08:36 +01:00
|
|
|
}
|
|
|
|
|
2006-07-29 00:03:44 +02:00
|
|
|
static bool AddPermissionBits(const Path &File, int bits) {
|
2004-12-13 20:59:50 +01:00
|
|
|
// Get the umask value from the operating system. We want to use it
|
|
|
|
// when changing the file's permissions. Since calling umask() sets
|
|
|
|
// the umask and returns its old value, we must call it a second
|
|
|
|
// time to reset it to the user's preference.
|
|
|
|
int mask = umask(0777); // The arg. to umask is arbitrary.
|
|
|
|
umask(mask); // Restore the umask.
|
|
|
|
|
|
|
|
// Get the file's current mode.
|
2007-04-07 20:52:17 +02:00
|
|
|
struct stat buf;
|
2009-08-24 00:45:37 +02:00
|
|
|
if (0 != stat(File.c_str(), &buf))
|
2004-12-13 20:59:50 +01:00
|
|
|
return false;
|
2007-04-07 20:52:17 +02:00
|
|
|
// Change the file to have whichever permissions bits from 'bits'
|
|
|
|
// that the umask would not disable.
|
|
|
|
if ((chmod(File.c_str(), (buf.st_mode | (bits & ~mask)))) == -1)
|
|
|
|
return false;
|
2004-12-13 20:59:50 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-08-23 01:27:23 +02:00
|
|
|
bool Path::makeReadableOnDisk(std::string* ErrMsg) {
|
2009-02-15 04:20:03 +01:00
|
|
|
if (!AddPermissionBits(*this, 0444))
|
2006-08-23 09:30:48 +02:00
|
|
|
return MakeErrMsg(ErrMsg, path + ": can't make file readable");
|
2006-08-23 01:27:23 +02:00
|
|
|
return false;
|
2004-12-13 20:59:50 +01:00
|
|
|
}
|
|
|
|
|
2006-08-23 01:27:23 +02:00
|
|
|
bool Path::makeWriteableOnDisk(std::string* ErrMsg) {
|
2006-08-23 09:30:48 +02:00
|
|
|
if (!AddPermissionBits(*this, 0222))
|
|
|
|
return MakeErrMsg(ErrMsg, path + ": can't make file writable");
|
2006-08-23 01:27:23 +02:00
|
|
|
return false;
|
2004-12-13 20:59:50 +01:00
|
|
|
}
|
|
|
|
|
2006-08-23 01:27:23 +02:00
|
|
|
bool Path::makeExecutableOnDisk(std::string* ErrMsg) {
|
2006-08-23 09:30:48 +02:00
|
|
|
if (!AddPermissionBits(*this, 0111))
|
|
|
|
return MakeErrMsg(ErrMsg, path + ": can't make file executable");
|
2006-08-23 01:27:23 +02:00
|
|
|
return false;
|
2004-12-13 20:59:50 +01:00
|
|
|
}
|
|
|
|
|
2004-11-14 23:08:36 +01:00
|
|
|
bool
|
2006-08-23 08:56:27 +02:00
|
|
|
Path::getDirectoryContents(std::set<Path>& result, std::string* ErrMsg) const {
|
2004-11-14 23:08:36 +01:00
|
|
|
DIR* direntries = ::opendir(path.c_str());
|
2006-08-23 09:30:48 +02:00
|
|
|
if (direntries == 0)
|
|
|
|
return MakeErrMsg(ErrMsg, path + ": can't open directory");
|
2004-11-14 23:08:36 +01:00
|
|
|
|
2005-07-28 18:25:57 +02:00
|
|
|
std::string dirPath = path;
|
|
|
|
if (!lastIsSlash(dirPath))
|
|
|
|
dirPath += '/';
|
|
|
|
|
2004-11-14 23:08:36 +01:00
|
|
|
result.clear();
|
|
|
|
struct dirent* de = ::readdir(direntries);
|
2005-04-20 06:04:07 +02:00
|
|
|
for ( ; de != 0; de = ::readdir(direntries)) {
|
2004-11-14 23:08:36 +01:00
|
|
|
if (de->d_name[0] != '.') {
|
2005-07-28 18:25:57 +02:00
|
|
|
Path aPath(dirPath + (const char*)de->d_name);
|
2006-07-07 23:21:06 +02:00
|
|
|
struct stat st;
|
|
|
|
if (0 != lstat(aPath.path.c_str(), &st)) {
|
|
|
|
if (S_ISLNK(st.st_mode))
|
2005-04-20 06:04:07 +02:00
|
|
|
continue; // dangling symlink -- ignore
|
2009-02-15 04:20:03 +01:00
|
|
|
return MakeErrMsg(ErrMsg,
|
2006-08-23 09:30:48 +02:00
|
|
|
aPath.path + ": can't determine file object type");
|
2005-04-20 06:04:07 +02:00
|
|
|
}
|
2004-11-16 07:15:19 +01:00
|
|
|
result.insert(aPath);
|
2004-11-14 23:08:36 +01:00
|
|
|
}
|
|
|
|
}
|
2009-02-15 04:20:03 +01:00
|
|
|
|
2004-11-14 23:08:36 +01:00
|
|
|
closedir(direntries);
|
2006-08-23 08:56:27 +02:00
|
|
|
return false;
|
2004-11-09 21:26:31 +01:00
|
|
|
}
|
|
|
|
|
2004-08-29 07:24:01 +02:00
|
|
|
bool
|
2009-12-17 22:02:39 +01:00
|
|
|
Path::set(StringRef a_path) {
|
2005-07-08 01:21:43 +02:00
|
|
|
if (a_path.empty())
|
2004-08-29 07:24:01 +02:00
|
|
|
return false;
|
|
|
|
path = a_path;
|
|
|
|
return true;
|
2004-08-25 08:20:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2009-12-17 22:02:39 +01:00
|
|
|
Path::appendComponent(StringRef name) {
|
2005-07-08 01:21:43 +02:00
|
|
|
if (name.empty())
|
2004-08-29 07:24:01 +02:00
|
|
|
return false;
|
2005-07-28 18:25:57 +02:00
|
|
|
if (!lastIsSlash(path))
|
|
|
|
path += '/';
|
2005-07-08 01:21:43 +02:00
|
|
|
path += name;
|
2004-08-29 07:24:01 +02:00
|
|
|
return true;
|
2004-08-25 08:20:07 +02:00
|
|
|
}
|
|
|
|
|
2004-08-29 07:24:01 +02:00
|
|
|
bool
|
2005-07-08 01:21:43 +02:00
|
|
|
Path::eraseComponent() {
|
2004-08-29 07:24:01 +02:00
|
|
|
size_t slashpos = path.rfind('/',path.size());
|
2005-07-08 01:21:43 +02:00
|
|
|
if (slashpos == 0 || slashpos == std::string::npos) {
|
|
|
|
path.erase();
|
|
|
|
return true;
|
|
|
|
}
|
2004-08-29 07:24:01 +02:00
|
|
|
if (slashpos == path.size() - 1)
|
|
|
|
slashpos = path.rfind('/',slashpos-1);
|
2005-07-08 01:21:43 +02:00
|
|
|
if (slashpos == std::string::npos) {
|
|
|
|
path.erase();
|
|
|
|
return true;
|
2004-08-25 08:20:07 +02:00
|
|
|
}
|
2005-07-08 01:21:43 +02:00
|
|
|
path.erase(slashpos);
|
2004-08-29 07:24:01 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-07-08 01:21:43 +02:00
|
|
|
bool
|
|
|
|
Path::eraseSuffix() {
|
2004-08-29 07:24:01 +02:00
|
|
|
size_t dotpos = path.rfind('.',path.size());
|
|
|
|
size_t slashpos = path.rfind('/',path.size());
|
2005-07-08 06:49:16 +02:00
|
|
|
if (dotpos != std::string::npos) {
|
2005-07-09 20:42:02 +02:00
|
|
|
if (slashpos == std::string::npos || dotpos > slashpos+1) {
|
2005-07-08 06:49:16 +02:00
|
|
|
path.erase(dotpos, path.size()-dotpos);
|
2005-07-08 07:02:13 +02:00
|
|
|
return true;
|
2005-07-08 06:49:16 +02:00
|
|
|
}
|
2005-07-08 01:21:43 +02:00
|
|
|
}
|
2005-07-08 06:49:16 +02:00
|
|
|
return false;
|
2004-08-29 07:24:01 +02:00
|
|
|
}
|
|
|
|
|
Re-implemented Path::createDirectoryOnDisk (for Unix).
This method allows one to create a directory, and optionally create all parent
directories that do not exist.
The original implementation would require that *all* directories along a path
are writable by the user, including directories that already exist. For example,
suppose we wanted to create the directory "/tmp/foo/bar", and the directory
"/tmp" already exists, but not "/tmp/foo". Since "/tmp" is writable by all
users, the original implementation would work, and create "/tmp/foo", followed
by "/tmp/bar".
A problem occurred, however if one wanted to created the directory
"/Users/myuser/bar" (or equivalently "/home/myuser/bar"), and "/Users/myuser"
already existed and is writable by the current user. The directory
"/User/myuser" is writable by the user, but "/User" is not. The original
implementation of createDirectoryOnDisk would return with failure since "/User"
is not writable, even though "/User/mysuser" is writable.
The new implementation works by recursively creating parents as needed, and thus
doesn't need to check the permissions on every directory in a path.
llvm-svn: 49162
2008-04-03 18:11:31 +02:00
|
|
|
static bool createDirectoryHelper(char* beg, char* end, bool create_parents) {
|
2009-02-15 04:20:03 +01:00
|
|
|
|
2009-07-29 02:02:58 +02:00
|
|
|
if (access(beg, R_OK | W_OK) == 0)
|
Re-implemented Path::createDirectoryOnDisk (for Unix).
This method allows one to create a directory, and optionally create all parent
directories that do not exist.
The original implementation would require that *all* directories along a path
are writable by the user, including directories that already exist. For example,
suppose we wanted to create the directory "/tmp/foo/bar", and the directory
"/tmp" already exists, but not "/tmp/foo". Since "/tmp" is writable by all
users, the original implementation would work, and create "/tmp/foo", followed
by "/tmp/bar".
A problem occurred, however if one wanted to created the directory
"/Users/myuser/bar" (or equivalently "/home/myuser/bar"), and "/Users/myuser"
already existed and is writable by the current user. The directory
"/User/myuser" is writable by the user, but "/User" is not. The original
implementation of createDirectoryOnDisk would return with failure since "/User"
is not writable, even though "/User/mysuser" is writable.
The new implementation works by recursively creating parents as needed, and thus
doesn't need to check the permissions on every directory in a path.
llvm-svn: 49162
2008-04-03 18:11:31 +02:00
|
|
|
return false;
|
2009-02-15 04:20:03 +01:00
|
|
|
|
Re-implemented Path::createDirectoryOnDisk (for Unix).
This method allows one to create a directory, and optionally create all parent
directories that do not exist.
The original implementation would require that *all* directories along a path
are writable by the user, including directories that already exist. For example,
suppose we wanted to create the directory "/tmp/foo/bar", and the directory
"/tmp" already exists, but not "/tmp/foo". Since "/tmp" is writable by all
users, the original implementation would work, and create "/tmp/foo", followed
by "/tmp/bar".
A problem occurred, however if one wanted to created the directory
"/Users/myuser/bar" (or equivalently "/home/myuser/bar"), and "/Users/myuser"
already existed and is writable by the current user. The directory
"/User/myuser" is writable by the user, but "/User" is not. The original
implementation of createDirectoryOnDisk would return with failure since "/User"
is not writable, even though "/User/mysuser" is writable.
The new implementation works by recursively creating parents as needed, and thus
doesn't need to check the permissions on every directory in a path.
llvm-svn: 49162
2008-04-03 18:11:31 +02:00
|
|
|
if (create_parents) {
|
2009-02-15 04:20:03 +01:00
|
|
|
|
Re-implemented Path::createDirectoryOnDisk (for Unix).
This method allows one to create a directory, and optionally create all parent
directories that do not exist.
The original implementation would require that *all* directories along a path
are writable by the user, including directories that already exist. For example,
suppose we wanted to create the directory "/tmp/foo/bar", and the directory
"/tmp" already exists, but not "/tmp/foo". Since "/tmp" is writable by all
users, the original implementation would work, and create "/tmp/foo", followed
by "/tmp/bar".
A problem occurred, however if one wanted to created the directory
"/Users/myuser/bar" (or equivalently "/home/myuser/bar"), and "/Users/myuser"
already existed and is writable by the current user. The directory
"/User/myuser" is writable by the user, but "/User" is not. The original
implementation of createDirectoryOnDisk would return with failure since "/User"
is not writable, even though "/User/mysuser" is writable.
The new implementation works by recursively creating parents as needed, and thus
doesn't need to check the permissions on every directory in a path.
llvm-svn: 49162
2008-04-03 18:11:31 +02:00
|
|
|
char* c = end;
|
2009-02-15 04:20:03 +01:00
|
|
|
|
Re-implemented Path::createDirectoryOnDisk (for Unix).
This method allows one to create a directory, and optionally create all parent
directories that do not exist.
The original implementation would require that *all* directories along a path
are writable by the user, including directories that already exist. For example,
suppose we wanted to create the directory "/tmp/foo/bar", and the directory
"/tmp" already exists, but not "/tmp/foo". Since "/tmp" is writable by all
users, the original implementation would work, and create "/tmp/foo", followed
by "/tmp/bar".
A problem occurred, however if one wanted to created the directory
"/Users/myuser/bar" (or equivalently "/home/myuser/bar"), and "/Users/myuser"
already existed and is writable by the current user. The directory
"/User/myuser" is writable by the user, but "/User" is not. The original
implementation of createDirectoryOnDisk would return with failure since "/User"
is not writable, even though "/User/mysuser" is writable.
The new implementation works by recursively creating parents as needed, and thus
doesn't need to check the permissions on every directory in a path.
llvm-svn: 49162
2008-04-03 18:11:31 +02:00
|
|
|
for (; c != beg; --c)
|
|
|
|
if (*c == '/') {
|
2009-02-15 04:20:03 +01:00
|
|
|
|
Re-implemented Path::createDirectoryOnDisk (for Unix).
This method allows one to create a directory, and optionally create all parent
directories that do not exist.
The original implementation would require that *all* directories along a path
are writable by the user, including directories that already exist. For example,
suppose we wanted to create the directory "/tmp/foo/bar", and the directory
"/tmp" already exists, but not "/tmp/foo". Since "/tmp" is writable by all
users, the original implementation would work, and create "/tmp/foo", followed
by "/tmp/bar".
A problem occurred, however if one wanted to created the directory
"/Users/myuser/bar" (or equivalently "/home/myuser/bar"), and "/Users/myuser"
already existed and is writable by the current user. The directory
"/User/myuser" is writable by the user, but "/User" is not. The original
implementation of createDirectoryOnDisk would return with failure since "/User"
is not writable, even though "/User/mysuser" is writable.
The new implementation works by recursively creating parents as needed, and thus
doesn't need to check the permissions on every directory in a path.
llvm-svn: 49162
2008-04-03 18:11:31 +02:00
|
|
|
// Recurse to handling the parent directory.
|
2009-02-15 04:20:03 +01:00
|
|
|
*c = '\0';
|
Re-implemented Path::createDirectoryOnDisk (for Unix).
This method allows one to create a directory, and optionally create all parent
directories that do not exist.
The original implementation would require that *all* directories along a path
are writable by the user, including directories that already exist. For example,
suppose we wanted to create the directory "/tmp/foo/bar", and the directory
"/tmp" already exists, but not "/tmp/foo". Since "/tmp" is writable by all
users, the original implementation would work, and create "/tmp/foo", followed
by "/tmp/bar".
A problem occurred, however if one wanted to created the directory
"/Users/myuser/bar" (or equivalently "/home/myuser/bar"), and "/Users/myuser"
already existed and is writable by the current user. The directory
"/User/myuser" is writable by the user, but "/User" is not. The original
implementation of createDirectoryOnDisk would return with failure since "/User"
is not writable, even though "/User/mysuser" is writable.
The new implementation works by recursively creating parents as needed, and thus
doesn't need to check the permissions on every directory in a path.
llvm-svn: 49162
2008-04-03 18:11:31 +02:00
|
|
|
bool x = createDirectoryHelper(beg, c, create_parents);
|
|
|
|
*c = '/';
|
2009-02-15 04:20:03 +01:00
|
|
|
|
Re-implemented Path::createDirectoryOnDisk (for Unix).
This method allows one to create a directory, and optionally create all parent
directories that do not exist.
The original implementation would require that *all* directories along a path
are writable by the user, including directories that already exist. For example,
suppose we wanted to create the directory "/tmp/foo/bar", and the directory
"/tmp" already exists, but not "/tmp/foo". Since "/tmp" is writable by all
users, the original implementation would work, and create "/tmp/foo", followed
by "/tmp/bar".
A problem occurred, however if one wanted to created the directory
"/Users/myuser/bar" (or equivalently "/home/myuser/bar"), and "/Users/myuser"
already existed and is writable by the current user. The directory
"/User/myuser" is writable by the user, but "/User" is not. The original
implementation of createDirectoryOnDisk would return with failure since "/User"
is not writable, even though "/User/mysuser" is writable.
The new implementation works by recursively creating parents as needed, and thus
doesn't need to check the permissions on every directory in a path.
llvm-svn: 49162
2008-04-03 18:11:31 +02:00
|
|
|
// Return if we encountered an error.
|
|
|
|
if (x)
|
|
|
|
return true;
|
2009-02-15 04:20:03 +01:00
|
|
|
|
Re-implemented Path::createDirectoryOnDisk (for Unix).
This method allows one to create a directory, and optionally create all parent
directories that do not exist.
The original implementation would require that *all* directories along a path
are writable by the user, including directories that already exist. For example,
suppose we wanted to create the directory "/tmp/foo/bar", and the directory
"/tmp" already exists, but not "/tmp/foo". Since "/tmp" is writable by all
users, the original implementation would work, and create "/tmp/foo", followed
by "/tmp/bar".
A problem occurred, however if one wanted to created the directory
"/Users/myuser/bar" (or equivalently "/home/myuser/bar"), and "/Users/myuser"
already existed and is writable by the current user. The directory
"/User/myuser" is writable by the user, but "/User" is not. The original
implementation of createDirectoryOnDisk would return with failure since "/User"
is not writable, even though "/User/mysuser" is writable.
The new implementation works by recursively creating parents as needed, and thus
doesn't need to check the permissions on every directory in a path.
llvm-svn: 49162
2008-04-03 18:11:31 +02:00
|
|
|
break;
|
|
|
|
}
|
2009-02-15 04:20:03 +01:00
|
|
|
}
|
|
|
|
|
Re-implemented Path::createDirectoryOnDisk (for Unix).
This method allows one to create a directory, and optionally create all parent
directories that do not exist.
The original implementation would require that *all* directories along a path
are writable by the user, including directories that already exist. For example,
suppose we wanted to create the directory "/tmp/foo/bar", and the directory
"/tmp" already exists, but not "/tmp/foo". Since "/tmp" is writable by all
users, the original implementation would work, and create "/tmp/foo", followed
by "/tmp/bar".
A problem occurred, however if one wanted to created the directory
"/Users/myuser/bar" (or equivalently "/home/myuser/bar"), and "/Users/myuser"
already existed and is writable by the current user. The directory
"/User/myuser" is writable by the user, but "/User" is not. The original
implementation of createDirectoryOnDisk would return with failure since "/User"
is not writable, even though "/User/mysuser" is writable.
The new implementation works by recursively creating parents as needed, and thus
doesn't need to check the permissions on every directory in a path.
llvm-svn: 49162
2008-04-03 18:11:31 +02:00
|
|
|
return mkdir(beg, S_IRWXU | S_IRWXG) != 0;
|
|
|
|
}
|
|
|
|
|
2004-08-29 07:24:01 +02:00
|
|
|
bool
|
2006-08-23 02:39:35 +02:00
|
|
|
Path::createDirectoryOnDisk( bool create_parents, std::string* ErrMsg ) {
|
2004-08-29 07:24:01 +02:00
|
|
|
// Get a writeable copy of the path name
|
2010-11-02 23:55:34 +01:00
|
|
|
std::string pathname(path);
|
2004-08-29 07:24:01 +02:00
|
|
|
|
|
|
|
// Null-terminate the last component
|
2008-05-05 20:30:58 +02:00
|
|
|
size_t lastchar = path.length() - 1 ;
|
2009-02-15 04:20:03 +01:00
|
|
|
|
Re-implemented Path::createDirectoryOnDisk (for Unix).
This method allows one to create a directory, and optionally create all parent
directories that do not exist.
The original implementation would require that *all* directories along a path
are writable by the user, including directories that already exist. For example,
suppose we wanted to create the directory "/tmp/foo/bar", and the directory
"/tmp" already exists, but not "/tmp/foo". Since "/tmp" is writable by all
users, the original implementation would work, and create "/tmp/foo", followed
by "/tmp/bar".
A problem occurred, however if one wanted to created the directory
"/Users/myuser/bar" (or equivalently "/home/myuser/bar"), and "/Users/myuser"
already existed and is writable by the current user. The directory
"/User/myuser" is writable by the user, but "/User" is not. The original
implementation of createDirectoryOnDisk would return with failure since "/User"
is not writable, even though "/User/mysuser" is writable.
The new implementation works by recursively creating parents as needed, and thus
doesn't need to check the permissions on every directory in a path.
llvm-svn: 49162
2008-04-03 18:11:31 +02:00
|
|
|
if (pathname[lastchar] != '/')
|
|
|
|
++lastchar;
|
2009-02-15 04:20:03 +01:00
|
|
|
|
2010-11-02 23:41:19 +01:00
|
|
|
pathname[lastchar] = '\0';
|
2009-02-15 04:20:03 +01:00
|
|
|
|
2010-11-02 23:55:34 +01:00
|
|
|
if (createDirectoryHelper(&pathname[0], &pathname[lastchar], create_parents))
|
2010-11-03 00:16:26 +01:00
|
|
|
return MakeErrMsg(ErrMsg, pathname + ": can't create directory");
|
2009-02-15 04:20:03 +01:00
|
|
|
|
2006-08-23 02:39:35 +02:00
|
|
|
return false;
|
2004-08-25 08:20:07 +02:00
|
|
|
}
|
|
|
|
|
2004-08-29 07:24:01 +02:00
|
|
|
bool
|
2006-08-23 02:39:35 +02:00
|
|
|
Path::createFileOnDisk(std::string* ErrMsg) {
|
2004-08-29 07:24:01 +02:00
|
|
|
// Create the file
|
2004-09-18 21:25:11 +02:00
|
|
|
int fd = ::creat(path.c_str(), S_IRUSR | S_IWUSR);
|
2006-08-23 09:30:48 +02:00
|
|
|
if (fd < 0)
|
|
|
|
return MakeErrMsg(ErrMsg, path + ": can't create file");
|
2004-09-18 21:25:11 +02:00
|
|
|
::close(fd);
|
2006-08-23 02:39:35 +02:00
|
|
|
return false;
|
2004-08-29 07:24:01 +02:00
|
|
|
}
|
|
|
|
|
2004-11-09 21:26:31 +01:00
|
|
|
bool
|
2006-08-23 02:39:35 +02:00
|
|
|
Path::createTemporaryFileOnDisk(bool reuse_current, std::string* ErrMsg) {
|
2004-12-15 02:50:13 +01:00
|
|
|
// Make this into a unique file name
|
2006-08-23 22:34:57 +02:00
|
|
|
if (makeUnique( reuse_current, ErrMsg ))
|
|
|
|
return true;
|
2004-12-15 02:50:13 +01:00
|
|
|
|
|
|
|
// create the file
|
2006-08-23 02:39:35 +02:00
|
|
|
int fd = ::open(path.c_str(), O_WRONLY|O_CREAT|O_TRUNC, 0666);
|
2009-02-15 04:20:03 +01:00
|
|
|
if (fd < 0)
|
2006-08-23 09:30:48 +02:00
|
|
|
return MakeErrMsg(ErrMsg, path + ": can't create temporary file");
|
2006-08-23 02:39:35 +02:00
|
|
|
::close(fd);
|
2004-12-15 02:50:13 +01:00
|
|
|
return false;
|
2004-11-09 21:26:31 +01:00
|
|
|
}
|
|
|
|
|
2004-08-29 07:24:01 +02:00
|
|
|
bool
|
2006-07-29 00:29:50 +02:00
|
|
|
Path::eraseFromDisk(bool remove_contents, std::string *ErrStr) const {
|
2010-03-30 22:04:57 +02:00
|
|
|
// Get the status so we can determine if it's a file or directory.
|
2007-04-07 20:52:17 +02:00
|
|
|
struct stat buf;
|
|
|
|
if (0 != stat(path.c_str(), &buf)) {
|
|
|
|
MakeErrMsg(ErrStr, path + ": can't get status of file");
|
2006-07-29 00:29:50 +02:00
|
|
|
return true;
|
2007-04-07 20:52:17 +02:00
|
|
|
}
|
|
|
|
|
2009-02-15 04:20:03 +01:00
|
|
|
// Note: this check catches strange situations. In all cases, LLVM should
|
|
|
|
// only be involved in the creation and deletion of regular files. This
|
|
|
|
// check ensures that what we're trying to erase is a regular file. It
|
|
|
|
// effectively prevents LLVM from erasing things like /dev/null, any block
|
|
|
|
// special file, or other things that aren't "regular" files.
|
2007-04-07 20:52:17 +02:00
|
|
|
if (S_ISREG(buf.st_mode)) {
|
|
|
|
if (unlink(path.c_str()) != 0)
|
|
|
|
return MakeErrMsg(ErrStr, path + ": can't destroy file");
|
|
|
|
return false;
|
|
|
|
}
|
2009-02-15 04:20:03 +01:00
|
|
|
|
2007-04-07 20:52:17 +02:00
|
|
|
if (!S_ISDIR(buf.st_mode)) {
|
|
|
|
if (ErrStr) *ErrStr = "not a file or directory";
|
|
|
|
return true;
|
|
|
|
}
|
2007-03-29 21:05:44 +02:00
|
|
|
|
2006-07-29 00:29:50 +02:00
|
|
|
if (remove_contents) {
|
|
|
|
// Recursively descend the directory to remove its contents.
|
|
|
|
std::string cmd = "/bin/rm -rf " + path;
|
2009-02-15 04:20:32 +01:00
|
|
|
if (system(cmd.c_str()) != 0) {
|
|
|
|
MakeErrMsg(ErrStr, path + ": failed to recursively remove directory.");
|
|
|
|
return true;
|
|
|
|
}
|
2005-07-08 01:21:43 +02:00
|
|
|
return false;
|
2006-07-29 00:29:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, try to just remove the one directory.
|
2010-11-02 23:55:34 +01:00
|
|
|
std::string pathname(path);
|
2008-05-05 20:30:58 +02:00
|
|
|
size_t lastchar = path.length() - 1;
|
2009-02-15 04:20:03 +01:00
|
|
|
if (pathname[lastchar] == '/')
|
2010-11-02 23:41:19 +01:00
|
|
|
pathname[lastchar] = '\0';
|
2006-07-29 00:29:50 +02:00
|
|
|
else
|
2010-11-02 23:41:19 +01:00
|
|
|
pathname[lastchar+1] = '\0';
|
2009-02-15 04:20:03 +01:00
|
|
|
|
2010-11-02 23:55:34 +01:00
|
|
|
if (rmdir(pathname.c_str()) != 0)
|
|
|
|
return MakeErrMsg(ErrStr, pathname + ": can't erase directory");
|
2006-07-29 00:29:50 +02:00
|
|
|
return false;
|
2004-08-29 07:24:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2006-08-23 09:30:48 +02:00
|
|
|
Path::renamePathOnDisk(const Path& newName, std::string* ErrMsg) {
|
2005-07-08 01:21:43 +02:00
|
|
|
if (0 != ::rename(path.c_str(), newName.c_str()))
|
2009-02-15 04:20:03 +01:00
|
|
|
return MakeErrMsg(ErrMsg, std::string("can't rename '") + path + "' as '" +
|
2009-08-24 00:45:37 +02:00
|
|
|
newName.str() + "'");
|
2006-08-23 09:30:48 +02:00
|
|
|
return false;
|
2004-11-14 23:08:36 +01:00
|
|
|
}
|
|
|
|
|
2009-02-15 04:20:03 +01:00
|
|
|
bool
|
2006-07-29 00:36:17 +02:00
|
|
|
Path::setStatusInfoOnDisk(const FileStatus &si, std::string *ErrStr) const {
|
2004-11-14 23:08:36 +01:00
|
|
|
struct utimbuf utb;
|
|
|
|
utb.actime = si.modTime.toPosixTime();
|
|
|
|
utb.modtime = utb.actime;
|
|
|
|
if (0 != ::utime(path.c_str(),&utb))
|
2006-08-23 22:34:57 +02:00
|
|
|
return MakeErrMsg(ErrStr, path + ": can't set file modification time");
|
2004-11-14 23:08:36 +01:00
|
|
|
if (0 != ::chmod(path.c_str(),si.mode))
|
2006-08-23 22:34:57 +02:00
|
|
|
return MakeErrMsg(ErrStr, path + ": can't set mode");
|
2006-07-29 00:36:17 +02:00
|
|
|
return false;
|
2004-08-25 08:20:07 +02:00
|
|
|
}
|
|
|
|
|
2009-02-15 04:20:03 +01:00
|
|
|
bool
|
2006-08-23 22:34:57 +02:00
|
|
|
sys::CopyFile(const sys::Path &Dest, const sys::Path &Src, std::string* ErrMsg){
|
2004-12-15 02:50:13 +01:00
|
|
|
int inFile = -1;
|
|
|
|
int outFile = -1;
|
2006-08-23 22:34:57 +02:00
|
|
|
inFile = ::open(Src.c_str(), O_RDONLY);
|
|
|
|
if (inFile == -1)
|
2009-08-24 00:45:37 +02:00
|
|
|
return MakeErrMsg(ErrMsg, Src.str() +
|
2006-08-23 22:34:57 +02:00
|
|
|
": can't open source file to copy");
|
|
|
|
|
|
|
|
outFile = ::open(Dest.c_str(), O_WRONLY|O_CREAT, 0666);
|
|
|
|
if (outFile == -1) {
|
|
|
|
::close(inFile);
|
2009-08-24 00:45:37 +02:00
|
|
|
return MakeErrMsg(ErrMsg, Dest.str() +
|
2006-08-23 22:34:57 +02:00
|
|
|
": can't create destination file for copy");
|
|
|
|
}
|
|
|
|
|
|
|
|
char Buffer[16*1024];
|
|
|
|
while (ssize_t Amt = ::read(inFile, Buffer, 16*1024)) {
|
|
|
|
if (Amt == -1) {
|
|
|
|
if (errno != EINTR && errno != EAGAIN) {
|
|
|
|
::close(inFile);
|
|
|
|
::close(outFile);
|
2009-08-24 00:45:37 +02:00
|
|
|
return MakeErrMsg(ErrMsg, Src.str()+": can't read source file");
|
2006-08-23 22:34:57 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
char *BufPtr = Buffer;
|
|
|
|
while (Amt) {
|
|
|
|
ssize_t AmtWritten = ::write(outFile, BufPtr, Amt);
|
|
|
|
if (AmtWritten == -1) {
|
|
|
|
if (errno != EINTR && errno != EAGAIN) {
|
|
|
|
::close(inFile);
|
|
|
|
::close(outFile);
|
2009-08-24 00:45:37 +02:00
|
|
|
return MakeErrMsg(ErrMsg, Dest.str() +
|
2009-04-20 22:50:13 +02:00
|
|
|
": can't write destination file");
|
2004-12-15 02:50:13 +01:00
|
|
|
}
|
2006-08-23 22:34:57 +02:00
|
|
|
} else {
|
|
|
|
Amt -= AmtWritten;
|
|
|
|
BufPtr += AmtWritten;
|
2004-12-15 02:50:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-08-23 22:34:57 +02:00
|
|
|
::close(inFile);
|
|
|
|
::close(outFile);
|
|
|
|
return false;
|
2004-12-15 02:50:13 +01:00
|
|
|
}
|
|
|
|
|
2009-02-15 04:20:03 +01:00
|
|
|
bool
|
2006-08-23 22:34:57 +02:00
|
|
|
Path::makeUnique(bool reuse_current, std::string* ErrMsg) {
|
2011-01-10 03:34:23 +01:00
|
|
|
bool Exists;
|
|
|
|
if (reuse_current && (fs::exists(path, Exists) || !Exists))
|
2006-08-23 22:34:57 +02:00
|
|
|
return false; // File doesn't exist already, just use it!
|
2004-12-15 02:50:13 +01:00
|
|
|
|
2009-02-15 04:20:03 +01:00
|
|
|
// Append an XXXXXX pattern to the end of the file for use with mkstemp,
|
2004-12-15 02:50:13 +01:00
|
|
|
// mktemp or our own implementation.
|
2010-04-19 17:54:44 +02:00
|
|
|
// This uses std::vector instead of SmallVector to avoid a dependence on
|
|
|
|
// libSupport. And performance isn't critical here.
|
|
|
|
std::vector<char> Buf;
|
|
|
|
Buf.resize(path.size()+8);
|
2010-04-19 18:33:28 +02:00
|
|
|
char *FNBuffer = &Buf[0];
|
2010-04-19 17:54:44 +02:00
|
|
|
path.copy(FNBuffer,path.size());
|
2011-01-11 02:21:55 +01:00
|
|
|
bool isdir;
|
|
|
|
if (!fs::is_directory(path, isdir) && isdir)
|
2010-04-19 17:54:44 +02:00
|
|
|
strcpy(FNBuffer+path.size(), "/XXXXXX");
|
2008-07-24 02:35:38 +02:00
|
|
|
else
|
2010-04-19 17:54:44 +02:00
|
|
|
strcpy(FNBuffer+path.size(), "-XXXXXX");
|
2004-12-15 02:50:13 +01:00
|
|
|
|
|
|
|
#if defined(HAVE_MKSTEMP)
|
|
|
|
int TempFD;
|
2010-04-19 17:54:44 +02:00
|
|
|
if ((TempFD = mkstemp(FNBuffer)) == -1)
|
2006-08-23 22:34:57 +02:00
|
|
|
return MakeErrMsg(ErrMsg, path + ": can't make unique filename");
|
2004-12-15 02:50:13 +01:00
|
|
|
|
|
|
|
// We don't need to hold the temp file descriptor... we will trust that no one
|
|
|
|
// will overwrite/delete the file before we can open it again.
|
|
|
|
close(TempFD);
|
|
|
|
|
|
|
|
// Save the name
|
2010-04-19 17:54:44 +02:00
|
|
|
path = FNBuffer;
|
2004-12-15 02:50:13 +01:00
|
|
|
#elif defined(HAVE_MKTEMP)
|
|
|
|
// If we don't have mkstemp, use the old and obsolete mktemp function.
|
2010-04-19 17:54:44 +02:00
|
|
|
if (mktemp(FNBuffer) == 0)
|
2006-08-23 22:34:57 +02:00
|
|
|
return MakeErrMsg(ErrMsg, path + ": can't make unique filename");
|
2004-12-15 02:50:13 +01:00
|
|
|
|
|
|
|
// Save the name
|
2010-04-19 17:54:44 +02:00
|
|
|
path = FNBuffer;
|
2004-12-15 02:50:13 +01:00
|
|
|
#else
|
|
|
|
// Okay, looks like we have to do it all by our lonesome.
|
|
|
|
static unsigned FCounter = 0;
|
2010-07-12 02:09:55 +02:00
|
|
|
// Try to initialize with unique value.
|
|
|
|
if (FCounter == 0) FCounter = ((unsigned)getpid() & 0xFFFF) << 8;
|
|
|
|
char* pos = strstr(FNBuffer, "XXXXXX");
|
|
|
|
do {
|
|
|
|
if (++FCounter > 0xFFFFFF) {
|
|
|
|
return MakeErrMsg(ErrMsg,
|
|
|
|
path + ": can't make unique filename: too many files");
|
|
|
|
}
|
|
|
|
sprintf(pos, "%06X", FCounter);
|
2010-04-19 17:54:44 +02:00
|
|
|
path = FNBuffer;
|
2010-07-12 02:09:55 +02:00
|
|
|
} while (exists());
|
|
|
|
// POSSIBLE SECURITY BUG: An attacker can easily guess the name and exploit
|
|
|
|
// LLVM.
|
2004-12-15 02:50:13 +01:00
|
|
|
#endif
|
2006-08-23 22:34:57 +02:00
|
|
|
return false;
|
2004-08-25 08:20:07 +02:00
|
|
|
}
|
|
|
|
|
2011-03-10 19:33:29 +01:00
|
|
|
const char *Path::MapInFilePages(int FD, size_t FileSize, off_t Offset) {
|
2008-04-01 08:16:24 +02:00
|
|
|
int Flags = MAP_PRIVATE;
|
|
|
|
#ifdef MAP_FILE
|
|
|
|
Flags |= MAP_FILE;
|
|
|
|
#endif
|
2011-03-10 19:33:29 +01:00
|
|
|
void *BasePtr = ::mmap(0, FileSize, PROT_READ, Flags, FD, Offset);
|
2008-04-01 08:16:24 +02:00
|
|
|
if (BasePtr == MAP_FAILED)
|
|
|
|
return 0;
|
2008-04-01 08:25:23 +02:00
|
|
|
return (const char*)BasePtr;
|
2008-04-01 08:00:12 +02:00
|
|
|
}
|
|
|
|
|
2011-03-10 19:33:29 +01:00
|
|
|
void Path::UnMapFilePages(const char *BasePtr, size_t FileSize) {
|
2008-04-01 08:25:23 +02:00
|
|
|
::munmap((void*)BasePtr, FileSize);
|
2008-04-01 08:00:12 +02:00
|
|
|
}
|
|
|
|
|
2006-08-23 22:34:57 +02:00
|
|
|
} // end llvm namespace
|