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 <malloc.h> |
Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 24 | |
Jeff Cohen | cb65255 | 2004-12-24 02:38:34 +0000 | [diff] [blame] | 25 | // We need to undo a macro defined in Windows.h, otherwise we won't compile: |
| 26 | #undef CopyFile |
| 27 | |
Jeff Cohen | 966fa41 | 2005-07-09 18:42:49 +0000 | [diff] [blame] | 28 | // Windows happily accepts either forward or backward slashes, though any path |
| 29 | // returned by a Win32 API will have backward slashes. As LLVM code basically |
| 30 | // assumes forward slashes are used, backward slashs are converted where they |
| 31 | // can be introduced into a path. |
| 32 | // |
| 33 | // Another invariant is that a path ends with a slash if and only if the path |
| 34 | // is a root directory. Any other use of a trailing slash is stripped. Unlike |
| 35 | // in Unix, Windows has a rather complicated notion of a root path and this |
| 36 | // invariant helps simply the code. |
| 37 | |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 38 | static void FlipBackSlashes(std::string& s) { |
| 39 | for (size_t i = 0; i < s.size(); i++) |
| 40 | if (s[i] == '\\') |
| 41 | s[i] = '/'; |
| 42 | } |
| 43 | |
Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 44 | namespace llvm { |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 45 | namespace sys { |
Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 46 | |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 47 | bool |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 48 | Path::isValid() const { |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 49 | if (path.empty()) |
| 50 | return false; |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 51 | |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 52 | // If there is a colon, it must be the second character, preceded by a letter |
| 53 | // and followed by something. |
| 54 | size_t len = path.size(); |
| 55 | size_t pos = path.rfind(':',len); |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 56 | size_t rootslash = 0; |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 57 | if (pos != std::string::npos) { |
| 58 | if (pos != 1 || !isalpha(path[0]) || len < 3) |
| 59 | return false; |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 60 | rootslash = 2; |
| 61 | } |
Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 62 | |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 63 | // Look for a UNC path, and if found adjust our notion of the root slash. |
| 64 | if (len > 3 && path[0] == '/' && path[1] == '/') { |
| 65 | rootslash = path.find('/', 2); |
| 66 | if (rootslash == std::string::npos) |
| 67 | rootslash = 0; |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 68 | } |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 69 | |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 70 | // Check for illegal characters. |
| 71 | if (path.find_first_of("\\<>\"|\001\002\003\004\005\006\007\010\011\012" |
| 72 | "\013\014\015\016\017\020\021\022\023\024\025\026" |
| 73 | "\027\030\031\032\033\034\035\036\037") |
| 74 | != std::string::npos) |
| 75 | return false; |
Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 76 | |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 77 | // Remove trailing slash, unless it's a root slash. |
| 78 | if (len > rootslash+1 && path[len-1] == '/') |
| 79 | path.erase(--len); |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 80 | |
Jeff Cohen | 3bbbcc1 | 2005-01-14 04:09:39 +0000 | [diff] [blame] | 81 | // Check each component for legality. |
| 82 | for (pos = 0; pos < len; ++pos) { |
| 83 | // A component may not end in a space. |
| 84 | if (path[pos] == ' ') { |
| 85 | if (path[pos+1] == '/' || path[pos+1] == '\0') |
| 86 | return false; |
| 87 | } |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 88 | |
Jeff Cohen | 3bbbcc1 | 2005-01-14 04:09:39 +0000 | [diff] [blame] | 89 | // A component may not end in a period. |
| 90 | if (path[pos] == '.') { |
| 91 | if (path[pos+1] == '/' || path[pos+1] == '\0') { |
| 92 | // Unless it is the pseudo-directory "."... |
| 93 | if (pos == 0 || path[pos-1] == '/' || path[pos-1] == ':') |
| 94 | return true; |
| 95 | // or "..". |
| 96 | if (pos > 0 && path[pos-1] == '.') { |
| 97 | if (pos == 1 || path[pos-2] == '/' || path[pos-2] == ':') |
| 98 | return true; |
| 99 | } |
| 100 | return false; |
| 101 | } |
| 102 | } |
| 103 | } |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 104 | |
| 105 | return true; |
Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Reid Spencer | 69cce81 | 2007-03-29 16:43:20 +0000 | [diff] [blame] | 108 | bool |
| 109 | Path::isAbsolute() const { |
| 110 | if (path.length() < 3) |
| 111 | return false; |
| 112 | return path[0] == 'C' && path[1] == ':' && path[2] == '\\'; |
| 113 | } |
| 114 | |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 115 | static Path *TempDirectory = NULL; |
| 116 | |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 117 | Path |
Reid Spencer | cab0e43 | 2006-08-22 22:46:39 +0000 | [diff] [blame] | 118 | Path::GetTemporaryDirectory(std::string* ErrMsg) { |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 119 | if (TempDirectory) |
| 120 | return *TempDirectory; |
| 121 | |
| 122 | char pathname[MAX_PATH]; |
Reid Spencer | cab0e43 | 2006-08-22 22:46:39 +0000 | [diff] [blame] | 123 | if (!GetTempPath(MAX_PATH, pathname)) { |
| 124 | if (ErrMsg) |
| 125 | *ErrMsg = "Can't determine temporary directory"; |
| 126 | return Path(); |
| 127 | } |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 128 | |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 129 | Path result; |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 130 | result.set(pathname); |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 131 | |
| 132 | // Append a subdirectory passed on our process id so multiple LLVMs don't |
| 133 | // step on each other's toes. |
Jeff Cohen | d41b30d | 2006-11-05 19:31:28 +0000 | [diff] [blame] | 134 | #ifdef __MINGW32__ |
| 135 | // Mingw's Win32 header files are broken. |
Reid Spencer | ab4d9b0 | 2006-06-08 18:08:43 +0000 | [diff] [blame] | 136 | sprintf(pathname, "LLVM_%u", unsigned(GetCurrentProcessId())); |
Jeff Cohen | d41b30d | 2006-11-05 19:31:28 +0000 | [diff] [blame] | 137 | #else |
| 138 | sprintf(pathname, "LLVM_%u", GetCurrentProcessId()); |
| 139 | #endif |
Jeff Cohen | edb9d6b | 2005-07-08 02:48:42 +0000 | [diff] [blame] | 140 | result.appendComponent(pathname); |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 141 | |
| 142 | // If there's a directory left over from a previous LLVM execution that |
| 143 | // happened to have the same process id, get rid of it. |
Reid Spencer | a229c5c | 2005-07-08 03:08:58 +0000 | [diff] [blame] | 144 | result.eraseFromDisk(true); |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 145 | |
| 146 | // And finally (re-)create the empty directory. |
Reid Spencer | a229c5c | 2005-07-08 03:08:58 +0000 | [diff] [blame] | 147 | result.createDirectoryOnDisk(false); |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 148 | TempDirectory = new Path(result); |
| 149 | return *TempDirectory; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 152 | // 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] | 153 | Path |
| 154 | Path::GetRootDirectory() { |
| 155 | Path result; |
Jeff Cohen | 966fa41 | 2005-07-09 18:42:49 +0000 | [diff] [blame] | 156 | result.set("C:/"); |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 157 | return result; |
| 158 | } |
| 159 | |
Reid Spencer | 6c4b7bd | 2004-12-13 03:03:42 +0000 | [diff] [blame] | 160 | static void getPathList(const char*path, std::vector<sys::Path>& Paths) { |
| 161 | const char* at = path; |
| 162 | const char* delim = strchr(at, ';'); |
| 163 | Path tmpPath; |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 164 | while (delim != 0) { |
Reid Spencer | 6c4b7bd | 2004-12-13 03:03:42 +0000 | [diff] [blame] | 165 | std::string tmp(at, size_t(delim-at)); |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 166 | if (tmpPath.set(tmp)) |
Reid Spencer | c7f0832 | 2005-07-07 18:21:42 +0000 | [diff] [blame] | 167 | if (tmpPath.canRead()) |
Reid Spencer | 6c4b7bd | 2004-12-13 03:03:42 +0000 | [diff] [blame] | 168 | Paths.push_back(tmpPath); |
| 169 | at = delim + 1; |
| 170 | delim = strchr(at, ';'); |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 171 | } |
Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 172 | |
Reid Spencer | 6c4b7bd | 2004-12-13 03:03:42 +0000 | [diff] [blame] | 173 | if (*at != 0) |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 174 | if (tmpPath.set(std::string(at))) |
Reid Spencer | c7f0832 | 2005-07-07 18:21:42 +0000 | [diff] [blame] | 175 | if (tmpPath.canRead()) |
Reid Spencer | 6c4b7bd | 2004-12-13 03:03:42 +0000 | [diff] [blame] | 176 | Paths.push_back(tmpPath); |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 179 | void |
Reid Spencer | 6c4b7bd | 2004-12-13 03:03:42 +0000 | [diff] [blame] | 180 | Path::GetSystemLibraryPaths(std::vector<sys::Path>& Paths) { |
Jeff Cohen | 966fa41 | 2005-07-09 18:42:49 +0000 | [diff] [blame] | 181 | Paths.push_back(sys::Path("C:/WINDOWS/SYSTEM32")); |
| 182 | Paths.push_back(sys::Path("C:/WINDOWS")); |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Reid Spencer | 6c4b7bd | 2004-12-13 03:03:42 +0000 | [diff] [blame] | 185 | void |
| 186 | Path::GetBytecodeLibraryPaths(std::vector<sys::Path>& Paths) { |
| 187 | char * env_var = getenv("LLVM_LIB_SEARCH_PATH"); |
| 188 | if (env_var != 0) { |
| 189 | getPathList(env_var,Paths); |
| 190 | } |
Reid Spencer | 6c4b7bd | 2004-12-13 03:03:42 +0000 | [diff] [blame] | 191 | #ifdef LLVM_LIBDIR |
| 192 | { |
| 193 | Path tmpPath; |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 194 | if (tmpPath.set(LLVM_LIBDIR)) |
Reid Spencer | c7f0832 | 2005-07-07 18:21:42 +0000 | [diff] [blame] | 195 | if (tmpPath.canRead()) |
Reid Spencer | 6c4b7bd | 2004-12-13 03:03:42 +0000 | [diff] [blame] | 196 | Paths.push_back(tmpPath); |
| 197 | } |
| 198 | #endif |
| 199 | GetSystemLibraryPaths(Paths); |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | Path |
| 203 | Path::GetLLVMDefaultConfigDir() { |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 204 | // TODO: this isn't going to fly on Windows |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 205 | return Path("/etc/llvm"); |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | Path |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 209 | Path::GetUserHomeDirectory() { |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 210 | // TODO: Typical Windows setup doesn't define HOME. |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 211 | const char* home = getenv("HOME"); |
| 212 | if (home) { |
| 213 | Path result; |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 214 | if (result.set(home)) |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 215 | return result; |
| 216 | } |
| 217 | return GetRootDirectory(); |
| 218 | } |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 219 | // FIXME: the above set of functions don't map to Windows very well. |
| 220 | |
Jeff Cohen | 966fa41 | 2005-07-09 18:42:49 +0000 | [diff] [blame] | 221 | |
| 222 | bool |
| 223 | Path::isRootDirectory() const { |
| 224 | size_t len = path.size(); |
| 225 | return len > 0 && path[len-1] == '/'; |
Reid Spencer | a229c5c | 2005-07-08 03:08:58 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 228 | std::string |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 229 | Path::getBasename() const { |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 230 | // Find the last slash |
| 231 | size_t slash = path.rfind('/'); |
| 232 | if (slash == std::string::npos) |
| 233 | slash = 0; |
| 234 | else |
| 235 | slash++; |
| 236 | |
Jeff Cohen | 966fa41 | 2005-07-09 18:42:49 +0000 | [diff] [blame] | 237 | size_t dot = path.rfind('.'); |
| 238 | if (dot == std::string::npos || dot < slash) |
| 239 | return path.substr(slash); |
| 240 | else |
| 241 | return path.substr(slash, dot - slash); |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 244 | bool Path::hasMagicNumber(const std::string &Magic) const { |
Jeff Cohen | 51b8d21 | 2004-12-31 19:01:08 +0000 | [diff] [blame] | 245 | std::string actualMagic; |
| 246 | if (getMagicNumber(actualMagic, Magic.size())) |
| 247 | return Magic == actualMagic; |
| 248 | return false; |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 251 | bool |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 252 | Path::isBytecodeFile() const { |
Jeff Cohen | 51b8d21 | 2004-12-31 19:01:08 +0000 | [diff] [blame] | 253 | std::string actualMagic; |
| 254 | if (!getMagicNumber(actualMagic, 4)) |
| 255 | return false; |
| 256 | return actualMagic == "llvc" || actualMagic == "llvm"; |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | bool |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 260 | Path::exists() const { |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 261 | DWORD attr = GetFileAttributes(path.c_str()); |
| 262 | return attr != INVALID_FILE_ATTRIBUTES; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | bool |
Reid Spencer | c7f0832 | 2005-07-07 18:21:42 +0000 | [diff] [blame] | 266 | Path::canRead() const { |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 267 | // FIXME: take security attributes into account. |
| 268 | DWORD attr = GetFileAttributes(path.c_str()); |
| 269 | return attr != INVALID_FILE_ATTRIBUTES; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | bool |
Reid Spencer | c7f0832 | 2005-07-07 18:21:42 +0000 | [diff] [blame] | 273 | Path::canWrite() const { |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 274 | // FIXME: take security attributes into account. |
| 275 | DWORD attr = GetFileAttributes(path.c_str()); |
| 276 | return (attr != INVALID_FILE_ATTRIBUTES) && !(attr & FILE_ATTRIBUTE_READONLY); |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | bool |
Reid Spencer | c7f0832 | 2005-07-07 18:21:42 +0000 | [diff] [blame] | 280 | Path::canExecute() const { |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 281 | // FIXME: take security attributes into account. |
| 282 | DWORD attr = GetFileAttributes(path.c_str()); |
| 283 | return attr != INVALID_FILE_ATTRIBUTES; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | std::string |
| 287 | Path::getLast() const { |
| 288 | // Find the last slash |
| 289 | size_t pos = path.rfind('/'); |
| 290 | |
| 291 | // Handle the corner cases |
| 292 | if (pos == std::string::npos) |
| 293 | return path; |
| 294 | |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 295 | // If the last character is a slash, we have a root directory |
| 296 | if (pos == path.length()-1) |
| 297 | return path; |
| 298 | |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 299 | // Return everything after the last slash |
| 300 | return path.substr(pos+1); |
| 301 | } |
| 302 | |
Chris Lattner | 7dea101 | 2006-07-28 22:32:09 +0000 | [diff] [blame] | 303 | bool |
Reid Spencer | 69cce81 | 2007-03-29 16:43:20 +0000 | [diff] [blame] | 304 | Path::getFileStatus(FileStatus &info, bool update, std::string *ErrStr) const { |
| 305 | if (status == 0 || update) { |
| 306 | WIN32_FILE_ATTRIBUTE_DATA fi; |
| 307 | if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi)) |
| 308 | return MakeErrMsg(ErrStr, "getStatusInfo():" + std::string(path) + |
| 309 | ": Can't get status: "); |
Jeff Cohen | 626e38e | 2004-12-14 05:26:43 +0000 | [diff] [blame] | 310 | |
Reid Spencer | 69cce81 | 2007-03-29 16:43:20 +0000 | [diff] [blame] | 311 | if (status == 0) |
| 312 | status = new FileStatus; |
Jeff Cohen | 626e38e | 2004-12-14 05:26:43 +0000 | [diff] [blame] | 313 | |
Reid Spencer | 69cce81 | 2007-03-29 16:43:20 +0000 | [diff] [blame] | 314 | status->fileSize = fi.nFileSizeHigh; |
| 315 | status->fileSize <<= sizeof(fi.nFileSizeHigh)*8; |
| 316 | status->fileSize += fi.nFileSizeLow; |
Jeff Cohen | 626e38e | 2004-12-14 05:26:43 +0000 | [diff] [blame] | 317 | |
Reid Spencer | 69cce81 | 2007-03-29 16:43:20 +0000 | [diff] [blame] | 318 | status->mode = fi.dwFileAttributes & FILE_ATTRIBUTE_READONLY ? 0555 : 0777; |
| 319 | status->user = 9999; // Not applicable to Windows, so... |
| 320 | status->group = 9999; // Not applicable to Windows, so... |
Jeff Cohen | 626e38e | 2004-12-14 05:26:43 +0000 | [diff] [blame] | 321 | |
Reid Spencer | 69cce81 | 2007-03-29 16:43:20 +0000 | [diff] [blame] | 322 | __int64 ft = *reinterpret_cast<__int64*>(&fi.ftLastWriteTime); |
| 323 | status->modTime.fromWin32Time(ft); |
| 324 | |
| 325 | status->isDir = fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; |
| 326 | } |
| 327 | info = *status; |
Chris Lattner | 7dea101 | 2006-07-28 22:32:09 +0000 | [diff] [blame] | 328 | return false; |
Jeff Cohen | 626e38e | 2004-12-14 05:26:43 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Reid Spencer | a34a157 | 2006-08-22 23:54:35 +0000 | [diff] [blame] | 331 | bool Path::makeReadableOnDisk(std::string* ErrMsg) { |
Jeff Cohen | 626e38e | 2004-12-14 05:26:43 +0000 | [diff] [blame] | 332 | // All files are readable on Windows (ignoring security attributes). |
Reid Spencer | a34a157 | 2006-08-22 23:54:35 +0000 | [diff] [blame] | 333 | return false; |
Reid Spencer | 77cc91d | 2004-12-13 19:59:50 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Anton Korobeynikov | 7d51544 | 2006-09-01 20:35:17 +0000 | [diff] [blame] | 336 | bool Path::makeWriteableOnDisk(std::string* ErrMsg) { |
Jeff Cohen | 626e38e | 2004-12-14 05:26:43 +0000 | [diff] [blame] | 337 | DWORD attr = GetFileAttributes(path.c_str()); |
| 338 | |
| 339 | // If it doesn't exist, we're done. |
| 340 | if (attr == INVALID_FILE_ATTRIBUTES) |
Anton Korobeynikov | 7d51544 | 2006-09-01 20:35:17 +0000 | [diff] [blame] | 341 | return false; |
Jeff Cohen | 626e38e | 2004-12-14 05:26:43 +0000 | [diff] [blame] | 342 | |
| 343 | if (attr & FILE_ATTRIBUTE_READONLY) { |
Reid Spencer | a34a157 | 2006-08-22 23:54:35 +0000 | [diff] [blame] | 344 | if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY)) { |
| 345 | MakeErrMsg(ErrMsg, std::string(path) + ": Can't make file writable: "); |
| 346 | return true; |
| 347 | } |
Jeff Cohen | 626e38e | 2004-12-14 05:26:43 +0000 | [diff] [blame] | 348 | } |
Reid Spencer | a34a157 | 2006-08-22 23:54:35 +0000 | [diff] [blame] | 349 | return false; |
Reid Spencer | 77cc91d | 2004-12-13 19:59:50 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Reid Spencer | a34a157 | 2006-08-22 23:54:35 +0000 | [diff] [blame] | 352 | bool Path::makeExecutableOnDisk(std::string* ErrMsg) { |
Jeff Cohen | 626e38e | 2004-12-14 05:26:43 +0000 | [diff] [blame] | 353 | // All files are executable on Windows (ignoring security attributes). |
Reid Spencer | a34a157 | 2006-08-22 23:54:35 +0000 | [diff] [blame] | 354 | return false; |
Reid Spencer | 77cc91d | 2004-12-13 19:59:50 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 357 | bool |
Reid Spencer | 142ca8e | 2006-08-23 06:56:27 +0000 | [diff] [blame] | 358 | Path::getDirectoryContents(std::set<Path>& result, std::string* ErrMsg) const { |
Anton Korobeynikov | 7d51544 | 2006-09-01 20:35:17 +0000 | [diff] [blame] | 359 | FileStatus Status; |
| 360 | if (getFileStatus(Status, ErrMsg)) |
| 361 | return true; |
| 362 | |
| 363 | if (!Status.isDir) { |
Reid Spencer | 142ca8e | 2006-08-23 06:56:27 +0000 | [diff] [blame] | 364 | MakeErrMsg(ErrMsg, path + ": not a directory"); |
| 365 | return true; |
| 366 | } |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 367 | |
| 368 | result.clear(); |
| 369 | WIN32_FIND_DATA fd; |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 370 | std::string searchpath = path; |
| 371 | if (path.size() == 0 || searchpath[path.size()-1] == '/') |
Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 372 | searchpath += "*"; |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 373 | else |
| 374 | searchpath += "/*"; |
Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 375 | |
Jeff Cohen | 9437bb6 | 2005-01-27 03:49:03 +0000 | [diff] [blame] | 376 | HANDLE h = FindFirstFile(searchpath.c_str(), &fd); |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 377 | if (h == INVALID_HANDLE_VALUE) { |
Jeff Cohen | 9437bb6 | 2005-01-27 03:49:03 +0000 | [diff] [blame] | 378 | if (GetLastError() == ERROR_FILE_NOT_FOUND) |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 379 | return true; // not really an error, now is it? |
Reid Spencer | 142ca8e | 2006-08-23 06:56:27 +0000 | [diff] [blame] | 380 | MakeErrMsg(ErrMsg, path + ": Can't read directory: "); |
| 381 | return true; |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | do { |
Jeff Cohen | d40a7de | 2004-12-31 05:07:26 +0000 | [diff] [blame] | 385 | if (fd.cFileName[0] == '.') |
| 386 | continue; |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 387 | Path aPath(path); |
| 388 | aPath.appendComponent(&fd.cFileName[0]); |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 389 | result.insert(aPath); |
| 390 | } while (FindNextFile(h, &fd)); |
| 391 | |
Jeff Cohen | 51b8d21 | 2004-12-31 19:01:08 +0000 | [diff] [blame] | 392 | DWORD err = GetLastError(); |
| 393 | FindClose(h); |
| 394 | if (err != ERROR_NO_MORE_FILES) { |
| 395 | SetLastError(err); |
Reid Spencer | 142ca8e | 2006-08-23 06:56:27 +0000 | [diff] [blame] | 396 | MakeErrMsg(ErrMsg, path + ": Can't read directory: "); |
| 397 | return true; |
Jeff Cohen | 51b8d21 | 2004-12-31 19:01:08 +0000 | [diff] [blame] | 398 | } |
Reid Spencer | 142ca8e | 2006-08-23 06:56:27 +0000 | [diff] [blame] | 399 | return false; |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | bool |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 403 | Path::set(const std::string& a_path) { |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 404 | if (a_path.size() == 0) |
| 405 | return false; |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 406 | std::string save(path); |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 407 | path = a_path; |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 408 | FlipBackSlashes(path); |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 409 | if (!isValid()) { |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 410 | path = save; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 411 | return false; |
| 412 | } |
| 413 | return true; |
| 414 | } |
| 415 | |
| 416 | bool |
Jeff Cohen | edb9d6b | 2005-07-08 02:48:42 +0000 | [diff] [blame] | 417 | Path::appendComponent(const std::string& name) { |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 418 | if (name.empty()) |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 419 | return false; |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 420 | std::string save(path); |
| 421 | if (!path.empty()) { |
| 422 | size_t last = path.size() - 1; |
Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 423 | if (path[last] != '/') |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 424 | path += '/'; |
| 425 | } |
| 426 | path += name; |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 427 | if (!isValid()) { |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 428 | path = save; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 429 | return false; |
| 430 | } |
| 431 | return true; |
| 432 | } |
| 433 | |
| 434 | bool |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 435 | Path::eraseComponent() { |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 436 | size_t slashpos = path.rfind('/',path.size()); |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 437 | if (slashpos == path.size() - 1 || slashpos == std::string::npos) |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 438 | return false; |
Jeff Cohen | 966fa41 | 2005-07-09 18:42:49 +0000 | [diff] [blame] | 439 | std::string save(path); |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 440 | path.erase(slashpos); |
Jeff Cohen | 966fa41 | 2005-07-09 18:42:49 +0000 | [diff] [blame] | 441 | if (!isValid()) { |
| 442 | path = save; |
| 443 | return false; |
| 444 | } |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 445 | return true; |
| 446 | } |
| 447 | |
| 448 | bool |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 449 | Path::appendSuffix(const std::string& suffix) { |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 450 | std::string save(path); |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 451 | path.append("."); |
| 452 | path.append(suffix); |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 453 | if (!isValid()) { |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 454 | path = save; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 455 | return false; |
| 456 | } |
| 457 | return true; |
| 458 | } |
| 459 | |
| 460 | bool |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 461 | Path::eraseSuffix() { |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 462 | size_t dotpos = path.rfind('.',path.size()); |
| 463 | size_t slashpos = path.rfind('/',path.size()); |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 464 | if (dotpos != std::string::npos) { |
Jeff Cohen | 966fa41 | 2005-07-09 18:42:49 +0000 | [diff] [blame] | 465 | if (slashpos == std::string::npos || dotpos > slashpos+1) { |
| 466 | std::string save(path); |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 467 | path.erase(dotpos, path.size()-dotpos); |
Jeff Cohen | 966fa41 | 2005-07-09 18:42:49 +0000 | [diff] [blame] | 468 | if (!isValid()) { |
| 469 | path = save; |
| 470 | return false; |
| 471 | } |
Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 472 | return true; |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 473 | } |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 474 | } |
| 475 | return false; |
| 476 | } |
| 477 | |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 478 | inline bool PathMsg(std::string* ErrMsg, const char* pathname, const char*msg) { |
| 479 | if (ErrMsg) |
| 480 | *ErrMsg = std::string(pathname) + ": " + std::string(msg); |
| 481 | return true; |
| 482 | } |
| 483 | |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 484 | bool |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 485 | Path::createDirectoryOnDisk(bool create_parents, std::string* ErrMsg) { |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 486 | // Get a writeable copy of the path name |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 487 | size_t len = path.length(); |
| 488 | char *pathname = reinterpret_cast<char *>(_alloca(len+2)); |
| 489 | path.copy(pathname, len); |
| 490 | pathname[len] = 0; |
Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 491 | |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 492 | // Make sure it ends with a slash. |
| 493 | if (len == 0 || pathname[len - 1] != '/') { |
| 494 | pathname[len] = '/'; |
| 495 | pathname[++len] = 0; |
| 496 | } |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 497 | |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 498 | // Determine starting point for initial / search. |
| 499 | char *next = pathname; |
| 500 | if (pathname[0] == '/' && pathname[1] == '/') { |
| 501 | // Skip host name. |
| 502 | next = strchr(pathname+2, '/'); |
| 503 | if (next == NULL) |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 504 | return PathMsg(ErrMsg, pathname, "badly formed remote directory"); |
| 505 | |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 506 | // Skip share name. |
| 507 | next = strchr(next+1, '/'); |
| 508 | if (next == NULL) |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 509 | return PathMsg(ErrMsg, pathname,"badly formed remote directory"); |
| 510 | |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 511 | next++; |
| 512 | if (*next == 0) |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 513 | return PathMsg(ErrMsg, pathname, "badly formed remote directory"); |
| 514 | |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 515 | } else { |
| 516 | if (pathname[1] == ':') |
| 517 | next += 2; // skip drive letter |
| 518 | if (*next == '/') |
| 519 | next++; // skip root directory |
| 520 | } |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 521 | |
| 522 | // If we're supposed to create intermediate directories |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 523 | if (create_parents) { |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 524 | // Loop through the directory components until we're done |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 525 | while (*next) { |
| 526 | next = strchr(next, '/'); |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 527 | *next = 0; |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 528 | if (!CreateDirectory(pathname, NULL)) |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 529 | return MakeErrMsg(ErrMsg, |
| 530 | std::string(pathname) + ": Can't create directory: "); |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 531 | *next++ = '/'; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 532 | } |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 533 | } else { |
| 534 | // Drop trailing slash. |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 535 | pathname[len-1] = 0; |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 536 | if (!CreateDirectory(pathname, NULL)) { |
Anton Korobeynikov | 7d51544 | 2006-09-01 20:35:17 +0000 | [diff] [blame] | 537 | return MakeErrMsg(ErrMsg, std::string(pathname) + ": Can't create directory: "); |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 538 | } |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 539 | } |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 540 | return false; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | bool |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 544 | Path::createFileOnDisk(std::string* ErrMsg) { |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 545 | // Create the file |
Reid Spencer | 6a0ec6f | 2004-09-29 00:01:17 +0000 | [diff] [blame] | 546 | HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW, |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 547 | FILE_ATTRIBUTE_NORMAL, NULL); |
| 548 | if (h == INVALID_HANDLE_VALUE) |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 549 | return MakeErrMsg(ErrMsg, path + ": Can't create file: "); |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 550 | |
Reid Spencer | d0c9e0e | 2004-09-18 19:29:16 +0000 | [diff] [blame] | 551 | CloseHandle(h); |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 552 | return false; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | bool |
Chris Lattner | 0c33231 | 2006-07-28 22:29:50 +0000 | [diff] [blame] | 556 | Path::eraseFromDisk(bool remove_contents, std::string *ErrStr) const { |
Chris Lattner | c7c453a | 2006-08-01 17:51:09 +0000 | [diff] [blame] | 557 | FileStatus Status; |
| 558 | if (getFileStatus(Status, ErrStr)) |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 559 | return false; |
Chris Lattner | c7c453a | 2006-08-01 17:51:09 +0000 | [diff] [blame] | 560 | |
| 561 | if (Status.isFile) { |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 562 | DWORD attr = GetFileAttributes(path.c_str()); |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 563 | |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 564 | // If it doesn't exist, we're done. |
| 565 | if (attr == INVALID_FILE_ATTRIBUTES) |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 566 | return false; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 567 | |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 568 | // Read-only files cannot be deleted on Windows. Must remove the read-only |
| 569 | // attribute first. |
| 570 | if (attr & FILE_ATTRIBUTE_READONLY) { |
| 571 | if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY)) |
Reid Spencer | 0554575 | 2006-08-25 21:37:17 +0000 | [diff] [blame] | 572 | return MakeErrMsg(ErrStr, path + ": Can't destroy file: "); |
Jeff Cohen | 51b8d21 | 2004-12-31 19:01:08 +0000 | [diff] [blame] | 573 | } |
Jeff Cohen | 51b8d21 | 2004-12-31 19:01:08 +0000 | [diff] [blame] | 574 | |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 575 | if (!DeleteFile(path.c_str())) |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 576 | return MakeErrMsg(ErrStr, path + ": Can't destroy file: "); |
Chris Lattner | c7c453a | 2006-08-01 17:51:09 +0000 | [diff] [blame] | 577 | return false; |
| 578 | } else if (Status.isDir) { |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 579 | // If it doesn't exist, we're done. |
Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 580 | if (!exists()) |
Chris Lattner | 0c33231 | 2006-07-28 22:29:50 +0000 | [diff] [blame] | 581 | return false; |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 582 | |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 583 | char *pathname = reinterpret_cast<char *>(_alloca(path.length()+3)); |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 584 | int lastchar = path.length() - 1 ; |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 585 | path.copy(pathname, lastchar+1); |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 586 | |
| 587 | // Make path end with '/*'. |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 588 | if (pathname[lastchar] != '/') |
| 589 | pathname[++lastchar] = '/'; |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 590 | pathname[lastchar+1] = '*'; |
| 591 | pathname[lastchar+2] = 0; |
| 592 | |
| 593 | if (remove_contents) { |
| 594 | WIN32_FIND_DATA fd; |
| 595 | HANDLE h = FindFirstFile(pathname, &fd); |
| 596 | |
| 597 | // It's a bad idea to alter the contents of a directory while enumerating |
| 598 | // its contents. So build a list of its contents first, then destroy them. |
| 599 | |
| 600 | if (h != INVALID_HANDLE_VALUE) { |
| 601 | std::vector<Path> list; |
| 602 | |
| 603 | do { |
| 604 | if (strcmp(fd.cFileName, ".") == 0) |
| 605 | continue; |
| 606 | if (strcmp(fd.cFileName, "..") == 0) |
| 607 | continue; |
| 608 | |
Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 609 | Path aPath(path); |
| 610 | aPath.appendComponent(&fd.cFileName[0]); |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 611 | list.push_back(aPath); |
| 612 | } while (FindNextFile(h, &fd)); |
| 613 | |
| 614 | DWORD err = GetLastError(); |
| 615 | FindClose(h); |
| 616 | if (err != ERROR_NO_MORE_FILES) { |
| 617 | SetLastError(err); |
Reid Spencer | 0554575 | 2006-08-25 21:37:17 +0000 | [diff] [blame] | 618 | return MakeErrMsg(ErrStr, path + ": Can't read directory: "); |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 619 | } |
| 620 | |
Jeff Cohen | 85c716f | 2005-07-08 05:02:13 +0000 | [diff] [blame] | 621 | for (std::vector<Path>::iterator I = list.begin(); I != list.end(); |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 622 | ++I) { |
| 623 | Path &aPath = *I; |
Reid Spencer | a229c5c | 2005-07-08 03:08:58 +0000 | [diff] [blame] | 624 | aPath.eraseFromDisk(true); |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 625 | } |
| 626 | } else { |
| 627 | if (GetLastError() != ERROR_FILE_NOT_FOUND) |
Reid Spencer | 0554575 | 2006-08-25 21:37:17 +0000 | [diff] [blame] | 628 | return MakeErrMsg(ErrStr, path + ": Can't read directory: "); |
Reid Spencer | 1cf2d04 | 2005-07-07 23:35:23 +0000 | [diff] [blame] | 629 | } |
| 630 | } |
| 631 | |
| 632 | pathname[lastchar] = 0; |
| 633 | if (!RemoveDirectory(pathname)) |
Reid Spencer | 0554575 | 2006-08-25 21:37:17 +0000 | [diff] [blame] | 634 | return MakeErrMsg(ErrStr, |
| 635 | std::string(pathname) + ": Can't destroy directory: "); |
Chris Lattner | 0c33231 | 2006-07-28 22:29:50 +0000 | [diff] [blame] | 636 | return false; |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 637 | } |
| 638 | // It appears the path doesn't exist. |
| 639 | return true; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 640 | } |
| 641 | |
Reid Spencer | 3b0cc78 | 2004-12-14 18:42:13 +0000 | [diff] [blame] | 642 | bool Path::getMagicNumber(std::string& Magic, unsigned len) const { |
Reid Spencer | 3b0cc78 | 2004-12-14 18:42:13 +0000 | [diff] [blame] | 643 | assert(len < 1024 && "Request for magic string too long"); |
| 644 | char* buf = (char*) alloca(1 + len); |
Jeff Cohen | 51b8d21 | 2004-12-31 19:01:08 +0000 | [diff] [blame] | 645 | |
| 646 | HANDLE h = CreateFile(path.c_str(), |
| 647 | GENERIC_READ, |
| 648 | FILE_SHARE_READ, |
| 649 | NULL, |
| 650 | OPEN_EXISTING, |
| 651 | FILE_ATTRIBUTE_NORMAL, |
| 652 | NULL); |
| 653 | if (h == INVALID_HANDLE_VALUE) |
Reid Spencer | 3b0cc78 | 2004-12-14 18:42:13 +0000 | [diff] [blame] | 654 | return false; |
Jeff Cohen | 51b8d21 | 2004-12-31 19:01:08 +0000 | [diff] [blame] | 655 | |
| 656 | DWORD nRead = 0; |
| 657 | BOOL ret = ReadFile(h, buf, len, &nRead, NULL); |
| 658 | CloseHandle(h); |
| 659 | |
| 660 | if (!ret || nRead != len) |
Reid Spencer | 3b0cc78 | 2004-12-14 18:42:13 +0000 | [diff] [blame] | 661 | return false; |
Jeff Cohen | 51b8d21 | 2004-12-31 19:01:08 +0000 | [diff] [blame] | 662 | |
Reid Spencer | 3b0cc78 | 2004-12-14 18:42:13 +0000 | [diff] [blame] | 663 | buf[len] = '\0'; |
| 664 | Magic = buf; |
| 665 | return true; |
| 666 | } |
| 667 | |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 668 | bool |
Reid Spencer | 5a06077 | 2006-08-23 07:30:48 +0000 | [diff] [blame] | 669 | Path::renamePathOnDisk(const Path& newName, std::string* ErrMsg) { |
Jeff Cohen | e564de2 | 2006-05-07 02:51:51 +0000 | [diff] [blame] | 670 | if (!MoveFileEx(path.c_str(), newName.c_str(), MOVEFILE_REPLACE_EXISTING)) |
Reid Spencer | 5a06077 | 2006-08-23 07:30:48 +0000 | [diff] [blame] | 671 | return MakeErrMsg(ErrMsg, "Can't move '" + path + "' to '" + newName.path |
| 672 | + "': "); |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 673 | return true; |
| 674 | } |
| 675 | |
| 676 | bool |
Anton Korobeynikov | 7d51544 | 2006-09-01 20:35:17 +0000 | [diff] [blame] | 677 | Path::setStatusInfoOnDisk(const FileStatus &si, std::string *ErrMsg) const { |
Jeff Cohen | 966fa41 | 2005-07-09 18:42:49 +0000 | [diff] [blame] | 678 | // FIXME: should work on directories also. |
Anton Korobeynikov | 7d51544 | 2006-09-01 20:35:17 +0000 | [diff] [blame] | 679 | if (!si.isFile) { |
| 680 | return true; |
| 681 | } |
| 682 | |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 683 | HANDLE h = CreateFile(path.c_str(), |
| 684 | FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES, |
| 685 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, |
Jeff Cohen | d40a7de | 2004-12-31 05:07:26 +0000 | [diff] [blame] | 686 | NULL, |
| 687 | OPEN_EXISTING, |
| 688 | FILE_ATTRIBUTE_NORMAL, |
| 689 | NULL); |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 690 | if (h == INVALID_HANDLE_VALUE) |
Chris Lattner | 1bebfb5 | 2006-07-28 22:36:17 +0000 | [diff] [blame] | 691 | return true; |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 692 | |
| 693 | BY_HANDLE_FILE_INFORMATION bhfi; |
| 694 | if (!GetFileInformationByHandle(h, &bhfi)) { |
Jeff Cohen | 51b8d21 | 2004-12-31 19:01:08 +0000 | [diff] [blame] | 695 | DWORD err = GetLastError(); |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 696 | CloseHandle(h); |
Jeff Cohen | 51b8d21 | 2004-12-31 19:01:08 +0000 | [diff] [blame] | 697 | SetLastError(err); |
Anton Korobeynikov | 7d51544 | 2006-09-01 20:35:17 +0000 | [diff] [blame] | 698 | return MakeErrMsg(ErrMsg, path + ": GetFileInformationByHandle: "); |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | FILETIME ft; |
| 702 | (uint64_t&)ft = si.modTime.toWin32Time(); |
| 703 | BOOL ret = SetFileTime(h, NULL, &ft, &ft); |
Jeff Cohen | 51b8d21 | 2004-12-31 19:01:08 +0000 | [diff] [blame] | 704 | DWORD err = GetLastError(); |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 705 | CloseHandle(h); |
Jeff Cohen | 51b8d21 | 2004-12-31 19:01:08 +0000 | [diff] [blame] | 706 | if (!ret) { |
| 707 | SetLastError(err); |
Anton Korobeynikov | 7d51544 | 2006-09-01 20:35:17 +0000 | [diff] [blame] | 708 | return MakeErrMsg(ErrMsg, path + ": SetFileTime: "); |
Jeff Cohen | 51b8d21 | 2004-12-31 19:01:08 +0000 | [diff] [blame] | 709 | } |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 710 | |
| 711 | // Best we can do with Unix permission bits is to interpret the owner |
| 712 | // writable bit. |
| 713 | if (si.mode & 0200) { |
| 714 | if (bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY) { |
| 715 | if (!SetFileAttributes(path.c_str(), |
Jeff Cohen | d40a7de | 2004-12-31 05:07:26 +0000 | [diff] [blame] | 716 | bhfi.dwFileAttributes & ~FILE_ATTRIBUTE_READONLY)) |
Anton Korobeynikov | 7d51544 | 2006-09-01 20:35:17 +0000 | [diff] [blame] | 717 | return MakeErrMsg(ErrMsg, path + ": SetFileAttributes: "); |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 718 | } |
| 719 | } else { |
| 720 | if (!(bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) { |
| 721 | if (!SetFileAttributes(path.c_str(), |
Jeff Cohen | d40a7de | 2004-12-31 05:07:26 +0000 | [diff] [blame] | 722 | bhfi.dwFileAttributes | FILE_ATTRIBUTE_READONLY)) |
Anton Korobeynikov | 7d51544 | 2006-09-01 20:35:17 +0000 | [diff] [blame] | 723 | return MakeErrMsg(ErrMsg, path + ": SetFileAttributes: "); |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 724 | } |
| 725 | } |
| 726 | |
Chris Lattner | 1bebfb5 | 2006-07-28 22:36:17 +0000 | [diff] [blame] | 727 | return false; |
Jeff Cohen | ebcb9b3 | 2004-12-31 04:39:07 +0000 | [diff] [blame] | 728 | } |
| 729 | |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 730 | bool |
| 731 | CopyFile(const sys::Path &Dest, const sys::Path &Src, std::string* ErrMsg) { |
Jeff Cohen | cb65255 | 2004-12-24 02:38:34 +0000 | [diff] [blame] | 732 | // Can't use CopyFile macro defined in Windows.h because it would mess up the |
| 733 | // above line. We use the expansion it would have in a non-UNICODE build. |
| 734 | if (!::CopyFileA(Src.c_str(), Dest.c_str(), false)) |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 735 | return MakeErrMsg(ErrMsg, "Can't copy '" + Src.toString() + |
Jeff Cohen | d40a7de | 2004-12-31 05:07:26 +0000 | [diff] [blame] | 736 | "' to '" + Dest.toString() + "': "); |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 737 | return false; |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 738 | } |
| 739 | |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 740 | bool |
| 741 | Path::makeUnique(bool reuse_current, std::string* ErrMsg) { |
Reid Spencer | 07f9f4e | 2004-12-15 08:32:45 +0000 | [diff] [blame] | 742 | if (reuse_current && !exists()) |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 743 | return false; // File doesn't exist already, just use it! |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 744 | |
Jeff Cohen | 9437bb6 | 2005-01-27 03:49:03 +0000 | [diff] [blame] | 745 | // Reserve space for -XXXXXX at the end. |
| 746 | char *FNBuffer = (char*) alloca(path.size()+8); |
| 747 | unsigned offset = path.size(); |
| 748 | path.copy(FNBuffer, offset); |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 749 | |
Jeff Cohen | 966fa41 | 2005-07-09 18:42:49 +0000 | [diff] [blame] | 750 | // Find a numeric suffix that isn't used by an existing file. Assume there |
| 751 | // won't be more than 1 million files with the same prefix. Probably a safe |
| 752 | // bet. |
Jeff Cohen | 9437bb6 | 2005-01-27 03:49:03 +0000 | [diff] [blame] | 753 | static unsigned FCounter = 0; |
| 754 | do { |
| 755 | sprintf(FNBuffer+offset, "-%06u", FCounter); |
| 756 | if (++FCounter > 999999) |
| 757 | FCounter = 0; |
| 758 | path = FNBuffer; |
| 759 | } while (exists()); |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 760 | return false; |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 761 | } |
| 762 | |
Reid Spencer | 07f9f4e | 2004-12-15 08:32:45 +0000 | [diff] [blame] | 763 | bool |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 764 | Path::createTemporaryFileOnDisk(bool reuse_current, std::string* ErrMsg) { |
Reid Spencer | 07f9f4e | 2004-12-15 08:32:45 +0000 | [diff] [blame] | 765 | // Make this into a unique file name |
Anton Korobeynikov | 7d51544 | 2006-09-01 20:35:17 +0000 | [diff] [blame] | 766 | makeUnique(reuse_current, ErrMsg); |
Jeff Cohen | 9437bb6 | 2005-01-27 03:49:03 +0000 | [diff] [blame] | 767 | |
| 768 | // Now go and create it |
| 769 | HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW, |
| 770 | FILE_ATTRIBUTE_NORMAL, NULL); |
| 771 | if (h == INVALID_HANDLE_VALUE) |
Anton Korobeynikov | 7d51544 | 2006-09-01 20:35:17 +0000 | [diff] [blame] | 772 | return MakeErrMsg(ErrMsg, path + ": can't create file"); |
Jeff Cohen | 9437bb6 | 2005-01-27 03:49:03 +0000 | [diff] [blame] | 773 | |
| 774 | CloseHandle(h); |
Reid Spencer | 3030099 | 2006-08-24 18:58:37 +0000 | [diff] [blame] | 775 | return false; |
Reid Spencer | 07f9f4e | 2004-12-15 08:32:45 +0000 | [diff] [blame] | 776 | } |
| 777 | |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 778 | } |
Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 779 | } |