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