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