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 | |
Misha Brukman | 210b32b | 2004-12-20 00:16:38 +0000 | [diff] [blame] | 19 | #include "llvm/Config/alloca.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 | |
Reid Spencer | 932e2e3 | 2005-06-02 05:38:20 +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 |
Reid Spencer | b89a223 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 65 | |
Reid Spencer | 3be872e | 2005-07-28 16:25:57 +0000 | [diff] [blame] | 66 | namespace { |
| 67 | inline bool lastIsSlash(const std::string& path) { |
| 68 | return !path.empty() && path[path.length() - 1] == '/'; |
| 69 | } |
| 70 | |
| 71 | } |
| 72 | |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 73 | namespace llvm { |
| 74 | using namespace sys; |
Reid Spencer | b89a223 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 75 | |
Chris Lattner | e1b332a | 2008-02-27 06:17:10 +0000 | [diff] [blame] | 76 | extern const char sys::PathSeparator = ':'; |
| 77 | |
Nick Lewycky | fff116f | 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 | 0eab5e2 | 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 | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame^] | 90 | bool |
Reid Spencer | 69a1616 | 2004-12-24 06:29:42 +0000 | [diff] [blame] | 91 | Path::isValid() const { |
Reid Spencer | 6371ccb | 2005-07-08 06:53:26 +0000 | [diff] [blame] | 92 | // Check some obvious things |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame^] | 93 | if (path.empty()) |
Reid Spencer | 69a1616 | 2004-12-24 06:29:42 +0000 | [diff] [blame] | 94 | return false; |
| 95 | else if (path.length() >= MAXPATHLEN) |
| 96 | return false; |
Reid Spencer | 6371ccb | 2005-07-08 06:53:26 +0000 | [diff] [blame] | 97 | |
| 98 | // Check that the characters are ascii chars |
| 99 | size_t len = path.length(); |
| 100 | unsigned i = 0; |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame^] | 101 | while (i < len && isascii(path[i])) |
Reid Spencer | 6371ccb | 2005-07-08 06:53:26 +0000 | [diff] [blame] | 102 | ++i; |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame^] | 103 | return i >= len; |
Reid Spencer | 69a1616 | 2004-12-24 06:29:42 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame^] | 106 | bool |
Reid Spencer | 69cce81 | 2007-03-29 16:43:20 +0000 | [diff] [blame] | 107 | Path::isAbsolute() const { |
| 108 | if (path.empty()) |
| 109 | return false; |
| 110 | return path[0] == '/'; |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame^] | 111 | } |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 112 | Path |
| 113 | Path::GetRootDirectory() { |
| 114 | Path result; |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 115 | result.set("/"); |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 116 | return result; |
| 117 | } |
| 118 | |
Reid Spencer | 69a1616 | 2004-12-24 06:29:42 +0000 | [diff] [blame] | 119 | Path |
Reid Spencer | 4874476 | 2006-08-22 19:01:30 +0000 | [diff] [blame] | 120 | Path::GetTemporaryDirectory(std::string* ErrMsg ) { |
Reid Spencer | 69a1616 | 2004-12-24 06:29:42 +0000 | [diff] [blame] | 121 | #if defined(HAVE_MKDTEMP) |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame^] | 122 | // 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] | 123 | // Linux and FreeBSD have it. Others probably won't. |
| 124 | char pathname[MAXPATHLEN]; |
| 125 | strcpy(pathname,"/tmp/llvm_XXXXXX"); |
Reid Spencer | 4874476 | 2006-08-22 19:01:30 +0000 | [diff] [blame] | 126 | if (0 == mkdtemp(pathname)) { |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame^] | 127 | MakeErrMsg(ErrMsg, |
Reid Spencer | 4874476 | 2006-08-22 19:01:30 +0000 | [diff] [blame] | 128 | std::string(pathname) + ": can't create temporary directory"); |
| 129 | return Path(); |
| 130 | } |
Reid Spencer | 69a1616 | 2004-12-24 06:29:42 +0000 | [diff] [blame] | 131 | Path result; |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 132 | result.set(pathname); |
Reid Spencer | 69a1616 | 2004-12-24 06:29:42 +0000 | [diff] [blame] | 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 | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame^] | 140 | // the time it is re-created as a directoy. |
Reid Spencer | 69a1616 | 2004-12-24 06:29:42 +0000 | [diff] [blame] | 141 | char pathname[MAXPATHLEN]; |
| 142 | strcpy(pathname, "/tmp/llvm_XXXXXX"); |
| 143 | int fd = 0; |
Reid Spencer | 4874476 | 2006-08-22 19:01:30 +0000 | [diff] [blame] | 144 | if (-1 == (fd = mkstemp(pathname))) { |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame^] | 145 | MakeErrMsg(ErrMsg, |
Reid Spencer | 4874476 | 2006-08-22 19:01:30 +0000 | [diff] [blame] | 146 | std::string(pathname) + ": can't create temporary directory"); |
| 147 | return Path(); |
| 148 | } |
Reid Spencer | 69a1616 | 2004-12-24 06:29:42 +0000 | [diff] [blame] | 149 | ::close(fd); |
| 150 | ::unlink(pathname); // start race condition, ignore errors |
Reid Spencer | 4874476 | 2006-08-22 19:01:30 +0000 | [diff] [blame] | 151 | if (-1 == ::mkdir(pathname, S_IRWXU)) { // end race condition |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame^] | 152 | MakeErrMsg(ErrMsg, |
Reid Spencer | 4874476 | 2006-08-22 19:01:30 +0000 | [diff] [blame] | 153 | std::string(pathname) + ": can't create temporary directory"); |
| 154 | return Path(); |
| 155 | } |
Reid Spencer | 69a1616 | 2004-12-24 06:29:42 +0000 | [diff] [blame] | 156 | Path result; |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 157 | result.set(pathname); |
Reid Spencer | 69a1616 | 2004-12-24 06:29:42 +0000 | [diff] [blame] | 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); |
Reid Spencer | 4874476 | 2006-08-22 19:01:30 +0000 | [diff] [blame] | 169 | if (TmpName == 0) { |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame^] | 170 | MakeErrMsg(ErrMsg, |
Reid Spencer | 4874476 | 2006-08-22 19:01:30 +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 | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame^] | 175 | MakeErrMsg(ErrMsg, |
Reid Spencer | 4874476 | 2006-08-22 19:01:30 +0000 | [diff] [blame] | 176 | std::string(TmpName) + ": can't create temporary directory"); |
| 177 | return Path(); |
| 178 | } |
Reid Spencer | 69a1616 | 2004-12-24 06:29:42 +0000 | [diff] [blame] | 179 | Path result; |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 180 | result.set(TmpName); |
Reid Spencer | 69a1616 | 2004-12-24 06:29:42 +0000 | [diff] [blame] | 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 | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame^] | 187 | // temporary name generated. So, we provide our own implementation that |
Reid Spencer | 69a1616 | 2004-12-24 06:29:42 +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 ) ); |
Reid Spencer | 4874476 | 2006-08-22 19:01:30 +0000 | [diff] [blame] | 198 | if (-1 == ::mkdir(pathname, S_IRWXU)) { |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame^] | 199 | MakeErrMsg(ErrMsg, |
Reid Spencer | 4874476 | 2006-08-22 19:01:30 +0000 | [diff] [blame] | 200 | std::string(pathname) + ": can't create temporary directory"); |
| 201 | return Path(); |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 202 | } |
Reid Spencer | 69a1616 | 2004-12-24 06:29:42 +0000 | [diff] [blame] | 203 | Path result; |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 204 | result.set(pathname); |
Reid Spencer | 69a1616 | 2004-12-24 06:29:42 +0000 | [diff] [blame] | 205 | assert(result.isValid() && "mkstemp didn't create a valid pathname!"); |
| 206 | return result; |
| 207 | #endif |
| 208 | } |
| 209 | |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame^] | 210 | void |
Reid Spencer | 1b6b99b | 2004-12-13 03:00:51 +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); |
Reid Spencer | 74e7261 | 2004-09-14 00:16:39 +0000 | [diff] [blame] | 216 | } |
Reid Spencer | 1b6b99b | 2004-12-13 03:00:51 +0000 | [diff] [blame] | 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/")); |
Reid Spencer | 74e7261 | 2004-09-14 00:16:39 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Reid Spencer | 1b6b99b | 2004-12-13 03:00:51 +0000 | [diff] [blame] | 225 | void |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 226 | Path::GetBitcodeLibraryPaths(std::vector<sys::Path>& Paths) { |
Reid Spencer | 1b6b99b | 2004-12-13 03:00:51 +0000 | [diff] [blame] | 227 | char * env_var = getenv("LLVM_LIB_SEARCH_PATH"); |
| 228 | if (env_var != 0) { |
| 229 | getPathList(env_var,Paths); |
| 230 | } |
Reid Spencer | 1b6b99b | 2004-12-13 03:00:51 +0000 | [diff] [blame] | 231 | #ifdef LLVM_LIBDIR |
| 232 | { |
| 233 | Path tmpPath; |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 234 | if (tmpPath.set(LLVM_LIBDIR)) |
Reid Spencer | c7f0832 | 2005-07-07 18:21:42 +0000 | [diff] [blame] | 235 | if (tmpPath.canRead()) |
Reid Spencer | 1b6b99b | 2004-12-13 03:00:51 +0000 | [diff] [blame] | 236 | Paths.push_back(tmpPath); |
| 237 | } |
| 238 | #endif |
| 239 | GetSystemLibraryPaths(Paths); |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame^] | 242 | Path |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 243 | Path::GetLLVMDefaultConfigDir() { |
| 244 | return Path("/etc/llvm/"); |
| 245 | } |
| 246 | |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 247 | Path |
| 248 | Path::GetUserHomeDirectory() { |
| 249 | const char* home = getenv("HOME"); |
| 250 | if (home) { |
| 251 | Path result; |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 252 | if (result.set(home)) |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 253 | return result; |
| 254 | } |
| 255 | return GetRootDirectory(); |
| 256 | } |
| 257 | |
Ted Kremenek | 7920078 | 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 | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame^] | 265 | |
Ted Kremenek | 7920078 | 2007-12-18 22:07:33 +0000 | [diff] [blame] | 266 | return Path(pathname); |
| 267 | } |
Reid Spencer | a229c5c | 2005-07-08 03:08:58 +0000 | [diff] [blame] | 268 | |
Chris Lattner | 1a09144 | 2008-03-03 02:55:43 +0000 | [diff] [blame] | 269 | /// GetMainExecutable - Return the path to the main executable, given the |
| 270 | /// value of argv[0] from program startup. |
| 271 | Path Path::GetMainExecutable(const char *argv0, void *MainAddr) { |
Seo Sanghyeon | fd6437f | 2008-06-27 22:55:30 +0000 | [diff] [blame] | 272 | #if defined(__linux__) || defined(__CYGWIN__) |
Chris Lattner | 3bdfa04 | 2008-03-13 05:22:05 +0000 | [diff] [blame] | 273 | char exe_path[MAXPATHLEN]; |
Seo Sanghyeon | fd6437f | 2008-06-27 22:55:30 +0000 | [diff] [blame] | 274 | ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path)); |
Chris Lattner | 3bdfa04 | 2008-03-13 05:22:05 +0000 | [diff] [blame] | 275 | if (len > 0 && len < MAXPATHLEN - 1) { |
| 276 | exe_path[len] = '\0'; |
| 277 | return Path(std::string(exe_path)); |
| 278 | } |
| 279 | #elif defined(HAVE_DLFCN_H) |
Chris Lattner | 1a09144 | 2008-03-03 02:55:43 +0000 | [diff] [blame] | 280 | // Use dladdr to get executable path if available. |
Chris Lattner | 1a09144 | 2008-03-03 02:55:43 +0000 | [diff] [blame] | 281 | Dl_info DLInfo; |
| 282 | int err = dladdr(MainAddr, &DLInfo); |
| 283 | if (err != 0) |
| 284 | return Path(std::string(DLInfo.dli_fname)); |
| 285 | #endif |
| 286 | return Path(); |
| 287 | } |
| 288 | |
| 289 | |
Ted Kremenek | 9b01cc0 | 2008-04-07 22:01:32 +0000 | [diff] [blame] | 290 | std::string Path::getDirname() const { |
| 291 | return getDirnameCharSep(path, '/'); |
| 292 | } |
Ted Kremenek | cf55c8e | 2008-04-07 21:53:57 +0000 | [diff] [blame] | 293 | |
Reid Spencer | 1b554b4 | 2004-09-11 04:55:08 +0000 | [diff] [blame] | 294 | std::string |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 295 | Path::getBasename() const { |
Reid Spencer | 1b554b4 | 2004-09-11 04:55:08 +0000 | [diff] [blame] | 296 | // Find the last slash |
Ted Kremenek | cf55c8e | 2008-04-07 21:53:57 +0000 | [diff] [blame] | 297 | std::string::size_type slash = path.rfind('/'); |
Reid Spencer | 1b554b4 | 2004-09-11 04:55:08 +0000 | [diff] [blame] | 298 | if (slash == std::string::npos) |
| 299 | slash = 0; |
| 300 | else |
| 301 | slash++; |
| 302 | |
Ted Kremenek | cf55c8e | 2008-04-07 21:53:57 +0000 | [diff] [blame] | 303 | std::string::size_type dot = path.rfind('.'); |
Jeff Cohen | 73f3667 | 2005-07-09 18:42:02 +0000 | [diff] [blame] | 304 | if (dot == std::string::npos || dot < slash) |
| 305 | return path.substr(slash); |
| 306 | else |
| 307 | return path.substr(slash, dot - slash); |
Reid Spencer | 1b554b4 | 2004-09-11 04:55:08 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Argyrios Kyrtzidis | fc19988 | 2008-06-15 15:15:19 +0000 | [diff] [blame] | 310 | std::string |
| 311 | Path::getSuffix() const { |
| 312 | // Find the last slash |
| 313 | std::string::size_type slash = path.rfind('/'); |
| 314 | if (slash == std::string::npos) |
| 315 | slash = 0; |
| 316 | else |
| 317 | slash++; |
| 318 | |
| 319 | std::string::size_type dot = path.rfind('.'); |
| 320 | if (dot == std::string::npos || dot < slash) |
Wojciech Matyjewicz | 93c5346 | 2008-06-15 18:02:47 +0000 | [diff] [blame] | 321 | return std::string(); |
Argyrios Kyrtzidis | fc19988 | 2008-06-15 15:15:19 +0000 | [diff] [blame] | 322 | else |
| 323 | return path.substr(dot + 1); |
| 324 | } |
| 325 | |
Reid Spencer | eaf1815 | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 326 | bool Path::getMagicNumber(std::string& Magic, unsigned len) const { |
Reid Spencer | be31d2a | 2004-11-16 17:14:08 +0000 | [diff] [blame] | 327 | assert(len < 1024 && "Request for magic string too long"); |
| 328 | char* buf = (char*) alloca(1 + len); |
Chris Lattner | c7c453a | 2006-08-01 17:51:09 +0000 | [diff] [blame] | 329 | int fd = ::open(path.c_str(), O_RDONLY); |
Reid Spencer | be31d2a | 2004-11-16 17:14:08 +0000 | [diff] [blame] | 330 | if (fd < 0) |
| 331 | return false; |
Reid Spencer | 86ac2dc | 2004-12-02 09:09:48 +0000 | [diff] [blame] | 332 | ssize_t bytes_read = ::read(fd, buf, len); |
| 333 | ::close(fd); |
| 334 | if (ssize_t(len) != bytes_read) { |
| 335 | Magic.clear(); |
Reid Spencer | be31d2a | 2004-11-16 17:14:08 +0000 | [diff] [blame] | 336 | return false; |
Reid Spencer | 86ac2dc | 2004-12-02 09:09:48 +0000 | [diff] [blame] | 337 | } |
| 338 | Magic.assign(buf,len); |
Reid Spencer | eaf1815 | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 339 | return true; |
| 340 | } |
| 341 | |
Reid Spencer | 1b554b4 | 2004-09-11 04:55:08 +0000 | [diff] [blame] | 342 | bool |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 343 | Path::exists() const { |
| 344 | return 0 == access(path.c_str(), F_OK ); |
| 345 | } |
| 346 | |
| 347 | bool |
Ted Kremenek | fd52711 | 2007-12-18 19:46:22 +0000 | [diff] [blame] | 348 | Path::isDirectory() const { |
| 349 | struct stat buf; |
| 350 | if (0 != stat(path.c_str(), &buf)) |
| 351 | return false; |
| 352 | return buf.st_mode & S_IFDIR ? true : false; |
| 353 | } |
| 354 | |
| 355 | bool |
Reid Spencer | c7f0832 | 2005-07-07 18:21:42 +0000 | [diff] [blame] | 356 | Path::canRead() const { |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 357 | return 0 == access(path.c_str(), F_OK | R_OK ); |
| 358 | } |
| 359 | |
| 360 | bool |
Reid Spencer | c7f0832 | 2005-07-07 18:21:42 +0000 | [diff] [blame] | 361 | Path::canWrite() const { |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 362 | return 0 == access(path.c_str(), F_OK | W_OK ); |
| 363 | } |
| 364 | |
| 365 | bool |
Reid Spencer | c7f0832 | 2005-07-07 18:21:42 +0000 | [diff] [blame] | 366 | Path::canExecute() const { |
Reid Spencer | 8b2d1aa | 2005-07-08 17:46:10 +0000 | [diff] [blame] | 367 | if (0 != access(path.c_str(), R_OK | X_OK )) |
| 368 | return false; |
Reid Spencer | 2ae9d11 | 2007-04-07 18:52:17 +0000 | [diff] [blame] | 369 | struct stat buf; |
| 370 | if (0 != stat(path.c_str(), &buf)) |
| 371 | return false; |
| 372 | if (!S_ISREG(buf.st_mode)) |
Misha Brukman | 8177bf8 | 2005-04-20 15:33:22 +0000 | [diff] [blame] | 373 | return false; |
Reid Spencer | 8b2d1aa | 2005-07-08 17:46:10 +0000 | [diff] [blame] | 374 | return true; |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame^] | 377 | std::string |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 378 | Path::getLast() const { |
| 379 | // Find the last slash |
| 380 | size_t pos = path.rfind('/'); |
| 381 | |
| 382 | // Handle the corner cases |
| 383 | if (pos == std::string::npos) |
| 384 | return path; |
| 385 | |
| 386 | // If the last character is a slash |
| 387 | if (pos == path.length()-1) { |
| 388 | // Find the second to last slash |
| 389 | size_t pos2 = path.rfind('/', pos-1); |
| 390 | if (pos2 == std::string::npos) |
| 391 | return path.substr(0,pos); |
| 392 | else |
| 393 | return path.substr(pos2+1,pos-pos2-1); |
| 394 | } |
| 395 | // Return everything after the last slash |
| 396 | return path.substr(pos+1); |
| 397 | } |
| 398 | |
Reid Spencer | 2ae9d11 | 2007-04-07 18:52:17 +0000 | [diff] [blame] | 399 | const FileStatus * |
| 400 | PathWithStatus::getFileStatus(bool update, std::string *ErrStr) const { |
| 401 | if (!fsIsValid || update) { |
Reid Spencer | 69cce81 | 2007-03-29 16:43:20 +0000 | [diff] [blame] | 402 | struct stat buf; |
Reid Spencer | 8475ec0 | 2007-03-29 19:05:44 +0000 | [diff] [blame] | 403 | if (0 != stat(path.c_str(), &buf)) { |
| 404 | MakeErrMsg(ErrStr, path + ": can't get status of file"); |
| 405 | return 0; |
| 406 | } |
Reid Spencer | 2ae9d11 | 2007-04-07 18:52:17 +0000 | [diff] [blame] | 407 | status.fileSize = buf.st_size; |
| 408 | status.modTime.fromEpochTime(buf.st_mtime); |
| 409 | status.mode = buf.st_mode; |
| 410 | status.user = buf.st_uid; |
| 411 | status.group = buf.st_gid; |
| 412 | status.uniqueID = uint64_t(buf.st_ino); |
| 413 | status.isDir = S_ISDIR(buf.st_mode); |
| 414 | status.isFile = S_ISREG(buf.st_mode); |
| 415 | fsIsValid = true; |
Reid Spencer | 69cce81 | 2007-03-29 16:43:20 +0000 | [diff] [blame] | 416 | } |
Reid Spencer | 2ae9d11 | 2007-04-07 18:52:17 +0000 | [diff] [blame] | 417 | return &status; |
Reid Spencer | eaf1815 | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Chris Lattner | 252ad03 | 2006-07-28 22:03:44 +0000 | [diff] [blame] | 420 | static bool AddPermissionBits(const Path &File, int bits) { |
Reid Spencer | 77cc91d | 2004-12-13 19:59:50 +0000 | [diff] [blame] | 421 | // Get the umask value from the operating system. We want to use it |
| 422 | // when changing the file's permissions. Since calling umask() sets |
| 423 | // the umask and returns its old value, we must call it a second |
| 424 | // time to reset it to the user's preference. |
| 425 | int mask = umask(0777); // The arg. to umask is arbitrary. |
| 426 | umask(mask); // Restore the umask. |
| 427 | |
| 428 | // Get the file's current mode. |
Reid Spencer | 2ae9d11 | 2007-04-07 18:52:17 +0000 | [diff] [blame] | 429 | struct stat buf; |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame^] | 430 | if (0 != stat(File.toString().c_str(), &buf)) |
Reid Spencer | 77cc91d | 2004-12-13 19:59:50 +0000 | [diff] [blame] | 431 | return false; |
Reid Spencer | 2ae9d11 | 2007-04-07 18:52:17 +0000 | [diff] [blame] | 432 | // Change the file to have whichever permissions bits from 'bits' |
| 433 | // that the umask would not disable. |
| 434 | if ((chmod(File.c_str(), (buf.st_mode | (bits & ~mask)))) == -1) |
| 435 | return false; |
Reid Spencer | 77cc91d | 2004-12-13 19:59:50 +0000 | [diff] [blame] | 436 | return true; |
| 437 | } |
| 438 | |
Reid Spencer | e1647f4 | 2006-08-22 23:27:23 +0000 | [diff] [blame] | 439 | bool Path::makeReadableOnDisk(std::string* ErrMsg) { |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame^] | 440 | if (!AddPermissionBits(*this, 0444)) |
Reid Spencer | 5a06077 | 2006-08-23 07:30:48 +0000 | [diff] [blame] | 441 | return MakeErrMsg(ErrMsg, path + ": can't make file readable"); |
Reid Spencer | e1647f4 | 2006-08-22 23:27:23 +0000 | [diff] [blame] | 442 | return false; |
Reid Spencer | 77cc91d | 2004-12-13 19:59:50 +0000 | [diff] [blame] | 443 | } |
| 444 | |
Reid Spencer | e1647f4 | 2006-08-22 23:27:23 +0000 | [diff] [blame] | 445 | bool Path::makeWriteableOnDisk(std::string* ErrMsg) { |
Reid Spencer | 5a06077 | 2006-08-23 07:30:48 +0000 | [diff] [blame] | 446 | if (!AddPermissionBits(*this, 0222)) |
| 447 | return MakeErrMsg(ErrMsg, path + ": can't make file writable"); |
Reid Spencer | e1647f4 | 2006-08-22 23:27:23 +0000 | [diff] [blame] | 448 | return false; |
Reid Spencer | 77cc91d | 2004-12-13 19:59:50 +0000 | [diff] [blame] | 449 | } |
| 450 | |
Reid Spencer | e1647f4 | 2006-08-22 23:27:23 +0000 | [diff] [blame] | 451 | bool Path::makeExecutableOnDisk(std::string* ErrMsg) { |
Reid Spencer | 5a06077 | 2006-08-23 07:30:48 +0000 | [diff] [blame] | 452 | if (!AddPermissionBits(*this, 0111)) |
| 453 | return MakeErrMsg(ErrMsg, path + ": can't make file executable"); |
Reid Spencer | e1647f4 | 2006-08-22 23:27:23 +0000 | [diff] [blame] | 454 | return false; |
Reid Spencer | 77cc91d | 2004-12-13 19:59:50 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Reid Spencer | eaf1815 | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 457 | bool |
Reid Spencer | 142ca8e | 2006-08-23 06:56:27 +0000 | [diff] [blame] | 458 | Path::getDirectoryContents(std::set<Path>& result, std::string* ErrMsg) const { |
Reid Spencer | eaf1815 | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 459 | DIR* direntries = ::opendir(path.c_str()); |
Reid Spencer | 5a06077 | 2006-08-23 07:30:48 +0000 | [diff] [blame] | 460 | if (direntries == 0) |
| 461 | return MakeErrMsg(ErrMsg, path + ": can't open directory"); |
Reid Spencer | eaf1815 | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 462 | |
Reid Spencer | 3be872e | 2005-07-28 16:25:57 +0000 | [diff] [blame] | 463 | std::string dirPath = path; |
| 464 | if (!lastIsSlash(dirPath)) |
| 465 | dirPath += '/'; |
| 466 | |
Reid Spencer | eaf1815 | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 467 | result.clear(); |
| 468 | struct dirent* de = ::readdir(direntries); |
Misha Brukman | 4619c75 | 2005-04-20 04:04:07 +0000 | [diff] [blame] | 469 | for ( ; de != 0; de = ::readdir(direntries)) { |
Reid Spencer | eaf1815 | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 470 | if (de->d_name[0] != '.') { |
Reid Spencer | 3be872e | 2005-07-28 16:25:57 +0000 | [diff] [blame] | 471 | Path aPath(dirPath + (const char*)de->d_name); |
Chris Lattner | b14c342 | 2006-07-07 21:21:06 +0000 | [diff] [blame] | 472 | struct stat st; |
| 473 | if (0 != lstat(aPath.path.c_str(), &st)) { |
| 474 | if (S_ISLNK(st.st_mode)) |
Misha Brukman | 4619c75 | 2005-04-20 04:04:07 +0000 | [diff] [blame] | 475 | continue; // dangling symlink -- ignore |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame^] | 476 | return MakeErrMsg(ErrMsg, |
Reid Spencer | 5a06077 | 2006-08-23 07:30:48 +0000 | [diff] [blame] | 477 | aPath.path + ": can't determine file object type"); |
Misha Brukman | 4619c75 | 2005-04-20 04:04:07 +0000 | [diff] [blame] | 478 | } |
Reid Spencer | b608a81 | 2004-11-16 06:15:19 +0000 | [diff] [blame] | 479 | result.insert(aPath); |
Reid Spencer | eaf1815 | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 480 | } |
Reid Spencer | eaf1815 | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 481 | } |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame^] | 482 | |
Reid Spencer | eaf1815 | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 483 | closedir(direntries); |
Reid Spencer | 142ca8e | 2006-08-23 06:56:27 +0000 | [diff] [blame] | 484 | return false; |
Reid Spencer | 9195f37 | 2004-11-09 20:26:31 +0000 | [diff] [blame] | 485 | } |
| 486 | |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 487 | bool |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 488 | Path::set(const std::string& a_path) { |
| 489 | if (a_path.empty()) |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 490 | return false; |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 491 | std::string save(path); |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 492 | path = a_path; |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 493 | if (!isValid()) { |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 494 | path = save; |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 495 | return false; |
| 496 | } |
| 497 | return true; |
| 498 | } |
| 499 | |
| 500 | bool |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 501 | Path::appendComponent(const std::string& name) { |
| 502 | if (name.empty()) |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 503 | return false; |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 504 | std::string save(path); |
Reid Spencer | 3be872e | 2005-07-28 16:25:57 +0000 | [diff] [blame] | 505 | if (!lastIsSlash(path)) |
| 506 | path += '/'; |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 507 | path += name; |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 508 | if (!isValid()) { |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 509 | path = save; |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 510 | return false; |
| 511 | } |
| 512 | return true; |
| 513 | } |
| 514 | |
| 515 | bool |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 516 | Path::eraseComponent() { |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 517 | size_t slashpos = path.rfind('/',path.size()); |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 518 | if (slashpos == 0 || slashpos == std::string::npos) { |
| 519 | path.erase(); |
| 520 | return true; |
| 521 | } |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 522 | if (slashpos == path.size() - 1) |
| 523 | slashpos = path.rfind('/',slashpos-1); |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 524 | if (slashpos == std::string::npos) { |
| 525 | path.erase(); |
| 526 | return true; |
| 527 | } |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 528 | path.erase(slashpos); |
| 529 | return true; |
| 530 | } |
| 531 | |
| 532 | bool |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 533 | Path::appendSuffix(const std::string& suffix) { |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 534 | std::string save(path); |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 535 | path.append("."); |
| 536 | path.append(suffix); |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 537 | if (!isValid()) { |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 538 | path = save; |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 539 | return false; |
| 540 | } |
| 541 | return true; |
| 542 | } |
| 543 | |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 544 | bool |
| 545 | Path::eraseSuffix() { |
Reid Spencer | 6371ccb | 2005-07-08 06:53:26 +0000 | [diff] [blame] | 546 | std::string save = path; |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 547 | size_t dotpos = path.rfind('.',path.size()); |
| 548 | size_t slashpos = path.rfind('/',path.size()); |
Jeff Cohen | 563a17f | 2005-07-08 04:49:16 +0000 | [diff] [blame] | 549 | if (dotpos != std::string::npos) { |
Jeff Cohen | 73f3667 | 2005-07-09 18:42:02 +0000 | [diff] [blame] | 550 | if (slashpos == std::string::npos || dotpos > slashpos+1) { |
Jeff Cohen | 563a17f | 2005-07-08 04:49:16 +0000 | [diff] [blame] | 551 | path.erase(dotpos, path.size()-dotpos); |
Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 552 | return true; |
Jeff Cohen | 563a17f | 2005-07-08 04:49:16 +0000 | [diff] [blame] | 553 | } |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 554 | } |
Reid Spencer | 6371ccb | 2005-07-08 06:53:26 +0000 | [diff] [blame] | 555 | if (!isValid()) |
| 556 | path = save; |
Jeff Cohen | 563a17f | 2005-07-08 04:49:16 +0000 | [diff] [blame] | 557 | return false; |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Ted Kremenek | c5412c5 | 2008-04-03 16:11:31 +0000 | [diff] [blame] | 560 | static bool createDirectoryHelper(char* beg, char* end, bool create_parents) { |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame^] | 561 | |
Ted Kremenek | c5412c5 | 2008-04-03 16:11:31 +0000 | [diff] [blame] | 562 | if (access(beg, F_OK | R_OK | W_OK) == 0) |
| 563 | return false; |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame^] | 564 | |
Ted Kremenek | c5412c5 | 2008-04-03 16:11:31 +0000 | [diff] [blame] | 565 | if (create_parents) { |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame^] | 566 | |
Ted Kremenek | c5412c5 | 2008-04-03 16:11:31 +0000 | [diff] [blame] | 567 | char* c = end; |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame^] | 568 | |
Ted Kremenek | c5412c5 | 2008-04-03 16:11:31 +0000 | [diff] [blame] | 569 | for (; c != beg; --c) |
| 570 | if (*c == '/') { |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame^] | 571 | |
Ted Kremenek | c5412c5 | 2008-04-03 16:11:31 +0000 | [diff] [blame] | 572 | // Recurse to handling the parent directory. |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame^] | 573 | *c = '\0'; |
Ted Kremenek | c5412c5 | 2008-04-03 16:11:31 +0000 | [diff] [blame] | 574 | bool x = createDirectoryHelper(beg, c, create_parents); |
| 575 | *c = '/'; |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame^] | 576 | |
Ted Kremenek | c5412c5 | 2008-04-03 16:11:31 +0000 | [diff] [blame] | 577 | // Return if we encountered an error. |
| 578 | if (x) |
| 579 | return true; |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame^] | 580 | |
Ted Kremenek | c5412c5 | 2008-04-03 16:11:31 +0000 | [diff] [blame] | 581 | break; |
| 582 | } |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame^] | 583 | } |
| 584 | |
Ted Kremenek | c5412c5 | 2008-04-03 16:11:31 +0000 | [diff] [blame] | 585 | return mkdir(beg, S_IRWXU | S_IRWXG) != 0; |
| 586 | } |
| 587 | |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 588 | bool |
Reid Spencer | e5c9cb5 | 2006-08-23 00:39:35 +0000 | [diff] [blame] | 589 | Path::createDirectoryOnDisk( bool create_parents, std::string* ErrMsg ) { |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 590 | // Get a writeable copy of the path name |
| 591 | char pathname[MAXPATHLEN]; |
| 592 | path.copy(pathname,MAXPATHLEN); |
| 593 | |
| 594 | // Null-terminate the last component |
Evan Cheng | 34cd4a4 | 2008-05-05 18:30:58 +0000 | [diff] [blame] | 595 | size_t lastchar = path.length() - 1 ; |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame^] | 596 | |
Ted Kremenek | c5412c5 | 2008-04-03 16:11:31 +0000 | [diff] [blame] | 597 | if (pathname[lastchar] != '/') |
| 598 | ++lastchar; |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame^] | 599 | |
Ted Kremenek | c5412c5 | 2008-04-03 16:11:31 +0000 | [diff] [blame] | 600 | pathname[lastchar] = 0; |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame^] | 601 | |
Ted Kremenek | c5412c5 | 2008-04-03 16:11:31 +0000 | [diff] [blame] | 602 | if (createDirectoryHelper(pathname, pathname+lastchar, create_parents)) |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame^] | 603 | return MakeErrMsg(ErrMsg, |
Ted Kremenek | c5412c5 | 2008-04-03 16:11:31 +0000 | [diff] [blame] | 604 | std::string(pathname) + ": can't create directory"); |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame^] | 605 | |
Reid Spencer | e5c9cb5 | 2006-08-23 00:39:35 +0000 | [diff] [blame] | 606 | return false; |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | bool |
Reid Spencer | e5c9cb5 | 2006-08-23 00:39:35 +0000 | [diff] [blame] | 610 | Path::createFileOnDisk(std::string* ErrMsg) { |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 611 | // Create the file |
Reid Spencer | 622e220 | 2004-09-18 19:25:11 +0000 | [diff] [blame] | 612 | int fd = ::creat(path.c_str(), S_IRUSR | S_IWUSR); |
Reid Spencer | 5a06077 | 2006-08-23 07:30:48 +0000 | [diff] [blame] | 613 | if (fd < 0) |
| 614 | return MakeErrMsg(ErrMsg, path + ": can't create file"); |
Reid Spencer | 622e220 | 2004-09-18 19:25:11 +0000 | [diff] [blame] | 615 | ::close(fd); |
Reid Spencer | e5c9cb5 | 2006-08-23 00:39:35 +0000 | [diff] [blame] | 616 | return false; |
Reid Spencer | b89a223 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 617 | } |
| 618 | |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 619 | bool |
Reid Spencer | e5c9cb5 | 2006-08-23 00:39:35 +0000 | [diff] [blame] | 620 | Path::createTemporaryFileOnDisk(bool reuse_current, std::string* ErrMsg) { |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 621 | // Make this into a unique file name |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 622 | if (makeUnique( reuse_current, ErrMsg )) |
| 623 | return true; |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 624 | |
| 625 | // create the file |
Reid Spencer | e5c9cb5 | 2006-08-23 00:39:35 +0000 | [diff] [blame] | 626 | 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^] | 627 | if (fd < 0) |
Reid Spencer | 5a06077 | 2006-08-23 07:30:48 +0000 | [diff] [blame] | 628 | return MakeErrMsg(ErrMsg, path + ": can't create temporary file"); |
Reid Spencer | e5c9cb5 | 2006-08-23 00:39:35 +0000 | [diff] [blame] | 629 | ::close(fd); |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 630 | return false; |
Reid Spencer | 9195f37 | 2004-11-09 20:26:31 +0000 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | bool |
Chris Lattner | 0c33231 | 2006-07-28 22:29:50 +0000 | [diff] [blame] | 634 | Path::eraseFromDisk(bool remove_contents, std::string *ErrStr) const { |
Reid Spencer | 2ae9d11 | 2007-04-07 18:52:17 +0000 | [diff] [blame] | 635 | // Get the status so we can determin if its a file or directory |
| 636 | struct stat buf; |
| 637 | if (0 != stat(path.c_str(), &buf)) { |
| 638 | MakeErrMsg(ErrStr, path + ": can't get status of file"); |
Chris Lattner | 0c33231 | 2006-07-28 22:29:50 +0000 | [diff] [blame] | 639 | return true; |
Reid Spencer | 2ae9d11 | 2007-04-07 18:52:17 +0000 | [diff] [blame] | 640 | } |
| 641 | |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame^] | 642 | // Note: this check catches strange situations. In all cases, LLVM should |
| 643 | // only be involved in the creation and deletion of regular files. This |
| 644 | // check ensures that what we're trying to erase is a regular file. It |
| 645 | // effectively prevents LLVM from erasing things like /dev/null, any block |
| 646 | // special file, or other things that aren't "regular" files. |
Reid Spencer | 2ae9d11 | 2007-04-07 18:52:17 +0000 | [diff] [blame] | 647 | if (S_ISREG(buf.st_mode)) { |
| 648 | if (unlink(path.c_str()) != 0) |
| 649 | return MakeErrMsg(ErrStr, path + ": can't destroy file"); |
| 650 | return false; |
| 651 | } |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame^] | 652 | |
Reid Spencer | 2ae9d11 | 2007-04-07 18:52:17 +0000 | [diff] [blame] | 653 | if (!S_ISDIR(buf.st_mode)) { |
| 654 | if (ErrStr) *ErrStr = "not a file or directory"; |
| 655 | return true; |
| 656 | } |
Reid Spencer | 8475ec0 | 2007-03-29 19:05:44 +0000 | [diff] [blame] | 657 | |
Chris Lattner | 0c33231 | 2006-07-28 22:29:50 +0000 | [diff] [blame] | 658 | if (remove_contents) { |
| 659 | // Recursively descend the directory to remove its contents. |
| 660 | std::string cmd = "/bin/rm -rf " + path; |
| 661 | system(cmd.c_str()); |
| 662 | return false; |
| 663 | } |
| 664 | |
| 665 | // Otherwise, try to just remove the one directory. |
| 666 | char pathname[MAXPATHLEN]; |
| 667 | path.copy(pathname, MAXPATHLEN); |
Evan Cheng | 34cd4a4 | 2008-05-05 18:30:58 +0000 | [diff] [blame] | 668 | size_t lastchar = path.length() - 1; |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame^] | 669 | if (pathname[lastchar] == '/') |
Chris Lattner | 0c33231 | 2006-07-28 22:29:50 +0000 | [diff] [blame] | 670 | pathname[lastchar] = 0; |
| 671 | else |
| 672 | pathname[lastchar+1] = 0; |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame^] | 673 | |
Chris Lattner | 0c33231 | 2006-07-28 22:29:50 +0000 | [diff] [blame] | 674 | if (rmdir(pathname) != 0) |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame^] | 675 | return MakeErrMsg(ErrStr, |
Reid Spencer | 8475ec0 | 2007-03-29 19:05:44 +0000 | [diff] [blame] | 676 | std::string(pathname) + ": can't erase directory"); |
Chris Lattner | 0c33231 | 2006-07-28 22:29:50 +0000 | [diff] [blame] | 677 | return false; |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | bool |
Reid Spencer | 5a06077 | 2006-08-23 07:30:48 +0000 | [diff] [blame] | 681 | Path::renamePathOnDisk(const Path& newName, std::string* ErrMsg) { |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 682 | if (0 != ::rename(path.c_str(), newName.c_str())) |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame^] | 683 | return MakeErrMsg(ErrMsg, std::string("can't rename '") + path + "' as '" + |
Reid Spencer | 2aafadc | 2005-04-21 02:50:10 +0000 | [diff] [blame] | 684 | newName.toString() + "' "); |
Reid Spencer | 5a06077 | 2006-08-23 07:30:48 +0000 | [diff] [blame] | 685 | return false; |
Reid Spencer | eaf1815 | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 686 | } |
| 687 | |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame^] | 688 | bool |
Chris Lattner | 1bebfb5 | 2006-07-28 22:36:17 +0000 | [diff] [blame] | 689 | Path::setStatusInfoOnDisk(const FileStatus &si, std::string *ErrStr) const { |
Reid Spencer | eaf1815 | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 690 | struct utimbuf utb; |
| 691 | utb.actime = si.modTime.toPosixTime(); |
| 692 | utb.modtime = utb.actime; |
| 693 | if (0 != ::utime(path.c_str(),&utb)) |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 694 | return MakeErrMsg(ErrStr, path + ": can't set file modification time"); |
Reid Spencer | eaf1815 | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 695 | if (0 != ::chmod(path.c_str(),si.mode)) |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 696 | return MakeErrMsg(ErrStr, path + ": can't set mode"); |
Chris Lattner | 1bebfb5 | 2006-07-28 22:36:17 +0000 | [diff] [blame] | 697 | return false; |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 698 | } |
| 699 | |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame^] | 700 | bool |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 701 | 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] | 702 | int inFile = -1; |
| 703 | int outFile = -1; |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 704 | inFile = ::open(Src.c_str(), O_RDONLY); |
| 705 | if (inFile == -1) |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame^] | 706 | return MakeErrMsg(ErrMsg, Src.toString() + |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 707 | ": can't open source file to copy"); |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 708 | |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 709 | outFile = ::open(Dest.c_str(), O_WRONLY|O_CREAT, 0666); |
| 710 | if (outFile == -1) { |
| 711 | ::close(inFile); |
| 712 | return MakeErrMsg(ErrMsg, Dest.toString() + |
| 713 | ": can't create destination file for copy"); |
| 714 | } |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 715 | |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 716 | char Buffer[16*1024]; |
| 717 | while (ssize_t Amt = ::read(inFile, Buffer, 16*1024)) { |
| 718 | if (Amt == -1) { |
| 719 | if (errno != EINTR && errno != EAGAIN) { |
| 720 | ::close(inFile); |
| 721 | ::close(outFile); |
| 722 | return MakeErrMsg(ErrMsg, Src.toString()+": can't read source file: "); |
| 723 | } |
| 724 | } else { |
| 725 | char *BufPtr = Buffer; |
| 726 | while (Amt) { |
| 727 | ssize_t AmtWritten = ::write(outFile, BufPtr, Amt); |
| 728 | if (AmtWritten == -1) { |
| 729 | if (errno != EINTR && errno != EAGAIN) { |
| 730 | ::close(inFile); |
| 731 | ::close(outFile); |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame^] | 732 | return MakeErrMsg(ErrMsg, Dest.toString() + |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 733 | ": can't write destination file: "); |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 734 | } |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 735 | } else { |
| 736 | Amt -= AmtWritten; |
| 737 | BufPtr += AmtWritten; |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 738 | } |
| 739 | } |
| 740 | } |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 741 | } |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 742 | ::close(inFile); |
| 743 | ::close(outFile); |
| 744 | return false; |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 745 | } |
| 746 | |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame^] | 747 | bool |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 748 | Path::makeUnique(bool reuse_current, std::string* ErrMsg) { |
Reid Spencer | 07f9f4e | 2004-12-15 08:32:45 +0000 | [diff] [blame] | 749 | if (reuse_current && !exists()) |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 750 | return false; // File doesn't exist already, just use it! |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 751 | |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame^] | 752 | // 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] | 753 | // mktemp or our own implementation. |
| 754 | char *FNBuffer = (char*) alloca(path.size()+8); |
Devang Patel | ce8fdf1 | 2008-07-22 20:02:39 +0000 | [diff] [blame] | 755 | path.copy(FNBuffer,path.size()); |
Devang Patel | bdfa9ac | 2008-07-24 00:35:38 +0000 | [diff] [blame] | 756 | if (isDirectory()) |
| 757 | strcpy(FNBuffer+path.size(), "/XXXXXX"); |
| 758 | else |
Devang Patel | ce8fdf1 | 2008-07-22 20:02:39 +0000 | [diff] [blame] | 759 | strcpy(FNBuffer+path.size(), "-XXXXXX"); |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 760 | |
| 761 | #if defined(HAVE_MKSTEMP) |
| 762 | int TempFD; |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 763 | if ((TempFD = mkstemp(FNBuffer)) == -1) |
| 764 | return MakeErrMsg(ErrMsg, path + ": can't make unique filename"); |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 765 | |
| 766 | // We don't need to hold the temp file descriptor... we will trust that no one |
| 767 | // will overwrite/delete the file before we can open it again. |
| 768 | close(TempFD); |
| 769 | |
| 770 | // Save the name |
| 771 | path = FNBuffer; |
| 772 | #elif defined(HAVE_MKTEMP) |
| 773 | // 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] | 774 | if (mktemp(FNBuffer) == 0) |
| 775 | return MakeErrMsg(ErrMsg, path + ": can't make unique filename"); |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 776 | |
| 777 | // Save the name |
| 778 | path = FNBuffer; |
| 779 | #else |
| 780 | // Okay, looks like we have to do it all by our lonesome. |
| 781 | static unsigned FCounter = 0; |
| 782 | unsigned offset = path.size() + 1; |
| 783 | while ( FCounter < 999999 && exists()) { |
| 784 | sprintf(FNBuffer+offset,"%06u",++FCounter); |
| 785 | path = FNBuffer; |
| 786 | } |
| 787 | if (FCounter > 999999) |
Mikhail Glushenkov | b8863a1 | 2009-02-15 03:20:03 +0000 | [diff] [blame^] | 788 | return MakeErrMsg(ErrMsg, |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 789 | path + ": can't make unique filename: too many files"); |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 790 | #endif |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 791 | return false; |
| 792 | } |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 793 | |
Chris Lattner | 799ed10 | 2008-04-01 06:00:12 +0000 | [diff] [blame] | 794 | const char *Path::MapInFilePages(int FD, uint64_t FileSize) { |
Chris Lattner | 9ffd19a | 2008-04-01 06:16:24 +0000 | [diff] [blame] | 795 | int Flags = MAP_PRIVATE; |
| 796 | #ifdef MAP_FILE |
| 797 | Flags |= MAP_FILE; |
| 798 | #endif |
| 799 | void *BasePtr = ::mmap(0, FileSize, PROT_READ, Flags, FD, 0); |
| 800 | if (BasePtr == MAP_FAILED) |
| 801 | return 0; |
Chris Lattner | 14c762d | 2008-04-01 06:25:23 +0000 | [diff] [blame] | 802 | return (const char*)BasePtr; |
Chris Lattner | 799ed10 | 2008-04-01 06:00:12 +0000 | [diff] [blame] | 803 | } |
| 804 | |
Chris Lattner | 9ffd19a | 2008-04-01 06:16:24 +0000 | [diff] [blame] | 805 | void Path::UnMapFilePages(const char *BasePtr, uint64_t FileSize) { |
Chris Lattner | 14c762d | 2008-04-01 06:25:23 +0000 | [diff] [blame] | 806 | ::munmap((void*)BasePtr, FileSize); |
Chris Lattner | 799ed10 | 2008-04-01 06:00:12 +0000 | [diff] [blame] | 807 | } |
| 808 | |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 809 | } // end llvm namespace |
Reid Spencer | b89a223 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 810 | |