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