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