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