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 | } |
| 88 | #ifdef LLVMGCCDIR |
| 89 | { |
| 90 | Path tmpPath(std::string(LLVMGCCDIR) + "bytecode-libs/"); |
| 91 | if (tmpPath.readable()) |
| 92 | Paths.push_back(tmpPath); |
| 93 | } |
| 94 | #endif |
| 95 | #ifdef LLVM_LIBDIR |
| 96 | { |
| 97 | Path tmpPath; |
| 98 | if (tmpPath.setDirectory(LLVM_LIBDIR)) |
| 99 | if (tmpPath.readable()) |
| 100 | Paths.push_back(tmpPath); |
| 101 | } |
| 102 | #endif |
| 103 | GetSystemLibraryPaths(Paths); |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | Path |
| 107 | Path::GetLLVMDefaultConfigDir() { |
| 108 | return Path("/etc/llvm/"); |
| 109 | } |
| 110 | |
| 111 | Path |
| 112 | Path::GetLLVMConfigDir() { |
| 113 | Path result; |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 114 | if (result.setDirectory(LLVM_ETCDIR)) |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 115 | return result; |
| 116 | return GetLLVMDefaultConfigDir(); |
| 117 | } |
| 118 | |
| 119 | Path |
| 120 | Path::GetUserHomeDirectory() { |
| 121 | const char* home = getenv("HOME"); |
| 122 | if (home) { |
| 123 | Path result; |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 124 | if (result.setDirectory(home)) |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 125 | return result; |
| 126 | } |
| 127 | return GetRootDirectory(); |
| 128 | } |
| 129 | |
| 130 | bool |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 131 | Path::isFile() const { |
| 132 | return (isValid() && path[path.length()-1] != '/'); |
Reid Spencer | 1b554b4 | 2004-09-11 04:55:08 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | bool |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 136 | Path::isDirectory() const { |
| 137 | return (isValid() && path[path.length()-1] == '/'); |
Reid Spencer | 1b554b4 | 2004-09-11 04:55:08 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | std::string |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 141 | Path::getBasename() const { |
Reid Spencer | 1b554b4 | 2004-09-11 04:55:08 +0000 | [diff] [blame] | 142 | // Find the last slash |
| 143 | size_t slash = path.rfind('/'); |
| 144 | if (slash == std::string::npos) |
| 145 | slash = 0; |
| 146 | else |
| 147 | slash++; |
| 148 | |
| 149 | return path.substr(slash, path.rfind('.')); |
| 150 | } |
| 151 | |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 152 | bool Path::hasMagicNumber(const std::string &Magic) const { |
Reid Spencer | 1b554b4 | 2004-09-11 04:55:08 +0000 | [diff] [blame] | 153 | size_t len = Magic.size(); |
Reid Spencer | be31d2a | 2004-11-16 17:14:08 +0000 | [diff] [blame] | 154 | assert(len < 1024 && "Request for magic string too long"); |
| 155 | char* buf = (char*) alloca(1 + len); |
| 156 | int fd = ::open(path.c_str(),O_RDONLY); |
| 157 | if (fd < 0) |
| 158 | return false; |
Reid Spencer | 1b6b99b | 2004-12-13 03:00:51 +0000 | [diff] [blame] | 159 | size_t read_len = ::read(fd, buf, len); |
Reid Spencer | be31d2a | 2004-11-16 17:14:08 +0000 | [diff] [blame] | 160 | close(fd); |
Reid Spencer | 1b6b99b | 2004-12-13 03:00:51 +0000 | [diff] [blame] | 161 | if (len != read_len) |
| 162 | return false; |
Reid Spencer | 1b554b4 | 2004-09-11 04:55:08 +0000 | [diff] [blame] | 163 | buf[len] = '\0'; |
| 164 | return Magic == buf; |
| 165 | } |
| 166 | |
Reid Spencer | eaf1815 | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 167 | bool Path::getMagicNumber(std::string& Magic, unsigned len) const { |
| 168 | if (!isFile()) |
| 169 | return false; |
Reid Spencer | be31d2a | 2004-11-16 17:14:08 +0000 | [diff] [blame] | 170 | assert(len < 1024 && "Request for magic string too long"); |
| 171 | char* buf = (char*) alloca(1 + len); |
| 172 | int fd = ::open(path.c_str(),O_RDONLY); |
| 173 | if (fd < 0) |
| 174 | return false; |
Reid Spencer | 86ac2dc | 2004-12-02 09:09:48 +0000 | [diff] [blame] | 175 | ssize_t bytes_read = ::read(fd, buf, len); |
| 176 | ::close(fd); |
| 177 | if (ssize_t(len) != bytes_read) { |
| 178 | Magic.clear(); |
Reid Spencer | be31d2a | 2004-11-16 17:14:08 +0000 | [diff] [blame] | 179 | return false; |
Reid Spencer | 86ac2dc | 2004-12-02 09:09:48 +0000 | [diff] [blame] | 180 | } |
| 181 | Magic.assign(buf,len); |
Reid Spencer | eaf1815 | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 182 | return true; |
| 183 | } |
| 184 | |
Reid Spencer | 1b554b4 | 2004-09-11 04:55:08 +0000 | [diff] [blame] | 185 | bool |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 186 | Path::isBytecodeFile() const { |
Reid Spencer | 9195f37 | 2004-11-09 20:26:31 +0000 | [diff] [blame] | 187 | char buffer[ 4]; |
| 188 | buffer[0] = 0; |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 189 | int fd = ::open(path.c_str(),O_RDONLY); |
| 190 | if (fd < 0) |
| 191 | return false; |
| 192 | ssize_t bytes_read = ::read(fd, buffer, 4); |
| 193 | ::close(fd); |
| 194 | if (4 != bytes_read) |
| 195 | return false; |
Reid Spencer | eaf1815 | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 196 | |
| 197 | return (buffer[0] == 'l' && buffer[1] == 'l' && buffer[2] == 'v' && |
| 198 | (buffer[3] == 'c' || buffer[3] == 'm')); |
Reid Spencer | 1b554b4 | 2004-09-11 04:55:08 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | bool |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 202 | Path::exists() const { |
| 203 | return 0 == access(path.c_str(), F_OK ); |
| 204 | } |
| 205 | |
| 206 | bool |
| 207 | Path::readable() const { |
| 208 | return 0 == access(path.c_str(), F_OK | R_OK ); |
| 209 | } |
| 210 | |
| 211 | bool |
| 212 | Path::writable() const { |
| 213 | return 0 == access(path.c_str(), F_OK | W_OK ); |
| 214 | } |
| 215 | |
| 216 | bool |
| 217 | Path::executable() const { |
| 218 | return 0 == access(path.c_str(), R_OK | X_OK ); |
| 219 | } |
| 220 | |
| 221 | std::string |
| 222 | Path::getLast() const { |
| 223 | // Find the last slash |
| 224 | size_t pos = path.rfind('/'); |
| 225 | |
| 226 | // Handle the corner cases |
| 227 | if (pos == std::string::npos) |
| 228 | return path; |
| 229 | |
| 230 | // If the last character is a slash |
| 231 | if (pos == path.length()-1) { |
| 232 | // Find the second to last slash |
| 233 | size_t pos2 = path.rfind('/', pos-1); |
| 234 | if (pos2 == std::string::npos) |
| 235 | return path.substr(0,pos); |
| 236 | else |
| 237 | return path.substr(pos2+1,pos-pos2-1); |
| 238 | } |
| 239 | // Return everything after the last slash |
| 240 | return path.substr(pos+1); |
| 241 | } |
| 242 | |
Reid Spencer | b608a81 | 2004-11-16 06:15:19 +0000 | [diff] [blame] | 243 | void |
| 244 | Path::getStatusInfo(StatusInfo& info) const { |
Reid Spencer | 9195f37 | 2004-11-09 20:26:31 +0000 | [diff] [blame] | 245 | struct stat buf; |
| 246 | if (0 != stat(path.c_str(), &buf)) { |
| 247 | ThrowErrno(std::string("Can't get status: ")+path); |
| 248 | } |
| 249 | info.fileSize = buf.st_size; |
Reid Spencer | eaf1815 | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 250 | info.modTime.fromEpochTime(buf.st_mtime); |
Reid Spencer | 9195f37 | 2004-11-09 20:26:31 +0000 | [diff] [blame] | 251 | info.mode = buf.st_mode; |
| 252 | info.user = buf.st_uid; |
| 253 | info.group = buf.st_gid; |
Reid Spencer | eaf1815 | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 254 | info.isDir = S_ISDIR(buf.st_mode); |
| 255 | if (info.isDir && path[path.length()-1] != '/') |
| 256 | path += '/'; |
| 257 | } |
| 258 | |
Reid Spencer | 77cc91d | 2004-12-13 19:59:50 +0000 | [diff] [blame] | 259 | static bool AddPermissionBits(const std::string& Filename, int bits) { |
| 260 | // Get the umask value from the operating system. We want to use it |
| 261 | // when changing the file's permissions. Since calling umask() sets |
| 262 | // the umask and returns its old value, we must call it a second |
| 263 | // time to reset it to the user's preference. |
| 264 | int mask = umask(0777); // The arg. to umask is arbitrary. |
| 265 | umask(mask); // Restore the umask. |
| 266 | |
| 267 | // Get the file's current mode. |
| 268 | struct stat st; |
| 269 | if ((stat(Filename.c_str(), &st)) == -1) |
| 270 | return false; |
| 271 | |
| 272 | // Change the file to have whichever permissions bits from 'bits' |
| 273 | // that the umask would not disable. |
| 274 | if ((chmod(Filename.c_str(), (st.st_mode | (bits & ~mask)))) == -1) |
| 275 | return false; |
| 276 | |
| 277 | return true; |
| 278 | } |
| 279 | |
| 280 | void Path::makeReadable() { |
| 281 | if (!AddPermissionBits(path,0444)) |
| 282 | ThrowErrno(path + ": can't make file readable"); |
| 283 | } |
| 284 | |
| 285 | void Path::makeWriteable() { |
| 286 | if (!AddPermissionBits(path,0222)) |
| 287 | ThrowErrno(path + ": can't make file writable"); |
| 288 | } |
| 289 | |
| 290 | void Path::makeExecutable() { |
| 291 | if (!AddPermissionBits(path,0111)) |
| 292 | ThrowErrno(path + ": can't make file executable"); |
| 293 | } |
| 294 | |
Reid Spencer | eaf1815 | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 295 | bool |
Reid Spencer | b608a81 | 2004-11-16 06:15:19 +0000 | [diff] [blame] | 296 | Path::getDirectoryContents(std::set<Path>& result) const { |
Reid Spencer | eaf1815 | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 297 | if (!isDirectory()) |
| 298 | return false; |
| 299 | DIR* direntries = ::opendir(path.c_str()); |
| 300 | if (direntries == 0) |
| 301 | ThrowErrno(path + ": can't open directory"); |
| 302 | |
| 303 | result.clear(); |
| 304 | struct dirent* de = ::readdir(direntries); |
| 305 | while (de != 0) { |
| 306 | if (de->d_name[0] != '.') { |
| 307 | Path aPath(path + (const char*)de->d_name); |
| 308 | struct stat buf; |
| 309 | if (0 != stat(aPath.path.c_str(), &buf)) |
| 310 | ThrowErrno(aPath.path + ": can't get status"); |
| 311 | if (S_ISDIR(buf.st_mode)) |
| 312 | aPath.path += "/"; |
Reid Spencer | b608a81 | 2004-11-16 06:15:19 +0000 | [diff] [blame] | 313 | result.insert(aPath); |
Reid Spencer | eaf1815 | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 314 | } |
| 315 | de = ::readdir(direntries); |
| 316 | } |
| 317 | |
| 318 | closedir(direntries); |
| 319 | return true; |
Reid Spencer | 9195f37 | 2004-11-09 20:26:31 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 322 | bool |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 323 | Path::setDirectory(const std::string& a_path) { |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 324 | if (a_path.size() == 0) |
| 325 | return false; |
| 326 | Path save(*this); |
| 327 | path = a_path; |
| 328 | size_t last = a_path.size() -1; |
Reid Spencer | 3d595cb | 2004-12-13 07:51:07 +0000 | [diff] [blame] | 329 | if (a_path[last] != '/') |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 330 | path += '/'; |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 331 | if (!isValid()) { |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 332 | path = save.path; |
| 333 | return false; |
| 334 | } |
| 335 | return true; |
| 336 | } |
| 337 | |
| 338 | bool |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 339 | Path::setFile(const std::string& a_path) { |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 340 | if (a_path.size() == 0) |
| 341 | return false; |
| 342 | Path save(*this); |
| 343 | path = a_path; |
| 344 | size_t last = a_path.size() - 1; |
| 345 | while (last > 0 && a_path[last] == '/') |
| 346 | last--; |
| 347 | path.erase(last+1); |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 348 | if (!isValid()) { |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 349 | path = save.path; |
| 350 | return false; |
| 351 | } |
| 352 | return true; |
| 353 | } |
| 354 | |
| 355 | bool |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 356 | Path::appendDirectory(const std::string& dir) { |
| 357 | if (isFile()) |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 358 | return false; |
| 359 | Path save(*this); |
| 360 | path += dir; |
| 361 | path += "/"; |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 362 | if (!isValid()) { |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 363 | path = save.path; |
| 364 | return false; |
| 365 | } |
| 366 | return true; |
| 367 | } |
| 368 | |
| 369 | bool |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 370 | Path::elideDirectory() { |
| 371 | if (isFile()) |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 372 | return false; |
| 373 | size_t slashpos = path.rfind('/',path.size()); |
| 374 | if (slashpos == 0 || slashpos == std::string::npos) |
| 375 | return false; |
| 376 | if (slashpos == path.size() - 1) |
| 377 | slashpos = path.rfind('/',slashpos-1); |
| 378 | if (slashpos == std::string::npos) |
| 379 | return false; |
| 380 | path.erase(slashpos); |
| 381 | return true; |
| 382 | } |
| 383 | |
| 384 | bool |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 385 | Path::appendFile(const std::string& file) { |
| 386 | if (!isDirectory()) |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 387 | return false; |
| 388 | Path save(*this); |
| 389 | path += file; |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 390 | if (!isValid()) { |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 391 | path = save.path; |
| 392 | return false; |
| 393 | } |
| 394 | return true; |
| 395 | } |
| 396 | |
| 397 | bool |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 398 | Path::elideFile() { |
| 399 | if (isDirectory()) |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 400 | return false; |
| 401 | size_t slashpos = path.rfind('/',path.size()); |
| 402 | if (slashpos == std::string::npos) |
| 403 | return false; |
| 404 | path.erase(slashpos+1); |
| 405 | return true; |
| 406 | } |
| 407 | |
| 408 | bool |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 409 | Path::appendSuffix(const std::string& suffix) { |
| 410 | if (isDirectory()) |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 411 | return false; |
| 412 | Path save(*this); |
| 413 | path.append("."); |
| 414 | path.append(suffix); |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 415 | if (!isValid()) { |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 416 | path = save.path; |
| 417 | return false; |
| 418 | } |
| 419 | return true; |
| 420 | } |
| 421 | |
| 422 | bool |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 423 | Path::elideSuffix() { |
| 424 | if (isDirectory()) return false; |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 425 | size_t dotpos = path.rfind('.',path.size()); |
| 426 | size_t slashpos = path.rfind('/',path.size()); |
| 427 | if (slashpos != std::string::npos && dotpos != std::string::npos && |
| 428 | dotpos > slashpos) { |
| 429 | path.erase(dotpos, path.size()-dotpos); |
| 430 | return true; |
| 431 | } |
| 432 | return false; |
| 433 | } |
| 434 | |
| 435 | |
| 436 | bool |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 437 | Path::createDirectory( bool create_parents) { |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 438 | // Make sure we're dealing with a directory |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 439 | if (!isDirectory()) return false; |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 440 | |
| 441 | // Get a writeable copy of the path name |
| 442 | char pathname[MAXPATHLEN]; |
| 443 | path.copy(pathname,MAXPATHLEN); |
| 444 | |
| 445 | // Null-terminate the last component |
| 446 | int lastchar = path.length() - 1 ; |
| 447 | if (pathname[lastchar] == '/') |
| 448 | pathname[lastchar] = 0; |
Reid Spencer | eaf1815 | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 449 | else |
| 450 | pathname[lastchar+1] = 0; |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 451 | |
| 452 | // If we're supposed to create intermediate directories |
| 453 | if ( create_parents ) { |
| 454 | // Find the end of the initial name component |
| 455 | char * next = strchr(pathname,'/'); |
| 456 | if ( pathname[0] == '/') |
| 457 | next = strchr(&pathname[1],'/'); |
| 458 | |
| 459 | // Loop through the directory components until we're done |
| 460 | while ( next != 0 ) { |
| 461 | *next = 0; |
| 462 | if (0 != access(pathname, F_OK | R_OK | W_OK)) |
| 463 | if (0 != mkdir(pathname, S_IRWXU | S_IRWXG)) |
| 464 | ThrowErrno(std::string(pathname) + ": Can't create directory"); |
| 465 | char* save = next; |
Reid Spencer | eaf1815 | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 466 | next = strchr(next+1,'/'); |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 467 | *save = '/'; |
| 468 | } |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 469 | } |
Reid Spencer | eaf1815 | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 470 | |
| 471 | if (0 != access(pathname, F_OK | R_OK)) |
| 472 | if (0 != mkdir(pathname, S_IRWXU | S_IRWXG)) |
| 473 | ThrowErrno(std::string(pathname) + ": Can't create directory"); |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 474 | return true; |
| 475 | } |
| 476 | |
| 477 | bool |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 478 | Path::createFile() { |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 479 | // Make sure we're dealing with a file |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 480 | if (!isFile()) return false; |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 481 | |
| 482 | // Create the file |
Reid Spencer | 622e220 | 2004-09-18 19:25:11 +0000 | [diff] [blame] | 483 | int fd = ::creat(path.c_str(), S_IRUSR | S_IWUSR); |
| 484 | if (fd < 0) |
Reid Spencer | 9195f37 | 2004-11-09 20:26:31 +0000 | [diff] [blame] | 485 | ThrowErrno(path + ": Can't create file"); |
Reid Spencer | 622e220 | 2004-09-18 19:25:11 +0000 | [diff] [blame] | 486 | ::close(fd); |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 487 | |
| 488 | return true; |
Reid Spencer | b89a223 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 491 | bool |
Reid Spencer | 9195f37 | 2004-11-09 20:26:31 +0000 | [diff] [blame] | 492 | Path::createTemporaryFile() { |
| 493 | // Make sure we're dealing with a file |
| 494 | if (!isFile()) return false; |
| 495 | |
| 496 | // Append the filename filler |
| 497 | char pathname[MAXPATHLEN]; |
| 498 | path.copy(pathname,MAXPATHLEN); |
Reid Spencer | eaf1815 | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 499 | pathname[path.length()] = 0; |
Reid Spencer | 9195f37 | 2004-11-09 20:26:31 +0000 | [diff] [blame] | 500 | strcat(pathname,"XXXXXX"); |
| 501 | int fd = ::mkstemp(pathname); |
| 502 | if (fd < 0) { |
| 503 | ThrowErrno(path + ": Can't create temporary file"); |
| 504 | } |
| 505 | path = pathname; |
| 506 | ::close(fd); |
| 507 | return true; |
| 508 | } |
| 509 | |
| 510 | bool |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 511 | Path::destroyDirectory(bool remove_contents) { |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 512 | // Make sure we're dealing with a directory |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 513 | if (!isDirectory()) return false; |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 514 | |
| 515 | // If it doesn't exist, we're done. |
| 516 | if (!exists()) return true; |
| 517 | |
| 518 | if (remove_contents) { |
| 519 | // Recursively descend the directory to remove its content |
| 520 | std::string cmd("/bin/rm -rf "); |
| 521 | cmd += path; |
| 522 | system(cmd.c_str()); |
| 523 | } else { |
| 524 | // Otherwise, try to just remove the one directory |
| 525 | char pathname[MAXPATHLEN]; |
| 526 | path.copy(pathname,MAXPATHLEN); |
| 527 | int lastchar = path.length() - 1 ; |
| 528 | if (pathname[lastchar] == '/') |
| 529 | pathname[lastchar] = 0; |
Reid Spencer | eaf1815 | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 530 | else |
| 531 | pathname[lastchar+1] = 0; |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 532 | if ( 0 != rmdir(pathname)) |
| 533 | ThrowErrno(std::string(pathname) + ": Can't destroy directory"); |
| 534 | } |
| 535 | return true; |
| 536 | } |
| 537 | |
| 538 | bool |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 539 | Path::destroyFile() { |
| 540 | if (!isFile()) return false; |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 541 | if (0 != unlink(path.c_str())) |
Reid Spencer | eaf1815 | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 542 | ThrowErrno(path + ": Can't destroy file"); |
| 543 | return true; |
| 544 | } |
| 545 | |
| 546 | bool |
| 547 | Path::renameFile(const Path& newName) { |
| 548 | if (!isFile()) return false; |
| 549 | if (0 != rename(path.c_str(), newName.c_str())) |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 550 | ThrowErrno(std::string("can't rename ") + path + " as " + |
| 551 | newName.toString()); |
Reid Spencer | eaf1815 | 2004-11-14 22:08:36 +0000 | [diff] [blame] | 552 | return true; |
| 553 | } |
| 554 | |
| 555 | bool |
| 556 | Path::setStatusInfo(const StatusInfo& si) const { |
| 557 | if (!isFile()) return false; |
| 558 | struct utimbuf utb; |
| 559 | utb.actime = si.modTime.toPosixTime(); |
| 560 | utb.modtime = utb.actime; |
| 561 | if (0 != ::utime(path.c_str(),&utb)) |
| 562 | ThrowErrno(path + ": can't set file modification time"); |
| 563 | if (0 != ::chmod(path.c_str(),si.mode)) |
| 564 | ThrowErrno(path + ": can't set mode"); |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 565 | return true; |
| 566 | } |
| 567 | |
Reid Spencer | b89a223 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | // vim: sw=2 |