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