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