Charles Davis | 53ca1f3 | 2010-11-29 19:44:50 +0000 | [diff] [blame] | 1 | //===- llvm/Support/Unix/Path.cpp - Unix Path Implementation -----*- C++ -*-===// |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 2 | // |
Reid Spencer | b89a223 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 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 | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 7 | // |
Reid Spencer | b89a223 | 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 | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 16 | //=== is guaranteed to work on *all* UNIX variants. |
Reid Spencer | b89a223 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 17 | //===----------------------------------------------------------------------===// |
| 18 | |
| 19 | #include "Unix.h" |
Reid Spencer | af2f208 | 2004-12-27 06:17:15 +0000 | [diff] [blame] | 20 | #if HAVE_SYS_STAT_H |
Reid Spencer | b89a223 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 21 | #include <sys/stat.h> |
Reid Spencer | af2f208 | 2004-12-27 06:17:15 +0000 | [diff] [blame] | 22 | #endif |
| 23 | #if HAVE_FCNTL_H |
Reid Spencer | b89a223 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 24 | #include <fcntl.h> |
Reid Spencer | af2f208 | 2004-12-27 06:17:15 +0000 | [diff] [blame] | 25 | #endif |
Chris Lattner | 14c762d | 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 | af2f208 | 2004-12-27 06:17:15 +0000 | [diff] [blame] | 32 | #if HAVE_UTIME_H |
Reid Spencer | eaf1815 | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 33 | #include <utime.h> |
Reid Spencer | af2f208 | 2004-12-27 06:17:15 +0000 | [diff] [blame] | 34 | #endif |
| 35 | #if HAVE_TIME_H |
Reid Spencer | 69a1616 | 2004-12-24 06:29:42 +0000 | [diff] [blame] | 36 | #include <time.h> |
Reid Spencer | af2f208 | 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 | 1a09144 | 2008-03-03 02:55:43 +0000 | [diff] [blame] | 55 | #if HAVE_DLFCN_H |
| 56 | #include <dlfcn.h> |
| 57 | #endif |
| 58 | |
Benjamin Kramer | d8b0630 | 2009-09-09 12:09:08 +0000 | [diff] [blame] | 59 | #ifdef __APPLE__ |
| 60 | #include <mach-o/dyld.h> |
| 61 | #endif |
| 62 | |
Reid Spencer | 932e2e3 | 2005-06-02 05:38:20 +0000 | [diff] [blame] | 63 | // Put in a hack for Cygwin which falsely reports that the mkdtemp function |
| 64 | // is available when it is not. |
| 65 | #ifdef __CYGWIN__ |
| 66 | # undef HAVE_MKDTEMP |
| 67 | #endif |
Reid Spencer | b89a223 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 68 | |
Reid Spencer | 3be872e | 2005-07-28 16:25:57 +0000 | [diff] [blame] | 69 | namespace { |
| 70 | inline bool lastIsSlash(const std::string& path) { |
| 71 | return !path.empty() && path[path.length() - 1] == '/'; |
| 72 | } |
| 73 | |
| 74 | } |
| 75 | |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 76 | namespace llvm { |
| 77 | using namespace sys; |
Reid Spencer | b89a223 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 78 | |
Daniel Dunbar | 164c7ba | 2009-12-09 03:26:33 +0000 | [diff] [blame] | 79 | const char sys::PathSeparator = ':'; |
Chris Lattner | e1b332a | 2008-02-27 06:17:10 +0000 | [diff] [blame] | 80 | |
Mikhail Glushenkov | f5a95ce | 2010-11-02 20:32:26 +0000 | [diff] [blame] | 81 | StringRef Path::GetEXESuffix() { |
Dan Gohman | 8eeb5ae | 2010-11-02 20:52:47 +0000 | [diff] [blame] | 82 | return StringRef(); |
Mikhail Glushenkov | f5a95ce | 2010-11-02 20:32:26 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Jeffrey Yasskin | 88cd358 | 2009-12-17 21:02:39 +0000 | [diff] [blame] | 85 | Path::Path(StringRef p) |
Nick Lewycky | fff116f | 2008-05-11 17:37:40 +0000 | [diff] [blame] | 86 | : path(p) {} |
| 87 | |
| 88 | Path::Path(const char *StrStart, unsigned StrLen) |
| 89 | : path(StrStart, StrLen) {} |
| 90 | |
Chris Lattner | 0eab5e2 | 2008-08-11 23:39:47 +0000 | [diff] [blame] | 91 | Path& |
Jeffrey Yasskin | 88cd358 | 2009-12-17 21:02:39 +0000 | [diff] [blame] | 92 | Path::operator=(StringRef that) { |
| 93 | path.assign(that.data(), that.size()); |
Chris Lattner | 0eab5e2 | 2008-08-11 23:39:47 +0000 | [diff] [blame] | 94 | return *this; |
| 95 | } |
| 96 | |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 97 | bool |
Reid Spencer | 69a1616 | 2004-12-24 06:29:42 +0000 | [diff] [blame] | 98 | Path::isValid() const { |
Dan Gohman | ac79382 | 2010-11-02 23:19:55 +0000 | [diff] [blame] | 99 | // Empty paths are considered invalid here. |
| 100 | // This code doesn't check MAXPATHLEN because there's no need. Nothing in |
| 101 | // LLVM manipulates Paths with fixed-sizes arrays, and if the OS can't |
| 102 | // handle names longer than some limit, it'll report this on demand using |
| 103 | // ENAMETOLONG. |
| 104 | return !path.empty(); |
Reid Spencer | 69a1616 | 2004-12-24 06:29:42 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 107 | bool |
Chris Lattner | 88d7e40 | 2009-06-15 04:17:07 +0000 | [diff] [blame] | 108 | Path::isAbsolute(const char *NameStart, unsigned NameLen) { |
| 109 | assert(NameStart); |
| 110 | if (NameLen == 0) |
| 111 | return false; |
| 112 | return NameStart[0] == '/'; |
| 113 | } |
| 114 | |
| 115 | bool |
Reid Spencer | 69cce81 | 2007-03-29 16:43:20 +0000 | [diff] [blame] | 116 | Path::isAbsolute() const { |
| 117 | if (path.empty()) |
| 118 | return false; |
| 119 | return path[0] == '/'; |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 120 | } |
Daniel Dunbar | a00e85c | 2009-07-12 20:23:56 +0000 | [diff] [blame] | 121 | |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 122 | Path |
| 123 | Path::GetRootDirectory() { |
| 124 | Path result; |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 125 | result.set("/"); |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 126 | return result; |
| 127 | } |
| 128 | |
Reid Spencer | 69a1616 | 2004-12-24 06:29:42 +0000 | [diff] [blame] | 129 | Path |
Chris Lattner | d18a2c1 | 2009-02-19 05:34:35 +0000 | [diff] [blame] | 130 | Path::GetTemporaryDirectory(std::string *ErrMsg) { |
Reid Spencer | 69a1616 | 2004-12-24 06:29:42 +0000 | [diff] [blame] | 131 | #if defined(HAVE_MKDTEMP) |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 132 | // The best way is with mkdtemp but that's not available on many systems, |
Reid Spencer | 69a1616 | 2004-12-24 06:29:42 +0000 | [diff] [blame] | 133 | // Linux and FreeBSD have it. Others probably won't. |
Dan Gohman | 0803ebe | 2010-11-02 22:50:10 +0000 | [diff] [blame] | 134 | char pathname[] = "/tmp/llvm_XXXXXX"; |
Reid Spencer | 4874476 | 2006-08-22 19:01:30 +0000 | [diff] [blame] | 135 | if (0 == mkdtemp(pathname)) { |
Dan Gohman | 6f6021e | 2010-11-02 22:56:51 +0000 | [diff] [blame] | 136 | MakeErrMsg(ErrMsg, |
| 137 | std::string(pathname) + ": can't create temporary directory"); |
Reid Spencer | 4874476 | 2006-08-22 19:01:30 +0000 | [diff] [blame] | 138 | return Path(); |
| 139 | } |
Dan Gohman | aabb9b6 | 2010-11-03 00:01:23 +0000 | [diff] [blame] | 140 | return Path(pathname); |
Reid Spencer | 69a1616 | 2004-12-24 06:29:42 +0000 | [diff] [blame] | 141 | #elif defined(HAVE_MKSTEMP) |
| 142 | // If no mkdtemp is available, mkstemp can be used to create a temporary file |
| 143 | // which is then removed and created as a directory. We prefer this over |
| 144 | // mktemp because of mktemp's inherent security and threading risks. We still |
| 145 | // have a slight race condition from the time the temporary file is created to |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 146 | // the time it is re-created as a directoy. |
Dan Gohman | 0803ebe | 2010-11-02 22:50:10 +0000 | [diff] [blame] | 147 | char pathname[] = "/tmp/llvm_XXXXXX"; |
Reid Spencer | 69a1616 | 2004-12-24 06:29:42 +0000 | [diff] [blame] | 148 | int fd = 0; |
Reid Spencer | 4874476 | 2006-08-22 19:01:30 +0000 | [diff] [blame] | 149 | if (-1 == (fd = mkstemp(pathname))) { |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 150 | MakeErrMsg(ErrMsg, |
Reid Spencer | 4874476 | 2006-08-22 19:01:30 +0000 | [diff] [blame] | 151 | std::string(pathname) + ": can't create temporary directory"); |
| 152 | return Path(); |
| 153 | } |
Reid Spencer | 69a1616 | 2004-12-24 06:29:42 +0000 | [diff] [blame] | 154 | ::close(fd); |
| 155 | ::unlink(pathname); // start race condition, ignore errors |
Reid Spencer | 4874476 | 2006-08-22 19:01:30 +0000 | [diff] [blame] | 156 | if (-1 == ::mkdir(pathname, S_IRWXU)) { // end race condition |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 157 | MakeErrMsg(ErrMsg, |
Reid Spencer | 4874476 | 2006-08-22 19:01:30 +0000 | [diff] [blame] | 158 | std::string(pathname) + ": can't create temporary directory"); |
| 159 | return Path(); |
| 160 | } |
Dan Gohman | aabb9b6 | 2010-11-03 00:01:23 +0000 | [diff] [blame] | 161 | return Path(pathname); |
Reid Spencer | 69a1616 | 2004-12-24 06:29:42 +0000 | [diff] [blame] | 162 | #elif defined(HAVE_MKTEMP) |
| 163 | // If a system doesn't have mkdtemp(3) or mkstemp(3) but it does have |
| 164 | // mktemp(3) then we'll assume that system (e.g. AIX) has a reasonable |
| 165 | // implementation of mktemp(3) and doesn't follow BSD 4.3's lead of replacing |
| 166 | // the XXXXXX with the pid of the process and a letter. That leads to only |
| 167 | // twenty six temporary files that can be generated. |
Dan Gohman | 0803ebe | 2010-11-02 22:50:10 +0000 | [diff] [blame] | 168 | char pathname[] = "/tmp/llvm_XXXXXX"; |
Reid Spencer | 69a1616 | 2004-12-24 06:29:42 +0000 | [diff] [blame] | 169 | char *TmpName = ::mktemp(pathname); |
Reid Spencer | 4874476 | 2006-08-22 19:01:30 +0000 | [diff] [blame] | 170 | if (TmpName == 0) { |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 171 | MakeErrMsg(ErrMsg, |
Reid Spencer | 4874476 | 2006-08-22 19:01:30 +0000 | [diff] [blame] | 172 | std::string(TmpName) + ": can't create unique directory name"); |
| 173 | return Path(); |
| 174 | } |
| 175 | if (-1 == ::mkdir(TmpName, S_IRWXU)) { |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 176 | MakeErrMsg(ErrMsg, |
Reid Spencer | 4874476 | 2006-08-22 19:01:30 +0000 | [diff] [blame] | 177 | std::string(TmpName) + ": can't create temporary directory"); |
| 178 | return Path(); |
| 179 | } |
Dan Gohman | aabb9b6 | 2010-11-03 00:01:23 +0000 | [diff] [blame] | 180 | return Path(TmpName); |
Reid Spencer | 69a1616 | 2004-12-24 06:29:42 +0000 | [diff] [blame] | 181 | #else |
| 182 | // This is the worst case implementation. tempnam(3) leaks memory unless its |
| 183 | // on an SVID2 (or later) system. On BSD 4.3 it leaks. tmpnam(3) has thread |
| 184 | // issues. The mktemp(3) function doesn't have enough variability in the |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 185 | // temporary name generated. So, we provide our own implementation that |
Reid Spencer | 69a1616 | 2004-12-24 06:29:42 +0000 | [diff] [blame] | 186 | // increments an integer from a random number seeded by the current time. This |
| 187 | // should be sufficiently unique that we don't have many collisions between |
| 188 | // processes. Generally LLVM processes don't run very long and don't use very |
| 189 | // many temporary files so this shouldn't be a big issue for LLVM. |
| 190 | static time_t num = ::time(0); |
| 191 | char pathname[MAXPATHLEN]; |
| 192 | do { |
| 193 | num++; |
| 194 | sprintf(pathname, "/tmp/llvm_%010u", unsigned(num)); |
| 195 | } while ( 0 == access(pathname, F_OK ) ); |
Reid Spencer | 4874476 | 2006-08-22 19:01:30 +0000 | [diff] [blame] | 196 | if (-1 == ::mkdir(pathname, S_IRWXU)) { |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 197 | MakeErrMsg(ErrMsg, |
Reid Spencer | 4874476 | 2006-08-22 19:01:30 +0000 | [diff] [blame] | 198 | std::string(pathname) + ": can't create temporary directory"); |
| 199 | return Path(); |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 200 | } |
Dan Gohman | aabb9b6 | 2010-11-03 00:01:23 +0000 | [diff] [blame] | 201 | return Path(pathname); |
Reid Spencer | 69a1616 | 2004-12-24 06:29:42 +0000 | [diff] [blame] | 202 | #endif |
| 203 | } |
| 204 | |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 205 | void |
Reid Spencer | 1b6b99b | 2004-12-13 03:00:51 +0000 | [diff] [blame] | 206 | Path::GetSystemLibraryPaths(std::vector<sys::Path>& Paths) { |
| 207 | #ifdef LTDL_SHLIBPATH_VAR |
| 208 | char* env_var = getenv(LTDL_SHLIBPATH_VAR); |
| 209 | if (env_var != 0) { |
| 210 | getPathList(env_var,Paths); |
Reid Spencer | 74e7261 | 2004-09-14 00:16:39 +0000 | [diff] [blame] | 211 | } |
Reid Spencer | 1b6b99b | 2004-12-13 03:00:51 +0000 | [diff] [blame] | 212 | #endif |
| 213 | // FIXME: Should this look at LD_LIBRARY_PATH too? |
| 214 | Paths.push_back(sys::Path("/usr/local/lib/")); |
| 215 | Paths.push_back(sys::Path("/usr/X11R6/lib/")); |
| 216 | Paths.push_back(sys::Path("/usr/lib/")); |
| 217 | Paths.push_back(sys::Path("/lib/")); |
Reid Spencer | 74e7261 | 2004-09-14 00:16:39 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Reid Spencer | 1b6b99b | 2004-12-13 03:00:51 +0000 | [diff] [blame] | 220 | void |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 221 | Path::GetBitcodeLibraryPaths(std::vector<sys::Path>& Paths) { |
Reid Spencer | 1b6b99b | 2004-12-13 03:00:51 +0000 | [diff] [blame] | 222 | char * env_var = getenv("LLVM_LIB_SEARCH_PATH"); |
| 223 | if (env_var != 0) { |
| 224 | getPathList(env_var,Paths); |
| 225 | } |
Reid Spencer | 1b6b99b | 2004-12-13 03:00:51 +0000 | [diff] [blame] | 226 | #ifdef LLVM_LIBDIR |
| 227 | { |
| 228 | Path tmpPath; |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 229 | if (tmpPath.set(LLVM_LIBDIR)) |
Reid Spencer | c7f0832 | 2005-07-07 18:21:42 +0000 | [diff] [blame] | 230 | if (tmpPath.canRead()) |
Reid Spencer | 1b6b99b | 2004-12-13 03:00:51 +0000 | [diff] [blame] | 231 | Paths.push_back(tmpPath); |
| 232 | } |
| 233 | #endif |
| 234 | GetSystemLibraryPaths(Paths); |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 237 | Path |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 238 | Path::GetLLVMDefaultConfigDir() { |
| 239 | return Path("/etc/llvm/"); |
| 240 | } |
| 241 | |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 242 | Path |
| 243 | Path::GetUserHomeDirectory() { |
| 244 | const char* home = getenv("HOME"); |
Michael J. Spencer | 9170a0f | 2010-12-27 03:21:41 +0000 | [diff] [blame] | 245 | Path result; |
| 246 | if (home && result.set(home)) |
| 247 | return result; |
| 248 | result.set("/"); |
| 249 | return result; |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Ted Kremenek | 7920078 | 2007-12-18 22:07:33 +0000 | [diff] [blame] | 252 | Path |
| 253 | Path::GetCurrentDirectory() { |
| 254 | char pathname[MAXPATHLEN]; |
| 255 | if (!getcwd(pathname,MAXPATHLEN)) { |
| 256 | assert (false && "Could not query current working directory."); |
Dan Gohman | d98af0a | 2010-08-04 01:39:08 +0000 | [diff] [blame] | 257 | return Path(); |
Ted Kremenek | 7920078 | 2007-12-18 22:07:33 +0000 | [diff] [blame] | 258 | } |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 259 | |
Ted Kremenek | 7920078 | 2007-12-18 22:07:33 +0000 | [diff] [blame] | 260 | return Path(pathname); |
| 261 | } |
Reid Spencer | a229c5c | 2005-07-08 03:08:58 +0000 | [diff] [blame] | 262 | |
Dan Gohman | 86759ac | 2010-09-02 18:24:46 +0000 | [diff] [blame] | 263 | #if defined(__FreeBSD__) || defined (__NetBSD__) || defined(__minix) |
Chris Lattner | 893a0f9 | 2009-03-02 22:17:15 +0000 | [diff] [blame] | 264 | static int |
| 265 | test_dir(char buf[PATH_MAX], char ret[PATH_MAX], |
| 266 | const char *dir, const char *bin) |
| 267 | { |
Bill Wendling | 51b16f4 | 2009-05-30 01:09:53 +0000 | [diff] [blame] | 268 | struct stat sb; |
Chris Lattner | 893a0f9 | 2009-03-02 22:17:15 +0000 | [diff] [blame] | 269 | |
Dan Gohman | 86759ac | 2010-09-02 18:24:46 +0000 | [diff] [blame] | 270 | snprintf(buf, PATH_MAX, "%s/%s", dir, bin); |
Bill Wendling | 51b16f4 | 2009-05-30 01:09:53 +0000 | [diff] [blame] | 271 | if (realpath(buf, ret) == NULL) |
| 272 | return (1); |
| 273 | if (stat(buf, &sb) != 0) |
| 274 | return (1); |
| 275 | |
| 276 | return (0); |
Chris Lattner | 893a0f9 | 2009-03-02 22:17:15 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | static char * |
| 280 | getprogpath(char ret[PATH_MAX], const char *bin) |
| 281 | { |
Bill Wendling | 51b16f4 | 2009-05-30 01:09:53 +0000 | [diff] [blame] | 282 | char *pv, *s, *t, buf[PATH_MAX]; |
Chris Lattner | 893a0f9 | 2009-03-02 22:17:15 +0000 | [diff] [blame] | 283 | |
Bill Wendling | 51b16f4 | 2009-05-30 01:09:53 +0000 | [diff] [blame] | 284 | /* First approach: absolute path. */ |
| 285 | if (bin[0] == '/') { |
| 286 | if (test_dir(buf, ret, "/", bin) == 0) |
| 287 | return (ret); |
| 288 | return (NULL); |
| 289 | } |
Chris Lattner | 893a0f9 | 2009-03-02 22:17:15 +0000 | [diff] [blame] | 290 | |
Bill Wendling | 51b16f4 | 2009-05-30 01:09:53 +0000 | [diff] [blame] | 291 | /* Second approach: relative path. */ |
| 292 | if (strchr(bin, '/') != NULL) { |
| 293 | if (getcwd(buf, PATH_MAX) == NULL) |
| 294 | return (NULL); |
| 295 | if (test_dir(buf, ret, buf, bin) == 0) |
| 296 | return (ret); |
| 297 | return (NULL); |
| 298 | } |
Chris Lattner | 893a0f9 | 2009-03-02 22:17:15 +0000 | [diff] [blame] | 299 | |
Bill Wendling | 51b16f4 | 2009-05-30 01:09:53 +0000 | [diff] [blame] | 300 | /* Third approach: $PATH */ |
| 301 | if ((pv = getenv("PATH")) == NULL) |
| 302 | return (NULL); |
| 303 | s = pv = strdup(pv); |
| 304 | if (pv == NULL) |
| 305 | return (NULL); |
| 306 | while ((t = strsep(&s, ":")) != NULL) { |
| 307 | if (test_dir(buf, ret, t, bin) == 0) { |
| 308 | free(pv); |
| 309 | return (ret); |
| 310 | } |
| 311 | } |
| 312 | free(pv); |
| 313 | return (NULL); |
Chris Lattner | 893a0f9 | 2009-03-02 22:17:15 +0000 | [diff] [blame] | 314 | } |
Anton Korobeynikov | 1d76ab7 | 2010-08-31 22:38:00 +0000 | [diff] [blame] | 315 | #endif // __FreeBSD__ || __NetBSD__ |
Chris Lattner | 893a0f9 | 2009-03-02 22:17:15 +0000 | [diff] [blame] | 316 | |
Chris Lattner | 1a09144 | 2008-03-03 02:55:43 +0000 | [diff] [blame] | 317 | /// GetMainExecutable - Return the path to the main executable, given the |
| 318 | /// value of argv[0] from program startup. |
| 319 | Path Path::GetMainExecutable(const char *argv0, void *MainAddr) { |
Benjamin Kramer | d8b0630 | 2009-09-09 12:09:08 +0000 | [diff] [blame] | 320 | #if defined(__APPLE__) |
| 321 | // On OS X the executable path is saved to the stack by dyld. Reading it |
| 322 | // from there is much faster than calling dladdr, especially for large |
| 323 | // binaries with symbols. |
| 324 | char exe_path[MAXPATHLEN]; |
| 325 | uint32_t size = sizeof(exe_path); |
| 326 | if (_NSGetExecutablePath(exe_path, &size) == 0) { |
| 327 | char link_path[MAXPATHLEN]; |
Kovarththanan Rajaratnam | d614673 | 2009-11-29 17:19:48 +0000 | [diff] [blame] | 328 | if (realpath(exe_path, link_path)) |
Dan Gohman | 8d4fd96 | 2010-11-02 22:07:47 +0000 | [diff] [blame] | 329 | return Path(link_path); |
Benjamin Kramer | d8b0630 | 2009-09-09 12:09:08 +0000 | [diff] [blame] | 330 | } |
Dan Gohman | 86759ac | 2010-09-02 18:24:46 +0000 | [diff] [blame] | 331 | #elif defined(__FreeBSD__) || defined (__NetBSD__) || defined(__minix) |
Chris Lattner | 893a0f9 | 2009-03-02 22:17:15 +0000 | [diff] [blame] | 332 | char exe_path[PATH_MAX]; |
| 333 | |
| 334 | if (getprogpath(exe_path, argv0) != NULL) |
Dan Gohman | 8d4fd96 | 2010-11-02 22:07:47 +0000 | [diff] [blame] | 335 | return Path(exe_path); |
Chris Lattner | 893a0f9 | 2009-03-02 22:17:15 +0000 | [diff] [blame] | 336 | #elif defined(__linux__) || defined(__CYGWIN__) |
Chris Lattner | 3bdfa04 | 2008-03-13 05:22:05 +0000 | [diff] [blame] | 337 | char exe_path[MAXPATHLEN]; |
Seo Sanghyeon | fd6437f | 2008-06-27 22:55:30 +0000 | [diff] [blame] | 338 | ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path)); |
Dan Gohman | 7b3544b | 2009-08-05 20:16:55 +0000 | [diff] [blame] | 339 | if (len >= 0) |
Dan Gohman | 8d4fd96 | 2010-11-02 22:07:47 +0000 | [diff] [blame] | 340 | return Path(StringRef(exe_path, len)); |
Chris Lattner | 3bdfa04 | 2008-03-13 05:22:05 +0000 | [diff] [blame] | 341 | #elif defined(HAVE_DLFCN_H) |
Chris Lattner | 1a09144 | 2008-03-03 02:55:43 +0000 | [diff] [blame] | 342 | // Use dladdr to get executable path if available. |
Chris Lattner | 1a09144 | 2008-03-03 02:55:43 +0000 | [diff] [blame] | 343 | Dl_info DLInfo; |
| 344 | int err = dladdr(MainAddr, &DLInfo); |
Chris Lattner | d18a2c1 | 2009-02-19 05:34:35 +0000 | [diff] [blame] | 345 | if (err == 0) |
| 346 | return Path(); |
Bill Wendling | 51b16f4 | 2009-05-30 01:09:53 +0000 | [diff] [blame] | 347 | |
Chris Lattner | d18a2c1 | 2009-02-19 05:34:35 +0000 | [diff] [blame] | 348 | // If the filename is a symlink, we need to resolve and return the location of |
| 349 | // the actual executable. |
| 350 | char link_path[MAXPATHLEN]; |
Kovarththanan Rajaratnam | d614673 | 2009-11-29 17:19:48 +0000 | [diff] [blame] | 351 | if (realpath(DLInfo.dli_fname, link_path)) |
Dan Gohman | 8d4fd96 | 2010-11-02 22:07:47 +0000 | [diff] [blame] | 352 | return Path(link_path); |
Dan Gohman | b52d44a | 2010-09-07 18:26:49 +0000 | [diff] [blame] | 353 | #else |
| 354 | #error GetMainExecutable is not implemented on this host yet. |
Chris Lattner | 1a09144 | 2008-03-03 02:55:43 +0000 | [diff] [blame] | 355 | #endif |
| 356 | return Path(); |
| 357 | } |
| 358 | |
| 359 | |
Jeffrey Yasskin | 88cd358 | 2009-12-17 21:02:39 +0000 | [diff] [blame] | 360 | StringRef Path::getDirname() const { |
| 361 | return getDirnameCharSep(path, "/"); |
Ted Kremenek | 9b01cc0 | 2008-04-07 22:01:32 +0000 | [diff] [blame] | 362 | } |
Ted Kremenek | cf55c8e | 2008-04-07 21:53:57 +0000 | [diff] [blame] | 363 | |
Jeffrey Yasskin | 88cd358 | 2009-12-17 21:02:39 +0000 | [diff] [blame] | 364 | StringRef |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 365 | Path::getBasename() const { |
Reid Spencer | 1b554b4 | 2004-09-11 04:55:08 +0000 | [diff] [blame] | 366 | // Find the last slash |
Ted Kremenek | cf55c8e | 2008-04-07 21:53:57 +0000 | [diff] [blame] | 367 | std::string::size_type slash = path.rfind('/'); |
Reid Spencer | 1b554b4 | 2004-09-11 04:55:08 +0000 | [diff] [blame] | 368 | if (slash == std::string::npos) |
| 369 | slash = 0; |
| 370 | else |
| 371 | slash++; |
| 372 | |
Ted Kremenek | cf55c8e | 2008-04-07 21:53:57 +0000 | [diff] [blame] | 373 | std::string::size_type dot = path.rfind('.'); |
Jeff Cohen | 73f3667 | 2005-07-09 18:42:02 +0000 | [diff] [blame] | 374 | if (dot == std::string::npos || dot < slash) |
Jeffrey Yasskin | 88cd358 | 2009-12-17 21:02:39 +0000 | [diff] [blame] | 375 | return StringRef(path).substr(slash); |
Jeff Cohen | 73f3667 | 2005-07-09 18:42:02 +0000 | [diff] [blame] | 376 | else |
Jeffrey Yasskin | 88cd358 | 2009-12-17 21:02:39 +0000 | [diff] [blame] | 377 | return StringRef(path).substr(slash, dot - slash); |
Reid Spencer | 1b554b4 | 2004-09-11 04:55:08 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Jeffrey Yasskin | 88cd358 | 2009-12-17 21:02:39 +0000 | [diff] [blame] | 380 | StringRef |
Argyrios Kyrtzidis | fc19988 | 2008-06-15 15:15:19 +0000 | [diff] [blame] | 381 | Path::getSuffix() const { |
| 382 | // Find the last slash |
| 383 | std::string::size_type slash = path.rfind('/'); |
| 384 | if (slash == std::string::npos) |
| 385 | slash = 0; |
| 386 | else |
| 387 | slash++; |
| 388 | |
| 389 | std::string::size_type dot = path.rfind('.'); |
| 390 | if (dot == std::string::npos || dot < slash) |
Dan Gohman | d98af0a | 2010-08-04 01:39:08 +0000 | [diff] [blame] | 391 | return StringRef(); |
Argyrios Kyrtzidis | fc19988 | 2008-06-15 15:15:19 +0000 | [diff] [blame] | 392 | else |
Jeffrey Yasskin | 88cd358 | 2009-12-17 21:02:39 +0000 | [diff] [blame] | 393 | return StringRef(path).substr(dot + 1); |
Argyrios Kyrtzidis | fc19988 | 2008-06-15 15:15:19 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Chris Lattner | e4b0cd2 | 2009-12-16 08:35:54 +0000 | [diff] [blame] | 396 | bool Path::getMagicNumber(std::string &Magic, unsigned len) const { |
Reid Spencer | be31d2a | 2004-11-16 17:14:08 +0000 | [diff] [blame] | 397 | assert(len < 1024 && "Request for magic string too long"); |
Chris Lattner | e4b0cd2 | 2009-12-16 08:35:54 +0000 | [diff] [blame] | 398 | char Buf[1025]; |
Chris Lattner | c7c453a | 2006-08-01 17:51:09 +0000 | [diff] [blame] | 399 | int fd = ::open(path.c_str(), O_RDONLY); |
Reid Spencer | be31d2a | 2004-11-16 17:14:08 +0000 | [diff] [blame] | 400 | if (fd < 0) |
| 401 | return false; |
Chris Lattner | e4b0cd2 | 2009-12-16 08:35:54 +0000 | [diff] [blame] | 402 | ssize_t bytes_read = ::read(fd, Buf, len); |
Reid Spencer | 86ac2dc | 2004-12-02 09:09:48 +0000 | [diff] [blame] | 403 | ::close(fd); |
Dan Gohman | 02d5824 | 2010-05-27 17:14:10 +0000 | [diff] [blame] | 404 | if (ssize_t(len) != bytes_read) |
Reid Spencer | be31d2a | 2004-11-16 17:14:08 +0000 | [diff] [blame] | 405 | return false; |
Chris Lattner | e4b0cd2 | 2009-12-16 08:35:54 +0000 | [diff] [blame] | 406 | Magic.assign(Buf, len); |
Reid Spencer | eaf1815 | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 407 | return true; |
| 408 | } |
| 409 | |
Reid Spencer | 1b554b4 | 2004-09-11 04:55:08 +0000 | [diff] [blame] | 410 | bool |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 411 | Path::exists() const { |
| 412 | return 0 == access(path.c_str(), F_OK ); |
| 413 | } |
| 414 | |
| 415 | bool |
Ted Kremenek | fd52711 | 2007-12-18 19:46:22 +0000 | [diff] [blame] | 416 | Path::isDirectory() const { |
| 417 | struct stat buf; |
| 418 | if (0 != stat(path.c_str(), &buf)) |
| 419 | return false; |
Evan Cheng | a98111c | 2010-10-07 22:05:57 +0000 | [diff] [blame] | 420 | return ((buf.st_mode & S_IFMT) == S_IFDIR) ? true : false; |
Ted Kremenek | fd52711 | 2007-12-18 19:46:22 +0000 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | bool |
Rafael Espindola | 9e07a14 | 2010-11-07 04:36:50 +0000 | [diff] [blame] | 424 | Path::isSymLink() const { |
| 425 | struct stat buf; |
| 426 | if (0 != lstat(path.c_str(), &buf)) |
| 427 | return false; |
| 428 | return S_ISLNK(buf.st_mode); |
| 429 | } |
| 430 | |
| 431 | |
| 432 | bool |
Reid Spencer | c7f0832 | 2005-07-07 18:21:42 +0000 | [diff] [blame] | 433 | Path::canRead() const { |
Dan Gohman | 73c74ea | 2009-07-28 23:22:01 +0000 | [diff] [blame] | 434 | return 0 == access(path.c_str(), R_OK); |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | bool |
Reid Spencer | c7f0832 | 2005-07-07 18:21:42 +0000 | [diff] [blame] | 438 | Path::canWrite() const { |
Dan Gohman | 73c74ea | 2009-07-28 23:22:01 +0000 | [diff] [blame] | 439 | return 0 == access(path.c_str(), W_OK); |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | bool |
Edward O'Callaghan | e49a8e4 | 2009-11-25 06:32:19 +0000 | [diff] [blame] | 443 | Path::isRegularFile() const { |
Dan Gohman | 4bb31bf | 2010-03-30 20:04:57 +0000 | [diff] [blame] | 444 | // Get the status so we can determine if it's a file or directory |
Edward O'Callaghan | d41e944 | 2009-11-24 15:19:10 +0000 | [diff] [blame] | 445 | struct stat buf; |
Edward O'Callaghan | d41e944 | 2009-11-24 15:19:10 +0000 | [diff] [blame] | 446 | |
Daniel Dunbar | 98e46d8 | 2009-11-24 19:03:33 +0000 | [diff] [blame] | 447 | if (0 != stat(path.c_str(), &buf)) |
Edward O'Callaghan | d41e944 | 2009-11-24 15:19:10 +0000 | [diff] [blame] | 448 | return false; |
Edward O'Callaghan | d41e944 | 2009-11-24 15:19:10 +0000 | [diff] [blame] | 449 | |
Edward O'Callaghan | e49a8e4 | 2009-11-25 06:32:19 +0000 | [diff] [blame] | 450 | if (S_ISREG(buf.st_mode)) |
| 451 | return true; |
| 452 | |
| 453 | return false; |
Edward O'Callaghan | d41e944 | 2009-11-24 15:19:10 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | bool |
Reid Spencer | c7f0832 | 2005-07-07 18:21:42 +0000 | [diff] [blame] | 457 | Path::canExecute() const { |
Reid Spencer | 8b2d1aa | 2005-07-08 17:46:10 +0000 | [diff] [blame] | 458 | if (0 != access(path.c_str(), R_OK | X_OK )) |
| 459 | return false; |
Reid Spencer | 2ae9d11 | 2007-04-07 18:52:17 +0000 | [diff] [blame] | 460 | struct stat buf; |
| 461 | if (0 != stat(path.c_str(), &buf)) |
| 462 | return false; |
| 463 | if (!S_ISREG(buf.st_mode)) |
Misha Brukman | 8177bf8 | 2005-04-20 15:33:22 +0000 | [diff] [blame] | 464 | return false; |
Reid Spencer | 8b2d1aa | 2005-07-08 17:46:10 +0000 | [diff] [blame] | 465 | return true; |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Jeffrey Yasskin | 88cd358 | 2009-12-17 21:02:39 +0000 | [diff] [blame] | 468 | StringRef |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 469 | Path::getLast() const { |
| 470 | // Find the last slash |
| 471 | size_t pos = path.rfind('/'); |
| 472 | |
| 473 | // Handle the corner cases |
| 474 | if (pos == std::string::npos) |
| 475 | return path; |
| 476 | |
| 477 | // If the last character is a slash |
| 478 | if (pos == path.length()-1) { |
| 479 | // Find the second to last slash |
| 480 | size_t pos2 = path.rfind('/', pos-1); |
| 481 | if (pos2 == std::string::npos) |
Jeffrey Yasskin | 88cd358 | 2009-12-17 21:02:39 +0000 | [diff] [blame] | 482 | return StringRef(path).substr(0,pos); |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 483 | else |
Jeffrey Yasskin | 88cd358 | 2009-12-17 21:02:39 +0000 | [diff] [blame] | 484 | return StringRef(path).substr(pos2+1,pos-pos2-1); |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 485 | } |
| 486 | // Return everything after the last slash |
Jeffrey Yasskin | 88cd358 | 2009-12-17 21:02:39 +0000 | [diff] [blame] | 487 | return StringRef(path).substr(pos+1); |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 488 | } |
| 489 | |
Reid Spencer | 2ae9d11 | 2007-04-07 18:52:17 +0000 | [diff] [blame] | 490 | const FileStatus * |
| 491 | PathWithStatus::getFileStatus(bool update, std::string *ErrStr) const { |
| 492 | if (!fsIsValid || update) { |
Reid Spencer | 69cce81 | 2007-03-29 16:43:20 +0000 | [diff] [blame] | 493 | struct stat buf; |
Reid Spencer | 8475ec0 | 2007-03-29 19:05:44 +0000 | [diff] [blame] | 494 | if (0 != stat(path.c_str(), &buf)) { |
| 495 | MakeErrMsg(ErrStr, path + ": can't get status of file"); |
| 496 | return 0; |
| 497 | } |
Reid Spencer | 2ae9d11 | 2007-04-07 18:52:17 +0000 | [diff] [blame] | 498 | status.fileSize = buf.st_size; |
| 499 | status.modTime.fromEpochTime(buf.st_mtime); |
| 500 | status.mode = buf.st_mode; |
| 501 | status.user = buf.st_uid; |
| 502 | status.group = buf.st_gid; |
| 503 | status.uniqueID = uint64_t(buf.st_ino); |
| 504 | status.isDir = S_ISDIR(buf.st_mode); |
| 505 | status.isFile = S_ISREG(buf.st_mode); |
| 506 | fsIsValid = true; |
Reid Spencer | 69cce81 | 2007-03-29 16:43:20 +0000 | [diff] [blame] | 507 | } |
Reid Spencer | 2ae9d11 | 2007-04-07 18:52:17 +0000 | [diff] [blame] | 508 | return &status; |
Reid Spencer | eaf1815 | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 509 | } |
| 510 | |
Chris Lattner | 252ad03 | 2006-07-28 22:03:44 +0000 | [diff] [blame] | 511 | static bool AddPermissionBits(const Path &File, int bits) { |
Reid Spencer | 77cc91d | 2004-12-13 19:59:50 +0000 | [diff] [blame] | 512 | // Get the umask value from the operating system. We want to use it |
| 513 | // when changing the file's permissions. Since calling umask() sets |
| 514 | // the umask and returns its old value, we must call it a second |
| 515 | // time to reset it to the user's preference. |
| 516 | int mask = umask(0777); // The arg. to umask is arbitrary. |
| 517 | umask(mask); // Restore the umask. |
| 518 | |
| 519 | // Get the file's current mode. |
Reid Spencer | 2ae9d11 | 2007-04-07 18:52:17 +0000 | [diff] [blame] | 520 | struct stat buf; |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 521 | if (0 != stat(File.c_str(), &buf)) |
Reid Spencer | 77cc91d | 2004-12-13 19:59:50 +0000 | [diff] [blame] | 522 | return false; |
Reid Spencer | 2ae9d11 | 2007-04-07 18:52:17 +0000 | [diff] [blame] | 523 | // Change the file to have whichever permissions bits from 'bits' |
| 524 | // that the umask would not disable. |
| 525 | if ((chmod(File.c_str(), (buf.st_mode | (bits & ~mask)))) == -1) |
| 526 | return false; |
Reid Spencer | 77cc91d | 2004-12-13 19:59:50 +0000 | [diff] [blame] | 527 | return true; |
| 528 | } |
| 529 | |
Reid Spencer | e1647f4 | 2006-08-22 23:27:23 +0000 | [diff] [blame] | 530 | bool Path::makeReadableOnDisk(std::string* ErrMsg) { |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 531 | if (!AddPermissionBits(*this, 0444)) |
Reid Spencer | 5a06077 | 2006-08-23 07:30:48 +0000 | [diff] [blame] | 532 | return MakeErrMsg(ErrMsg, path + ": can't make file readable"); |
Reid Spencer | e1647f4 | 2006-08-22 23:27:23 +0000 | [diff] [blame] | 533 | return false; |
Reid Spencer | 77cc91d | 2004-12-13 19:59:50 +0000 | [diff] [blame] | 534 | } |
| 535 | |
Reid Spencer | e1647f4 | 2006-08-22 23:27:23 +0000 | [diff] [blame] | 536 | bool Path::makeWriteableOnDisk(std::string* ErrMsg) { |
Reid Spencer | 5a06077 | 2006-08-23 07:30:48 +0000 | [diff] [blame] | 537 | if (!AddPermissionBits(*this, 0222)) |
| 538 | return MakeErrMsg(ErrMsg, path + ": can't make file writable"); |
Reid Spencer | e1647f4 | 2006-08-22 23:27:23 +0000 | [diff] [blame] | 539 | return false; |
Reid Spencer | 77cc91d | 2004-12-13 19:59:50 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Reid Spencer | e1647f4 | 2006-08-22 23:27:23 +0000 | [diff] [blame] | 542 | bool Path::makeExecutableOnDisk(std::string* ErrMsg) { |
Reid Spencer | 5a06077 | 2006-08-23 07:30:48 +0000 | [diff] [blame] | 543 | if (!AddPermissionBits(*this, 0111)) |
| 544 | return MakeErrMsg(ErrMsg, path + ": can't make file executable"); |
Reid Spencer | e1647f4 | 2006-08-22 23:27:23 +0000 | [diff] [blame] | 545 | return false; |
Reid Spencer | 77cc91d | 2004-12-13 19:59:50 +0000 | [diff] [blame] | 546 | } |
| 547 | |
Reid Spencer | eaf1815 | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 548 | bool |
Reid Spencer | 142ca8e | 2006-08-23 06:56:27 +0000 | [diff] [blame] | 549 | Path::getDirectoryContents(std::set<Path>& result, std::string* ErrMsg) const { |
Reid Spencer | eaf1815 | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 550 | DIR* direntries = ::opendir(path.c_str()); |
Reid Spencer | 5a06077 | 2006-08-23 07:30:48 +0000 | [diff] [blame] | 551 | if (direntries == 0) |
| 552 | return MakeErrMsg(ErrMsg, path + ": can't open directory"); |
Reid Spencer | eaf1815 | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 553 | |
Reid Spencer | 3be872e | 2005-07-28 16:25:57 +0000 | [diff] [blame] | 554 | std::string dirPath = path; |
| 555 | if (!lastIsSlash(dirPath)) |
| 556 | dirPath += '/'; |
| 557 | |
Reid Spencer | eaf1815 | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 558 | result.clear(); |
| 559 | struct dirent* de = ::readdir(direntries); |
Misha Brukman | 4619c75 | 2005-04-20 04:04:07 +0000 | [diff] [blame] | 560 | for ( ; de != 0; de = ::readdir(direntries)) { |
Reid Spencer | eaf1815 | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 561 | if (de->d_name[0] != '.') { |
Reid Spencer | 3be872e | 2005-07-28 16:25:57 +0000 | [diff] [blame] | 562 | Path aPath(dirPath + (const char*)de->d_name); |
Chris Lattner | b14c342 | 2006-07-07 21:21:06 +0000 | [diff] [blame] | 563 | struct stat st; |
| 564 | if (0 != lstat(aPath.path.c_str(), &st)) { |
| 565 | if (S_ISLNK(st.st_mode)) |
Misha Brukman | 4619c75 | 2005-04-20 04:04:07 +0000 | [diff] [blame] | 566 | continue; // dangling symlink -- ignore |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 567 | return MakeErrMsg(ErrMsg, |
Reid Spencer | 5a06077 | 2006-08-23 07:30:48 +0000 | [diff] [blame] | 568 | aPath.path + ": can't determine file object type"); |
Misha Brukman | 4619c75 | 2005-04-20 04:04:07 +0000 | [diff] [blame] | 569 | } |
Reid Spencer | b608a81 | 2004-11-16 06:15:19 +0000 | [diff] [blame] | 570 | result.insert(aPath); |
Reid Spencer | eaf1815 | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 571 | } |
Reid Spencer | eaf1815 | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 572 | } |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 573 | |
Reid Spencer | eaf1815 | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 574 | closedir(direntries); |
Reid Spencer | 142ca8e | 2006-08-23 06:56:27 +0000 | [diff] [blame] | 575 | return false; |
Reid Spencer | 9195f37 | 2004-11-09 20:26:31 +0000 | [diff] [blame] | 576 | } |
| 577 | |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 578 | bool |
Jeffrey Yasskin | 88cd358 | 2009-12-17 21:02:39 +0000 | [diff] [blame] | 579 | Path::set(StringRef a_path) { |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 580 | if (a_path.empty()) |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 581 | return false; |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 582 | path = a_path; |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 583 | return true; |
| 584 | } |
| 585 | |
| 586 | bool |
Jeffrey Yasskin | 88cd358 | 2009-12-17 21:02:39 +0000 | [diff] [blame] | 587 | Path::appendComponent(StringRef name) { |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 588 | if (name.empty()) |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 589 | return false; |
Reid Spencer | 3be872e | 2005-07-28 16:25:57 +0000 | [diff] [blame] | 590 | if (!lastIsSlash(path)) |
| 591 | path += '/'; |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 592 | path += name; |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 593 | return true; |
| 594 | } |
| 595 | |
| 596 | bool |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 597 | Path::eraseComponent() { |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 598 | size_t slashpos = path.rfind('/',path.size()); |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 599 | if (slashpos == 0 || slashpos == std::string::npos) { |
| 600 | path.erase(); |
| 601 | return true; |
| 602 | } |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 603 | if (slashpos == path.size() - 1) |
| 604 | slashpos = path.rfind('/',slashpos-1); |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 605 | if (slashpos == std::string::npos) { |
| 606 | path.erase(); |
| 607 | return true; |
| 608 | } |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 609 | path.erase(slashpos); |
| 610 | return true; |
| 611 | } |
| 612 | |
| 613 | bool |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 614 | Path::eraseSuffix() { |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 615 | size_t dotpos = path.rfind('.',path.size()); |
| 616 | size_t slashpos = path.rfind('/',path.size()); |
Jeff Cohen | 563a17f | 2005-07-08 04:49:16 +0000 | [diff] [blame] | 617 | if (dotpos != std::string::npos) { |
Jeff Cohen | 73f3667 | 2005-07-09 18:42:02 +0000 | [diff] [blame] | 618 | if (slashpos == std::string::npos || dotpos > slashpos+1) { |
Jeff Cohen | 563a17f | 2005-07-08 04:49:16 +0000 | [diff] [blame] | 619 | path.erase(dotpos, path.size()-dotpos); |
Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 620 | return true; |
Jeff Cohen | 563a17f | 2005-07-08 04:49:16 +0000 | [diff] [blame] | 621 | } |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 622 | } |
Jeff Cohen | 563a17f | 2005-07-08 04:49:16 +0000 | [diff] [blame] | 623 | return false; |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 624 | } |
| 625 | |
Ted Kremenek | c5412c5 | 2008-04-03 16:11:31 +0000 | [diff] [blame] | 626 | static bool createDirectoryHelper(char* beg, char* end, bool create_parents) { |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 627 | |
Dan Gohman | 28daa10 | 2009-07-29 00:02:58 +0000 | [diff] [blame] | 628 | if (access(beg, R_OK | W_OK) == 0) |
Ted Kremenek | c5412c5 | 2008-04-03 16:11:31 +0000 | [diff] [blame] | 629 | return false; |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 630 | |
Ted Kremenek | c5412c5 | 2008-04-03 16:11:31 +0000 | [diff] [blame] | 631 | if (create_parents) { |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 632 | |
Ted Kremenek | c5412c5 | 2008-04-03 16:11:31 +0000 | [diff] [blame] | 633 | char* c = end; |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 634 | |
Ted Kremenek | c5412c5 | 2008-04-03 16:11:31 +0000 | [diff] [blame] | 635 | for (; c != beg; --c) |
| 636 | if (*c == '/') { |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 637 | |
Ted Kremenek | c5412c5 | 2008-04-03 16:11:31 +0000 | [diff] [blame] | 638 | // Recurse to handling the parent directory. |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 639 | *c = '\0'; |
Ted Kremenek | c5412c5 | 2008-04-03 16:11:31 +0000 | [diff] [blame] | 640 | bool x = createDirectoryHelper(beg, c, create_parents); |
| 641 | *c = '/'; |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 642 | |
Ted Kremenek | c5412c5 | 2008-04-03 16:11:31 +0000 | [diff] [blame] | 643 | // Return if we encountered an error. |
| 644 | if (x) |
| 645 | return true; |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 646 | |
Ted Kremenek | c5412c5 | 2008-04-03 16:11:31 +0000 | [diff] [blame] | 647 | break; |
| 648 | } |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 649 | } |
| 650 | |
Ted Kremenek | c5412c5 | 2008-04-03 16:11:31 +0000 | [diff] [blame] | 651 | return mkdir(beg, S_IRWXU | S_IRWXG) != 0; |
| 652 | } |
| 653 | |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 654 | bool |
Reid Spencer | e5c9cb5 | 2006-08-23 00:39:35 +0000 | [diff] [blame] | 655 | Path::createDirectoryOnDisk( bool create_parents, std::string* ErrMsg ) { |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 656 | // Get a writeable copy of the path name |
Dan Gohman | b48a8fd | 2010-11-02 22:55:34 +0000 | [diff] [blame] | 657 | std::string pathname(path); |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 658 | |
| 659 | // Null-terminate the last component |
Evan Cheng | 34cd4a4 | 2008-05-05 18:30:58 +0000 | [diff] [blame] | 660 | size_t lastchar = path.length() - 1 ; |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 661 | |
Ted Kremenek | c5412c5 | 2008-04-03 16:11:31 +0000 | [diff] [blame] | 662 | if (pathname[lastchar] != '/') |
| 663 | ++lastchar; |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 664 | |
Dan Gohman | a0f1a2b | 2010-11-02 22:41:19 +0000 | [diff] [blame] | 665 | pathname[lastchar] = '\0'; |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 666 | |
Dan Gohman | b48a8fd | 2010-11-02 22:55:34 +0000 | [diff] [blame] | 667 | if (createDirectoryHelper(&pathname[0], &pathname[lastchar], create_parents)) |
Dan Gohman | 9e2e0c3 | 2010-11-02 23:16:26 +0000 | [diff] [blame] | 668 | return MakeErrMsg(ErrMsg, pathname + ": can't create directory"); |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 669 | |
Reid Spencer | e5c9cb5 | 2006-08-23 00:39:35 +0000 | [diff] [blame] | 670 | return false; |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | bool |
Reid Spencer | e5c9cb5 | 2006-08-23 00:39:35 +0000 | [diff] [blame] | 674 | Path::createFileOnDisk(std::string* ErrMsg) { |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 675 | // Create the file |
Reid Spencer | 622e220 | 2004-09-18 19:25:11 +0000 | [diff] [blame] | 676 | int fd = ::creat(path.c_str(), S_IRUSR | S_IWUSR); |
Reid Spencer | 5a06077 | 2006-08-23 07:30:48 +0000 | [diff] [blame] | 677 | if (fd < 0) |
| 678 | return MakeErrMsg(ErrMsg, path + ": can't create file"); |
Reid Spencer | 622e220 | 2004-09-18 19:25:11 +0000 | [diff] [blame] | 679 | ::close(fd); |
Reid Spencer | e5c9cb5 | 2006-08-23 00:39:35 +0000 | [diff] [blame] | 680 | return false; |
Reid Spencer | b89a223 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 683 | bool |
Reid Spencer | e5c9cb5 | 2006-08-23 00:39:35 +0000 | [diff] [blame] | 684 | Path::createTemporaryFileOnDisk(bool reuse_current, std::string* ErrMsg) { |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 685 | // Make this into a unique file name |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 686 | if (makeUnique( reuse_current, ErrMsg )) |
| 687 | return true; |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 688 | |
| 689 | // create the file |
Reid Spencer | e5c9cb5 | 2006-08-23 00:39:35 +0000 | [diff] [blame] | 690 | int fd = ::open(path.c_str(), O_WRONLY|O_CREAT|O_TRUNC, 0666); |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 691 | if (fd < 0) |
Reid Spencer | 5a06077 | 2006-08-23 07:30:48 +0000 | [diff] [blame] | 692 | return MakeErrMsg(ErrMsg, path + ": can't create temporary file"); |
Reid Spencer | e5c9cb5 | 2006-08-23 00:39:35 +0000 | [diff] [blame] | 693 | ::close(fd); |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 694 | return false; |
Reid Spencer | 9195f37 | 2004-11-09 20:26:31 +0000 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | bool |
Chris Lattner | 0c33231 | 2006-07-28 22:29:50 +0000 | [diff] [blame] | 698 | Path::eraseFromDisk(bool remove_contents, std::string *ErrStr) const { |
Dan Gohman | 4bb31bf | 2010-03-30 20:04:57 +0000 | [diff] [blame] | 699 | // Get the status so we can determine if it's a file or directory. |
Reid Spencer | 2ae9d11 | 2007-04-07 18:52:17 +0000 | [diff] [blame] | 700 | struct stat buf; |
| 701 | if (0 != stat(path.c_str(), &buf)) { |
| 702 | MakeErrMsg(ErrStr, path + ": can't get status of file"); |
Chris Lattner | 0c33231 | 2006-07-28 22:29:50 +0000 | [diff] [blame] | 703 | return true; |
Reid Spencer | 2ae9d11 | 2007-04-07 18:52:17 +0000 | [diff] [blame] | 704 | } |
| 705 | |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 706 | // Note: this check catches strange situations. In all cases, LLVM should |
| 707 | // only be involved in the creation and deletion of regular files. This |
| 708 | // check ensures that what we're trying to erase is a regular file. It |
| 709 | // effectively prevents LLVM from erasing things like /dev/null, any block |
| 710 | // special file, or other things that aren't "regular" files. |
Reid Spencer | 2ae9d11 | 2007-04-07 18:52:17 +0000 | [diff] [blame] | 711 | if (S_ISREG(buf.st_mode)) { |
| 712 | if (unlink(path.c_str()) != 0) |
| 713 | return MakeErrMsg(ErrStr, path + ": can't destroy file"); |
| 714 | return false; |
| 715 | } |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 716 | |
Reid Spencer | 2ae9d11 | 2007-04-07 18:52:17 +0000 | [diff] [blame] | 717 | if (!S_ISDIR(buf.st_mode)) { |
| 718 | if (ErrStr) *ErrStr = "not a file or directory"; |
| 719 | return true; |
| 720 | } |
Reid Spencer | 8475ec0 | 2007-03-29 19:05:44 +0000 | [diff] [blame] | 721 | |
Chris Lattner | 0c33231 | 2006-07-28 22:29:50 +0000 | [diff] [blame] | 722 | if (remove_contents) { |
| 723 | // Recursively descend the directory to remove its contents. |
| 724 | std::string cmd = "/bin/rm -rf " + path; |
Mikhail Glushenkov | 8eada62 | 2009-02-15 03:20:32 +0000 | [diff] [blame] | 725 | if (system(cmd.c_str()) != 0) { |
| 726 | MakeErrMsg(ErrStr, path + ": failed to recursively remove directory."); |
| 727 | return true; |
| 728 | } |
Chris Lattner | 0c33231 | 2006-07-28 22:29:50 +0000 | [diff] [blame] | 729 | return false; |
| 730 | } |
| 731 | |
| 732 | // Otherwise, try to just remove the one directory. |
Dan Gohman | b48a8fd | 2010-11-02 22:55:34 +0000 | [diff] [blame] | 733 | std::string pathname(path); |
Evan Cheng | 34cd4a4 | 2008-05-05 18:30:58 +0000 | [diff] [blame] | 734 | size_t lastchar = path.length() - 1; |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 735 | if (pathname[lastchar] == '/') |
Dan Gohman | a0f1a2b | 2010-11-02 22:41:19 +0000 | [diff] [blame] | 736 | pathname[lastchar] = '\0'; |
Chris Lattner | 0c33231 | 2006-07-28 22:29:50 +0000 | [diff] [blame] | 737 | else |
Dan Gohman | a0f1a2b | 2010-11-02 22:41:19 +0000 | [diff] [blame] | 738 | pathname[lastchar+1] = '\0'; |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 739 | |
Dan Gohman | b48a8fd | 2010-11-02 22:55:34 +0000 | [diff] [blame] | 740 | if (rmdir(pathname.c_str()) != 0) |
| 741 | return MakeErrMsg(ErrStr, pathname + ": can't erase directory"); |
Chris Lattner | 0c33231 | 2006-07-28 22:29:50 +0000 | [diff] [blame] | 742 | return false; |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | bool |
Reid Spencer | 5a06077 | 2006-08-23 07:30:48 +0000 | [diff] [blame] | 746 | Path::renamePathOnDisk(const Path& newName, std::string* ErrMsg) { |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 747 | if (0 != ::rename(path.c_str(), newName.c_str())) |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 748 | return MakeErrMsg(ErrMsg, std::string("can't rename '") + path + "' as '" + |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 749 | newName.str() + "'"); |
Reid Spencer | 5a06077 | 2006-08-23 07:30:48 +0000 | [diff] [blame] | 750 | return false; |
Reid Spencer | eaf1815 | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 751 | } |
| 752 | |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 753 | bool |
Chris Lattner | 1bebfb5 | 2006-07-28 22:36:17 +0000 | [diff] [blame] | 754 | Path::setStatusInfoOnDisk(const FileStatus &si, std::string *ErrStr) const { |
Reid Spencer | eaf1815 | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 755 | struct utimbuf utb; |
| 756 | utb.actime = si.modTime.toPosixTime(); |
| 757 | utb.modtime = utb.actime; |
| 758 | if (0 != ::utime(path.c_str(),&utb)) |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 759 | return MakeErrMsg(ErrStr, path + ": can't set file modification time"); |
Reid Spencer | eaf1815 | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 760 | if (0 != ::chmod(path.c_str(),si.mode)) |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 761 | return MakeErrMsg(ErrStr, path + ": can't set mode"); |
Chris Lattner | 1bebfb5 | 2006-07-28 22:36:17 +0000 | [diff] [blame] | 762 | return false; |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 763 | } |
| 764 | |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 765 | bool |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 766 | sys::CopyFile(const sys::Path &Dest, const sys::Path &Src, std::string* ErrMsg){ |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 767 | int inFile = -1; |
| 768 | int outFile = -1; |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 769 | inFile = ::open(Src.c_str(), O_RDONLY); |
| 770 | if (inFile == -1) |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 771 | return MakeErrMsg(ErrMsg, Src.str() + |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 772 | ": can't open source file to copy"); |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 773 | |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 774 | outFile = ::open(Dest.c_str(), O_WRONLY|O_CREAT, 0666); |
| 775 | if (outFile == -1) { |
| 776 | ::close(inFile); |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 777 | return MakeErrMsg(ErrMsg, Dest.str() + |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 778 | ": can't create destination file for copy"); |
| 779 | } |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 780 | |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 781 | char Buffer[16*1024]; |
| 782 | while (ssize_t Amt = ::read(inFile, Buffer, 16*1024)) { |
| 783 | if (Amt == -1) { |
| 784 | if (errno != EINTR && errno != EAGAIN) { |
| 785 | ::close(inFile); |
| 786 | ::close(outFile); |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 787 | return MakeErrMsg(ErrMsg, Src.str()+": can't read source file"); |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 788 | } |
| 789 | } else { |
| 790 | char *BufPtr = Buffer; |
| 791 | while (Amt) { |
| 792 | ssize_t AmtWritten = ::write(outFile, BufPtr, Amt); |
| 793 | if (AmtWritten == -1) { |
| 794 | if (errno != EINTR && errno != EAGAIN) { |
| 795 | ::close(inFile); |
| 796 | ::close(outFile); |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 797 | return MakeErrMsg(ErrMsg, Dest.str() + |
Daniel Dunbar | a7f2a9e | 2009-04-20 20:50:13 +0000 | [diff] [blame] | 798 | ": can't write destination file"); |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 799 | } |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 800 | } else { |
| 801 | Amt -= AmtWritten; |
| 802 | BufPtr += AmtWritten; |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 803 | } |
| 804 | } |
| 805 | } |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 806 | } |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 807 | ::close(inFile); |
| 808 | ::close(outFile); |
| 809 | return false; |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 810 | } |
| 811 | |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 812 | bool |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 813 | Path::makeUnique(bool reuse_current, std::string* ErrMsg) { |
Michael J. Spencer | 54453f2 | 2011-01-10 02:34:23 +0000 | [diff] [blame] | 814 | bool Exists; |
| 815 | if (reuse_current && (fs::exists(path, Exists) || !Exists)) |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 816 | return false; // File doesn't exist already, just use it! |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 817 | |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame] | 818 | // Append an XXXXXX pattern to the end of the file for use with mkstemp, |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 819 | // mktemp or our own implementation. |
Dan Gohman | 72bdd4c | 2010-04-19 15:54:44 +0000 | [diff] [blame] | 820 | // This uses std::vector instead of SmallVector to avoid a dependence on |
| 821 | // libSupport. And performance isn't critical here. |
| 822 | std::vector<char> Buf; |
| 823 | Buf.resize(path.size()+8); |
Dan Gohman | 17192a1 | 2010-04-19 16:33:28 +0000 | [diff] [blame] | 824 | char *FNBuffer = &Buf[0]; |
Dan Gohman | 72bdd4c | 2010-04-19 15:54:44 +0000 | [diff] [blame] | 825 | path.copy(FNBuffer,path.size()); |
Michael J. Spencer | 218dc3e | 2011-01-11 01:21:55 +0000 | [diff] [blame^] | 826 | bool isdir; |
| 827 | if (!fs::is_directory(path, isdir) && isdir) |
Dan Gohman | 72bdd4c | 2010-04-19 15:54:44 +0000 | [diff] [blame] | 828 | strcpy(FNBuffer+path.size(), "/XXXXXX"); |
Devang Patel | bdfa9ac | 2008-07-24 00:35:38 +0000 | [diff] [blame] | 829 | else |
Dan Gohman | 72bdd4c | 2010-04-19 15:54:44 +0000 | [diff] [blame] | 830 | strcpy(FNBuffer+path.size(), "-XXXXXX"); |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 831 | |
| 832 | #if defined(HAVE_MKSTEMP) |
| 833 | int TempFD; |
Dan Gohman | 72bdd4c | 2010-04-19 15:54:44 +0000 | [diff] [blame] | 834 | if ((TempFD = mkstemp(FNBuffer)) == -1) |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 835 | return MakeErrMsg(ErrMsg, path + ": can't make unique filename"); |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 836 | |
| 837 | // We don't need to hold the temp file descriptor... we will trust that no one |
| 838 | // will overwrite/delete the file before we can open it again. |
| 839 | close(TempFD); |
| 840 | |
| 841 | // Save the name |
Dan Gohman | 72bdd4c | 2010-04-19 15:54:44 +0000 | [diff] [blame] | 842 | path = FNBuffer; |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 843 | #elif defined(HAVE_MKTEMP) |
| 844 | // If we don't have mkstemp, use the old and obsolete mktemp function. |
Dan Gohman | 72bdd4c | 2010-04-19 15:54:44 +0000 | [diff] [blame] | 845 | if (mktemp(FNBuffer) == 0) |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 846 | return MakeErrMsg(ErrMsg, path + ": can't make unique filename"); |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 847 | |
| 848 | // Save the name |
Dan Gohman | 72bdd4c | 2010-04-19 15:54:44 +0000 | [diff] [blame] | 849 | path = FNBuffer; |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 850 | #else |
| 851 | // Okay, looks like we have to do it all by our lonesome. |
| 852 | static unsigned FCounter = 0; |
Chris Lattner | 1f01109 | 2010-07-12 00:09:55 +0000 | [diff] [blame] | 853 | // Try to initialize with unique value. |
| 854 | if (FCounter == 0) FCounter = ((unsigned)getpid() & 0xFFFF) << 8; |
| 855 | char* pos = strstr(FNBuffer, "XXXXXX"); |
| 856 | do { |
| 857 | if (++FCounter > 0xFFFFFF) { |
| 858 | return MakeErrMsg(ErrMsg, |
| 859 | path + ": can't make unique filename: too many files"); |
| 860 | } |
| 861 | sprintf(pos, "%06X", FCounter); |
Dan Gohman | 72bdd4c | 2010-04-19 15:54:44 +0000 | [diff] [blame] | 862 | path = FNBuffer; |
Chris Lattner | 1f01109 | 2010-07-12 00:09:55 +0000 | [diff] [blame] | 863 | } while (exists()); |
| 864 | // POSSIBLE SECURITY BUG: An attacker can easily guess the name and exploit |
| 865 | // LLVM. |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 866 | #endif |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 867 | return false; |
| 868 | } |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 869 | |
Chris Lattner | 799ed10 | 2008-04-01 06:00:12 +0000 | [diff] [blame] | 870 | const char *Path::MapInFilePages(int FD, uint64_t FileSize) { |
Chris Lattner | 9ffd19a | 2008-04-01 06:16:24 +0000 | [diff] [blame] | 871 | int Flags = MAP_PRIVATE; |
| 872 | #ifdef MAP_FILE |
| 873 | Flags |= MAP_FILE; |
| 874 | #endif |
| 875 | void *BasePtr = ::mmap(0, FileSize, PROT_READ, Flags, FD, 0); |
| 876 | if (BasePtr == MAP_FAILED) |
| 877 | return 0; |
Chris Lattner | 14c762d | 2008-04-01 06:25:23 +0000 | [diff] [blame] | 878 | return (const char*)BasePtr; |
Chris Lattner | 799ed10 | 2008-04-01 06:00:12 +0000 | [diff] [blame] | 879 | } |
| 880 | |
Chris Lattner | 9ffd19a | 2008-04-01 06:16:24 +0000 | [diff] [blame] | 881 | void Path::UnMapFilePages(const char *BasePtr, uint64_t FileSize) { |
Chris Lattner | 14c762d | 2008-04-01 06:25:23 +0000 | [diff] [blame] | 882 | ::munmap((void*)BasePtr, FileSize); |
Chris Lattner | 799ed10 | 2008-04-01 06:00:12 +0000 | [diff] [blame] | 883 | } |
| 884 | |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 885 | } // end llvm namespace |