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