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