2010-11-30 03:44:50 +08:00
|
|
|
//===- llvm/Support/Unix/Path.cpp - Unix Path Implementation -----*- C++ -*-===//
|
2009-02-15 11:20:03 +08:00
|
|
|
//
|
2004-08-25 14:20:07 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2009-02-15 11:20:03 +08:00
|
|
|
//
|
2004-08-25 14:20:07 +08: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 13:24:01 +08:00
|
|
|
//=== is guaranteed to work on *all* UNIX variants.
|
2004-08-25 14:20:07 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Unix.h"
|
2004-12-27 14:17:15 +08:00
|
|
|
#if HAVE_SYS_STAT_H
|
2004-08-25 14:20:07 +08:00
|
|
|
#include <sys/stat.h>
|
2004-12-27 14:17:15 +08:00
|
|
|
#endif
|
|
|
|
#if HAVE_FCNTL_H
|
2004-08-25 14:20:07 +08:00
|
|
|
#include <fcntl.h>
|
2004-12-27 14:17:15 +08:00
|
|
|
#endif
|
2008-04-01 14:25:23 +08:00
|
|
|
#ifdef HAVE_SYS_MMAN_H
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_STAT_H
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
2004-12-27 14:17:15 +08:00
|
|
|
#if HAVE_UTIME_H
|
2004-11-15 06:08:36 +08:00
|
|
|
#include <utime.h>
|
2004-12-27 14:17:15 +08:00
|
|
|
#endif
|
|
|
|
#if HAVE_TIME_H
|
2004-12-24 14:29:42 +08:00
|
|
|
#include <time.h>
|
2004-12-27 14:17:15 +08: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 10:55:43 +08:00
|
|
|
#if HAVE_DLFCN_H
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#endif
|
|
|
|
|
2009-09-09 20:09:08 +08:00
|
|
|
#ifdef __APPLE__
|
|
|
|
#include <mach-o/dyld.h>
|
|
|
|
#endif
|
|
|
|
|
2012-04-11 23:35:36 +08:00
|
|
|
// For GNU Hurd
|
|
|
|
#if defined(__GNU__) && !defined(MAXPATHLEN)
|
|
|
|
# define MAXPATHLEN 4096
|
|
|
|
#endif
|
|
|
|
|
2005-06-02 13:38:20 +08: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 13:24:01 +08:00
|
|
|
|
2005-07-29 00:25:57 +08:00
|
|
|
namespace {
|
|
|
|
inline bool lastIsSlash(const std::string& path) {
|
|
|
|
return !path.empty() && path[path.length() - 1] == '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2004-08-29 13:24:01 +08:00
|
|
|
namespace llvm {
|
|
|
|
using namespace sys;
|
|
|
|
|
2009-12-09 11:26:33 +08:00
|
|
|
const char sys::PathSeparator = ':';
|
2008-02-27 14:17:10 +08:00
|
|
|
|
2010-11-03 04:32:26 +08:00
|
|
|
StringRef Path::GetEXESuffix() {
|
2010-11-03 04:52:47 +08:00
|
|
|
return StringRef();
|
2010-11-03 04:32:26 +08:00
|
|
|
}
|
|
|
|
|
2009-12-18 05:02:39 +08:00
|
|
|
Path::Path(StringRef p)
|
2008-05-12 01:37:40 +08:00
|
|
|
: path(p) {}
|
|
|
|
|
|
|
|
Path::Path(const char *StrStart, unsigned StrLen)
|
|
|
|
: path(StrStart, StrLen) {}
|
|
|
|
|
2008-08-12 07:39:47 +08:00
|
|
|
Path&
|
2009-12-18 05:02:39 +08:00
|
|
|
Path::operator=(StringRef that) {
|
|
|
|
path.assign(that.data(), that.size());
|
2008-08-12 07:39:47 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2009-02-15 11:20:03 +08:00
|
|
|
bool
|
2004-12-24 14:29:42 +08:00
|
|
|
Path::isValid() const {
|
2010-11-03 07:19:55 +08: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 14:29:42 +08:00
|
|
|
}
|
|
|
|
|
2009-06-15 12:17:07 +08:00
|
|
|
bool
|
|
|
|
Path::isAbsolute(const char *NameStart, unsigned NameLen) {
|
|
|
|
assert(NameStart);
|
|
|
|
if (NameLen == 0)
|
|
|
|
return false;
|
|
|
|
return NameStart[0] == '/';
|
|
|
|
}
|
|
|
|
|
2009-02-15 11:20:03 +08:00
|
|
|
bool
|
2007-03-30 00:43:20 +08:00
|
|
|
Path::isAbsolute() const {
|
|
|
|
if (path.empty())
|
|
|
|
return false;
|
|
|
|
return path[0] == '/';
|
2009-02-15 11:20:03 +08:00
|
|
|
}
|
2009-07-13 04:23:56 +08:00
|
|
|
|
2004-08-29 13:24:01 +08:00
|
|
|
Path
|
|
|
|
Path::GetRootDirectory() {
|
|
|
|
Path result;
|
2005-07-08 07:21:43 +08:00
|
|
|
result.set("/");
|
2004-08-29 13:24:01 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2004-12-24 14:29:42 +08:00
|
|
|
Path
|
2009-02-19 13:34:35 +08:00
|
|
|
Path::GetTemporaryDirectory(std::string *ErrMsg) {
|
2004-12-24 14:29:42 +08:00
|
|
|
#if defined(HAVE_MKDTEMP)
|
2009-02-15 11:20:03 +08:00
|
|
|
// The best way is with mkdtemp but that's not available on many systems,
|
2004-12-24 14:29:42 +08:00
|
|
|
// Linux and FreeBSD have it. Others probably won't.
|
2010-11-03 06:50:10 +08:00
|
|
|
char pathname[] = "/tmp/llvm_XXXXXX";
|
2006-08-23 03:01:30 +08:00
|
|
|
if (0 == mkdtemp(pathname)) {
|
2010-11-03 06:56:51 +08:00
|
|
|
MakeErrMsg(ErrMsg,
|
|
|
|
std::string(pathname) + ": can't create temporary directory");
|
2006-08-23 03:01:30 +08:00
|
|
|
return Path();
|
|
|
|
}
|
2010-11-03 08:01:23 +08:00
|
|
|
return Path(pathname);
|
2004-12-24 14:29:42 +08: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 11:20:03 +08:00
|
|
|
// the time it is re-created as a directoy.
|
2010-11-03 06:50:10 +08:00
|
|
|
char pathname[] = "/tmp/llvm_XXXXXX";
|
2004-12-24 14:29:42 +08:00
|
|
|
int fd = 0;
|
2006-08-23 03:01:30 +08:00
|
|
|
if (-1 == (fd = mkstemp(pathname))) {
|
2009-02-15 11:20:03 +08:00
|
|
|
MakeErrMsg(ErrMsg,
|
2006-08-23 03:01:30 +08:00
|
|
|
std::string(pathname) + ": can't create temporary directory");
|
|
|
|
return Path();
|
|
|
|
}
|
2004-12-24 14:29:42 +08:00
|
|
|
::close(fd);
|
|
|
|
::unlink(pathname); // start race condition, ignore errors
|
2006-08-23 03:01:30 +08:00
|
|
|
if (-1 == ::mkdir(pathname, S_IRWXU)) { // end race condition
|
2009-02-15 11:20:03 +08:00
|
|
|
MakeErrMsg(ErrMsg,
|
2006-08-23 03:01:30 +08:00
|
|
|
std::string(pathname) + ": can't create temporary directory");
|
|
|
|
return Path();
|
|
|
|
}
|
2010-11-03 08:01:23 +08:00
|
|
|
return Path(pathname);
|
2004-12-24 14:29:42 +08: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-03 06:50:10 +08:00
|
|
|
char pathname[] = "/tmp/llvm_XXXXXX";
|
2004-12-24 14:29:42 +08:00
|
|
|
char *TmpName = ::mktemp(pathname);
|
2006-08-23 03:01:30 +08:00
|
|
|
if (TmpName == 0) {
|
2009-02-15 11:20:03 +08:00
|
|
|
MakeErrMsg(ErrMsg,
|
2006-08-23 03:01:30 +08:00
|
|
|
std::string(TmpName) + ": can't create unique directory name");
|
|
|
|
return Path();
|
|
|
|
}
|
|
|
|
if (-1 == ::mkdir(TmpName, S_IRWXU)) {
|
2009-02-15 11:20:03 +08:00
|
|
|
MakeErrMsg(ErrMsg,
|
2006-08-23 03:01:30 +08:00
|
|
|
std::string(TmpName) + ": can't create temporary directory");
|
|
|
|
return Path();
|
|
|
|
}
|
2010-11-03 08:01:23 +08:00
|
|
|
return Path(TmpName);
|
2004-12-24 14:29:42 +08: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 11:20:03 +08:00
|
|
|
// temporary name generated. So, we provide our own implementation that
|
2004-12-24 14:29:42 +08: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-23 03:01:30 +08:00
|
|
|
if (-1 == ::mkdir(pathname, S_IRWXU)) {
|
2009-02-15 11:20:03 +08:00
|
|
|
MakeErrMsg(ErrMsg,
|
2006-08-23 03:01:30 +08:00
|
|
|
std::string(pathname) + ": can't create temporary directory");
|
|
|
|
return Path();
|
2006-08-24 04:34:57 +08:00
|
|
|
}
|
2010-11-03 08:01:23 +08:00
|
|
|
return Path(pathname);
|
2004-12-24 14:29:42 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-02-15 11:20:03 +08:00
|
|
|
void
|
2004-12-13 11:00:51 +08: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 08:16:39 +08:00
|
|
|
}
|
2004-12-13 11:00:51 +08: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-06 01:07:56 +08:00
|
|
|
Path::GetBitcodeLibraryPaths(std::vector<sys::Path>& Paths) {
|
2004-12-13 11:00:51 +08: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 07:21:43 +08:00
|
|
|
if (tmpPath.set(LLVM_LIBDIR))
|
2005-07-08 02:21:42 +08:00
|
|
|
if (tmpPath.canRead())
|
2004-12-13 11:00:51 +08:00
|
|
|
Paths.push_back(tmpPath);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
GetSystemLibraryPaths(Paths);
|
2004-08-29 13:24:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Path
|
|
|
|
Path::GetUserHomeDirectory() {
|
|
|
|
const char* home = getenv("HOME");
|
2010-12-27 11:21:41 +08:00
|
|
|
Path result;
|
|
|
|
if (home && result.set(home))
|
|
|
|
return result;
|
|
|
|
result.set("/");
|
|
|
|
return result;
|
2004-08-29 13:24:01 +08:00
|
|
|
}
|
2004-08-25 14:20:07 +08:00
|
|
|
|
2007-12-19 06:07:33 +08:00
|
|
|
Path
|
|
|
|
Path::GetCurrentDirectory() {
|
|
|
|
char pathname[MAXPATHLEN];
|
2011-07-29 12:42:39 +08:00
|
|
|
if (!getcwd(pathname, MAXPATHLEN)) {
|
|
|
|
assert(false && "Could not query current working directory.");
|
2010-08-04 09:39:08 +08:00
|
|
|
return Path();
|
2007-12-19 06:07:33 +08:00
|
|
|
}
|
2009-02-15 11:20:03 +08:00
|
|
|
|
2007-12-19 06:07:33 +08:00
|
|
|
return Path(pathname);
|
|
|
|
}
|
2005-07-08 11:08:58 +08:00
|
|
|
|
2012-08-07 04:52:18 +08:00
|
|
|
#if defined(__FreeBSD__) || defined (__NetBSD__) || defined(__Bitrig__) || \
|
2012-03-26 20:05:51 +08:00
|
|
|
defined(__OpenBSD__) || defined(__minix) || defined(__FreeBSD_kernel__)
|
2009-03-03 06:17:15 +08:00
|
|
|
static int
|
|
|
|
test_dir(char buf[PATH_MAX], char ret[PATH_MAX],
|
|
|
|
const char *dir, const char *bin)
|
|
|
|
{
|
2009-05-30 09:09:53 +08:00
|
|
|
struct stat sb;
|
2009-03-03 06:17:15 +08:00
|
|
|
|
2010-09-03 02:24:46 +08:00
|
|
|
snprintf(buf, PATH_MAX, "%s/%s", dir, bin);
|
2009-05-30 09:09:53 +08:00
|
|
|
if (realpath(buf, ret) == NULL)
|
|
|
|
return (1);
|
|
|
|
if (stat(buf, &sb) != 0)
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
return (0);
|
2009-03-03 06:17:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
getprogpath(char ret[PATH_MAX], const char *bin)
|
|
|
|
{
|
2009-05-30 09:09:53 +08: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-03 06:17:15 +08:00
|
|
|
}
|
2012-03-26 20:05:51 +08:00
|
|
|
#endif // __FreeBSD__ || __NetBSD__ || __FreeBSD_kernel__
|
2009-03-03 06:17:15 +08:00
|
|
|
|
2008-03-03 10:55:43 +08: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 20:09:08 +08: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-30 01:19:48 +08:00
|
|
|
if (realpath(exe_path, link_path))
|
2010-11-03 06:07:47 +08:00
|
|
|
return Path(link_path);
|
2009-09-09 20:09:08 +08:00
|
|
|
}
|
2012-08-07 04:52:18 +08:00
|
|
|
#elif defined(__FreeBSD__) || defined (__NetBSD__) || defined(__Bitrig__) || \
|
2012-03-26 20:05:51 +08:00
|
|
|
defined(__OpenBSD__) || defined(__minix) || defined(__FreeBSD_kernel__)
|
2009-03-03 06:17:15 +08:00
|
|
|
char exe_path[PATH_MAX];
|
|
|
|
|
|
|
|
if (getprogpath(exe_path, argv0) != NULL)
|
2010-11-03 06:07:47 +08:00
|
|
|
return Path(exe_path);
|
2009-03-03 06:17:15 +08:00
|
|
|
#elif defined(__linux__) || defined(__CYGWIN__)
|
2008-03-13 13:22:05 +08:00
|
|
|
char exe_path[MAXPATHLEN];
|
2008-06-28 06:55:30 +08:00
|
|
|
ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path));
|
2009-08-06 04:16:55 +08:00
|
|
|
if (len >= 0)
|
2010-11-03 06:07:47 +08:00
|
|
|
return Path(StringRef(exe_path, len));
|
2008-03-13 13:22:05 +08:00
|
|
|
#elif defined(HAVE_DLFCN_H)
|
2008-03-03 10:55:43 +08:00
|
|
|
// Use dladdr to get executable path if available.
|
|
|
|
Dl_info DLInfo;
|
|
|
|
int err = dladdr(MainAddr, &DLInfo);
|
2009-02-19 13:34:35 +08:00
|
|
|
if (err == 0)
|
|
|
|
return Path();
|
2009-05-30 09:09:53 +08:00
|
|
|
|
2009-02-19 13:34:35 +08: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-30 01:19:48 +08:00
|
|
|
if (realpath(DLInfo.dli_fname, link_path))
|
2010-11-03 06:07:47 +08:00
|
|
|
return Path(link_path);
|
2010-09-08 02:26:49 +08:00
|
|
|
#else
|
|
|
|
#error GetMainExecutable is not implemented on this host yet.
|
2008-03-03 10:55:43 +08:00
|
|
|
#endif
|
|
|
|
return Path();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-18 05:02:39 +08:00
|
|
|
StringRef Path::getDirname() const {
|
|
|
|
return getDirnameCharSep(path, "/");
|
2008-04-08 06:01:32 +08:00
|
|
|
}
|
2008-04-08 05:53:57 +08:00
|
|
|
|
2009-12-18 05:02:39 +08:00
|
|
|
StringRef
|
2004-11-06 06:15:36 +08:00
|
|
|
Path::getBasename() const {
|
2004-09-11 12:55:08 +08:00
|
|
|
// Find the last slash
|
2008-04-08 05:53:57 +08:00
|
|
|
std::string::size_type slash = path.rfind('/');
|
2004-09-11 12:55:08 +08:00
|
|
|
if (slash == std::string::npos)
|
|
|
|
slash = 0;
|
|
|
|
else
|
|
|
|
slash++;
|
|
|
|
|
2008-04-08 05:53:57 +08:00
|
|
|
std::string::size_type dot = path.rfind('.');
|
2005-07-10 02:42:02 +08:00
|
|
|
if (dot == std::string::npos || dot < slash)
|
2009-12-18 05:02:39 +08:00
|
|
|
return StringRef(path).substr(slash);
|
2005-07-10 02:42:02 +08:00
|
|
|
else
|
2009-12-18 05:02:39 +08:00
|
|
|
return StringRef(path).substr(slash, dot - slash);
|
2004-09-11 12:55:08 +08:00
|
|
|
}
|
|
|
|
|
2009-12-18 05:02:39 +08:00
|
|
|
StringRef
|
2008-06-15 23:15:19 +08: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 09:39:08 +08:00
|
|
|
return StringRef();
|
2008-06-15 23:15:19 +08:00
|
|
|
else
|
2009-12-18 05:02:39 +08:00
|
|
|
return StringRef(path).substr(dot + 1);
|
2008-06-15 23:15:19 +08:00
|
|
|
}
|
|
|
|
|
2009-12-16 16:35:54 +08:00
|
|
|
bool Path::getMagicNumber(std::string &Magic, unsigned len) const {
|
2004-11-17 01:14:08 +08:00
|
|
|
assert(len < 1024 && "Request for magic string too long");
|
2009-12-16 16:35:54 +08:00
|
|
|
char Buf[1025];
|
2006-08-02 01:51:09 +08:00
|
|
|
int fd = ::open(path.c_str(), O_RDONLY);
|
2004-11-17 01:14:08 +08:00
|
|
|
if (fd < 0)
|
|
|
|
return false;
|
2009-12-16 16:35:54 +08:00
|
|
|
ssize_t bytes_read = ::read(fd, Buf, len);
|
2004-12-02 17:09:48 +08:00
|
|
|
::close(fd);
|
2010-05-28 01:14:10 +08:00
|
|
|
if (ssize_t(len) != bytes_read)
|
2004-11-17 01:14:08 +08:00
|
|
|
return false;
|
2009-12-16 16:35:54 +08:00
|
|
|
Magic.assign(Buf, len);
|
2004-11-15 06:08:36 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-08-25 14:20:07 +08:00
|
|
|
bool
|
2004-08-29 13:24:01 +08:00
|
|
|
Path::exists() const {
|
|
|
|
return 0 == access(path.c_str(), F_OK );
|
2004-08-25 14:20:07 +08:00
|
|
|
}
|
|
|
|
|
2007-12-19 03:46:22 +08:00
|
|
|
bool
|
|
|
|
Path::isDirectory() const {
|
|
|
|
struct stat buf;
|
|
|
|
if (0 != stat(path.c_str(), &buf))
|
|
|
|
return false;
|
2010-10-08 06:05:57 +08:00
|
|
|
return ((buf.st_mode & S_IFMT) == S_IFDIR) ? true : false;
|
2007-12-19 03:46:22 +08:00
|
|
|
}
|
|
|
|
|
2010-11-07 12:36:50 +08: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 14:20:07 +08:00
|
|
|
bool
|
2005-07-08 02:21:42 +08:00
|
|
|
Path::canRead() const {
|
2009-07-29 07:22:01 +08:00
|
|
|
return 0 == access(path.c_str(), R_OK);
|
2004-08-29 13:24:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2005-07-08 02:21:42 +08:00
|
|
|
Path::canWrite() const {
|
2009-07-29 07:22:01 +08:00
|
|
|
return 0 == access(path.c_str(), W_OK);
|
2004-08-25 14:20:07 +08:00
|
|
|
}
|
|
|
|
|
2009-11-24 23:19:10 +08:00
|
|
|
bool
|
2009-11-25 14:32:19 +08:00
|
|
|
Path::isRegularFile() const {
|
2010-03-31 04:04:57 +08:00
|
|
|
// Get the status so we can determine if it's a file or directory
|
2009-11-24 23:19:10 +08:00
|
|
|
struct stat buf;
|
|
|
|
|
2009-11-25 03:03:33 +08:00
|
|
|
if (0 != stat(path.c_str(), &buf))
|
2009-11-24 23:19:10 +08:00
|
|
|
return false;
|
|
|
|
|
2009-11-25 14:32:19 +08:00
|
|
|
if (S_ISREG(buf.st_mode))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
2009-11-24 23:19:10 +08:00
|
|
|
}
|
|
|
|
|
2004-08-29 13:24:01 +08:00
|
|
|
bool
|
2005-07-08 02:21:42 +08:00
|
|
|
Path::canExecute() const {
|
2005-07-09 01:46:10 +08:00
|
|
|
if (0 != access(path.c_str(), R_OK | X_OK ))
|
|
|
|
return false;
|
2007-04-08 02:52:17 +08:00
|
|
|
struct stat buf;
|
|
|
|
if (0 != stat(path.c_str(), &buf))
|
|
|
|
return false;
|
|
|
|
if (!S_ISREG(buf.st_mode))
|
2005-04-20 23:33:22 +08:00
|
|
|
return false;
|
2005-07-09 01:46:10 +08:00
|
|
|
return true;
|
2004-08-29 13:24:01 +08:00
|
|
|
}
|
|
|
|
|
2009-12-18 05:02:39 +08:00
|
|
|
StringRef
|
2004-08-29 13:24:01 +08: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-18 05:02:39 +08:00
|
|
|
return StringRef(path).substr(0,pos);
|
2004-08-29 13:24:01 +08:00
|
|
|
else
|
2009-12-18 05:02:39 +08:00
|
|
|
return StringRef(path).substr(pos2+1,pos-pos2-1);
|
2004-08-25 14:20:07 +08:00
|
|
|
}
|
2004-08-29 13:24:01 +08:00
|
|
|
// Return everything after the last slash
|
2009-12-18 05:02:39 +08:00
|
|
|
return StringRef(path).substr(pos+1);
|
2004-08-25 14:20:07 +08:00
|
|
|
}
|
|
|
|
|
2007-04-08 02:52:17 +08:00
|
|
|
const FileStatus *
|
|
|
|
PathWithStatus::getFileStatus(bool update, std::string *ErrStr) const {
|
|
|
|
if (!fsIsValid || update) {
|
2007-03-30 00:43:20 +08:00
|
|
|
struct stat buf;
|
2007-03-30 03:05:44 +08:00
|
|
|
if (0 != stat(path.c_str(), &buf)) {
|
|
|
|
MakeErrMsg(ErrStr, path + ": can't get status of file");
|
|
|
|
return 0;
|
|
|
|
}
|
2007-04-08 02:52:17 +08: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-30 00:43:20 +08:00
|
|
|
}
|
2007-04-08 02:52:17 +08:00
|
|
|
return &status;
|
2004-11-15 06:08:36 +08:00
|
|
|
}
|
|
|
|
|
2006-07-29 06:03:44 +08:00
|
|
|
static bool AddPermissionBits(const Path &File, int bits) {
|
2004-12-14 03:59:50 +08: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-08 02:52:17 +08:00
|
|
|
struct stat buf;
|
2009-08-24 06:45:37 +08:00
|
|
|
if (0 != stat(File.c_str(), &buf))
|
2004-12-14 03:59:50 +08:00
|
|
|
return false;
|
2007-04-08 02:52:17 +08: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-14 03:59:50 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-08-23 07:27:23 +08:00
|
|
|
bool Path::makeReadableOnDisk(std::string* ErrMsg) {
|
2009-02-15 11:20:03 +08:00
|
|
|
if (!AddPermissionBits(*this, 0444))
|
2006-08-23 15:30:48 +08:00
|
|
|
return MakeErrMsg(ErrMsg, path + ": can't make file readable");
|
2006-08-23 07:27:23 +08:00
|
|
|
return false;
|
2004-12-14 03:59:50 +08:00
|
|
|
}
|
|
|
|
|
2006-08-23 07:27:23 +08:00
|
|
|
bool Path::makeWriteableOnDisk(std::string* ErrMsg) {
|
2006-08-23 15:30:48 +08:00
|
|
|
if (!AddPermissionBits(*this, 0222))
|
|
|
|
return MakeErrMsg(ErrMsg, path + ": can't make file writable");
|
2006-08-23 07:27:23 +08:00
|
|
|
return false;
|
2004-12-14 03:59:50 +08:00
|
|
|
}
|
|
|
|
|
2006-08-23 07:27:23 +08:00
|
|
|
bool Path::makeExecutableOnDisk(std::string* ErrMsg) {
|
2006-08-23 15:30:48 +08:00
|
|
|
if (!AddPermissionBits(*this, 0111))
|
|
|
|
return MakeErrMsg(ErrMsg, path + ": can't make file executable");
|
2006-08-23 07:27:23 +08:00
|
|
|
return false;
|
2004-12-14 03:59:50 +08:00
|
|
|
}
|
|
|
|
|
2004-11-15 06:08:36 +08:00
|
|
|
bool
|
2006-08-23 14:56:27 +08:00
|
|
|
Path::getDirectoryContents(std::set<Path>& result, std::string* ErrMsg) const {
|
2004-11-15 06:08:36 +08:00
|
|
|
DIR* direntries = ::opendir(path.c_str());
|
2006-08-23 15:30:48 +08:00
|
|
|
if (direntries == 0)
|
|
|
|
return MakeErrMsg(ErrMsg, path + ": can't open directory");
|
2004-11-15 06:08:36 +08:00
|
|
|
|
2005-07-29 00:25:57 +08:00
|
|
|
std::string dirPath = path;
|
|
|
|
if (!lastIsSlash(dirPath))
|
|
|
|
dirPath += '/';
|
|
|
|
|
2004-11-15 06:08:36 +08:00
|
|
|
result.clear();
|
|
|
|
struct dirent* de = ::readdir(direntries);
|
2005-04-20 12:04:07 +08:00
|
|
|
for ( ; de != 0; de = ::readdir(direntries)) {
|
2004-11-15 06:08:36 +08:00
|
|
|
if (de->d_name[0] != '.') {
|
2005-07-29 00:25:57 +08:00
|
|
|
Path aPath(dirPath + (const char*)de->d_name);
|
2006-07-08 05:21:06 +08:00
|
|
|
struct stat st;
|
|
|
|
if (0 != lstat(aPath.path.c_str(), &st)) {
|
|
|
|
if (S_ISLNK(st.st_mode))
|
2005-04-20 12:04:07 +08:00
|
|
|
continue; // dangling symlink -- ignore
|
2009-02-15 11:20:03 +08:00
|
|
|
return MakeErrMsg(ErrMsg,
|
2006-08-23 15:30:48 +08:00
|
|
|
aPath.path + ": can't determine file object type");
|
2005-04-20 12:04:07 +08:00
|
|
|
}
|
2004-11-16 14:15:19 +08:00
|
|
|
result.insert(aPath);
|
2004-11-15 06:08:36 +08:00
|
|
|
}
|
|
|
|
}
|
2009-02-15 11:20:03 +08:00
|
|
|
|
2004-11-15 06:08:36 +08:00
|
|
|
closedir(direntries);
|
2006-08-23 14:56:27 +08:00
|
|
|
return false;
|
2004-11-10 04:26:31 +08:00
|
|
|
}
|
|
|
|
|
2004-08-29 13:24:01 +08:00
|
|
|
bool
|
2009-12-18 05:02:39 +08:00
|
|
|
Path::set(StringRef a_path) {
|
2005-07-08 07:21:43 +08:00
|
|
|
if (a_path.empty())
|
2004-08-29 13:24:01 +08:00
|
|
|
return false;
|
|
|
|
path = a_path;
|
|
|
|
return true;
|
2004-08-25 14:20:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2009-12-18 05:02:39 +08:00
|
|
|
Path::appendComponent(StringRef name) {
|
2005-07-08 07:21:43 +08:00
|
|
|
if (name.empty())
|
2004-08-29 13:24:01 +08:00
|
|
|
return false;
|
2005-07-29 00:25:57 +08:00
|
|
|
if (!lastIsSlash(path))
|
|
|
|
path += '/';
|
2005-07-08 07:21:43 +08:00
|
|
|
path += name;
|
2004-08-29 13:24:01 +08:00
|
|
|
return true;
|
2004-08-25 14:20:07 +08:00
|
|
|
}
|
|
|
|
|
2004-08-29 13:24:01 +08:00
|
|
|
bool
|
2005-07-08 07:21:43 +08:00
|
|
|
Path::eraseComponent() {
|
2004-08-29 13:24:01 +08:00
|
|
|
size_t slashpos = path.rfind('/',path.size());
|
2005-07-08 07:21:43 +08:00
|
|
|
if (slashpos == 0 || slashpos == std::string::npos) {
|
|
|
|
path.erase();
|
|
|
|
return true;
|
|
|
|
}
|
2004-08-29 13:24:01 +08:00
|
|
|
if (slashpos == path.size() - 1)
|
|
|
|
slashpos = path.rfind('/',slashpos-1);
|
2005-07-08 07:21:43 +08:00
|
|
|
if (slashpos == std::string::npos) {
|
|
|
|
path.erase();
|
|
|
|
return true;
|
2004-08-25 14:20:07 +08:00
|
|
|
}
|
2005-07-08 07:21:43 +08:00
|
|
|
path.erase(slashpos);
|
2004-08-29 13:24:01 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-07-08 07:21:43 +08:00
|
|
|
bool
|
|
|
|
Path::eraseSuffix() {
|
2004-08-29 13:24:01 +08:00
|
|
|
size_t dotpos = path.rfind('.',path.size());
|
|
|
|
size_t slashpos = path.rfind('/',path.size());
|
2005-07-08 12:49:16 +08:00
|
|
|
if (dotpos != std::string::npos) {
|
2005-07-10 02:42:02 +08:00
|
|
|
if (slashpos == std::string::npos || dotpos > slashpos+1) {
|
2005-07-08 12:49:16 +08:00
|
|
|
path.erase(dotpos, path.size()-dotpos);
|
2005-07-08 13:02:13 +08:00
|
|
|
return true;
|
2005-07-08 12:49:16 +08:00
|
|
|
}
|
2005-07-08 07:21:43 +08:00
|
|
|
}
|
2005-07-08 12:49:16 +08:00
|
|
|
return false;
|
2004-08-29 13:24:01 +08: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-04 00:11:31 +08:00
|
|
|
static bool createDirectoryHelper(char* beg, char* end, bool create_parents) {
|
2009-02-15 11:20:03 +08:00
|
|
|
|
2009-07-29 08:02:58 +08: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-04 00:11:31 +08:00
|
|
|
return false;
|
2009-02-15 11:20:03 +08: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-04 00:11:31 +08:00
|
|
|
if (create_parents) {
|
2009-02-15 11:20:03 +08: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-04 00:11:31 +08:00
|
|
|
char* c = end;
|
2009-02-15 11:20:03 +08: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-04 00:11:31 +08:00
|
|
|
for (; c != beg; --c)
|
|
|
|
if (*c == '/') {
|
2009-02-15 11:20:03 +08: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-04 00:11:31 +08:00
|
|
|
// Recurse to handling the parent directory.
|
2009-02-15 11:20:03 +08: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-04 00:11:31 +08:00
|
|
|
bool x = createDirectoryHelper(beg, c, create_parents);
|
|
|
|
*c = '/';
|
2009-02-15 11:20:03 +08: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-04 00:11:31 +08:00
|
|
|
// Return if we encountered an error.
|
|
|
|
if (x)
|
|
|
|
return true;
|
2009-02-15 11:20:03 +08: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-04 00:11:31 +08:00
|
|
|
break;
|
|
|
|
}
|
2009-02-15 11:20:03 +08: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-04 00:11:31 +08:00
|
|
|
return mkdir(beg, S_IRWXU | S_IRWXG) != 0;
|
|
|
|
}
|
|
|
|
|
2004-08-29 13:24:01 +08:00
|
|
|
bool
|
2006-08-23 08:39:35 +08:00
|
|
|
Path::createDirectoryOnDisk( bool create_parents, std::string* ErrMsg ) {
|
2004-08-29 13:24:01 +08:00
|
|
|
// Get a writeable copy of the path name
|
2010-11-03 06:55:34 +08:00
|
|
|
std::string pathname(path);
|
2004-08-29 13:24:01 +08:00
|
|
|
|
|
|
|
// Null-terminate the last component
|
2008-05-06 02:30:58 +08:00
|
|
|
size_t lastchar = path.length() - 1 ;
|
2009-02-15 11:20:03 +08: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-04 00:11:31 +08:00
|
|
|
if (pathname[lastchar] != '/')
|
|
|
|
++lastchar;
|
2009-02-15 11:20:03 +08:00
|
|
|
|
2010-11-03 06:41:19 +08:00
|
|
|
pathname[lastchar] = '\0';
|
2009-02-15 11:20:03 +08:00
|
|
|
|
2010-11-03 06:55:34 +08:00
|
|
|
if (createDirectoryHelper(&pathname[0], &pathname[lastchar], create_parents))
|
2010-11-03 07:16:26 +08:00
|
|
|
return MakeErrMsg(ErrMsg, pathname + ": can't create directory");
|
2009-02-15 11:20:03 +08:00
|
|
|
|
2006-08-23 08:39:35 +08:00
|
|
|
return false;
|
2004-08-25 14:20:07 +08:00
|
|
|
}
|
|
|
|
|
2004-08-29 13:24:01 +08:00
|
|
|
bool
|
2006-08-23 08:39:35 +08:00
|
|
|
Path::createFileOnDisk(std::string* ErrMsg) {
|
2004-08-29 13:24:01 +08:00
|
|
|
// Create the file
|
2004-09-19 03:25:11 +08:00
|
|
|
int fd = ::creat(path.c_str(), S_IRUSR | S_IWUSR);
|
2006-08-23 15:30:48 +08:00
|
|
|
if (fd < 0)
|
|
|
|
return MakeErrMsg(ErrMsg, path + ": can't create file");
|
2004-09-19 03:25:11 +08:00
|
|
|
::close(fd);
|
2006-08-23 08:39:35 +08:00
|
|
|
return false;
|
2004-08-29 13:24:01 +08:00
|
|
|
}
|
|
|
|
|
2004-11-10 04:26:31 +08:00
|
|
|
bool
|
2006-08-23 08:39:35 +08:00
|
|
|
Path::createTemporaryFileOnDisk(bool reuse_current, std::string* ErrMsg) {
|
2004-12-15 09:50:13 +08:00
|
|
|
// Make this into a unique file name
|
2006-08-24 04:34:57 +08:00
|
|
|
if (makeUnique( reuse_current, ErrMsg ))
|
|
|
|
return true;
|
2004-12-15 09:50:13 +08:00
|
|
|
|
|
|
|
// create the file
|
2006-08-23 08:39:35 +08:00
|
|
|
int fd = ::open(path.c_str(), O_WRONLY|O_CREAT|O_TRUNC, 0666);
|
2009-02-15 11:20:03 +08:00
|
|
|
if (fd < 0)
|
2006-08-23 15:30:48 +08:00
|
|
|
return MakeErrMsg(ErrMsg, path + ": can't create temporary file");
|
2006-08-23 08:39:35 +08:00
|
|
|
::close(fd);
|
2004-12-15 09:50:13 +08:00
|
|
|
return false;
|
2004-11-10 04:26:31 +08:00
|
|
|
}
|
|
|
|
|
2004-08-29 13:24:01 +08:00
|
|
|
bool
|
2006-07-29 06:29:50 +08:00
|
|
|
Path::eraseFromDisk(bool remove_contents, std::string *ErrStr) const {
|
2010-03-31 04:04:57 +08:00
|
|
|
// Get the status so we can determine if it's a file or directory.
|
2007-04-08 02:52:17 +08:00
|
|
|
struct stat buf;
|
|
|
|
if (0 != stat(path.c_str(), &buf)) {
|
|
|
|
MakeErrMsg(ErrStr, path + ": can't get status of file");
|
2006-07-29 06:29:50 +08:00
|
|
|
return true;
|
2007-04-08 02:52:17 +08:00
|
|
|
}
|
|
|
|
|
2009-02-15 11:20:03 +08: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-08 02:52:17 +08: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 11:20:03 +08:00
|
|
|
|
2007-04-08 02:52:17 +08:00
|
|
|
if (!S_ISDIR(buf.st_mode)) {
|
|
|
|
if (ErrStr) *ErrStr = "not a file or directory";
|
|
|
|
return true;
|
|
|
|
}
|
2007-03-30 03:05:44 +08:00
|
|
|
|
2006-07-29 06:29:50 +08:00
|
|
|
if (remove_contents) {
|
|
|
|
// Recursively descend the directory to remove its contents.
|
|
|
|
std::string cmd = "/bin/rm -rf " + path;
|
2009-02-15 11:20:32 +08:00
|
|
|
if (system(cmd.c_str()) != 0) {
|
|
|
|
MakeErrMsg(ErrStr, path + ": failed to recursively remove directory.");
|
|
|
|
return true;
|
|
|
|
}
|
2005-07-08 07:21:43 +08:00
|
|
|
return false;
|
2006-07-29 06:29:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, try to just remove the one directory.
|
2010-11-03 06:55:34 +08:00
|
|
|
std::string pathname(path);
|
2008-05-06 02:30:58 +08:00
|
|
|
size_t lastchar = path.length() - 1;
|
2009-02-15 11:20:03 +08:00
|
|
|
if (pathname[lastchar] == '/')
|
2010-11-03 06:41:19 +08:00
|
|
|
pathname[lastchar] = '\0';
|
2006-07-29 06:29:50 +08:00
|
|
|
else
|
2010-11-03 06:41:19 +08:00
|
|
|
pathname[lastchar+1] = '\0';
|
2009-02-15 11:20:03 +08:00
|
|
|
|
2010-11-03 06:55:34 +08:00
|
|
|
if (rmdir(pathname.c_str()) != 0)
|
|
|
|
return MakeErrMsg(ErrStr, pathname + ": can't erase directory");
|
2006-07-29 06:29:50 +08:00
|
|
|
return false;
|
2004-08-29 13:24:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2006-08-23 15:30:48 +08:00
|
|
|
Path::renamePathOnDisk(const Path& newName, std::string* ErrMsg) {
|
2005-07-08 07:21:43 +08:00
|
|
|
if (0 != ::rename(path.c_str(), newName.c_str()))
|
2009-02-15 11:20:03 +08:00
|
|
|
return MakeErrMsg(ErrMsg, std::string("can't rename '") + path + "' as '" +
|
2009-08-24 06:45:37 +08:00
|
|
|
newName.str() + "'");
|
2006-08-23 15:30:48 +08:00
|
|
|
return false;
|
2004-11-15 06:08:36 +08:00
|
|
|
}
|
|
|
|
|
2009-02-15 11:20:03 +08:00
|
|
|
bool
|
2006-07-29 06:36:17 +08:00
|
|
|
Path::setStatusInfoOnDisk(const FileStatus &si, std::string *ErrStr) const {
|
2004-11-15 06:08:36 +08:00
|
|
|
struct utimbuf utb;
|
|
|
|
utb.actime = si.modTime.toPosixTime();
|
|
|
|
utb.modtime = utb.actime;
|
|
|
|
if (0 != ::utime(path.c_str(),&utb))
|
2006-08-24 04:34:57 +08:00
|
|
|
return MakeErrMsg(ErrStr, path + ": can't set file modification time");
|
2004-11-15 06:08:36 +08:00
|
|
|
if (0 != ::chmod(path.c_str(),si.mode))
|
2006-08-24 04:34:57 +08:00
|
|
|
return MakeErrMsg(ErrStr, path + ": can't set mode");
|
2006-07-29 06:36:17 +08:00
|
|
|
return false;
|
2004-08-25 14:20:07 +08:00
|
|
|
}
|
|
|
|
|
2009-02-15 11:20:03 +08:00
|
|
|
bool
|
2006-08-24 04:34:57 +08:00
|
|
|
sys::CopyFile(const sys::Path &Dest, const sys::Path &Src, std::string* ErrMsg){
|
2004-12-15 09:50:13 +08:00
|
|
|
int inFile = -1;
|
|
|
|
int outFile = -1;
|
2006-08-24 04:34:57 +08:00
|
|
|
inFile = ::open(Src.c_str(), O_RDONLY);
|
|
|
|
if (inFile == -1)
|
2009-08-24 06:45:37 +08:00
|
|
|
return MakeErrMsg(ErrMsg, Src.str() +
|
2006-08-24 04:34:57 +08: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 06:45:37 +08:00
|
|
|
return MakeErrMsg(ErrMsg, Dest.str() +
|
2006-08-24 04:34:57 +08: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 06:45:37 +08:00
|
|
|
return MakeErrMsg(ErrMsg, Src.str()+": can't read source file");
|
2006-08-24 04:34:57 +08: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 06:45:37 +08:00
|
|
|
return MakeErrMsg(ErrMsg, Dest.str() +
|
2009-04-21 04:50:13 +08:00
|
|
|
": can't write destination file");
|
2004-12-15 09:50:13 +08:00
|
|
|
}
|
2006-08-24 04:34:57 +08:00
|
|
|
} else {
|
|
|
|
Amt -= AmtWritten;
|
|
|
|
BufPtr += AmtWritten;
|
2004-12-15 09:50:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-08-24 04:34:57 +08:00
|
|
|
::close(inFile);
|
|
|
|
::close(outFile);
|
|
|
|
return false;
|
2004-12-15 09:50:13 +08:00
|
|
|
}
|
|
|
|
|
2009-02-15 11:20:03 +08:00
|
|
|
bool
|
2006-08-24 04:34:57 +08:00
|
|
|
Path::makeUnique(bool reuse_current, std::string* ErrMsg) {
|
2011-01-10 10:34:23 +08:00
|
|
|
bool Exists;
|
|
|
|
if (reuse_current && (fs::exists(path, Exists) || !Exists))
|
2006-08-24 04:34:57 +08:00
|
|
|
return false; // File doesn't exist already, just use it!
|
2004-12-15 09:50:13 +08:00
|
|
|
|
2009-02-15 11:20:03 +08:00
|
|
|
// Append an XXXXXX pattern to the end of the file for use with mkstemp,
|
2004-12-15 09:50:13 +08:00
|
|
|
// mktemp or our own implementation.
|
2010-04-19 23:54:44 +08: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-20 00:33:28 +08:00
|
|
|
char *FNBuffer = &Buf[0];
|
2010-04-19 23:54:44 +08:00
|
|
|
path.copy(FNBuffer,path.size());
|
2011-01-11 09:21:55 +08:00
|
|
|
bool isdir;
|
|
|
|
if (!fs::is_directory(path, isdir) && isdir)
|
2010-04-19 23:54:44 +08:00
|
|
|
strcpy(FNBuffer+path.size(), "/XXXXXX");
|
2008-07-24 08:35:38 +08:00
|
|
|
else
|
2010-04-19 23:54:44 +08:00
|
|
|
strcpy(FNBuffer+path.size(), "-XXXXXX");
|
2004-12-15 09:50:13 +08:00
|
|
|
|
|
|
|
#if defined(HAVE_MKSTEMP)
|
|
|
|
int TempFD;
|
2010-04-19 23:54:44 +08:00
|
|
|
if ((TempFD = mkstemp(FNBuffer)) == -1)
|
2006-08-24 04:34:57 +08:00
|
|
|
return MakeErrMsg(ErrMsg, path + ": can't make unique filename");
|
2004-12-15 09:50:13 +08: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 23:54:44 +08:00
|
|
|
path = FNBuffer;
|
2011-07-06 02:55:31 +08:00
|
|
|
|
|
|
|
// By default mkstemp sets the mode to 0600, so update mode bits now.
|
|
|
|
AddPermissionBits (*this, 0666);
|
2004-12-15 09:50:13 +08:00
|
|
|
#elif defined(HAVE_MKTEMP)
|
|
|
|
// If we don't have mkstemp, use the old and obsolete mktemp function.
|
2010-04-19 23:54:44 +08:00
|
|
|
if (mktemp(FNBuffer) == 0)
|
2006-08-24 04:34:57 +08:00
|
|
|
return MakeErrMsg(ErrMsg, path + ": can't make unique filename");
|
2004-12-15 09:50:13 +08:00
|
|
|
|
|
|
|
// Save the name
|
2010-04-19 23:54:44 +08:00
|
|
|
path = FNBuffer;
|
2004-12-15 09:50:13 +08:00
|
|
|
#else
|
|
|
|
// Okay, looks like we have to do it all by our lonesome.
|
|
|
|
static unsigned FCounter = 0;
|
2010-07-12 08:09:55 +08: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 23:54:44 +08:00
|
|
|
path = FNBuffer;
|
2010-07-12 08:09:55 +08:00
|
|
|
} while (exists());
|
|
|
|
// POSSIBLE SECURITY BUG: An attacker can easily guess the name and exploit
|
|
|
|
// LLVM.
|
2004-12-15 09:50:13 +08:00
|
|
|
#endif
|
2006-08-24 04:34:57 +08:00
|
|
|
return false;
|
2004-08-25 14:20:07 +08:00
|
|
|
}
|
|
|
|
|
2011-03-11 02:33:29 +08:00
|
|
|
const char *Path::MapInFilePages(int FD, size_t FileSize, off_t Offset) {
|
2008-04-01 14:16:24 +08:00
|
|
|
int Flags = MAP_PRIVATE;
|
|
|
|
#ifdef MAP_FILE
|
|
|
|
Flags |= MAP_FILE;
|
|
|
|
#endif
|
2011-03-11 02:33:29 +08:00
|
|
|
void *BasePtr = ::mmap(0, FileSize, PROT_READ, Flags, FD, Offset);
|
2008-04-01 14:16:24 +08:00
|
|
|
if (BasePtr == MAP_FAILED)
|
|
|
|
return 0;
|
2008-04-01 14:25:23 +08:00
|
|
|
return (const char*)BasePtr;
|
2008-04-01 14:00:12 +08:00
|
|
|
}
|
|
|
|
|
2011-03-11 02:33:29 +08:00
|
|
|
void Path::UnMapFilePages(const char *BasePtr, size_t FileSize) {
|
2012-07-13 04:45:36 +08:00
|
|
|
const void *Addr = static_cast<const void *>(BasePtr);
|
|
|
|
::munmap(const_cast<void *>(Addr), FileSize);
|
2008-04-01 14:00:12 +08:00
|
|
|
}
|
|
|
|
|
2006-08-24 04:34:57 +08:00
|
|
|
} // end llvm namespace
|