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