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