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