Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 1 | //===- llvm/System/Linux/Path.cpp - Linux Path Implementation ---*- C++ -*-===// |
| 2 | // |
Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 5 | // This file was developed by Reid Spencer and is distributed under the |
Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 7 | // |
| 8 | // Modified by Henrik Bach to comply with at least MinGW. |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 9 | // Ported to Win32 by Jeff Cohen. |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 10 | // |
Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 11 | //===----------------------------------------------------------------------===// |
| 12 | // |
| 13 | // This file provides the Win32 specific implementation of the Path class. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | //===----------------------------------------------------------------------===// |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 18 | //=== WARNING: Implementation here must contain only generic Win32 code that |
| 19 | //=== is guaranteed to work on *all* Win32 variants. |
Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 20 | //===----------------------------------------------------------------------===// |
| 21 | |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 22 | #include "Win32.h" |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 23 | #include <fstream> |
| 24 | #include <malloc.h> |
Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 25 | |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 26 | static void FlipBackSlashes(std::string& s) { |
| 27 | for (size_t i = 0; i < s.size(); i++) |
| 28 | if (s[i] == '\\') |
| 29 | s[i] = '/'; |
| 30 | } |
| 31 | |
Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 32 | namespace llvm { |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 33 | namespace sys { |
Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 34 | |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 35 | bool |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 36 | Path::isValid() const { |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 37 | if (path.empty()) |
| 38 | return false; |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 39 | |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 40 | // If there is a colon, it must be the second character, preceded by a letter |
| 41 | // and followed by something. |
| 42 | size_t len = path.size(); |
| 43 | size_t pos = path.rfind(':',len); |
| 44 | if (pos != std::string::npos) { |
| 45 | if (pos != 1 || !isalpha(path[0]) || len < 3) |
| 46 | return false; |
| 47 | } |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 48 | |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 49 | // Check for illegal characters. |
| 50 | if (path.find_first_of("\\<>\"|\001\002\003\004\005\006\007\010\011\012" |
| 51 | "\013\014\015\016\017\020\021\022\023\024\025\026" |
| 52 | "\027\030\031\032\033\034\035\036\037") |
| 53 | != std::string::npos) |
| 54 | return false; |
| 55 | |
| 56 | // A file or directory name may not end in a period. |
| 57 | if (path[len-1] == '.') |
| 58 | return false; |
| 59 | if (len >= 2 && path[len-2] == '.' && path[len-1] == '/') |
| 60 | return false; |
| 61 | |
| 62 | // A file or directory name may not end in a space. |
| 63 | if (path[len-1] == ' ') |
| 64 | return false; |
| 65 | if (len >= 2 && path[len-2] == ' ' && path[len-1] == '/') |
| 66 | return false; |
| 67 | |
| 68 | return true; |
Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 71 | static Path *TempDirectory = NULL; |
| 72 | |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 73 | Path |
| 74 | Path::GetTemporaryDirectory() { |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 75 | if (TempDirectory) |
| 76 | return *TempDirectory; |
| 77 | |
| 78 | char pathname[MAX_PATH]; |
| 79 | if (!GetTempPath(MAX_PATH, pathname)) |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 80 | throw std::string("Can't determine temporary directory"); |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 81 | |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 82 | Path result; |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 83 | result.setDirectory(pathname); |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 84 | |
| 85 | // Append a subdirectory passed on our process id so multiple LLVMs don't |
| 86 | // step on each other's toes. |
| 87 | sprintf(pathname, "LLVM_%u", GetCurrentProcessId()); |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 88 | result.appendDirectory(pathname); |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 89 | |
| 90 | // If there's a directory left over from a previous LLVM execution that |
| 91 | // happened to have the same process id, get rid of it. |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 92 | result.destroyDirectory(true); |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 93 | |
| 94 | // And finally (re-)create the empty directory. |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 95 | result.createDirectory(false); |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 96 | TempDirectory = new Path(result); |
| 97 | return *TempDirectory; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | Path::Path(std::string unverified_path) |
| 101 | : path(unverified_path) |
| 102 | { |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 103 | FlipBackSlashes(path); |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 104 | if (unverified_path.empty()) |
| 105 | return; |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 106 | if (this->isValid()) |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 107 | return; |
| 108 | // oops, not valid. |
| 109 | path.clear(); |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 110 | throw std::string(unverified_path + ": path is not valid"); |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 113 | // FIXME: the following set of functions don't map to Windows very well. |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 114 | Path |
| 115 | Path::GetRootDirectory() { |
| 116 | Path result; |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 117 | result.setDirectory("/"); |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 118 | return result; |
| 119 | } |
| 120 | |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 121 | std::string |
| 122 | Path::GetDLLSuffix() { |
| 123 | return "dll"; |
| 124 | } |
| 125 | |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 126 | static inline bool IsLibrary(Path& path, const std::string& basename) { |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 127 | if (path.appendFile(std::string("lib") + basename)) { |
| 128 | if (path.appendSuffix(Path::GetDLLSuffix()) && path.readable()) |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 129 | return true; |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 130 | else if (path.elideSuffix() && path.appendSuffix("a") && path.readable()) |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 131 | return true; |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 132 | else if (path.elideSuffix() && path.appendSuffix("o") && path.readable()) |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 133 | return true; |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 134 | else if (path.elideSuffix() && path.appendSuffix("bc") && path.readable()) |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 135 | return true; |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 136 | } else if (path.elideFile() && path.appendFile(basename)) { |
| 137 | if (path.appendSuffix(Path::GetDLLSuffix()) && path.readable()) |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 138 | return true; |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 139 | else if (path.elideSuffix() && path.appendSuffix("a") && path.readable()) |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 140 | return true; |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 141 | else if (path.elideSuffix() && path.appendSuffix("o") && path.readable()) |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 142 | return true; |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 143 | else if (path.elideSuffix() && path.appendSuffix("bc") && path.readable()) |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 144 | return true; |
| 145 | } |
| 146 | path.clear(); |
| 147 | return false; |
| 148 | } |
| 149 | |
| 150 | Path |
| 151 | Path::GetLibraryPath(const std::string& basename, |
| 152 | const std::vector<std::string>& LibPaths) { |
| 153 | Path result; |
| 154 | |
| 155 | // Try the paths provided |
| 156 | for (std::vector<std::string>::const_iterator I = LibPaths.begin(), |
| 157 | E = LibPaths.end(); I != E; ++I ) { |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 158 | if (result.setDirectory(*I) && IsLibrary(result,basename)) |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 159 | return result; |
| 160 | } |
| 161 | |
| 162 | // Try the LLVM lib directory in the LLVM install area |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 163 | //if (result.setDirectory(LLVM_LIBDIR) && IsLibrary(result,basename)) |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 164 | // return result; |
| 165 | |
| 166 | // Try /usr/lib |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 167 | if (result.setDirectory("/usr/lib/") && IsLibrary(result,basename)) |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 168 | return result; |
| 169 | |
| 170 | // Try /lib |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 171 | if (result.setDirectory("/lib/") && IsLibrary(result,basename)) |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 172 | return result; |
| 173 | |
| 174 | // Can't find it, give up and return invalid path. |
| 175 | result.clear(); |
| 176 | return result; |
| 177 | } |
| 178 | |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 179 | Path |
| 180 | Path::GetSystemLibraryPath1() { |
| 181 | return Path("/lib/"); |
| 182 | } |
| 183 | |
| 184 | Path |
| 185 | Path::GetSystemLibraryPath2() { |
| 186 | return Path("/usr/lib/"); |
| 187 | } |
| 188 | |
| 189 | Path |
| 190 | Path::GetLLVMDefaultConfigDir() { |
| 191 | return Path("/etc/llvm/"); |
| 192 | } |
| 193 | |
| 194 | Path |
| 195 | Path::GetLLVMConfigDir() { |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 196 | return GetLLVMDefaultConfigDir(); |
| 197 | } |
| 198 | |
| 199 | Path |
| 200 | Path::GetUserHomeDirectory() { |
| 201 | const char* home = getenv("HOME"); |
| 202 | if (home) { |
| 203 | Path result; |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 204 | if (result.setDirectory(home)) |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 205 | return result; |
| 206 | } |
| 207 | return GetRootDirectory(); |
| 208 | } |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 209 | // FIXME: the above set of functions don't map to Windows very well. |
| 210 | |
| 211 | bool |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 212 | Path::isFile() const { |
| 213 | return (isValid() && path[path.length()-1] != '/'); |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | bool |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 217 | Path::isDirectory() const { |
| 218 | return (isValid() && path[path.length()-1] == '/'); |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | std::string |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 222 | Path::getBasename() const { |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 223 | // Find the last slash |
| 224 | size_t slash = path.rfind('/'); |
| 225 | if (slash == std::string::npos) |
| 226 | slash = 0; |
| 227 | else |
| 228 | slash++; |
| 229 | |
| 230 | return path.substr(slash, path.rfind('.')); |
| 231 | } |
| 232 | |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 233 | bool Path::hasMagicNumber(const std::string &Magic) const { |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 234 | size_t len = Magic.size(); |
| 235 | char *buf = reinterpret_cast<char *>(_alloca(len+1)); |
| 236 | std::ifstream f(path.c_str()); |
| 237 | f.read(buf, len); |
| 238 | buf[len] = '\0'; |
| 239 | return Magic == buf; |
| 240 | } |
| 241 | |
| 242 | bool |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 243 | Path::isBytecodeFile() const { |
Reid Spencer | 4b82681 | 2004-11-09 20:27:23 +0000 | [diff] [blame] | 244 | char buffer[ 4]; |
| 245 | buffer[0] = 0; |
| 246 | std::ifstream f(path.c_str()); |
| 247 | f.read(buffer, 4); |
| 248 | if (f.bad()) |
| 249 | ThrowErrno("can't read file signature"); |
| 250 | return 0 == memcmp(buffer,"llvc",4) || 0 == memcmp(buffer,"llvm",4); |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | bool |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 254 | Path::isArchive() const { |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 255 | if (readable()) { |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 256 | return hasMagicNumber("!<arch>\012"); |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 257 | } |
| 258 | return false; |
| 259 | } |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 260 | |
| 261 | bool |
| 262 | Path::exists() const { |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 263 | DWORD attr = GetFileAttributes(path.c_str()); |
| 264 | return attr != INVALID_FILE_ATTRIBUTES; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | bool |
| 268 | Path::readable() const { |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 269 | // FIXME: take security attributes into account. |
| 270 | DWORD attr = GetFileAttributes(path.c_str()); |
| 271 | return attr != INVALID_FILE_ATTRIBUTES; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | bool |
| 275 | Path::writable() const { |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 276 | // FIXME: take security attributes into account. |
| 277 | DWORD attr = GetFileAttributes(path.c_str()); |
| 278 | return (attr != INVALID_FILE_ATTRIBUTES) && !(attr & FILE_ATTRIBUTE_READONLY); |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | bool |
| 282 | Path::executable() const { |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 283 | // FIXME: take security attributes into account. |
| 284 | DWORD attr = GetFileAttributes(path.c_str()); |
| 285 | return attr != INVALID_FILE_ATTRIBUTES; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | std::string |
| 289 | Path::getLast() const { |
| 290 | // Find the last slash |
| 291 | size_t pos = path.rfind('/'); |
| 292 | |
| 293 | // Handle the corner cases |
| 294 | if (pos == std::string::npos) |
| 295 | return path; |
| 296 | |
| 297 | // If the last character is a slash |
| 298 | if (pos == path.length()-1) { |
| 299 | // Find the second to last slash |
| 300 | size_t pos2 = path.rfind('/', pos-1); |
| 301 | if (pos2 == std::string::npos) |
| 302 | return path.substr(0,pos); |
| 303 | else |
| 304 | return path.substr(pos2+1,pos-pos2-1); |
| 305 | } |
| 306 | // Return everything after the last slash |
| 307 | return path.substr(pos+1); |
| 308 | } |
| 309 | |
| 310 | bool |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 311 | Path::setDirectory(const std::string& a_path) { |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 312 | if (a_path.size() == 0) |
| 313 | return false; |
| 314 | Path save(*this); |
| 315 | path = a_path; |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 316 | FlipBackSlashes(path); |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 317 | size_t last = a_path.size() -1; |
| 318 | if (last != 0 && a_path[last] != '/') |
| 319 | path += '/'; |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 320 | if (!isValid()) { |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 321 | path = save.path; |
| 322 | return false; |
| 323 | } |
| 324 | return true; |
| 325 | } |
| 326 | |
| 327 | bool |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 328 | Path::setFile(const std::string& a_path) { |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 329 | if (a_path.size() == 0) |
| 330 | return false; |
| 331 | Path save(*this); |
| 332 | path = a_path; |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 333 | FlipBackSlashes(path); |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 334 | size_t last = a_path.size() - 1; |
| 335 | while (last > 0 && a_path[last] == '/') |
| 336 | last--; |
| 337 | path.erase(last+1); |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 338 | if (!isValid()) { |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 339 | path = save.path; |
| 340 | return false; |
| 341 | } |
| 342 | return true; |
| 343 | } |
| 344 | |
| 345 | bool |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 346 | Path::appendDirectory(const std::string& dir) { |
| 347 | if (isFile()) |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 348 | return false; |
| 349 | Path save(*this); |
| 350 | path += dir; |
| 351 | path += "/"; |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 352 | if (!isValid()) { |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 353 | path = save.path; |
| 354 | return false; |
| 355 | } |
| 356 | return true; |
| 357 | } |
| 358 | |
| 359 | bool |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 360 | Path::elideDirectory() { |
| 361 | if (isFile()) |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 362 | return false; |
| 363 | size_t slashpos = path.rfind('/',path.size()); |
| 364 | if (slashpos == 0 || slashpos == std::string::npos) |
| 365 | return false; |
| 366 | if (slashpos == path.size() - 1) |
| 367 | slashpos = path.rfind('/',slashpos-1); |
| 368 | if (slashpos == std::string::npos) |
| 369 | return false; |
| 370 | path.erase(slashpos); |
| 371 | return true; |
| 372 | } |
| 373 | |
| 374 | bool |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 375 | Path::appendFile(const std::string& file) { |
| 376 | if (!isDirectory()) |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 377 | return false; |
| 378 | Path save(*this); |
| 379 | path += file; |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 380 | if (!isValid()) { |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 381 | path = save.path; |
| 382 | return false; |
| 383 | } |
| 384 | return true; |
| 385 | } |
| 386 | |
| 387 | bool |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 388 | Path::elideFile() { |
| 389 | if (isDirectory()) |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 390 | return false; |
| 391 | size_t slashpos = path.rfind('/',path.size()); |
| 392 | if (slashpos == std::string::npos) |
| 393 | return false; |
| 394 | path.erase(slashpos+1); |
| 395 | return true; |
| 396 | } |
| 397 | |
| 398 | bool |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 399 | Path::appendSuffix(const std::string& suffix) { |
| 400 | if (isDirectory()) |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 401 | return false; |
| 402 | Path save(*this); |
| 403 | path.append("."); |
| 404 | path.append(suffix); |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 405 | if (!isValid()) { |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 406 | path = save.path; |
| 407 | return false; |
| 408 | } |
| 409 | return true; |
| 410 | } |
| 411 | |
| 412 | bool |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 413 | Path::elideSuffix() { |
| 414 | if (isDirectory()) return false; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 415 | size_t dotpos = path.rfind('.',path.size()); |
| 416 | size_t slashpos = path.rfind('/',path.size()); |
| 417 | if (slashpos != std::string::npos && dotpos != std::string::npos && |
| 418 | dotpos > slashpos) { |
| 419 | path.erase(dotpos, path.size()-dotpos); |
| 420 | return true; |
| 421 | } |
| 422 | return false; |
| 423 | } |
| 424 | |
| 425 | |
| 426 | bool |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 427 | Path::createDirectory( bool create_parents) { |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 428 | // Make sure we're dealing with a directory |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 429 | if (!isDirectory()) return false; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 430 | |
| 431 | // Get a writeable copy of the path name |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 432 | char *pathname = reinterpret_cast<char *>(_alloca(path.length()+1)); |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 433 | path.copy(pathname,path.length()); |
| 434 | pathname[path.length()] = 0; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 435 | |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 436 | // Determine starting point for initial / search. |
| 437 | char *next = pathname; |
| 438 | if (pathname[0] == '/' && pathname[1] == '/') { |
| 439 | // Skip host name. |
| 440 | next = strchr(pathname+2, '/'); |
| 441 | if (next == NULL) |
| 442 | throw std::string(pathname) + ": badly formed remote directory"; |
| 443 | // Skip share name. |
| 444 | next = strchr(next+1, '/'); |
| 445 | if (next == NULL) |
| 446 | throw std::string(pathname) + ": badly formed remote directory"; |
| 447 | next++; |
| 448 | if (*next == 0) |
| 449 | throw std::string(pathname) + ": badly formed remote directory"; |
| 450 | } else { |
| 451 | if (pathname[1] == ':') |
| 452 | next += 2; // skip drive letter |
| 453 | if (*next == '/') |
| 454 | next++; // skip root directory |
| 455 | } |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 456 | |
| 457 | // If we're supposed to create intermediate directories |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 458 | if (create_parents) { |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 459 | // Loop through the directory components until we're done |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 460 | while (*next) { |
| 461 | next = strchr(next, '/'); |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 462 | *next = 0; |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 463 | if (!CreateDirectory(pathname, NULL)) |
| 464 | ThrowError(std::string(pathname) + ": Can't create directory: "); |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 465 | *next++ = '/'; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 466 | } |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 467 | } else { |
| 468 | // Drop trailing slash. |
| 469 | pathname[path.size()-1] = 0; |
| 470 | if (!CreateDirectory(pathname, NULL)) { |
| 471 | ThrowError(std::string(pathname) + ": Can't create directory: "); |
| 472 | } |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 473 | } |
| 474 | return true; |
| 475 | } |
| 476 | |
| 477 | bool |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 478 | Path::createFile() { |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +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 | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 481 | |
| 482 | // Create the file |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 483 | HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW, |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 484 | FILE_ATTRIBUTE_NORMAL, NULL); |
| 485 | if (h == INVALID_HANDLE_VALUE) |
| 486 | ThrowError(std::string(path.c_str()) + ": Can't create file: "); |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 487 | |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 488 | CloseHandle(h); |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 489 | return true; |
| 490 | } |
| 491 | |
| 492 | bool |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 493 | Path::destroyDirectory(bool remove_contents) { |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 494 | // Make sure we're dealing with a directory |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 495 | if (!isDirectory()) return false; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 496 | |
| 497 | // If it doesn't exist, we're done. |
| 498 | if (!exists()) return true; |
| 499 | |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 500 | char *pathname = reinterpret_cast<char *>(_alloca(path.length()+1)); |
| 501 | path.copy(pathname,path.length()+1); |
| 502 | int lastchar = path.length() - 1 ; |
| 503 | if (pathname[lastchar] == '/') |
| 504 | pathname[lastchar] = 0; |
| 505 | |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 506 | if (remove_contents) { |
| 507 | // Recursively descend the directory to remove its content |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 508 | // FIXME: The correct way of doing this on Windows isn't pretty... |
| 509 | // but this may work if unix-like utils are present. |
| 510 | std::string cmd("rm -rf "); |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 511 | cmd += path; |
| 512 | system(cmd.c_str()); |
| 513 | } else { |
| 514 | // Otherwise, try to just remove the one directory |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 515 | if (!RemoveDirectory(pathname)) |
| 516 | ThrowError(std::string(pathname) + ": Can't destroy directory: "); |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 517 | } |
| 518 | return true; |
| 519 | } |
| 520 | |
| 521 | bool |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 522 | Path::destroyFile() { |
| 523 | if (!isFile()) return false; |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 524 | |
| 525 | DWORD attr = GetFileAttributes(path.c_str()); |
| 526 | |
| 527 | // If it doesn't exist, we're done. |
| 528 | if (attr == INVALID_FILE_ATTRIBUTES) |
| 529 | return true; |
| 530 | |
| 531 | // Read-only files cannot be deleted on Windows. Must remove the read-only |
| 532 | // attribute first. |
| 533 | if (attr & FILE_ATTRIBUTE_READONLY) { |
| 534 | if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY)) |
| 535 | ThrowError(std::string(path.c_str()) + ": Can't destroy file: "); |
| 536 | } |
| 537 | |
| 538 | if (!DeleteFile(path.c_str())) |
| 539 | ThrowError(std::string(path.c_str()) + ": Can't destroy file: "); |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 540 | return true; |
| 541 | } |
| 542 | |
| 543 | } |
Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 547 | |