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