Rafael Espindola | f1fc382 | 2013-06-26 19:33:03 +0000 | [diff] [blame] | 1 | //===- llvm/Support/Windows/Path.inc - Windows Path Impl --------*- C++ -*-===// |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Rafael Espindola | f1fc382 | 2013-06-26 19:33:03 +0000 | [diff] [blame] | 10 | // This file implements the Windows specific implementation of the Path API. |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | //=== WARNING: Implementation here must contain only generic Windows code that |
| 16 | //=== is guaranteed to work on *all* Windows variants. |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
Craig Topper | f18edae | 2013-07-15 07:15:05 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/STLExtras.h" |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 20 | #include "Windows.h" |
Michael J. Spencer | c20a032 | 2010-12-03 17:54:07 +0000 | [diff] [blame] | 21 | #include <fcntl.h> |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 22 | #include <io.h> |
Michael J. Spencer | c20a032 | 2010-12-03 17:54:07 +0000 | [diff] [blame] | 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 25 | |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 26 | #undef max |
| 27 | |
Michael J. Spencer | 6025247 | 2010-12-03 18:03:28 +0000 | [diff] [blame] | 28 | // MinGW doesn't define this. |
Michael J. Spencer | 521c321 | 2010-12-03 18:04:11 +0000 | [diff] [blame] | 29 | #ifndef _ERRNO_T_DEFINED |
| 30 | #define _ERRNO_T_DEFINED |
| 31 | typedef int errno_t; |
Michael J. Spencer | 6025247 | 2010-12-03 18:03:28 +0000 | [diff] [blame] | 32 | #endif |
| 33 | |
Reid Kleckner | 11da004 | 2013-08-07 20:19:31 +0000 | [diff] [blame] | 34 | #ifdef _MSC_VER |
| 35 | # pragma comment(lib, "advapi32.lib") // This provides CryptAcquireContextW. |
| 36 | #endif |
| 37 | |
Michael J. Spencer | 9fc1d9d | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 38 | using namespace llvm; |
| 39 | |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 40 | using llvm::sys::windows::UTF8ToUTF16; |
| 41 | using llvm::sys::windows::UTF16ToUTF8; |
| 42 | |
Michael J. Spencer | 9fc1d9d | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 43 | namespace { |
Michael J. Spencer | 772caff5 | 2010-12-03 17:53:23 +0000 | [diff] [blame] | 44 | typedef BOOLEAN (WINAPI *PtrCreateSymbolicLinkW)( |
| 45 | /*__in*/ LPCWSTR lpSymlinkFileName, |
| 46 | /*__in*/ LPCWSTR lpTargetFileName, |
| 47 | /*__in*/ DWORD dwFlags); |
| 48 | |
David Majnemer | 17a4496 | 2013-10-07 09:52:36 +0000 | [diff] [blame] | 49 | PtrCreateSymbolicLinkW create_symbolic_link_api = |
| 50 | PtrCreateSymbolicLinkW(::GetProcAddress( |
| 51 | ::GetModuleHandleW(L"Kernel32.dll"), "CreateSymbolicLinkW")); |
Michael J. Spencer | 7ee6d5d | 2010-12-03 07:41:25 +0000 | [diff] [blame] | 52 | |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 53 | error_code TempDir(SmallVectorImpl<wchar_t> &result) { |
| 54 | retry_temp_dir: |
| 55 | DWORD len = ::GetTempPathW(result.capacity(), result.begin()); |
| 56 | |
| 57 | if (len == 0) |
Michael J. Spencer | 66a1f86 | 2010-12-04 18:45:32 +0000 | [diff] [blame] | 58 | return windows_error(::GetLastError()); |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 59 | |
| 60 | if (len > result.capacity()) { |
| 61 | result.reserve(len); |
| 62 | goto retry_temp_dir; |
| 63 | } |
| 64 | |
| 65 | result.set_size(len); |
David Blaikie | 18544b9 | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 66 | return error_code::success(); |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 69 | bool is_separator(const wchar_t value) { |
| 70 | switch (value) { |
| 71 | case L'\\': |
| 72 | case L'/': |
| 73 | return true; |
| 74 | default: |
| 75 | return false; |
| 76 | } |
| 77 | } |
Michael J. Spencer | 9fc1d9d | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Rafael Espindola | e79a872 | 2013-06-28 03:48:47 +0000 | [diff] [blame] | 80 | // FIXME: mode should be used here and default to user r/w only, |
| 81 | // it currently comes in as a UNIX mode. |
| 82 | static error_code createUniqueEntity(const Twine &model, int &result_fd, |
| 83 | SmallVectorImpl<char> &result_path, |
| 84 | bool makeAbsolute, unsigned mode, |
| 85 | FSEntity Type) { |
| 86 | // Use result_path as temp storage. |
| 87 | result_path.set_size(0); |
| 88 | StringRef m = model.toStringRef(result_path); |
| 89 | |
| 90 | SmallVector<wchar_t, 128> model_utf16; |
| 91 | if (error_code ec = UTF8ToUTF16(m, model_utf16)) return ec; |
| 92 | |
| 93 | if (makeAbsolute) { |
| 94 | // Make model absolute by prepending a temp directory if it's not already. |
| 95 | bool absolute = sys::path::is_absolute(m); |
| 96 | |
| 97 | if (!absolute) { |
| 98 | SmallVector<wchar_t, 64> temp_dir; |
| 99 | if (error_code ec = TempDir(temp_dir)) return ec; |
| 100 | // Handle c: by removing it. |
| 101 | if (model_utf16.size() > 2 && model_utf16[1] == L':') { |
| 102 | model_utf16.erase(model_utf16.begin(), model_utf16.begin() + 2); |
| 103 | } |
| 104 | model_utf16.insert(model_utf16.begin(), temp_dir.begin(), temp_dir.end()); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | // Replace '%' with random chars. From here on, DO NOT modify model. It may be |
| 109 | // needed if the randomly chosen path already exists. |
| 110 | SmallVector<wchar_t, 128> random_path_utf16; |
| 111 | |
| 112 | // Get a Crypto Provider for CryptGenRandom. |
| 113 | HCRYPTPROV HCPC; |
| 114 | if (!::CryptAcquireContextW(&HCPC, |
| 115 | NULL, |
| 116 | NULL, |
| 117 | PROV_RSA_FULL, |
| 118 | CRYPT_VERIFYCONTEXT)) |
| 119 | return windows_error(::GetLastError()); |
| 120 | ScopedCryptContext CryptoProvider(HCPC); |
| 121 | |
| 122 | retry_random_path: |
| 123 | random_path_utf16.set_size(0); |
| 124 | for (SmallVectorImpl<wchar_t>::const_iterator i = model_utf16.begin(), |
| 125 | e = model_utf16.end(); |
| 126 | i != e; ++i) { |
| 127 | if (*i == L'%') { |
| 128 | BYTE val = 0; |
| 129 | if (!::CryptGenRandom(CryptoProvider, 1, &val)) |
| 130 | return windows_error(::GetLastError()); |
David Majnemer | 61eae2e | 2013-10-07 01:00:07 +0000 | [diff] [blame] | 131 | random_path_utf16.push_back(L"0123456789abcdef"[val & 15]); |
Rafael Espindola | e79a872 | 2013-06-28 03:48:47 +0000 | [diff] [blame] | 132 | } |
| 133 | else |
| 134 | random_path_utf16.push_back(*i); |
| 135 | } |
| 136 | // Make random_path_utf16 null terminated. |
| 137 | random_path_utf16.push_back(0); |
| 138 | random_path_utf16.pop_back(); |
| 139 | |
Serge Pavlov | 4c2a09d | 2013-07-19 16:23:54 +0000 | [diff] [blame] | 140 | HANDLE TempFileHandle = INVALID_HANDLE_VALUE; |
Rafael Espindola | e79a872 | 2013-06-28 03:48:47 +0000 | [diff] [blame] | 141 | |
| 142 | switch (Type) { |
| 143 | case FS_File: { |
| 144 | // Try to create + open the path. |
| 145 | TempFileHandle = |
| 146 | ::CreateFileW(random_path_utf16.begin(), GENERIC_READ | GENERIC_WRITE, |
| 147 | FILE_SHARE_READ, NULL, |
| 148 | // Return ERROR_FILE_EXISTS if the file |
| 149 | // already exists. |
| 150 | CREATE_NEW, FILE_ATTRIBUTE_TEMPORARY, NULL); |
| 151 | if (TempFileHandle == INVALID_HANDLE_VALUE) { |
| 152 | // If the file existed, try again, otherwise, error. |
| 153 | error_code ec = windows_error(::GetLastError()); |
| 154 | if (ec == windows_error::file_exists) |
| 155 | goto retry_random_path; |
| 156 | |
| 157 | return ec; |
| 158 | } |
| 159 | |
| 160 | // Convert the Windows API file handle into a C-runtime handle. |
| 161 | int fd = ::_open_osfhandle(intptr_t(TempFileHandle), 0); |
| 162 | if (fd == -1) { |
| 163 | ::CloseHandle(TempFileHandle); |
| 164 | ::DeleteFileW(random_path_utf16.begin()); |
| 165 | // MSDN doesn't say anything about _open_osfhandle setting errno or |
| 166 | // GetLastError(), so just return invalid_handle. |
| 167 | return windows_error::invalid_handle; |
| 168 | } |
| 169 | |
| 170 | result_fd = fd; |
| 171 | break; |
| 172 | } |
| 173 | |
| 174 | case FS_Name: { |
| 175 | DWORD attributes = ::GetFileAttributesW(random_path_utf16.begin()); |
| 176 | if (attributes != INVALID_FILE_ATTRIBUTES) |
| 177 | goto retry_random_path; |
| 178 | error_code EC = make_error_code(windows_error(::GetLastError())); |
| 179 | if (EC != windows_error::file_not_found && |
| 180 | EC != windows_error::path_not_found) |
| 181 | return EC; |
| 182 | break; |
| 183 | } |
| 184 | |
| 185 | case FS_Dir: |
| 186 | if (!::CreateDirectoryW(random_path_utf16.begin(), NULL)) { |
| 187 | error_code EC = windows_error(::GetLastError()); |
| 188 | if (EC != windows_error::already_exists) |
| 189 | return EC; |
| 190 | goto retry_random_path; |
| 191 | } |
| 192 | break; |
| 193 | } |
| 194 | |
| 195 | // Set result_path to the utf-8 representation of the path. |
| 196 | if (error_code ec = UTF16ToUTF8(random_path_utf16.begin(), |
| 197 | random_path_utf16.size(), result_path)) { |
| 198 | switch (Type) { |
| 199 | case FS_File: |
| 200 | ::CloseHandle(TempFileHandle); |
| 201 | ::DeleteFileW(random_path_utf16.begin()); |
| 202 | case FS_Name: |
| 203 | break; |
| 204 | case FS_Dir: |
| 205 | ::RemoveDirectoryW(random_path_utf16.begin()); |
| 206 | break; |
| 207 | } |
| 208 | return ec; |
| 209 | } |
| 210 | |
| 211 | return error_code::success(); |
| 212 | } |
| 213 | |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 214 | namespace llvm { |
| 215 | namespace sys { |
Michael J. Spencer | 20daa28 | 2010-12-07 01:22:31 +0000 | [diff] [blame] | 216 | namespace fs { |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 217 | |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 218 | std::string getMainExecutable(const char *argv0, void *MainExecAddr) { |
David Majnemer | 17a4496 | 2013-10-07 09:52:36 +0000 | [diff] [blame] | 219 | SmallVector<wchar_t, MAX_PATH> PathName; |
| 220 | DWORD Size = ::GetModuleFileNameW(NULL, PathName.data(), PathName.capacity()); |
| 221 | |
| 222 | // A zero return value indicates a failure other than insufficient space. |
| 223 | if (Size == 0) |
| 224 | return ""; |
| 225 | |
| 226 | // Insufficient space is determined by a return value equal to the size of |
| 227 | // the buffer passed in. |
| 228 | if (Size == PathName.capacity()) |
| 229 | return ""; |
| 230 | |
| 231 | // On success, GetModuleFileNameW returns the number of characters written to |
| 232 | // the buffer not including the NULL terminator. |
| 233 | PathName.set_size(Size); |
| 234 | |
| 235 | // Convert the result from UTF-16 to UTF-8. |
| 236 | SmallVector<char, MAX_PATH> PathNameUTF8; |
| 237 | if (UTF16ToUTF8(PathName.data(), PathName.size(), PathNameUTF8)) |
| 238 | return ""; |
| 239 | |
| 240 | return std::string(PathNameUTF8.data()); |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Rafael Espindola | d123099 | 2013-07-29 21:55:38 +0000 | [diff] [blame] | 243 | UniqueID file_status::getUniqueID() const { |
Rafael Espindola | 7f822a9 | 2013-07-29 21:26:49 +0000 | [diff] [blame] | 244 | // The file is uniquely identified by the volume serial number along |
| 245 | // with the 64-bit file identifier. |
| 246 | uint64_t FileID = (static_cast<uint64_t>(FileIndexHigh) << 32ULL) | |
| 247 | static_cast<uint64_t>(FileIndexLow); |
| 248 | |
| 249 | return UniqueID(VolumeSerialNumber, FileID); |
| 250 | } |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 251 | |
Rafael Espindola | be3ede7 | 2013-06-20 21:51:49 +0000 | [diff] [blame] | 252 | TimeValue file_status::getLastModificationTime() const { |
Rafael Espindola | db5d8fe | 2013-06-20 18:42:04 +0000 | [diff] [blame] | 253 | ULARGE_INTEGER UI; |
| 254 | UI.LowPart = LastWriteTimeLow; |
| 255 | UI.HighPart = LastWriteTimeHigh; |
| 256 | |
| 257 | TimeValue Ret; |
| 258 | Ret.fromWin32Time(UI.QuadPart); |
| 259 | return Ret; |
| 260 | } |
| 261 | |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 262 | error_code current_path(SmallVectorImpl<char> &result) { |
David Majnemer | 61eae2e | 2013-10-07 01:00:07 +0000 | [diff] [blame] | 263 | SmallVector<wchar_t, MAX_PATH> cur_path; |
| 264 | DWORD len = MAX_PATH; |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 265 | |
David Majnemer | 61eae2e | 2013-10-07 01:00:07 +0000 | [diff] [blame] | 266 | do { |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 267 | cur_path.reserve(len); |
David Majnemer | 61eae2e | 2013-10-07 01:00:07 +0000 | [diff] [blame] | 268 | len = ::GetCurrentDirectoryW(cur_path.capacity(), cur_path.data()); |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 269 | |
David Majnemer | 61eae2e | 2013-10-07 01:00:07 +0000 | [diff] [blame] | 270 | // A zero return value indicates a failure other than insufficient space. |
| 271 | if (len == 0) |
| 272 | return windows_error(::GetLastError()); |
| 273 | |
| 274 | // If there's insufficient space, the len returned is larger than the len |
| 275 | // given. |
| 276 | } while (len > cur_path.capacity()); |
| 277 | |
| 278 | // On success, GetCurrentDirectoryW returns the number of characters not |
| 279 | // including the null-terminator. |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 280 | cur_path.set_size(len); |
Aaron Ballman | b16cf53 | 2013-08-16 17:53:28 +0000 | [diff] [blame] | 281 | return UTF16ToUTF8(cur_path.begin(), cur_path.size(), result); |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Michael J. Spencer | 31e310c | 2010-12-03 05:42:11 +0000 | [diff] [blame] | 284 | error_code create_directory(const Twine &path, bool &existed) { |
| 285 | SmallString<128> path_storage; |
| 286 | SmallVector<wchar_t, 128> path_utf16; |
| 287 | |
| 288 | if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage), |
| 289 | path_utf16)) |
| 290 | return ec; |
| 291 | |
| 292 | if (!::CreateDirectoryW(path_utf16.begin(), NULL)) { |
Michael J. Spencer | 66a1f86 | 2010-12-04 18:45:32 +0000 | [diff] [blame] | 293 | error_code ec = windows_error(::GetLastError()); |
| 294 | if (ec == windows_error::already_exists) |
Michael J. Spencer | 31e310c | 2010-12-03 05:42:11 +0000 | [diff] [blame] | 295 | existed = true; |
| 296 | else |
| 297 | return ec; |
| 298 | } else |
| 299 | existed = false; |
| 300 | |
David Blaikie | 18544b9 | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 301 | return error_code::success(); |
Michael J. Spencer | 31e310c | 2010-12-03 05:42:11 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Michael J. Spencer | e0c4560 | 2010-12-03 05:58:41 +0000 | [diff] [blame] | 304 | error_code create_hard_link(const Twine &to, const Twine &from) { |
| 305 | // Get arguments. |
| 306 | SmallString<128> from_storage; |
| 307 | SmallString<128> to_storage; |
| 308 | StringRef f = from.toStringRef(from_storage); |
| 309 | StringRef t = to.toStringRef(to_storage); |
| 310 | |
| 311 | // Convert to utf-16. |
| 312 | SmallVector<wchar_t, 128> wide_from; |
| 313 | SmallVector<wchar_t, 128> wide_to; |
| 314 | if (error_code ec = UTF8ToUTF16(f, wide_from)) return ec; |
| 315 | if (error_code ec = UTF8ToUTF16(t, wide_to)) return ec; |
| 316 | |
| 317 | if (!::CreateHardLinkW(wide_from.begin(), wide_to.begin(), NULL)) |
Michael J. Spencer | 66a1f86 | 2010-12-04 18:45:32 +0000 | [diff] [blame] | 318 | return windows_error(::GetLastError()); |
Michael J. Spencer | e0c4560 | 2010-12-03 05:58:41 +0000 | [diff] [blame] | 319 | |
David Blaikie | 18544b9 | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 320 | return error_code::success(); |
Michael J. Spencer | e0c4560 | 2010-12-03 05:58:41 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Michael J. Spencer | 7ee6d5d | 2010-12-03 07:41:25 +0000 | [diff] [blame] | 323 | error_code create_symlink(const Twine &to, const Twine &from) { |
| 324 | // Only do it if the function is available at runtime. |
| 325 | if (!create_symbolic_link_api) |
| 326 | return make_error_code(errc::function_not_supported); |
| 327 | |
| 328 | // Get arguments. |
| 329 | SmallString<128> from_storage; |
| 330 | SmallString<128> to_storage; |
| 331 | StringRef f = from.toStringRef(from_storage); |
| 332 | StringRef t = to.toStringRef(to_storage); |
| 333 | |
| 334 | // Convert to utf-16. |
| 335 | SmallVector<wchar_t, 128> wide_from; |
| 336 | SmallVector<wchar_t, 128> wide_to; |
| 337 | if (error_code ec = UTF8ToUTF16(f, wide_from)) return ec; |
| 338 | if (error_code ec = UTF8ToUTF16(t, wide_to)) return ec; |
| 339 | |
Michael J. Spencer | 962fbc4 | 2010-12-04 00:32:14 +0000 | [diff] [blame] | 340 | if (!create_symbolic_link_api(wide_from.begin(), wide_to.begin(), 0)) |
Michael J. Spencer | 66a1f86 | 2010-12-04 18:45:32 +0000 | [diff] [blame] | 341 | return windows_error(::GetLastError()); |
Michael J. Spencer | 7ee6d5d | 2010-12-03 07:41:25 +0000 | [diff] [blame] | 342 | |
David Blaikie | 18544b9 | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 343 | return error_code::success(); |
Michael J. Spencer | 7ee6d5d | 2010-12-03 07:41:25 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Michael J. Spencer | 6e74e11 | 2010-12-03 17:53:43 +0000 | [diff] [blame] | 346 | error_code remove(const Twine &path, bool &existed) { |
| 347 | SmallString<128> path_storage; |
| 348 | SmallVector<wchar_t, 128> path_utf16; |
| 349 | |
Michael J. Spencer | 153749b | 2011-01-05 16:39:22 +0000 | [diff] [blame] | 350 | file_status st; |
Rafael Espindola | 107b74c | 2013-07-31 00:10:25 +0000 | [diff] [blame] | 351 | error_code EC = status(path, st); |
| 352 | if (EC) { |
| 353 | if (EC == windows_error::file_not_found || |
| 354 | EC == windows_error::path_not_found) { |
| 355 | existed = false; |
| 356 | return error_code::success(); |
| 357 | } |
| 358 | return EC; |
| 359 | } |
Michael J. Spencer | 153749b | 2011-01-05 16:39:22 +0000 | [diff] [blame] | 360 | |
Michael J. Spencer | 6e74e11 | 2010-12-03 17:53:43 +0000 | [diff] [blame] | 361 | if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage), |
| 362 | path_utf16)) |
| 363 | return ec; |
| 364 | |
Michael J. Spencer | 153749b | 2011-01-05 16:39:22 +0000 | [diff] [blame] | 365 | if (st.type() == file_type::directory_file) { |
| 366 | if (!::RemoveDirectoryW(c_str(path_utf16))) { |
| 367 | error_code ec = windows_error(::GetLastError()); |
| 368 | if (ec != windows_error::file_not_found) |
| 369 | return ec; |
| 370 | existed = false; |
| 371 | } else |
| 372 | existed = true; |
| 373 | } else { |
| 374 | if (!::DeleteFileW(c_str(path_utf16))) { |
| 375 | error_code ec = windows_error(::GetLastError()); |
| 376 | if (ec != windows_error::file_not_found) |
| 377 | return ec; |
| 378 | existed = false; |
| 379 | } else |
| 380 | existed = true; |
| 381 | } |
Michael J. Spencer | 6e74e11 | 2010-12-03 17:53:43 +0000 | [diff] [blame] | 382 | |
David Blaikie | 18544b9 | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 383 | return error_code::success(); |
Michael J. Spencer | 6e74e11 | 2010-12-03 17:53:43 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Michael J. Spencer | 409f556 | 2010-12-03 17:53:55 +0000 | [diff] [blame] | 386 | error_code rename(const Twine &from, const Twine &to) { |
| 387 | // Get arguments. |
| 388 | SmallString<128> from_storage; |
| 389 | SmallString<128> to_storage; |
| 390 | StringRef f = from.toStringRef(from_storage); |
| 391 | StringRef t = to.toStringRef(to_storage); |
| 392 | |
| 393 | // Convert to utf-16. |
| 394 | SmallVector<wchar_t, 128> wide_from; |
| 395 | SmallVector<wchar_t, 128> wide_to; |
| 396 | if (error_code ec = UTF8ToUTF16(f, wide_from)) return ec; |
| 397 | if (error_code ec = UTF8ToUTF16(t, wide_to)) return ec; |
| 398 | |
NAKAMURA Takumi | 3b7f995 | 2012-05-08 14:31:46 +0000 | [diff] [blame] | 399 | error_code ec = error_code::success(); |
| 400 | for (int i = 0; i < 2000; i++) { |
| 401 | if (::MoveFileExW(wide_from.begin(), wide_to.begin(), |
| 402 | MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING)) |
| 403 | return error_code::success(); |
| 404 | ec = windows_error(::GetLastError()); |
| 405 | if (ec != windows_error::access_denied) |
| 406 | break; |
| 407 | // Retry MoveFile() at ACCESS_DENIED. |
| 408 | // System scanners (eg. indexer) might open the source file when |
| 409 | // It is written and closed. |
| 410 | ::Sleep(1); |
| 411 | } |
Michael J. Spencer | 409f556 | 2010-12-03 17:53:55 +0000 | [diff] [blame] | 412 | |
NAKAMURA Takumi | 3b7f995 | 2012-05-08 14:31:46 +0000 | [diff] [blame] | 413 | return ec; |
Michael J. Spencer | 409f556 | 2010-12-03 17:53:55 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Michael J. Spencer | c20a032 | 2010-12-03 17:54:07 +0000 | [diff] [blame] | 416 | error_code resize_file(const Twine &path, uint64_t size) { |
| 417 | SmallString<128> path_storage; |
| 418 | SmallVector<wchar_t, 128> path_utf16; |
| 419 | |
| 420 | if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage), |
| 421 | path_utf16)) |
| 422 | return ec; |
| 423 | |
Michael J. Spencer | 20abb20 | 2012-12-03 22:09:31 +0000 | [diff] [blame] | 424 | int fd = ::_wopen(path_utf16.begin(), O_BINARY | _O_RDWR, S_IWRITE); |
Michael J. Spencer | c20a032 | 2010-12-03 17:54:07 +0000 | [diff] [blame] | 425 | if (fd == -1) |
| 426 | return error_code(errno, generic_category()); |
Michael J. Spencer | ca242f2 | 2010-12-03 18:48:56 +0000 | [diff] [blame] | 427 | #ifdef HAVE__CHSIZE_S |
Michael J. Spencer | c20a032 | 2010-12-03 17:54:07 +0000 | [diff] [blame] | 428 | errno_t error = ::_chsize_s(fd, size); |
Michael J. Spencer | ca242f2 | 2010-12-03 18:48:56 +0000 | [diff] [blame] | 429 | #else |
| 430 | errno_t error = ::_chsize(fd, size); |
| 431 | #endif |
Michael J. Spencer | c20a032 | 2010-12-03 17:54:07 +0000 | [diff] [blame] | 432 | ::close(fd); |
| 433 | return error_code(error, generic_category()); |
| 434 | } |
| 435 | |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 436 | error_code exists(const Twine &path, bool &result) { |
| 437 | SmallString<128> path_storage; |
| 438 | SmallVector<wchar_t, 128> path_utf16; |
| 439 | |
| 440 | if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage), |
| 441 | path_utf16)) |
| 442 | return ec; |
| 443 | |
| 444 | DWORD attributes = ::GetFileAttributesW(path_utf16.begin()); |
| 445 | |
| 446 | if (attributes == INVALID_FILE_ATTRIBUTES) { |
| 447 | // See if the file didn't actually exist. |
| 448 | error_code ec = make_error_code(windows_error(::GetLastError())); |
Michael J. Spencer | 66a1f86 | 2010-12-04 18:45:32 +0000 | [diff] [blame] | 449 | if (ec != windows_error::file_not_found && |
| 450 | ec != windows_error::path_not_found) |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 451 | return ec; |
| 452 | result = false; |
| 453 | } else |
| 454 | result = true; |
David Blaikie | 18544b9 | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 455 | return error_code::success(); |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Rafael Espindola | a1280c1 | 2013-06-18 20:56:38 +0000 | [diff] [blame] | 458 | bool can_write(const Twine &Path) { |
| 459 | // FIXME: take security attributes into account. |
| 460 | SmallString<128> PathStorage; |
| 461 | SmallVector<wchar_t, 128> PathUtf16; |
| 462 | |
| 463 | if (UTF8ToUTF16(Path.toStringRef(PathStorage), PathUtf16)) |
| 464 | return false; |
| 465 | |
| 466 | DWORD Attr = ::GetFileAttributesW(PathUtf16.begin()); |
| 467 | return (Attr != INVALID_FILE_ATTRIBUTES) && !(Attr & FILE_ATTRIBUTE_READONLY); |
| 468 | } |
| 469 | |
Rafael Espindola | b0a5c96 | 2013-06-14 19:38:45 +0000 | [diff] [blame] | 470 | bool can_execute(const Twine &Path) { |
| 471 | SmallString<128> PathStorage; |
| 472 | SmallVector<wchar_t, 128> PathUtf16; |
| 473 | |
| 474 | if (UTF8ToUTF16(Path.toStringRef(PathStorage), PathUtf16)) |
| 475 | return false; |
| 476 | |
| 477 | DWORD Attr = ::GetFileAttributesW(PathUtf16.begin()); |
| 478 | return Attr != INVALID_FILE_ATTRIBUTES; |
| 479 | } |
| 480 | |
Michael J. Spencer | 203d780 | 2011-12-12 06:04:28 +0000 | [diff] [blame] | 481 | bool equivalent(file_status A, file_status B) { |
| 482 | assert(status_known(A) && status_known(B)); |
| 483 | return A.FileIndexHigh == B.FileIndexHigh && |
| 484 | A.FileIndexLow == B.FileIndexLow && |
| 485 | A.FileSizeHigh == B.FileSizeHigh && |
| 486 | A.FileSizeLow == B.FileSizeLow && |
| 487 | A.LastWriteTimeHigh == B.LastWriteTimeHigh && |
| 488 | A.LastWriteTimeLow == B.LastWriteTimeLow && |
| 489 | A.VolumeSerialNumber == B.VolumeSerialNumber; |
| 490 | } |
| 491 | |
Michael J. Spencer | 376d387 | 2010-12-03 18:49:13 +0000 | [diff] [blame] | 492 | error_code equivalent(const Twine &A, const Twine &B, bool &result) { |
Michael J. Spencer | 203d780 | 2011-12-12 06:04:28 +0000 | [diff] [blame] | 493 | file_status fsA, fsB; |
| 494 | if (error_code ec = status(A, fsA)) return ec; |
| 495 | if (error_code ec = status(B, fsB)) return ec; |
| 496 | result = equivalent(fsA, fsB); |
David Blaikie | 18544b9 | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 497 | return error_code::success(); |
Michael J. Spencer | 376d387 | 2010-12-03 18:49:13 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Benjamin Kramer | 58994ec | 2011-08-20 21:36:38 +0000 | [diff] [blame] | 500 | static bool isReservedName(StringRef path) { |
| 501 | // This list of reserved names comes from MSDN, at: |
| 502 | // http://msdn.microsoft.com/en-us/library/aa365247%28v=vs.85%29.aspx |
| 503 | static const char *sReservedNames[] = { "nul", "con", "prn", "aux", |
| 504 | "com1", "com2", "com3", "com4", "com5", "com6", |
| 505 | "com7", "com8", "com9", "lpt1", "lpt2", "lpt3", |
| 506 | "lpt4", "lpt5", "lpt6", "lpt7", "lpt8", "lpt9" }; |
| 507 | |
| 508 | // First, check to see if this is a device namespace, which always |
| 509 | // starts with \\.\, since device namespaces are not legal file paths. |
| 510 | if (path.startswith("\\\\.\\")) |
| 511 | return true; |
| 512 | |
| 513 | // Then compare against the list of ancient reserved names |
Craig Topper | 5871321 | 2013-07-15 04:27:47 +0000 | [diff] [blame] | 514 | for (size_t i = 0; i < array_lengthof(sReservedNames); ++i) { |
Benjamin Kramer | 58994ec | 2011-08-20 21:36:38 +0000 | [diff] [blame] | 515 | if (path.equals_lower(sReservedNames[i])) |
| 516 | return true; |
| 517 | } |
| 518 | |
| 519 | // The path isn't what we consider reserved. |
| 520 | return false; |
| 521 | } |
| 522 | |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 523 | static error_code getStatus(HANDLE FileHandle, file_status &Result) { |
| 524 | if (FileHandle == INVALID_HANDLE_VALUE) |
| 525 | goto handle_status_error; |
| 526 | |
NAKAMURA Takumi | 8b01da4 | 2013-07-18 17:00:54 +0000 | [diff] [blame] | 527 | switch (::GetFileType(FileHandle)) { |
| 528 | default: |
Rafael Espindola | 81177c5 | 2013-07-18 18:42:52 +0000 | [diff] [blame] | 529 | llvm_unreachable("Don't know anything about this file type"); |
| 530 | case FILE_TYPE_UNKNOWN: { |
| 531 | DWORD Err = ::GetLastError(); |
| 532 | if (Err != NO_ERROR) |
| 533 | return windows_error(Err); |
| 534 | Result = file_status(file_type::type_unknown); |
| 535 | return error_code::success(); |
| 536 | } |
NAKAMURA Takumi | 8b01da4 | 2013-07-18 17:00:54 +0000 | [diff] [blame] | 537 | case FILE_TYPE_DISK: |
| 538 | break; |
| 539 | case FILE_TYPE_CHAR: |
| 540 | Result = file_status(file_type::character_file); |
| 541 | return error_code::success(); |
| 542 | case FILE_TYPE_PIPE: |
| 543 | Result = file_status(file_type::fifo_file); |
| 544 | return error_code::success(); |
| 545 | } |
| 546 | |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 547 | BY_HANDLE_FILE_INFORMATION Info; |
| 548 | if (!::GetFileInformationByHandle(FileHandle, &Info)) |
| 549 | goto handle_status_error; |
| 550 | |
Rafael Espindola | a5932af | 2013-07-30 20:25:53 +0000 | [diff] [blame] | 551 | { |
| 552 | file_type Type = (Info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) |
| 553 | ? file_type::directory_file |
| 554 | : file_type::regular_file; |
| 555 | Result = |
| 556 | file_status(Type, Info.ftLastWriteTime.dwHighDateTime, |
| 557 | Info.ftLastWriteTime.dwLowDateTime, |
| 558 | Info.dwVolumeSerialNumber, Info.nFileSizeHigh, |
| 559 | Info.nFileSizeLow, Info.nFileIndexHigh, Info.nFileIndexLow); |
| 560 | return error_code::success(); |
| 561 | } |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 562 | |
| 563 | handle_status_error: |
| 564 | error_code EC = windows_error(::GetLastError()); |
| 565 | if (EC == windows_error::file_not_found || |
| 566 | EC == windows_error::path_not_found) |
| 567 | Result = file_status(file_type::file_not_found); |
| 568 | else if (EC == windows_error::sharing_violation) |
| 569 | Result = file_status(file_type::type_unknown); |
Rafael Espindola | 107b74c | 2013-07-31 00:10:25 +0000 | [diff] [blame] | 570 | else |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 571 | Result = file_status(file_type::status_error); |
Rafael Espindola | 107b74c | 2013-07-31 00:10:25 +0000 | [diff] [blame] | 572 | return EC; |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 573 | } |
| 574 | |
Michael J. Spencer | db5576a | 2010-12-04 00:32:40 +0000 | [diff] [blame] | 575 | error_code status(const Twine &path, file_status &result) { |
| 576 | SmallString<128> path_storage; |
| 577 | SmallVector<wchar_t, 128> path_utf16; |
| 578 | |
NAKAMURA Takumi | a3d4749 | 2011-03-16 02:53:32 +0000 | [diff] [blame] | 579 | StringRef path8 = path.toStringRef(path_storage); |
Benjamin Kramer | 58994ec | 2011-08-20 21:36:38 +0000 | [diff] [blame] | 580 | if (isReservedName(path8)) { |
NAKAMURA Takumi | a3d4749 | 2011-03-16 02:53:32 +0000 | [diff] [blame] | 581 | result = file_status(file_type::character_file); |
David Blaikie | 18544b9 | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 582 | return error_code::success(); |
NAKAMURA Takumi | a3d4749 | 2011-03-16 02:53:32 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Michael J. Spencer | 203d780 | 2011-12-12 06:04:28 +0000 | [diff] [blame] | 585 | if (error_code ec = UTF8ToUTF16(path8, path_utf16)) |
Michael J. Spencer | db5576a | 2010-12-04 00:32:40 +0000 | [diff] [blame] | 586 | return ec; |
| 587 | |
| 588 | DWORD attr = ::GetFileAttributesW(path_utf16.begin()); |
| 589 | if (attr == INVALID_FILE_ATTRIBUTES) |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 590 | return getStatus(INVALID_HANDLE_VALUE, result); |
Michael J. Spencer | db5576a | 2010-12-04 00:32:40 +0000 | [diff] [blame] | 591 | |
| 592 | // Handle reparse points. |
| 593 | if (attr & FILE_ATTRIBUTE_REPARSE_POINT) { |
Michael J. Spencer | 513f1b6 | 2011-12-12 06:03:33 +0000 | [diff] [blame] | 594 | ScopedFileHandle h( |
Michael J. Spencer | db5576a | 2010-12-04 00:32:40 +0000 | [diff] [blame] | 595 | ::CreateFileW(path_utf16.begin(), |
| 596 | 0, // Attributes only. |
| 597 | FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, |
| 598 | NULL, |
| 599 | OPEN_EXISTING, |
| 600 | FILE_FLAG_BACKUP_SEMANTICS, |
| 601 | 0)); |
Michael J. Spencer | 513f1b6 | 2011-12-12 06:03:33 +0000 | [diff] [blame] | 602 | if (!h) |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 603 | return getStatus(INVALID_HANDLE_VALUE, result); |
Michael J. Spencer | db5576a | 2010-12-04 00:32:40 +0000 | [diff] [blame] | 604 | } |
| 605 | |
Rafael Espindola | a5932af | 2013-07-30 20:25:53 +0000 | [diff] [blame] | 606 | ScopedFileHandle h( |
| 607 | ::CreateFileW(path_utf16.begin(), 0, // Attributes only. |
Michael J. Spencer | 203d780 | 2011-12-12 06:04:28 +0000 | [diff] [blame] | 608 | FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, |
Rafael Espindola | a5932af | 2013-07-30 20:25:53 +0000 | [diff] [blame] | 609 | NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0)); |
Michael J. Spencer | 203d780 | 2011-12-12 06:04:28 +0000 | [diff] [blame] | 610 | if (!h) |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 611 | return getStatus(INVALID_HANDLE_VALUE, result); |
Michael J. Spencer | db5576a | 2010-12-04 00:32:40 +0000 | [diff] [blame] | 612 | |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 613 | return getStatus(h, result); |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 614 | } |
Michael J. Spencer | db5576a | 2010-12-04 00:32:40 +0000 | [diff] [blame] | 615 | |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 616 | error_code status(int FD, file_status &Result) { |
| 617 | HANDLE FileHandle = reinterpret_cast<HANDLE>(_get_osfhandle(FD)); |
| 618 | return getStatus(FileHandle, Result); |
Michael J. Spencer | db5576a | 2010-12-04 00:32:40 +0000 | [diff] [blame] | 619 | } |
| 620 | |
Rafael Espindola | 4a3365c | 2013-06-20 20:56:14 +0000 | [diff] [blame] | 621 | error_code setLastModificationAndAccessTime(int FD, TimeValue Time) { |
| 622 | ULARGE_INTEGER UI; |
| 623 | UI.QuadPart = Time.toWin32Time(); |
| 624 | FILETIME FT; |
| 625 | FT.dwLowDateTime = UI.LowPart; |
| 626 | FT.dwHighDateTime = UI.HighPart; |
| 627 | HANDLE FileHandle = reinterpret_cast<HANDLE>(_get_osfhandle(FD)); |
| 628 | if (!SetFileTime(FileHandle, NULL, &FT, &FT)) |
| 629 | return windows_error(::GetLastError()); |
| 630 | return error_code::success(); |
| 631 | } |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 632 | |
Michael J. Spencer | ee1699c | 2011-01-15 18:52:33 +0000 | [diff] [blame] | 633 | error_code get_magic(const Twine &path, uint32_t len, |
| 634 | SmallVectorImpl<char> &result) { |
| 635 | SmallString<128> path_storage; |
| 636 | SmallVector<wchar_t, 128> path_utf16; |
| 637 | result.set_size(0); |
| 638 | |
| 639 | // Convert path to UTF-16. |
| 640 | if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage), |
| 641 | path_utf16)) |
| 642 | return ec; |
| 643 | |
| 644 | // Open file. |
| 645 | HANDLE file = ::CreateFileW(c_str(path_utf16), |
| 646 | GENERIC_READ, |
| 647 | FILE_SHARE_READ, |
| 648 | NULL, |
| 649 | OPEN_EXISTING, |
| 650 | FILE_ATTRIBUTE_READONLY, |
| 651 | NULL); |
| 652 | if (file == INVALID_HANDLE_VALUE) |
| 653 | return windows_error(::GetLastError()); |
| 654 | |
| 655 | // Allocate buffer. |
| 656 | result.reserve(len); |
| 657 | |
| 658 | // Get magic! |
| 659 | DWORD bytes_read = 0; |
| 660 | BOOL read_success = ::ReadFile(file, result.data(), len, &bytes_read, NULL); |
| 661 | error_code ec = windows_error(::GetLastError()); |
| 662 | ::CloseHandle(file); |
| 663 | if (!read_success || (bytes_read != len)) { |
| 664 | // Set result size to the number of bytes read if it's valid. |
NAKAMURA Takumi | 3e76b50 | 2011-03-07 00:12:53 +0000 | [diff] [blame] | 665 | if (bytes_read <= len) |
Michael J. Spencer | ee1699c | 2011-01-15 18:52:33 +0000 | [diff] [blame] | 666 | result.set_size(bytes_read); |
| 667 | // ERROR_HANDLE_EOF is mapped to errc::value_too_large. |
| 668 | return ec; |
| 669 | } |
| 670 | |
| 671 | result.set_size(len); |
David Blaikie | 18544b9 | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 672 | return error_code::success(); |
Michael J. Spencer | ee1699c | 2011-01-15 18:52:33 +0000 | [diff] [blame] | 673 | } |
| 674 | |
Michael J. Spencer | 42ad29f | 2013-03-14 00:20:10 +0000 | [diff] [blame] | 675 | error_code mapped_file_region::init(int FD, bool CloseFD, uint64_t Offset) { |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 676 | FileDescriptor = FD; |
| 677 | // Make sure that the requested size fits within SIZE_T. |
| 678 | if (Size > std::numeric_limits<SIZE_T>::max()) { |
Michael J. Spencer | d932d41 | 2013-03-15 19:25:47 +0000 | [diff] [blame] | 679 | if (FileDescriptor) { |
Michael J. Spencer | 42ad29f | 2013-03-14 00:20:10 +0000 | [diff] [blame] | 680 | if (CloseFD) |
| 681 | _close(FileDescriptor); |
Michael J. Spencer | d932d41 | 2013-03-15 19:25:47 +0000 | [diff] [blame] | 682 | } else |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 683 | ::CloseHandle(FileHandle); |
| 684 | return make_error_code(errc::invalid_argument); |
| 685 | } |
| 686 | |
| 687 | DWORD flprotect; |
| 688 | switch (Mode) { |
| 689 | case readonly: flprotect = PAGE_READONLY; break; |
| 690 | case readwrite: flprotect = PAGE_READWRITE; break; |
| 691 | case priv: flprotect = PAGE_WRITECOPY; break; |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 692 | } |
| 693 | |
David Majnemer | 17a4496 | 2013-10-07 09:52:36 +0000 | [diff] [blame] | 694 | FileMappingHandle = |
| 695 | ::CreateFileMappingW(FileHandle, 0, flprotect, |
| 696 | (Offset + Size) >> 32, |
| 697 | (Offset + Size) & 0xffffffff, |
| 698 | 0); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 699 | if (FileMappingHandle == NULL) { |
| 700 | error_code ec = windows_error(GetLastError()); |
Michael J. Spencer | d932d41 | 2013-03-15 19:25:47 +0000 | [diff] [blame] | 701 | if (FileDescriptor) { |
Michael J. Spencer | 42ad29f | 2013-03-14 00:20:10 +0000 | [diff] [blame] | 702 | if (CloseFD) |
| 703 | _close(FileDescriptor); |
Michael J. Spencer | d932d41 | 2013-03-15 19:25:47 +0000 | [diff] [blame] | 704 | } else |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 705 | ::CloseHandle(FileHandle); |
| 706 | return ec; |
| 707 | } |
| 708 | |
| 709 | DWORD dwDesiredAccess; |
| 710 | switch (Mode) { |
| 711 | case readonly: dwDesiredAccess = FILE_MAP_READ; break; |
| 712 | case readwrite: dwDesiredAccess = FILE_MAP_WRITE; break; |
| 713 | case priv: dwDesiredAccess = FILE_MAP_COPY; break; |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 714 | } |
| 715 | Mapping = ::MapViewOfFile(FileMappingHandle, |
| 716 | dwDesiredAccess, |
| 717 | Offset >> 32, |
| 718 | Offset & 0xffffffff, |
| 719 | Size); |
| 720 | if (Mapping == NULL) { |
| 721 | error_code ec = windows_error(GetLastError()); |
| 722 | ::CloseHandle(FileMappingHandle); |
Michael J. Spencer | d932d41 | 2013-03-15 19:25:47 +0000 | [diff] [blame] | 723 | if (FileDescriptor) { |
Michael J. Spencer | 42ad29f | 2013-03-14 00:20:10 +0000 | [diff] [blame] | 724 | if (CloseFD) |
| 725 | _close(FileDescriptor); |
Michael J. Spencer | d932d41 | 2013-03-15 19:25:47 +0000 | [diff] [blame] | 726 | } else |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 727 | ::CloseHandle(FileHandle); |
| 728 | return ec; |
| 729 | } |
| 730 | |
| 731 | if (Size == 0) { |
| 732 | MEMORY_BASIC_INFORMATION mbi; |
| 733 | SIZE_T Result = VirtualQuery(Mapping, &mbi, sizeof(mbi)); |
| 734 | if (Result == 0) { |
| 735 | error_code ec = windows_error(GetLastError()); |
| 736 | ::UnmapViewOfFile(Mapping); |
| 737 | ::CloseHandle(FileMappingHandle); |
Michael J. Spencer | d932d41 | 2013-03-15 19:25:47 +0000 | [diff] [blame] | 738 | if (FileDescriptor) { |
Michael J. Spencer | 42ad29f | 2013-03-14 00:20:10 +0000 | [diff] [blame] | 739 | if (CloseFD) |
| 740 | _close(FileDescriptor); |
Michael J. Spencer | d932d41 | 2013-03-15 19:25:47 +0000 | [diff] [blame] | 741 | } else |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 742 | ::CloseHandle(FileHandle); |
| 743 | return ec; |
| 744 | } |
| 745 | Size = mbi.RegionSize; |
| 746 | } |
Michael J. Spencer | 42ad29f | 2013-03-14 00:20:10 +0000 | [diff] [blame] | 747 | |
| 748 | // Close all the handles except for the view. It will keep the other handles |
| 749 | // alive. |
| 750 | ::CloseHandle(FileMappingHandle); |
Michael J. Spencer | d932d41 | 2013-03-15 19:25:47 +0000 | [diff] [blame] | 751 | if (FileDescriptor) { |
Michael J. Spencer | 42ad29f | 2013-03-14 00:20:10 +0000 | [diff] [blame] | 752 | if (CloseFD) |
| 753 | _close(FileDescriptor); // Also closes FileHandle. |
Michael J. Spencer | d932d41 | 2013-03-15 19:25:47 +0000 | [diff] [blame] | 754 | } else |
Michael J. Spencer | 42ad29f | 2013-03-14 00:20:10 +0000 | [diff] [blame] | 755 | ::CloseHandle(FileHandle); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 756 | return error_code::success(); |
| 757 | } |
| 758 | |
| 759 | mapped_file_region::mapped_file_region(const Twine &path, |
| 760 | mapmode mode, |
| 761 | uint64_t length, |
| 762 | uint64_t offset, |
NAKAMURA Takumi | edf7615 | 2013-08-22 15:14:45 +0000 | [diff] [blame] | 763 | error_code &ec) |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 764 | : Mode(mode) |
| 765 | , Size(length) |
| 766 | , Mapping() |
| 767 | , FileDescriptor() |
| 768 | , FileHandle(INVALID_HANDLE_VALUE) |
| 769 | , FileMappingHandle() { |
| 770 | SmallString<128> path_storage; |
| 771 | SmallVector<wchar_t, 128> path_utf16; |
| 772 | |
| 773 | // Convert path to UTF-16. |
Nico Weber | eada11b | 2012-09-25 05:24:16 +0000 | [diff] [blame] | 774 | if ((ec = UTF8ToUTF16(path.toStringRef(path_storage), path_utf16))) |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 775 | return; |
| 776 | |
| 777 | // Get file handle for creating a file mapping. |
| 778 | FileHandle = ::CreateFileW(c_str(path_utf16), |
| 779 | Mode == readonly ? GENERIC_READ |
| 780 | : GENERIC_READ | GENERIC_WRITE, |
| 781 | Mode == readonly ? FILE_SHARE_READ |
| 782 | : 0, |
| 783 | 0, |
| 784 | Mode == readonly ? OPEN_EXISTING |
| 785 | : OPEN_ALWAYS, |
| 786 | Mode == readonly ? FILE_ATTRIBUTE_READONLY |
| 787 | : FILE_ATTRIBUTE_NORMAL, |
| 788 | 0); |
| 789 | if (FileHandle == INVALID_HANDLE_VALUE) { |
| 790 | ec = windows_error(::GetLastError()); |
| 791 | return; |
| 792 | } |
| 793 | |
| 794 | FileDescriptor = 0; |
Michael J. Spencer | 42ad29f | 2013-03-14 00:20:10 +0000 | [diff] [blame] | 795 | ec = init(FileDescriptor, true, offset); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 796 | if (ec) { |
| 797 | Mapping = FileMappingHandle = 0; |
| 798 | FileHandle = INVALID_HANDLE_VALUE; |
| 799 | FileDescriptor = 0; |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | mapped_file_region::mapped_file_region(int fd, |
Michael J. Spencer | 42ad29f | 2013-03-14 00:20:10 +0000 | [diff] [blame] | 804 | bool closefd, |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 805 | mapmode mode, |
| 806 | uint64_t length, |
| 807 | uint64_t offset, |
| 808 | error_code &ec) |
| 809 | : Mode(mode) |
| 810 | , Size(length) |
| 811 | , Mapping() |
| 812 | , FileDescriptor(fd) |
| 813 | , FileHandle(INVALID_HANDLE_VALUE) |
| 814 | , FileMappingHandle() { |
| 815 | FileHandle = reinterpret_cast<HANDLE>(_get_osfhandle(fd)); |
| 816 | if (FileHandle == INVALID_HANDLE_VALUE) { |
Michael J. Spencer | 42ad29f | 2013-03-14 00:20:10 +0000 | [diff] [blame] | 817 | if (closefd) |
| 818 | _close(FileDescriptor); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 819 | FileDescriptor = 0; |
| 820 | ec = make_error_code(errc::bad_file_descriptor); |
| 821 | return; |
| 822 | } |
| 823 | |
Michael J. Spencer | 42ad29f | 2013-03-14 00:20:10 +0000 | [diff] [blame] | 824 | ec = init(FileDescriptor, closefd, offset); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 825 | if (ec) { |
| 826 | Mapping = FileMappingHandle = 0; |
| 827 | FileHandle = INVALID_HANDLE_VALUE; |
| 828 | FileDescriptor = 0; |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | mapped_file_region::~mapped_file_region() { |
| 833 | if (Mapping) |
| 834 | ::UnmapViewOfFile(Mapping); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 835 | } |
| 836 | |
Chandler Carruth | f12e3a6 | 2012-11-30 11:45:22 +0000 | [diff] [blame] | 837 | #if LLVM_HAS_RVALUE_REFERENCES |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 838 | mapped_file_region::mapped_file_region(mapped_file_region &&other) |
| 839 | : Mode(other.Mode) |
| 840 | , Size(other.Size) |
| 841 | , Mapping(other.Mapping) |
| 842 | , FileDescriptor(other.FileDescriptor) |
| 843 | , FileHandle(other.FileHandle) |
| 844 | , FileMappingHandle(other.FileMappingHandle) { |
| 845 | other.Mapping = other.FileMappingHandle = 0; |
| 846 | other.FileHandle = INVALID_HANDLE_VALUE; |
| 847 | other.FileDescriptor = 0; |
| 848 | } |
| 849 | #endif |
| 850 | |
| 851 | mapped_file_region::mapmode mapped_file_region::flags() const { |
| 852 | assert(Mapping && "Mapping failed but used anyway!"); |
| 853 | return Mode; |
| 854 | } |
| 855 | |
| 856 | uint64_t mapped_file_region::size() const { |
| 857 | assert(Mapping && "Mapping failed but used anyway!"); |
| 858 | return Size; |
| 859 | } |
| 860 | |
| 861 | char *mapped_file_region::data() const { |
| 862 | assert(Mode != readonly && "Cannot get non const data for readonly mapping!"); |
| 863 | assert(Mapping && "Mapping failed but used anyway!"); |
| 864 | return reinterpret_cast<char*>(Mapping); |
| 865 | } |
| 866 | |
| 867 | const char *mapped_file_region::const_data() const { |
| 868 | assert(Mapping && "Mapping failed but used anyway!"); |
| 869 | return reinterpret_cast<const char*>(Mapping); |
| 870 | } |
| 871 | |
| 872 | int mapped_file_region::alignment() { |
| 873 | SYSTEM_INFO SysInfo; |
| 874 | ::GetSystemInfo(&SysInfo); |
| 875 | return SysInfo.dwAllocationGranularity; |
| 876 | } |
| 877 | |
Michael J. Spencer | 0a7625d | 2011-12-08 22:50:09 +0000 | [diff] [blame] | 878 | error_code detail::directory_iterator_construct(detail::DirIterState &it, |
| 879 | StringRef path){ |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 880 | SmallVector<wchar_t, 128> path_utf16; |
| 881 | |
| 882 | if (error_code ec = UTF8ToUTF16(path, |
| 883 | path_utf16)) |
| 884 | return ec; |
| 885 | |
| 886 | // Convert path to the format that Windows is happy with. |
| 887 | if (path_utf16.size() > 0 && |
| 888 | !is_separator(path_utf16[path.size() - 1]) && |
| 889 | path_utf16[path.size() - 1] != L':') { |
| 890 | path_utf16.push_back(L'\\'); |
| 891 | path_utf16.push_back(L'*'); |
| 892 | } else { |
| 893 | path_utf16.push_back(L'*'); |
| 894 | } |
| 895 | |
| 896 | // Get the first directory entry. |
| 897 | WIN32_FIND_DATAW FirstFind; |
Michael J. Spencer | 751e9aa | 2010-12-09 17:37:18 +0000 | [diff] [blame] | 898 | ScopedFindHandle FindHandle(::FindFirstFileW(c_str(path_utf16), &FirstFind)); |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 899 | if (!FindHandle) |
| 900 | return windows_error(::GetLastError()); |
| 901 | |
Michael J. Spencer | 98879d7 | 2011-01-05 16:39:30 +0000 | [diff] [blame] | 902 | size_t FilenameLen = ::wcslen(FirstFind.cFileName); |
| 903 | while ((FilenameLen == 1 && FirstFind.cFileName[0] == L'.') || |
| 904 | (FilenameLen == 2 && FirstFind.cFileName[0] == L'.' && |
| 905 | FirstFind.cFileName[1] == L'.')) |
| 906 | if (!::FindNextFileW(FindHandle, &FirstFind)) { |
| 907 | error_code ec = windows_error(::GetLastError()); |
| 908 | // Check for end. |
| 909 | if (ec == windows_error::no_more_files) |
Michael J. Spencer | 0a7625d | 2011-12-08 22:50:09 +0000 | [diff] [blame] | 910 | return detail::directory_iterator_destruct(it); |
Michael J. Spencer | 98879d7 | 2011-01-05 16:39:30 +0000 | [diff] [blame] | 911 | return ec; |
| 912 | } else |
| 913 | FilenameLen = ::wcslen(FirstFind.cFileName); |
| 914 | |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 915 | // Construct the current directory entry. |
Michael J. Spencer | 98879d7 | 2011-01-05 16:39:30 +0000 | [diff] [blame] | 916 | SmallString<128> directory_entry_name_utf8; |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 917 | if (error_code ec = UTF16ToUTF8(FirstFind.cFileName, |
| 918 | ::wcslen(FirstFind.cFileName), |
Michael J. Spencer | 98879d7 | 2011-01-05 16:39:30 +0000 | [diff] [blame] | 919 | directory_entry_name_utf8)) |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 920 | return ec; |
| 921 | |
| 922 | it.IterationHandle = intptr_t(FindHandle.take()); |
Michael J. Spencer | 98879d7 | 2011-01-05 16:39:30 +0000 | [diff] [blame] | 923 | SmallString<128> directory_entry_path(path); |
| 924 | path::append(directory_entry_path, directory_entry_name_utf8.str()); |
| 925 | it.CurrentEntry = directory_entry(directory_entry_path.str()); |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 926 | |
David Blaikie | 18544b9 | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 927 | return error_code::success(); |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 928 | } |
| 929 | |
Michael J. Spencer | 0a7625d | 2011-12-08 22:50:09 +0000 | [diff] [blame] | 930 | error_code detail::directory_iterator_destruct(detail::DirIterState &it) { |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 931 | if (it.IterationHandle != 0) |
| 932 | // Closes the handle if it's valid. |
| 933 | ScopedFindHandle close(HANDLE(it.IterationHandle)); |
| 934 | it.IterationHandle = 0; |
| 935 | it.CurrentEntry = directory_entry(); |
David Blaikie | 18544b9 | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 936 | return error_code::success(); |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 937 | } |
| 938 | |
Michael J. Spencer | 0a7625d | 2011-12-08 22:50:09 +0000 | [diff] [blame] | 939 | error_code detail::directory_iterator_increment(detail::DirIterState &it) { |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 940 | WIN32_FIND_DATAW FindData; |
| 941 | if (!::FindNextFileW(HANDLE(it.IterationHandle), &FindData)) { |
| 942 | error_code ec = windows_error(::GetLastError()); |
| 943 | // Check for end. |
| 944 | if (ec == windows_error::no_more_files) |
Michael J. Spencer | 0a7625d | 2011-12-08 22:50:09 +0000 | [diff] [blame] | 945 | return detail::directory_iterator_destruct(it); |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 946 | return ec; |
| 947 | } |
| 948 | |
Michael J. Spencer | 98879d7 | 2011-01-05 16:39:30 +0000 | [diff] [blame] | 949 | size_t FilenameLen = ::wcslen(FindData.cFileName); |
| 950 | if ((FilenameLen == 1 && FindData.cFileName[0] == L'.') || |
| 951 | (FilenameLen == 2 && FindData.cFileName[0] == L'.' && |
| 952 | FindData.cFileName[1] == L'.')) |
| 953 | return directory_iterator_increment(it); |
| 954 | |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 955 | SmallString<128> directory_entry_path_utf8; |
| 956 | if (error_code ec = UTF16ToUTF8(FindData.cFileName, |
| 957 | ::wcslen(FindData.cFileName), |
| 958 | directory_entry_path_utf8)) |
| 959 | return ec; |
| 960 | |
| 961 | it.CurrentEntry.replace_filename(Twine(directory_entry_path_utf8)); |
David Blaikie | 18544b9 | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 962 | return error_code::success(); |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 963 | } |
| 964 | |
NAKAMURA Takumi | edf7615 | 2013-08-22 15:14:45 +0000 | [diff] [blame] | 965 | error_code map_file_pages(const Twine &path, off_t file_offset, size_t size, |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 966 | bool map_writable, void *&result) { |
| 967 | assert(0 && "NOT IMPLEMENTED"); |
Francois Pichet | 5dc987a | 2012-06-20 04:08:49 +0000 | [diff] [blame] | 968 | return windows_error::invalid_function; |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | error_code unmap_file_pages(void *base, size_t size) { |
| 972 | assert(0 && "NOT IMPLEMENTED"); |
Francois Pichet | 5dc987a | 2012-06-20 04:08:49 +0000 | [diff] [blame] | 973 | return windows_error::invalid_function; |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 974 | } |
| 975 | |
Rafael Espindola | a0d9b6b | 2013-07-17 14:58:25 +0000 | [diff] [blame] | 976 | error_code openFileForRead(const Twine &Name, int &ResultFD) { |
| 977 | SmallString<128> PathStorage; |
| 978 | SmallVector<wchar_t, 128> PathUTF16; |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 979 | |
Rafael Espindola | a0d9b6b | 2013-07-17 14:58:25 +0000 | [diff] [blame] | 980 | if (error_code EC = UTF8ToUTF16(Name.toStringRef(PathStorage), |
| 981 | PathUTF16)) |
| 982 | return EC; |
| 983 | |
| 984 | HANDLE H = ::CreateFileW(PathUTF16.begin(), GENERIC_READ, |
Rafael Espindola | 16431fe | 2013-07-17 19:44:07 +0000 | [diff] [blame] | 985 | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, |
Rafael Espindola | a0d9b6b | 2013-07-17 14:58:25 +0000 | [diff] [blame] | 986 | OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
| 987 | if (H == INVALID_HANDLE_VALUE) { |
| 988 | error_code EC = windows_error(::GetLastError()); |
Rafael Espindola | 331aeba | 2013-07-17 19:58:28 +0000 | [diff] [blame] | 989 | // Provide a better error message when trying to open directories. |
Rafael Espindola | a0d9b6b | 2013-07-17 14:58:25 +0000 | [diff] [blame] | 990 | // This only runs if we failed to open the file, so there is probably |
| 991 | // no performances issues. |
| 992 | if (EC != windows_error::access_denied) |
| 993 | return EC; |
| 994 | if (is_directory(Name)) |
| 995 | return error_code(errc::is_a_directory, posix_category()); |
| 996 | return EC; |
| 997 | } |
| 998 | |
| 999 | int FD = ::_open_osfhandle(intptr_t(H), 0); |
| 1000 | if (FD == -1) { |
| 1001 | ::CloseHandle(H); |
| 1002 | return windows_error::invalid_handle; |
| 1003 | } |
| 1004 | |
| 1005 | ResultFD = FD; |
| 1006 | return error_code::success(); |
| 1007 | } |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 1008 | |
Rafael Espindola | 67080ce | 2013-07-19 15:02:03 +0000 | [diff] [blame] | 1009 | error_code openFileForWrite(const Twine &Name, int &ResultFD, |
| 1010 | sys::fs::OpenFlags Flags, unsigned Mode) { |
| 1011 | // Verify that we don't have both "append" and "excl". |
| 1012 | assert((!(Flags & sys::fs::F_Excl) || !(Flags & sys::fs::F_Append)) && |
| 1013 | "Cannot specify both 'excl' and 'append' file creation flags!"); |
| 1014 | |
| 1015 | SmallString<128> PathStorage; |
| 1016 | SmallVector<wchar_t, 128> PathUTF16; |
| 1017 | |
| 1018 | if (error_code EC = UTF8ToUTF16(Name.toStringRef(PathStorage), |
| 1019 | PathUTF16)) |
| 1020 | return EC; |
| 1021 | |
| 1022 | DWORD CreationDisposition; |
| 1023 | if (Flags & F_Excl) |
| 1024 | CreationDisposition = CREATE_NEW; |
NAKAMURA Takumi | edf7615 | 2013-08-22 15:14:45 +0000 | [diff] [blame] | 1025 | else if (Flags & F_Append) |
Rafael Espindola | 67080ce | 2013-07-19 15:02:03 +0000 | [diff] [blame] | 1026 | CreationDisposition = OPEN_ALWAYS; |
| 1027 | else |
| 1028 | CreationDisposition = CREATE_ALWAYS; |
| 1029 | |
| 1030 | HANDLE H = ::CreateFileW(PathUTF16.begin(), GENERIC_WRITE, |
| 1031 | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, |
| 1032 | CreationDisposition, FILE_ATTRIBUTE_NORMAL, NULL); |
| 1033 | |
| 1034 | if (H == INVALID_HANDLE_VALUE) { |
| 1035 | error_code EC = windows_error(::GetLastError()); |
| 1036 | // Provide a better error message when trying to open directories. |
| 1037 | // This only runs if we failed to open the file, so there is probably |
| 1038 | // no performances issues. |
| 1039 | if (EC != windows_error::access_denied) |
| 1040 | return EC; |
| 1041 | if (is_directory(Name)) |
| 1042 | return error_code(errc::is_a_directory, posix_category()); |
| 1043 | return EC; |
| 1044 | } |
| 1045 | |
| 1046 | int OpenFlags = 0; |
| 1047 | if (Flags & F_Append) |
| 1048 | OpenFlags |= _O_APPEND; |
| 1049 | |
| 1050 | if (!(Flags & F_Binary)) |
| 1051 | OpenFlags |= _O_TEXT; |
| 1052 | |
| 1053 | int FD = ::_open_osfhandle(intptr_t(H), OpenFlags); |
| 1054 | if (FD == -1) { |
| 1055 | ::CloseHandle(H); |
| 1056 | return windows_error::invalid_handle; |
| 1057 | } |
| 1058 | |
| 1059 | ResultFD = FD; |
| 1060 | return error_code::success(); |
| 1061 | } |
Michael J. Spencer | 9fc1d9d | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 1062 | } // end namespace fs |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 1063 | |
| 1064 | namespace windows { |
| 1065 | llvm::error_code UTF8ToUTF16(llvm::StringRef utf8, |
| 1066 | llvm::SmallVectorImpl<wchar_t> &utf16) { |
| 1067 | int len = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, |
| 1068 | utf8.begin(), utf8.size(), |
| 1069 | utf16.begin(), 0); |
| 1070 | |
| 1071 | if (len == 0) |
| 1072 | return llvm::windows_error(::GetLastError()); |
| 1073 | |
| 1074 | utf16.reserve(len + 1); |
| 1075 | utf16.set_size(len); |
| 1076 | |
| 1077 | len = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, |
| 1078 | utf8.begin(), utf8.size(), |
| 1079 | utf16.begin(), utf16.size()); |
| 1080 | |
| 1081 | if (len == 0) |
| 1082 | return llvm::windows_error(::GetLastError()); |
| 1083 | |
| 1084 | // Make utf16 null terminated. |
| 1085 | utf16.push_back(0); |
| 1086 | utf16.pop_back(); |
| 1087 | |
| 1088 | return llvm::error_code::success(); |
| 1089 | } |
| 1090 | |
| 1091 | llvm::error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len, |
| 1092 | llvm::SmallVectorImpl<char> &utf8) { |
| 1093 | // Get length. |
| 1094 | int len = ::WideCharToMultiByte(CP_UTF8, 0, |
| 1095 | utf16, utf16_len, |
| 1096 | utf8.begin(), 0, |
| 1097 | NULL, NULL); |
| 1098 | |
| 1099 | if (len == 0) |
| 1100 | return llvm::windows_error(::GetLastError()); |
| 1101 | |
| 1102 | utf8.reserve(len); |
| 1103 | utf8.set_size(len); |
| 1104 | |
| 1105 | // Now do the actual conversion. |
| 1106 | len = ::WideCharToMultiByte(CP_UTF8, 0, |
| 1107 | utf16, utf16_len, |
| 1108 | utf8.data(), utf8.size(), |
| 1109 | NULL, NULL); |
| 1110 | |
| 1111 | if (len == 0) |
| 1112 | return llvm::windows_error(::GetLastError()); |
| 1113 | |
| 1114 | // Make utf8 null terminated. |
| 1115 | utf8.push_back(0); |
| 1116 | utf8.pop_back(); |
| 1117 | |
| 1118 | return llvm::error_code::success(); |
| 1119 | } |
| 1120 | } // end namespace windows |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 1121 | } // end namespace sys |
| 1122 | } // end namespace llvm |