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