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