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