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