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