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