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