Charles Davis | 54c9eb6 | 2010-11-29 19:44:50 +0000 | [diff] [blame] | 1 | //===- llvm/Support/Unix/Path.cpp - Unix Path Implementation -----*- C++ -*-===// |
Mikhail Glushenkov | a539836 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 2 | // |
Reid Spencer | 814ba57 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Mikhail Glushenkov | a539836 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 7 | // |
Reid Spencer | 814ba57 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Unix specific portion of the Path class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | //=== WARNING: Implementation here must contain only generic UNIX code that |
Reid Spencer | 6df221d | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 16 | //=== is guaranteed to work on *all* UNIX variants. |
Reid Spencer | 814ba57 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 17 | //===----------------------------------------------------------------------===// |
| 18 | |
| 19 | #include "Unix.h" |
Reid Spencer | d103e08 | 2004-12-27 06:17:15 +0000 | [diff] [blame] | 20 | #if HAVE_SYS_STAT_H |
Reid Spencer | 814ba57 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 21 | #include <sys/stat.h> |
Reid Spencer | d103e08 | 2004-12-27 06:17:15 +0000 | [diff] [blame] | 22 | #endif |
| 23 | #if HAVE_FCNTL_H |
Reid Spencer | 814ba57 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 24 | #include <fcntl.h> |
Reid Spencer | d103e08 | 2004-12-27 06:17:15 +0000 | [diff] [blame] | 25 | #endif |
Chris Lattner | ba98fca | 2008-04-01 06:25:23 +0000 | [diff] [blame] | 26 | #ifdef HAVE_SYS_MMAN_H |
| 27 | #include <sys/mman.h> |
| 28 | #endif |
| 29 | #ifdef HAVE_SYS_STAT_H |
| 30 | #include <sys/stat.h> |
| 31 | #endif |
Reid Spencer | d103e08 | 2004-12-27 06:17:15 +0000 | [diff] [blame] | 32 | #if HAVE_UTIME_H |
Reid Spencer | c1d474f | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 33 | #include <utime.h> |
Reid Spencer | d103e08 | 2004-12-27 06:17:15 +0000 | [diff] [blame] | 34 | #endif |
| 35 | #if HAVE_TIME_H |
Reid Spencer | 2054031 | 2004-12-24 06:29:42 +0000 | [diff] [blame] | 36 | #include <time.h> |
Reid Spencer | d103e08 | 2004-12-27 06:17:15 +0000 | [diff] [blame] | 37 | #endif |
| 38 | #if HAVE_DIRENT_H |
| 39 | # include <dirent.h> |
| 40 | # define NAMLEN(dirent) strlen((dirent)->d_name) |
| 41 | #else |
| 42 | # define dirent direct |
| 43 | # define NAMLEN(dirent) (dirent)->d_namlen |
| 44 | # if HAVE_SYS_NDIR_H |
| 45 | # include <sys/ndir.h> |
| 46 | # endif |
| 47 | # if HAVE_SYS_DIR_H |
| 48 | # include <sys/dir.h> |
| 49 | # endif |
| 50 | # if HAVE_NDIR_H |
| 51 | # include <ndir.h> |
| 52 | # endif |
| 53 | #endif |
| 54 | |
Chris Lattner | e209be4 | 2008-03-03 02:55:43 +0000 | [diff] [blame] | 55 | #if HAVE_DLFCN_H |
| 56 | #include <dlfcn.h> |
| 57 | #endif |
| 58 | |
Benjamin Kramer | 2a1131a | 2009-09-09 12:09:08 +0000 | [diff] [blame] | 59 | #ifdef __APPLE__ |
| 60 | #include <mach-o/dyld.h> |
| 61 | #endif |
| 62 | |
Sylvestre Ledru | 14ada94 | 2012-04-11 15:35:36 +0000 | [diff] [blame] | 63 | // For GNU Hurd |
| 64 | #if defined(__GNU__) && !defined(MAXPATHLEN) |
| 65 | # define MAXPATHLEN 4096 |
| 66 | #endif |
| 67 | |
Reid Spencer | d1ef1df | 2005-06-02 05:38:20 +0000 | [diff] [blame] | 68 | // Put in a hack for Cygwin which falsely reports that the mkdtemp function |
| 69 | // is available when it is not. |
| 70 | #ifdef __CYGWIN__ |
| 71 | # undef HAVE_MKDTEMP |
| 72 | #endif |
Reid Spencer | 814ba57 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 73 | |
Reid Spencer | 29f83e0 | 2005-07-28 16:25:57 +0000 | [diff] [blame] | 74 | namespace { |
| 75 | inline bool lastIsSlash(const std::string& path) { |
| 76 | return !path.empty() && path[path.length() - 1] == '/'; |
| 77 | } |
| 78 | |
| 79 | } |
| 80 | |
Reid Spencer | 6df221d | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 81 | namespace llvm { |
| 82 | using namespace sys; |
Reid Spencer | 814ba57 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 83 | |
Daniel Dunbar | 5ea6200 | 2009-12-09 03:26:33 +0000 | [diff] [blame] | 84 | const char sys::PathSeparator = ':'; |
Chris Lattner | 6fca938 | 2008-02-27 06:17:10 +0000 | [diff] [blame] | 85 | |
Mikhail Glushenkov | fcfaf51 | 2010-11-02 20:32:26 +0000 | [diff] [blame] | 86 | StringRef Path::GetEXESuffix() { |
Dan Gohman | bba8585 | 2010-11-02 20:52:47 +0000 | [diff] [blame] | 87 | return StringRef(); |
Mikhail Glushenkov | fcfaf51 | 2010-11-02 20:32:26 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Jeffrey Yasskin | 5908f1e | 2009-12-17 21:02:39 +0000 | [diff] [blame] | 90 | Path::Path(StringRef p) |
Nick Lewycky | c38c171 | 2008-05-11 17:37:40 +0000 | [diff] [blame] | 91 | : path(p) {} |
| 92 | |
| 93 | Path::Path(const char *StrStart, unsigned StrLen) |
| 94 | : path(StrStart, StrLen) {} |
| 95 | |
Chris Lattner | b56e07e | 2008-08-11 23:39:47 +0000 | [diff] [blame] | 96 | Path& |
Jeffrey Yasskin | 5908f1e | 2009-12-17 21:02:39 +0000 | [diff] [blame] | 97 | Path::operator=(StringRef that) { |
| 98 | path.assign(that.data(), that.size()); |
Chris Lattner | b56e07e | 2008-08-11 23:39:47 +0000 | [diff] [blame] | 99 | return *this; |
| 100 | } |
| 101 | |
Mikhail Glushenkov | a539836 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 102 | bool |
Reid Spencer | 2054031 | 2004-12-24 06:29:42 +0000 | [diff] [blame] | 103 | Path::isValid() const { |
Dan Gohman | c0a8bee | 2010-11-02 23:19:55 +0000 | [diff] [blame] | 104 | // Empty paths are considered invalid here. |
| 105 | // This code doesn't check MAXPATHLEN because there's no need. Nothing in |
| 106 | // LLVM manipulates Paths with fixed-sizes arrays, and if the OS can't |
| 107 | // handle names longer than some limit, it'll report this on demand using |
| 108 | // ENAMETOLONG. |
| 109 | return !path.empty(); |
Reid Spencer | 2054031 | 2004-12-24 06:29:42 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Reid Spencer | 6df221d | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 112 | Path |
Chris Lattner | 3c50fdf | 2009-02-19 05:34:35 +0000 | [diff] [blame] | 113 | Path::GetTemporaryDirectory(std::string *ErrMsg) { |
Reid Spencer | 2054031 | 2004-12-24 06:29:42 +0000 | [diff] [blame] | 114 | #if defined(HAVE_MKDTEMP) |
Mikhail Glushenkov | a539836 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 115 | // The best way is with mkdtemp but that's not available on many systems, |
Reid Spencer | 2054031 | 2004-12-24 06:29:42 +0000 | [diff] [blame] | 116 | // Linux and FreeBSD have it. Others probably won't. |
Dan Gohman | 32d51fa | 2010-11-02 22:50:10 +0000 | [diff] [blame] | 117 | char pathname[] = "/tmp/llvm_XXXXXX"; |
Reid Spencer | 6ba87bb | 2006-08-22 19:01:30 +0000 | [diff] [blame] | 118 | if (0 == mkdtemp(pathname)) { |
Dan Gohman | 6e05d6c | 2010-11-02 22:56:51 +0000 | [diff] [blame] | 119 | MakeErrMsg(ErrMsg, |
| 120 | std::string(pathname) + ": can't create temporary directory"); |
Reid Spencer | 6ba87bb | 2006-08-22 19:01:30 +0000 | [diff] [blame] | 121 | return Path(); |
| 122 | } |
Dan Gohman | ecc4d73 | 2010-11-03 00:01:23 +0000 | [diff] [blame] | 123 | return Path(pathname); |
Reid Spencer | 2054031 | 2004-12-24 06:29:42 +0000 | [diff] [blame] | 124 | #elif defined(HAVE_MKSTEMP) |
| 125 | // If no mkdtemp is available, mkstemp can be used to create a temporary file |
| 126 | // which is then removed and created as a directory. We prefer this over |
| 127 | // mktemp because of mktemp's inherent security and threading risks. We still |
| 128 | // have a slight race condition from the time the temporary file is created to |
Mikhail Glushenkov | a539836 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 129 | // the time it is re-created as a directoy. |
Dan Gohman | 32d51fa | 2010-11-02 22:50:10 +0000 | [diff] [blame] | 130 | char pathname[] = "/tmp/llvm_XXXXXX"; |
Reid Spencer | 2054031 | 2004-12-24 06:29:42 +0000 | [diff] [blame] | 131 | int fd = 0; |
Reid Spencer | 6ba87bb | 2006-08-22 19:01:30 +0000 | [diff] [blame] | 132 | if (-1 == (fd = mkstemp(pathname))) { |
Mikhail Glushenkov | a539836 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 133 | MakeErrMsg(ErrMsg, |
Reid Spencer | 6ba87bb | 2006-08-22 19:01:30 +0000 | [diff] [blame] | 134 | std::string(pathname) + ": can't create temporary directory"); |
| 135 | return Path(); |
| 136 | } |
Reid Spencer | 2054031 | 2004-12-24 06:29:42 +0000 | [diff] [blame] | 137 | ::close(fd); |
| 138 | ::unlink(pathname); // start race condition, ignore errors |
Reid Spencer | 6ba87bb | 2006-08-22 19:01:30 +0000 | [diff] [blame] | 139 | if (-1 == ::mkdir(pathname, S_IRWXU)) { // end race condition |
Mikhail Glushenkov | a539836 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 140 | MakeErrMsg(ErrMsg, |
Reid Spencer | 6ba87bb | 2006-08-22 19:01:30 +0000 | [diff] [blame] | 141 | std::string(pathname) + ": can't create temporary directory"); |
| 142 | return Path(); |
| 143 | } |
Dan Gohman | ecc4d73 | 2010-11-03 00:01:23 +0000 | [diff] [blame] | 144 | return Path(pathname); |
Reid Spencer | 2054031 | 2004-12-24 06:29:42 +0000 | [diff] [blame] | 145 | #elif defined(HAVE_MKTEMP) |
| 146 | // If a system doesn't have mkdtemp(3) or mkstemp(3) but it does have |
| 147 | // mktemp(3) then we'll assume that system (e.g. AIX) has a reasonable |
| 148 | // implementation of mktemp(3) and doesn't follow BSD 4.3's lead of replacing |
| 149 | // the XXXXXX with the pid of the process and a letter. That leads to only |
| 150 | // twenty six temporary files that can be generated. |
Dan Gohman | 32d51fa | 2010-11-02 22:50:10 +0000 | [diff] [blame] | 151 | char pathname[] = "/tmp/llvm_XXXXXX"; |
Reid Spencer | 2054031 | 2004-12-24 06:29:42 +0000 | [diff] [blame] | 152 | char *TmpName = ::mktemp(pathname); |
Reid Spencer | 6ba87bb | 2006-08-22 19:01:30 +0000 | [diff] [blame] | 153 | if (TmpName == 0) { |
Mikhail Glushenkov | a539836 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 154 | MakeErrMsg(ErrMsg, |
Reid Spencer | 6ba87bb | 2006-08-22 19:01:30 +0000 | [diff] [blame] | 155 | std::string(TmpName) + ": can't create unique directory name"); |
| 156 | return Path(); |
| 157 | } |
| 158 | if (-1 == ::mkdir(TmpName, S_IRWXU)) { |
Mikhail Glushenkov | a539836 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 159 | MakeErrMsg(ErrMsg, |
Reid Spencer | 6ba87bb | 2006-08-22 19:01:30 +0000 | [diff] [blame] | 160 | std::string(TmpName) + ": can't create temporary directory"); |
| 161 | return Path(); |
| 162 | } |
Dan Gohman | ecc4d73 | 2010-11-03 00:01:23 +0000 | [diff] [blame] | 163 | return Path(TmpName); |
Reid Spencer | 2054031 | 2004-12-24 06:29:42 +0000 | [diff] [blame] | 164 | #else |
| 165 | // This is the worst case implementation. tempnam(3) leaks memory unless its |
| 166 | // on an SVID2 (or later) system. On BSD 4.3 it leaks. tmpnam(3) has thread |
| 167 | // issues. The mktemp(3) function doesn't have enough variability in the |
Mikhail Glushenkov | a539836 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 168 | // temporary name generated. So, we provide our own implementation that |
Reid Spencer | 2054031 | 2004-12-24 06:29:42 +0000 | [diff] [blame] | 169 | // increments an integer from a random number seeded by the current time. This |
| 170 | // should be sufficiently unique that we don't have many collisions between |
| 171 | // processes. Generally LLVM processes don't run very long and don't use very |
| 172 | // many temporary files so this shouldn't be a big issue for LLVM. |
| 173 | static time_t num = ::time(0); |
| 174 | char pathname[MAXPATHLEN]; |
| 175 | do { |
| 176 | num++; |
| 177 | sprintf(pathname, "/tmp/llvm_%010u", unsigned(num)); |
| 178 | } while ( 0 == access(pathname, F_OK ) ); |
Reid Spencer | 6ba87bb | 2006-08-22 19:01:30 +0000 | [diff] [blame] | 179 | if (-1 == ::mkdir(pathname, S_IRWXU)) { |
Mikhail Glushenkov | a539836 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 180 | MakeErrMsg(ErrMsg, |
Reid Spencer | 6ba87bb | 2006-08-22 19:01:30 +0000 | [diff] [blame] | 181 | std::string(pathname) + ": can't create temporary directory"); |
| 182 | return Path(); |
Reid Spencer | e4ca722 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 183 | } |
Dan Gohman | ecc4d73 | 2010-11-03 00:01:23 +0000 | [diff] [blame] | 184 | return Path(pathname); |
Reid Spencer | 2054031 | 2004-12-24 06:29:42 +0000 | [diff] [blame] | 185 | #endif |
| 186 | } |
| 187 | |
Mikhail Glushenkov | a539836 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 188 | Path |
Ted Kremenek | 1402070 | 2007-12-18 22:07:33 +0000 | [diff] [blame] | 189 | Path::GetCurrentDirectory() { |
| 190 | char pathname[MAXPATHLEN]; |
Nick Lewycky | 38b9b56 | 2011-07-29 04:42:39 +0000 | [diff] [blame] | 191 | if (!getcwd(pathname, MAXPATHLEN)) { |
| 192 | assert(false && "Could not query current working directory."); |
Dan Gohman | 5cae103 | 2010-08-04 01:39:08 +0000 | [diff] [blame] | 193 | return Path(); |
Ted Kremenek | 1402070 | 2007-12-18 22:07:33 +0000 | [diff] [blame] | 194 | } |
Mikhail Glushenkov | a539836 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 195 | |
Ted Kremenek | 1402070 | 2007-12-18 22:07:33 +0000 | [diff] [blame] | 196 | return Path(pathname); |
| 197 | } |
Reid Spencer | af48d86 | 2005-07-08 03:08:58 +0000 | [diff] [blame] | 198 | |
Eric Christopher | 22738d0 | 2012-08-06 20:52:18 +0000 | [diff] [blame] | 199 | #if defined(__FreeBSD__) || defined (__NetBSD__) || defined(__Bitrig__) || \ |
Sylvestre Ledru | 12c44e5 | 2012-09-26 08:30:35 +0000 | [diff] [blame] | 200 | defined(__OpenBSD__) || defined(__minix) || defined(__FreeBSD_kernel__) || \ |
| 201 | defined(__linux__) || defined(__CYGWIN__) |
Chris Lattner | bab4417 | 2009-03-02 22:17:15 +0000 | [diff] [blame] | 202 | static int |
| 203 | test_dir(char buf[PATH_MAX], char ret[PATH_MAX], |
| 204 | const char *dir, const char *bin) |
| 205 | { |
Bill Wendling | 09f17a8 | 2009-05-30 01:09:53 +0000 | [diff] [blame] | 206 | struct stat sb; |
Chris Lattner | bab4417 | 2009-03-02 22:17:15 +0000 | [diff] [blame] | 207 | |
Dan Gohman | 9e85744 | 2010-09-02 18:24:46 +0000 | [diff] [blame] | 208 | snprintf(buf, PATH_MAX, "%s/%s", dir, bin); |
Bill Wendling | 09f17a8 | 2009-05-30 01:09:53 +0000 | [diff] [blame] | 209 | if (realpath(buf, ret) == NULL) |
| 210 | return (1); |
| 211 | if (stat(buf, &sb) != 0) |
| 212 | return (1); |
| 213 | |
| 214 | return (0); |
Chris Lattner | bab4417 | 2009-03-02 22:17:15 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | static char * |
| 218 | getprogpath(char ret[PATH_MAX], const char *bin) |
| 219 | { |
Bill Wendling | 09f17a8 | 2009-05-30 01:09:53 +0000 | [diff] [blame] | 220 | char *pv, *s, *t, buf[PATH_MAX]; |
Chris Lattner | bab4417 | 2009-03-02 22:17:15 +0000 | [diff] [blame] | 221 | |
Bill Wendling | 09f17a8 | 2009-05-30 01:09:53 +0000 | [diff] [blame] | 222 | /* First approach: absolute path. */ |
| 223 | if (bin[0] == '/') { |
| 224 | if (test_dir(buf, ret, "/", bin) == 0) |
| 225 | return (ret); |
| 226 | return (NULL); |
| 227 | } |
Chris Lattner | bab4417 | 2009-03-02 22:17:15 +0000 | [diff] [blame] | 228 | |
Bill Wendling | 09f17a8 | 2009-05-30 01:09:53 +0000 | [diff] [blame] | 229 | /* Second approach: relative path. */ |
| 230 | if (strchr(bin, '/') != NULL) { |
| 231 | if (getcwd(buf, PATH_MAX) == NULL) |
| 232 | return (NULL); |
| 233 | if (test_dir(buf, ret, buf, bin) == 0) |
| 234 | return (ret); |
| 235 | return (NULL); |
| 236 | } |
Chris Lattner | bab4417 | 2009-03-02 22:17:15 +0000 | [diff] [blame] | 237 | |
Bill Wendling | 09f17a8 | 2009-05-30 01:09:53 +0000 | [diff] [blame] | 238 | /* Third approach: $PATH */ |
| 239 | if ((pv = getenv("PATH")) == NULL) |
| 240 | return (NULL); |
| 241 | s = pv = strdup(pv); |
| 242 | if (pv == NULL) |
| 243 | return (NULL); |
| 244 | while ((t = strsep(&s, ":")) != NULL) { |
| 245 | if (test_dir(buf, ret, t, bin) == 0) { |
| 246 | free(pv); |
| 247 | return (ret); |
| 248 | } |
| 249 | } |
| 250 | free(pv); |
| 251 | return (NULL); |
Chris Lattner | bab4417 | 2009-03-02 22:17:15 +0000 | [diff] [blame] | 252 | } |
Anton Korobeynikov | 4547077 | 2012-03-26 12:05:51 +0000 | [diff] [blame] | 253 | #endif // __FreeBSD__ || __NetBSD__ || __FreeBSD_kernel__ |
Chris Lattner | bab4417 | 2009-03-02 22:17:15 +0000 | [diff] [blame] | 254 | |
Chris Lattner | e209be4 | 2008-03-03 02:55:43 +0000 | [diff] [blame] | 255 | /// GetMainExecutable - Return the path to the main executable, given the |
| 256 | /// value of argv[0] from program startup. |
| 257 | Path Path::GetMainExecutable(const char *argv0, void *MainAddr) { |
Benjamin Kramer | 2a1131a | 2009-09-09 12:09:08 +0000 | [diff] [blame] | 258 | #if defined(__APPLE__) |
| 259 | // On OS X the executable path is saved to the stack by dyld. Reading it |
| 260 | // from there is much faster than calling dladdr, especially for large |
| 261 | // binaries with symbols. |
| 262 | char exe_path[MAXPATHLEN]; |
| 263 | uint32_t size = sizeof(exe_path); |
| 264 | if (_NSGetExecutablePath(exe_path, &size) == 0) { |
| 265 | char link_path[MAXPATHLEN]; |
Kovarththanan Rajaratnam | 4b9f0b6 | 2009-11-29 17:19:48 +0000 | [diff] [blame] | 266 | if (realpath(exe_path, link_path)) |
Dan Gohman | 44e24e5 | 2010-11-02 22:07:47 +0000 | [diff] [blame] | 267 | return Path(link_path); |
Benjamin Kramer | 2a1131a | 2009-09-09 12:09:08 +0000 | [diff] [blame] | 268 | } |
Eric Christopher | 22738d0 | 2012-08-06 20:52:18 +0000 | [diff] [blame] | 269 | #elif defined(__FreeBSD__) || defined (__NetBSD__) || defined(__Bitrig__) || \ |
Anton Korobeynikov | 4547077 | 2012-03-26 12:05:51 +0000 | [diff] [blame] | 270 | defined(__OpenBSD__) || defined(__minix) || defined(__FreeBSD_kernel__) |
Chris Lattner | bab4417 | 2009-03-02 22:17:15 +0000 | [diff] [blame] | 271 | char exe_path[PATH_MAX]; |
| 272 | |
| 273 | if (getprogpath(exe_path, argv0) != NULL) |
Dan Gohman | 44e24e5 | 2010-11-02 22:07:47 +0000 | [diff] [blame] | 274 | return Path(exe_path); |
Chris Lattner | bab4417 | 2009-03-02 22:17:15 +0000 | [diff] [blame] | 275 | #elif defined(__linux__) || defined(__CYGWIN__) |
Chris Lattner | 02e727f | 2008-03-13 05:22:05 +0000 | [diff] [blame] | 276 | char exe_path[MAXPATHLEN]; |
Sylvestre Ledru | 12c44e5 | 2012-09-26 08:30:35 +0000 | [diff] [blame] | 277 | StringRef aPath("/proc/self/exe"); |
| 278 | if (sys::fs::exists(aPath)) { |
| 279 | // /proc is not always mounted under Linux (chroot for example). |
| 280 | ssize_t len = readlink(aPath.str().c_str(), exe_path, sizeof(exe_path)); |
| 281 | if (len >= 0) |
| 282 | return Path(StringRef(exe_path, len)); |
| 283 | } else { |
| 284 | // Fall back to the classical detection. |
| 285 | if (getprogpath(exe_path, argv0) != NULL) |
| 286 | return Path(exe_path); |
| 287 | } |
Chris Lattner | 02e727f | 2008-03-13 05:22:05 +0000 | [diff] [blame] | 288 | #elif defined(HAVE_DLFCN_H) |
Chris Lattner | e209be4 | 2008-03-03 02:55:43 +0000 | [diff] [blame] | 289 | // Use dladdr to get executable path if available. |
Chris Lattner | e209be4 | 2008-03-03 02:55:43 +0000 | [diff] [blame] | 290 | Dl_info DLInfo; |
| 291 | int err = dladdr(MainAddr, &DLInfo); |
Chris Lattner | 3c50fdf | 2009-02-19 05:34:35 +0000 | [diff] [blame] | 292 | if (err == 0) |
| 293 | return Path(); |
Bill Wendling | 09f17a8 | 2009-05-30 01:09:53 +0000 | [diff] [blame] | 294 | |
Chris Lattner | 3c50fdf | 2009-02-19 05:34:35 +0000 | [diff] [blame] | 295 | // If the filename is a symlink, we need to resolve and return the location of |
| 296 | // the actual executable. |
| 297 | char link_path[MAXPATHLEN]; |
Kovarththanan Rajaratnam | 4b9f0b6 | 2009-11-29 17:19:48 +0000 | [diff] [blame] | 298 | if (realpath(DLInfo.dli_fname, link_path)) |
Dan Gohman | 44e24e5 | 2010-11-02 22:07:47 +0000 | [diff] [blame] | 299 | return Path(link_path); |
Dan Gohman | d51b431 | 2010-09-07 18:26:49 +0000 | [diff] [blame] | 300 | #else |
| 301 | #error GetMainExecutable is not implemented on this host yet. |
Chris Lattner | e209be4 | 2008-03-03 02:55:43 +0000 | [diff] [blame] | 302 | #endif |
| 303 | return Path(); |
| 304 | } |
| 305 | |
Reid Spencer | ae9bbda | 2004-09-11 04:55:08 +0000 | [diff] [blame] | 306 | bool |
Reid Spencer | 6df221d | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 307 | Path::exists() const { |
| 308 | return 0 == access(path.c_str(), F_OK ); |
| 309 | } |
| 310 | |
| 311 | bool |
Ted Kremenek | 74db042 | 2007-12-18 19:46:22 +0000 | [diff] [blame] | 312 | Path::isDirectory() const { |
| 313 | struct stat buf; |
| 314 | if (0 != stat(path.c_str(), &buf)) |
| 315 | return false; |
Evan Cheng | 139edad | 2010-10-07 22:05:57 +0000 | [diff] [blame] | 316 | return ((buf.st_mode & S_IFMT) == S_IFDIR) ? true : false; |
Ted Kremenek | 74db042 | 2007-12-18 19:46:22 +0000 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | bool |
Rafael Espindola | 559b8fb | 2010-11-07 04:36:50 +0000 | [diff] [blame] | 320 | Path::isSymLink() const { |
| 321 | struct stat buf; |
| 322 | if (0 != lstat(path.c_str(), &buf)) |
| 323 | return false; |
| 324 | return S_ISLNK(buf.st_mode); |
| 325 | } |
| 326 | |
| 327 | |
| 328 | bool |
Reid Spencer | 5b891e9 | 2005-07-07 18:21:42 +0000 | [diff] [blame] | 329 | Path::canRead() const { |
Dan Gohman | 3670065 | 2009-07-28 23:22:01 +0000 | [diff] [blame] | 330 | return 0 == access(path.c_str(), R_OK); |
Reid Spencer | 6df221d | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | bool |
Reid Spencer | 5b891e9 | 2005-07-07 18:21:42 +0000 | [diff] [blame] | 334 | Path::canWrite() const { |
Dan Gohman | 3670065 | 2009-07-28 23:22:01 +0000 | [diff] [blame] | 335 | return 0 == access(path.c_str(), W_OK); |
Reid Spencer | 6df221d | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | bool |
Edward O'Callaghan | 746782d | 2009-11-25 06:32:19 +0000 | [diff] [blame] | 339 | Path::isRegularFile() const { |
Dan Gohman | 39027c4 | 2010-03-30 20:04:57 +0000 | [diff] [blame] | 340 | // Get the status so we can determine if it's a file or directory |
Edward O'Callaghan | dddf134 | 2009-11-24 15:19:10 +0000 | [diff] [blame] | 341 | struct stat buf; |
Edward O'Callaghan | dddf134 | 2009-11-24 15:19:10 +0000 | [diff] [blame] | 342 | |
Daniel Dunbar | 7402c47 | 2009-11-24 19:03:33 +0000 | [diff] [blame] | 343 | if (0 != stat(path.c_str(), &buf)) |
Edward O'Callaghan | dddf134 | 2009-11-24 15:19:10 +0000 | [diff] [blame] | 344 | return false; |
Edward O'Callaghan | dddf134 | 2009-11-24 15:19:10 +0000 | [diff] [blame] | 345 | |
Edward O'Callaghan | 746782d | 2009-11-25 06:32:19 +0000 | [diff] [blame] | 346 | if (S_ISREG(buf.st_mode)) |
| 347 | return true; |
| 348 | |
| 349 | return false; |
Edward O'Callaghan | dddf134 | 2009-11-24 15:19:10 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | bool |
Reid Spencer | 5b891e9 | 2005-07-07 18:21:42 +0000 | [diff] [blame] | 353 | Path::canExecute() const { |
Reid Spencer | 2d85f56 | 2005-07-08 17:46:10 +0000 | [diff] [blame] | 354 | if (0 != access(path.c_str(), R_OK | X_OK )) |
| 355 | return false; |
Reid Spencer | ceeb918 | 2007-04-07 18:52:17 +0000 | [diff] [blame] | 356 | struct stat buf; |
| 357 | if (0 != stat(path.c_str(), &buf)) |
| 358 | return false; |
| 359 | if (!S_ISREG(buf.st_mode)) |
Misha Brukman | a9a8c1b | 2005-04-20 15:33:22 +0000 | [diff] [blame] | 360 | return false; |
Reid Spencer | 2d85f56 | 2005-07-08 17:46:10 +0000 | [diff] [blame] | 361 | return true; |
Reid Spencer | 6df221d | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Reid Spencer | ceeb918 | 2007-04-07 18:52:17 +0000 | [diff] [blame] | 364 | const FileStatus * |
| 365 | PathWithStatus::getFileStatus(bool update, std::string *ErrStr) const { |
| 366 | if (!fsIsValid || update) { |
Reid Spencer | 0f92f0e | 2007-03-29 16:43:20 +0000 | [diff] [blame] | 367 | struct stat buf; |
Reid Spencer | 200c6f9 | 2007-03-29 19:05:44 +0000 | [diff] [blame] | 368 | if (0 != stat(path.c_str(), &buf)) { |
| 369 | MakeErrMsg(ErrStr, path + ": can't get status of file"); |
| 370 | return 0; |
| 371 | } |
Reid Spencer | ceeb918 | 2007-04-07 18:52:17 +0000 | [diff] [blame] | 372 | status.fileSize = buf.st_size; |
| 373 | status.modTime.fromEpochTime(buf.st_mtime); |
| 374 | status.mode = buf.st_mode; |
| 375 | status.user = buf.st_uid; |
| 376 | status.group = buf.st_gid; |
| 377 | status.uniqueID = uint64_t(buf.st_ino); |
| 378 | status.isDir = S_ISDIR(buf.st_mode); |
| 379 | status.isFile = S_ISREG(buf.st_mode); |
| 380 | fsIsValid = true; |
Reid Spencer | 0f92f0e | 2007-03-29 16:43:20 +0000 | [diff] [blame] | 381 | } |
Reid Spencer | ceeb918 | 2007-04-07 18:52:17 +0000 | [diff] [blame] | 382 | return &status; |
Reid Spencer | c1d474f | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Chris Lattner | 4857253 | 2006-07-28 22:03:44 +0000 | [diff] [blame] | 385 | static bool AddPermissionBits(const Path &File, int bits) { |
Reid Spencer | 94bf226 | 2004-12-13 19:59:50 +0000 | [diff] [blame] | 386 | // Get the umask value from the operating system. We want to use it |
| 387 | // when changing the file's permissions. Since calling umask() sets |
| 388 | // the umask and returns its old value, we must call it a second |
| 389 | // time to reset it to the user's preference. |
| 390 | int mask = umask(0777); // The arg. to umask is arbitrary. |
| 391 | umask(mask); // Restore the umask. |
| 392 | |
| 393 | // Get the file's current mode. |
Reid Spencer | ceeb918 | 2007-04-07 18:52:17 +0000 | [diff] [blame] | 394 | struct stat buf; |
Chris Lattner | c521f54 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 395 | if (0 != stat(File.c_str(), &buf)) |
Reid Spencer | 94bf226 | 2004-12-13 19:59:50 +0000 | [diff] [blame] | 396 | return false; |
Reid Spencer | ceeb918 | 2007-04-07 18:52:17 +0000 | [diff] [blame] | 397 | // Change the file to have whichever permissions bits from 'bits' |
| 398 | // that the umask would not disable. |
| 399 | if ((chmod(File.c_str(), (buf.st_mode | (bits & ~mask)))) == -1) |
| 400 | return false; |
Reid Spencer | 94bf226 | 2004-12-13 19:59:50 +0000 | [diff] [blame] | 401 | return true; |
| 402 | } |
| 403 | |
Reid Spencer | 9d2f19c | 2006-08-22 23:27:23 +0000 | [diff] [blame] | 404 | bool Path::makeReadableOnDisk(std::string* ErrMsg) { |
Mikhail Glushenkov | a539836 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 405 | if (!AddPermissionBits(*this, 0444)) |
Reid Spencer | 879ed5a | 2006-08-23 07:30:48 +0000 | [diff] [blame] | 406 | return MakeErrMsg(ErrMsg, path + ": can't make file readable"); |
Reid Spencer | 9d2f19c | 2006-08-22 23:27:23 +0000 | [diff] [blame] | 407 | return false; |
Reid Spencer | 94bf226 | 2004-12-13 19:59:50 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Reid Spencer | 9d2f19c | 2006-08-22 23:27:23 +0000 | [diff] [blame] | 410 | bool Path::makeWriteableOnDisk(std::string* ErrMsg) { |
Reid Spencer | 879ed5a | 2006-08-23 07:30:48 +0000 | [diff] [blame] | 411 | if (!AddPermissionBits(*this, 0222)) |
| 412 | return MakeErrMsg(ErrMsg, path + ": can't make file writable"); |
Reid Spencer | 9d2f19c | 2006-08-22 23:27:23 +0000 | [diff] [blame] | 413 | return false; |
Reid Spencer | 94bf226 | 2004-12-13 19:59:50 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Reid Spencer | c1d474f | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 416 | bool |
Reid Spencer | 51edba1 | 2006-08-23 06:56:27 +0000 | [diff] [blame] | 417 | Path::getDirectoryContents(std::set<Path>& result, std::string* ErrMsg) const { |
Reid Spencer | c1d474f | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 418 | DIR* direntries = ::opendir(path.c_str()); |
Reid Spencer | 879ed5a | 2006-08-23 07:30:48 +0000 | [diff] [blame] | 419 | if (direntries == 0) |
| 420 | return MakeErrMsg(ErrMsg, path + ": can't open directory"); |
Reid Spencer | c1d474f | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 421 | |
Reid Spencer | 29f83e0 | 2005-07-28 16:25:57 +0000 | [diff] [blame] | 422 | std::string dirPath = path; |
| 423 | if (!lastIsSlash(dirPath)) |
| 424 | dirPath += '/'; |
| 425 | |
Reid Spencer | c1d474f | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 426 | result.clear(); |
| 427 | struct dirent* de = ::readdir(direntries); |
Misha Brukman | 1001aea | 2005-04-20 04:04:07 +0000 | [diff] [blame] | 428 | for ( ; de != 0; de = ::readdir(direntries)) { |
Reid Spencer | c1d474f | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 429 | if (de->d_name[0] != '.') { |
Reid Spencer | 29f83e0 | 2005-07-28 16:25:57 +0000 | [diff] [blame] | 430 | Path aPath(dirPath + (const char*)de->d_name); |
Chris Lattner | 0b2890e | 2006-07-07 21:21:06 +0000 | [diff] [blame] | 431 | struct stat st; |
| 432 | if (0 != lstat(aPath.path.c_str(), &st)) { |
| 433 | if (S_ISLNK(st.st_mode)) |
Misha Brukman | 1001aea | 2005-04-20 04:04:07 +0000 | [diff] [blame] | 434 | continue; // dangling symlink -- ignore |
Mikhail Glushenkov | a539836 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 435 | return MakeErrMsg(ErrMsg, |
Reid Spencer | 879ed5a | 2006-08-23 07:30:48 +0000 | [diff] [blame] | 436 | aPath.path + ": can't determine file object type"); |
Misha Brukman | 1001aea | 2005-04-20 04:04:07 +0000 | [diff] [blame] | 437 | } |
Reid Spencer | 91f505e | 2004-11-16 06:15:19 +0000 | [diff] [blame] | 438 | result.insert(aPath); |
Reid Spencer | c1d474f | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 439 | } |
Reid Spencer | c1d474f | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 440 | } |
Mikhail Glushenkov | a539836 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 441 | |
Reid Spencer | c1d474f | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 442 | closedir(direntries); |
Reid Spencer | 51edba1 | 2006-08-23 06:56:27 +0000 | [diff] [blame] | 443 | return false; |
Reid Spencer | fb1f735 | 2004-11-09 20:26:31 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Reid Spencer | 6df221d | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 446 | bool |
Jeffrey Yasskin | 5908f1e | 2009-12-17 21:02:39 +0000 | [diff] [blame] | 447 | Path::set(StringRef a_path) { |
Reid Spencer | c9c0473 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 448 | if (a_path.empty()) |
Reid Spencer | 6df221d | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 449 | return false; |
Reid Spencer | 6df221d | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 450 | path = a_path; |
Reid Spencer | 6df221d | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 451 | return true; |
| 452 | } |
| 453 | |
| 454 | bool |
Jeffrey Yasskin | 5908f1e | 2009-12-17 21:02:39 +0000 | [diff] [blame] | 455 | Path::appendComponent(StringRef name) { |
Reid Spencer | c9c0473 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 456 | if (name.empty()) |
Reid Spencer | 6df221d | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 457 | return false; |
Reid Spencer | 29f83e0 | 2005-07-28 16:25:57 +0000 | [diff] [blame] | 458 | if (!lastIsSlash(path)) |
| 459 | path += '/'; |
Reid Spencer | c9c0473 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 460 | path += name; |
Reid Spencer | 6df221d | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 461 | return true; |
| 462 | } |
| 463 | |
| 464 | bool |
Reid Spencer | c9c0473 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 465 | Path::eraseComponent() { |
Reid Spencer | 6df221d | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 466 | size_t slashpos = path.rfind('/',path.size()); |
Reid Spencer | c9c0473 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 467 | if (slashpos == 0 || slashpos == std::string::npos) { |
| 468 | path.erase(); |
| 469 | return true; |
| 470 | } |
Reid Spencer | 6df221d | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 471 | if (slashpos == path.size() - 1) |
| 472 | slashpos = path.rfind('/',slashpos-1); |
Reid Spencer | c9c0473 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 473 | if (slashpos == std::string::npos) { |
| 474 | path.erase(); |
| 475 | return true; |
| 476 | } |
Reid Spencer | 6df221d | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 477 | path.erase(slashpos); |
| 478 | return true; |
| 479 | } |
| 480 | |
| 481 | bool |
Reid Spencer | c9c0473 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 482 | Path::eraseSuffix() { |
Reid Spencer | 6df221d | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 483 | size_t dotpos = path.rfind('.',path.size()); |
| 484 | size_t slashpos = path.rfind('/',path.size()); |
Jeff Cohen | 4c24144 | 2005-07-08 04:49:16 +0000 | [diff] [blame] | 485 | if (dotpos != std::string::npos) { |
Jeff Cohen | 5b106d0 | 2005-07-09 18:42:02 +0000 | [diff] [blame] | 486 | if (slashpos == std::string::npos || dotpos > slashpos+1) { |
Jeff Cohen | 4c24144 | 2005-07-08 04:49:16 +0000 | [diff] [blame] | 487 | path.erase(dotpos, path.size()-dotpos); |
Jeff Cohen | f506776 | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 488 | return true; |
Jeff Cohen | 4c24144 | 2005-07-08 04:49:16 +0000 | [diff] [blame] | 489 | } |
Reid Spencer | 6df221d | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 490 | } |
Jeff Cohen | 4c24144 | 2005-07-08 04:49:16 +0000 | [diff] [blame] | 491 | return false; |
Reid Spencer | 6df221d | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Ted Kremenek | 1d0436c | 2008-04-03 16:11:31 +0000 | [diff] [blame] | 494 | static bool createDirectoryHelper(char* beg, char* end, bool create_parents) { |
Mikhail Glushenkov | a539836 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 495 | |
Dan Gohman | 4723c10 | 2009-07-29 00:02:58 +0000 | [diff] [blame] | 496 | if (access(beg, R_OK | W_OK) == 0) |
Ted Kremenek | 1d0436c | 2008-04-03 16:11:31 +0000 | [diff] [blame] | 497 | return false; |
Mikhail Glushenkov | a539836 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 498 | |
Ted Kremenek | 1d0436c | 2008-04-03 16:11:31 +0000 | [diff] [blame] | 499 | if (create_parents) { |
Mikhail Glushenkov | a539836 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 500 | |
Ted Kremenek | 1d0436c | 2008-04-03 16:11:31 +0000 | [diff] [blame] | 501 | char* c = end; |
Mikhail Glushenkov | a539836 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 502 | |
Ted Kremenek | 1d0436c | 2008-04-03 16:11:31 +0000 | [diff] [blame] | 503 | for (; c != beg; --c) |
| 504 | if (*c == '/') { |
Mikhail Glushenkov | a539836 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 505 | |
Ted Kremenek | 1d0436c | 2008-04-03 16:11:31 +0000 | [diff] [blame] | 506 | // Recurse to handling the parent directory. |
Mikhail Glushenkov | a539836 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 507 | *c = '\0'; |
Ted Kremenek | 1d0436c | 2008-04-03 16:11:31 +0000 | [diff] [blame] | 508 | bool x = createDirectoryHelper(beg, c, create_parents); |
| 509 | *c = '/'; |
Mikhail Glushenkov | a539836 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 510 | |
Ted Kremenek | 1d0436c | 2008-04-03 16:11:31 +0000 | [diff] [blame] | 511 | // Return if we encountered an error. |
| 512 | if (x) |
| 513 | return true; |
Mikhail Glushenkov | a539836 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 514 | |
Ted Kremenek | 1d0436c | 2008-04-03 16:11:31 +0000 | [diff] [blame] | 515 | break; |
| 516 | } |
Mikhail Glushenkov | a539836 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Ted Kremenek | 1d0436c | 2008-04-03 16:11:31 +0000 | [diff] [blame] | 519 | return mkdir(beg, S_IRWXU | S_IRWXG) != 0; |
| 520 | } |
| 521 | |
Reid Spencer | 6df221d | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 522 | bool |
Reid Spencer | 8db8442 | 2006-08-23 00:39:35 +0000 | [diff] [blame] | 523 | Path::createDirectoryOnDisk( bool create_parents, std::string* ErrMsg ) { |
Reid Spencer | 6df221d | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 524 | // Get a writeable copy of the path name |
Dan Gohman | a47bfef | 2010-11-02 22:55:34 +0000 | [diff] [blame] | 525 | std::string pathname(path); |
Reid Spencer | 6df221d | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 526 | |
| 527 | // Null-terminate the last component |
Evan Cheng | 86cb318 | 2008-05-05 18:30:58 +0000 | [diff] [blame] | 528 | size_t lastchar = path.length() - 1 ; |
Mikhail Glushenkov | a539836 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 529 | |
Ted Kremenek | 1d0436c | 2008-04-03 16:11:31 +0000 | [diff] [blame] | 530 | if (pathname[lastchar] != '/') |
| 531 | ++lastchar; |
Mikhail Glushenkov | a539836 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 532 | |
Dan Gohman | f6e13ce | 2010-11-02 22:41:19 +0000 | [diff] [blame] | 533 | pathname[lastchar] = '\0'; |
Mikhail Glushenkov | a539836 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 534 | |
Dan Gohman | a47bfef | 2010-11-02 22:55:34 +0000 | [diff] [blame] | 535 | if (createDirectoryHelper(&pathname[0], &pathname[lastchar], create_parents)) |
Dan Gohman | a8b6315 | 2010-11-02 23:16:26 +0000 | [diff] [blame] | 536 | return MakeErrMsg(ErrMsg, pathname + ": can't create directory"); |
Mikhail Glushenkov | a539836 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 537 | |
Reid Spencer | 8db8442 | 2006-08-23 00:39:35 +0000 | [diff] [blame] | 538 | return false; |
Reid Spencer | 6df221d | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | bool |
Reid Spencer | 8db8442 | 2006-08-23 00:39:35 +0000 | [diff] [blame] | 542 | Path::createTemporaryFileOnDisk(bool reuse_current, std::string* ErrMsg) { |
Reid Spencer | f66d932 | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 543 | // Make this into a unique file name |
Reid Spencer | e4ca722 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 544 | if (makeUnique( reuse_current, ErrMsg )) |
| 545 | return true; |
Reid Spencer | f66d932 | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 546 | |
| 547 | // create the file |
Reid Spencer | 8db8442 | 2006-08-23 00:39:35 +0000 | [diff] [blame] | 548 | int fd = ::open(path.c_str(), O_WRONLY|O_CREAT|O_TRUNC, 0666); |
Mikhail Glushenkov | a539836 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 549 | if (fd < 0) |
Reid Spencer | 879ed5a | 2006-08-23 07:30:48 +0000 | [diff] [blame] | 550 | return MakeErrMsg(ErrMsg, path + ": can't create temporary file"); |
Reid Spencer | 8db8442 | 2006-08-23 00:39:35 +0000 | [diff] [blame] | 551 | ::close(fd); |
Reid Spencer | f66d932 | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 552 | return false; |
Reid Spencer | fb1f735 | 2004-11-09 20:26:31 +0000 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | bool |
Chris Lattner | 3cec109 | 2006-07-28 22:29:50 +0000 | [diff] [blame] | 556 | Path::eraseFromDisk(bool remove_contents, std::string *ErrStr) const { |
Dan Gohman | 39027c4 | 2010-03-30 20:04:57 +0000 | [diff] [blame] | 557 | // Get the status so we can determine if it's a file or directory. |
Reid Spencer | ceeb918 | 2007-04-07 18:52:17 +0000 | [diff] [blame] | 558 | struct stat buf; |
| 559 | if (0 != stat(path.c_str(), &buf)) { |
| 560 | MakeErrMsg(ErrStr, path + ": can't get status of file"); |
Chris Lattner | 3cec109 | 2006-07-28 22:29:50 +0000 | [diff] [blame] | 561 | return true; |
Reid Spencer | ceeb918 | 2007-04-07 18:52:17 +0000 | [diff] [blame] | 562 | } |
| 563 | |
Mikhail Glushenkov | a539836 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 564 | // Note: this check catches strange situations. In all cases, LLVM should |
| 565 | // only be involved in the creation and deletion of regular files. This |
| 566 | // check ensures that what we're trying to erase is a regular file. It |
| 567 | // effectively prevents LLVM from erasing things like /dev/null, any block |
| 568 | // special file, or other things that aren't "regular" files. |
Reid Spencer | ceeb918 | 2007-04-07 18:52:17 +0000 | [diff] [blame] | 569 | if (S_ISREG(buf.st_mode)) { |
| 570 | if (unlink(path.c_str()) != 0) |
| 571 | return MakeErrMsg(ErrStr, path + ": can't destroy file"); |
| 572 | return false; |
| 573 | } |
Mikhail Glushenkov | a539836 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 574 | |
Reid Spencer | ceeb918 | 2007-04-07 18:52:17 +0000 | [diff] [blame] | 575 | if (!S_ISDIR(buf.st_mode)) { |
| 576 | if (ErrStr) *ErrStr = "not a file or directory"; |
| 577 | return true; |
| 578 | } |
Reid Spencer | 200c6f9 | 2007-03-29 19:05:44 +0000 | [diff] [blame] | 579 | |
Chris Lattner | 3cec109 | 2006-07-28 22:29:50 +0000 | [diff] [blame] | 580 | if (remove_contents) { |
| 581 | // Recursively descend the directory to remove its contents. |
| 582 | std::string cmd = "/bin/rm -rf " + path; |
Mikhail Glushenkov | 58a1a8e | 2009-02-15 03:20:32 +0000 | [diff] [blame] | 583 | if (system(cmd.c_str()) != 0) { |
| 584 | MakeErrMsg(ErrStr, path + ": failed to recursively remove directory."); |
| 585 | return true; |
| 586 | } |
Chris Lattner | 3cec109 | 2006-07-28 22:29:50 +0000 | [diff] [blame] | 587 | return false; |
| 588 | } |
| 589 | |
| 590 | // Otherwise, try to just remove the one directory. |
Dan Gohman | a47bfef | 2010-11-02 22:55:34 +0000 | [diff] [blame] | 591 | std::string pathname(path); |
Evan Cheng | 86cb318 | 2008-05-05 18:30:58 +0000 | [diff] [blame] | 592 | size_t lastchar = path.length() - 1; |
Mikhail Glushenkov | a539836 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 593 | if (pathname[lastchar] == '/') |
Dan Gohman | f6e13ce | 2010-11-02 22:41:19 +0000 | [diff] [blame] | 594 | pathname[lastchar] = '\0'; |
Chris Lattner | 3cec109 | 2006-07-28 22:29:50 +0000 | [diff] [blame] | 595 | else |
Dan Gohman | f6e13ce | 2010-11-02 22:41:19 +0000 | [diff] [blame] | 596 | pathname[lastchar+1] = '\0'; |
Mikhail Glushenkov | a539836 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 597 | |
Dan Gohman | a47bfef | 2010-11-02 22:55:34 +0000 | [diff] [blame] | 598 | if (rmdir(pathname.c_str()) != 0) |
| 599 | return MakeErrMsg(ErrStr, pathname + ": can't erase directory"); |
Chris Lattner | 3cec109 | 2006-07-28 22:29:50 +0000 | [diff] [blame] | 600 | return false; |
Reid Spencer | 6df221d | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | bool |
Reid Spencer | 879ed5a | 2006-08-23 07:30:48 +0000 | [diff] [blame] | 604 | Path::renamePathOnDisk(const Path& newName, std::string* ErrMsg) { |
Reid Spencer | c9c0473 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 605 | if (0 != ::rename(path.c_str(), newName.c_str())) |
Mikhail Glushenkov | a539836 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 606 | return MakeErrMsg(ErrMsg, std::string("can't rename '") + path + "' as '" + |
Chris Lattner | c521f54 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 607 | newName.str() + "'"); |
Reid Spencer | 879ed5a | 2006-08-23 07:30:48 +0000 | [diff] [blame] | 608 | return false; |
Reid Spencer | c1d474f | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 609 | } |
| 610 | |
Mikhail Glushenkov | a539836 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 611 | bool |
Chris Lattner | 60c5064 | 2006-07-28 22:36:17 +0000 | [diff] [blame] | 612 | Path::setStatusInfoOnDisk(const FileStatus &si, std::string *ErrStr) const { |
Reid Spencer | c1d474f | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 613 | struct utimbuf utb; |
| 614 | utb.actime = si.modTime.toPosixTime(); |
| 615 | utb.modtime = utb.actime; |
| 616 | if (0 != ::utime(path.c_str(),&utb)) |
Reid Spencer | e4ca722 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 617 | return MakeErrMsg(ErrStr, path + ": can't set file modification time"); |
Reid Spencer | c1d474f | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 618 | if (0 != ::chmod(path.c_str(),si.mode)) |
Reid Spencer | e4ca722 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 619 | return MakeErrMsg(ErrStr, path + ": can't set mode"); |
Chris Lattner | 60c5064 | 2006-07-28 22:36:17 +0000 | [diff] [blame] | 620 | return false; |
Reid Spencer | 6df221d | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 621 | } |
| 622 | |
Mikhail Glushenkov | a539836 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 623 | bool |
Reid Spencer | e4ca722 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 624 | Path::makeUnique(bool reuse_current, std::string* ErrMsg) { |
Michael J. Spencer | 58df2e0 | 2011-01-10 02:34:23 +0000 | [diff] [blame] | 625 | bool Exists; |
| 626 | if (reuse_current && (fs::exists(path, Exists) || !Exists)) |
Reid Spencer | e4ca722 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 627 | return false; // File doesn't exist already, just use it! |
Reid Spencer | f66d932 | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 628 | |
Mikhail Glushenkov | a539836 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 629 | // Append an XXXXXX pattern to the end of the file for use with mkstemp, |
Reid Spencer | f66d932 | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 630 | // mktemp or our own implementation. |
Dan Gohman | a84dc0c | 2010-04-19 15:54:44 +0000 | [diff] [blame] | 631 | // This uses std::vector instead of SmallVector to avoid a dependence on |
| 632 | // libSupport. And performance isn't critical here. |
| 633 | std::vector<char> Buf; |
| 634 | Buf.resize(path.size()+8); |
Dan Gohman | 3457061 | 2010-04-19 16:33:28 +0000 | [diff] [blame] | 635 | char *FNBuffer = &Buf[0]; |
Dan Gohman | a84dc0c | 2010-04-19 15:54:44 +0000 | [diff] [blame] | 636 | path.copy(FNBuffer,path.size()); |
Michael J. Spencer | 0d771ed | 2011-01-11 01:21:55 +0000 | [diff] [blame] | 637 | bool isdir; |
| 638 | if (!fs::is_directory(path, isdir) && isdir) |
Dan Gohman | a84dc0c | 2010-04-19 15:54:44 +0000 | [diff] [blame] | 639 | strcpy(FNBuffer+path.size(), "/XXXXXX"); |
Devang Patel | cbfc1a4 | 2008-07-24 00:35:38 +0000 | [diff] [blame] | 640 | else |
Dan Gohman | a84dc0c | 2010-04-19 15:54:44 +0000 | [diff] [blame] | 641 | strcpy(FNBuffer+path.size(), "-XXXXXX"); |
Reid Spencer | f66d932 | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 642 | |
| 643 | #if defined(HAVE_MKSTEMP) |
| 644 | int TempFD; |
Dan Gohman | a84dc0c | 2010-04-19 15:54:44 +0000 | [diff] [blame] | 645 | if ((TempFD = mkstemp(FNBuffer)) == -1) |
Reid Spencer | e4ca722 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 646 | return MakeErrMsg(ErrMsg, path + ": can't make unique filename"); |
Reid Spencer | f66d932 | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 647 | |
| 648 | // We don't need to hold the temp file descriptor... we will trust that no one |
| 649 | // will overwrite/delete the file before we can open it again. |
| 650 | close(TempFD); |
| 651 | |
| 652 | // Save the name |
Dan Gohman | a84dc0c | 2010-04-19 15:54:44 +0000 | [diff] [blame] | 653 | path = FNBuffer; |
Chad Rosier | 30c3463 | 2011-07-05 18:55:31 +0000 | [diff] [blame] | 654 | |
| 655 | // By default mkstemp sets the mode to 0600, so update mode bits now. |
| 656 | AddPermissionBits (*this, 0666); |
Reid Spencer | f66d932 | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 657 | #elif defined(HAVE_MKTEMP) |
| 658 | // If we don't have mkstemp, use the old and obsolete mktemp function. |
Dan Gohman | a84dc0c | 2010-04-19 15:54:44 +0000 | [diff] [blame] | 659 | if (mktemp(FNBuffer) == 0) |
Reid Spencer | e4ca722 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 660 | return MakeErrMsg(ErrMsg, path + ": can't make unique filename"); |
Reid Spencer | f66d932 | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 661 | |
| 662 | // Save the name |
Dan Gohman | a84dc0c | 2010-04-19 15:54:44 +0000 | [diff] [blame] | 663 | path = FNBuffer; |
Reid Spencer | f66d932 | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 664 | #else |
| 665 | // Okay, looks like we have to do it all by our lonesome. |
| 666 | static unsigned FCounter = 0; |
Chris Lattner | cda39c4 | 2010-07-12 00:09:55 +0000 | [diff] [blame] | 667 | // Try to initialize with unique value. |
| 668 | if (FCounter == 0) FCounter = ((unsigned)getpid() & 0xFFFF) << 8; |
| 669 | char* pos = strstr(FNBuffer, "XXXXXX"); |
| 670 | do { |
| 671 | if (++FCounter > 0xFFFFFF) { |
| 672 | return MakeErrMsg(ErrMsg, |
| 673 | path + ": can't make unique filename: too many files"); |
| 674 | } |
| 675 | sprintf(pos, "%06X", FCounter); |
Dan Gohman | a84dc0c | 2010-04-19 15:54:44 +0000 | [diff] [blame] | 676 | path = FNBuffer; |
Chris Lattner | cda39c4 | 2010-07-12 00:09:55 +0000 | [diff] [blame] | 677 | } while (exists()); |
| 678 | // POSSIBLE SECURITY BUG: An attacker can easily guess the name and exploit |
| 679 | // LLVM. |
Reid Spencer | f66d932 | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 680 | #endif |
Reid Spencer | e4ca722 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 681 | return false; |
| 682 | } |
Reid Spencer | e4ca722 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 683 | } // end llvm namespace |