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" |
Rafael Espindola | 5c4f829 | 2014-06-11 19:05:50 +0000 | [diff] [blame] | 20 | #include "llvm/Support/WindowsError.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 | |
NAKAMURA Takumi | 04d39d7 | 2014-02-12 11:50:22 +0000 | [diff] [blame] | 26 | // These two headers must be included last, and make sure shlobj is required |
| 27 | // after Windows.h to make sure it picks up our definition of _WIN32_WINNT |
Reid Kleckner | d59e2fa | 2014-02-12 21:26:20 +0000 | [diff] [blame] | 28 | #include "WindowsSupport.h" |
NAKAMURA Takumi | 04d39d7 | 2014-02-12 11:50:22 +0000 | [diff] [blame] | 29 | #include <shlobj.h> |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 30 | |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 31 | #undef max |
| 32 | |
Michael J. Spencer | 6025247 | 2010-12-03 18:03:28 +0000 | [diff] [blame] | 33 | // MinGW doesn't define this. |
Michael J. Spencer | 521c321 | 2010-12-03 18:04:11 +0000 | [diff] [blame] | 34 | #ifndef _ERRNO_T_DEFINED |
| 35 | #define _ERRNO_T_DEFINED |
| 36 | typedef int errno_t; |
Michael J. Spencer | 6025247 | 2010-12-03 18:03:28 +0000 | [diff] [blame] | 37 | #endif |
| 38 | |
Reid Kleckner | 11da004 | 2013-08-07 20:19:31 +0000 | [diff] [blame] | 39 | #ifdef _MSC_VER |
| 40 | # pragma comment(lib, "advapi32.lib") // This provides CryptAcquireContextW. |
| 41 | #endif |
| 42 | |
Michael J. Spencer | 9fc1d9d | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 43 | using namespace llvm; |
| 44 | |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 45 | using llvm::sys::windows::UTF8ToUTF16; |
| 46 | using llvm::sys::windows::UTF16ToUTF8; |
| 47 | |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 48 | static error_code windows_error(DWORD E) { |
Rafael Espindola | 5c4f829 | 2014-06-11 19:05:50 +0000 | [diff] [blame] | 49 | return mapWindowsError(E); |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Rafael Espindola | 7a0b640 | 2014-02-24 03:07:41 +0000 | [diff] [blame] | 52 | static error_code TempDir(SmallVectorImpl<char> &Result) { |
| 53 | SmallVector<wchar_t, 64> Res; |
Rafael Espindola | 37b012d | 2014-02-23 15:16:03 +0000 | [diff] [blame] | 54 | retry_temp_dir: |
Rafael Espindola | 7a0b640 | 2014-02-24 03:07:41 +0000 | [diff] [blame] | 55 | DWORD Len = ::GetTempPathW(Res.capacity(), Res.begin()); |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 56 | |
Rafael Espindola | 7a0b640 | 2014-02-24 03:07:41 +0000 | [diff] [blame] | 57 | if (Len == 0) |
Rafael Espindola | 37b012d | 2014-02-23 15:16:03 +0000 | [diff] [blame] | 58 | return windows_error(::GetLastError()); |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 59 | |
Rafael Espindola | 7a0b640 | 2014-02-24 03:07:41 +0000 | [diff] [blame] | 60 | if (Len > Res.capacity()) { |
| 61 | Res.reserve(Len); |
Rafael Espindola | 37b012d | 2014-02-23 15:16:03 +0000 | [diff] [blame] | 62 | goto retry_temp_dir; |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Rafael Espindola | 7a0b640 | 2014-02-24 03:07:41 +0000 | [diff] [blame] | 65 | Res.set_size(Len); |
| 66 | return UTF16ToUTF8(Res.begin(), Res.size(), Result); |
Rafael Espindola | 37b012d | 2014-02-23 15:16:03 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | static bool is_separator(const wchar_t value) { |
| 70 | switch (value) { |
| 71 | case L'\\': |
| 72 | case L'/': |
| 73 | return true; |
| 74 | default: |
| 75 | return false; |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 76 | } |
Michael J. Spencer | 9fc1d9d | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 79 | namespace llvm { |
| 80 | namespace sys { |
Michael J. Spencer | 20daa28 | 2010-12-07 01:22:31 +0000 | [diff] [blame] | 81 | namespace fs { |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 82 | |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 83 | std::string getMainExecutable(const char *argv0, void *MainExecAddr) { |
David Majnemer | 17a4496 | 2013-10-07 09:52:36 +0000 | [diff] [blame] | 84 | SmallVector<wchar_t, MAX_PATH> PathName; |
| 85 | DWORD Size = ::GetModuleFileNameW(NULL, PathName.data(), PathName.capacity()); |
| 86 | |
| 87 | // A zero return value indicates a failure other than insufficient space. |
| 88 | if (Size == 0) |
| 89 | return ""; |
| 90 | |
| 91 | // Insufficient space is determined by a return value equal to the size of |
| 92 | // the buffer passed in. |
| 93 | if (Size == PathName.capacity()) |
| 94 | return ""; |
| 95 | |
| 96 | // On success, GetModuleFileNameW returns the number of characters written to |
| 97 | // the buffer not including the NULL terminator. |
| 98 | PathName.set_size(Size); |
| 99 | |
| 100 | // Convert the result from UTF-16 to UTF-8. |
| 101 | SmallVector<char, MAX_PATH> PathNameUTF8; |
| 102 | if (UTF16ToUTF8(PathName.data(), PathName.size(), PathNameUTF8)) |
| 103 | return ""; |
| 104 | |
| 105 | return std::string(PathNameUTF8.data()); |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Rafael Espindola | d123099 | 2013-07-29 21:55:38 +0000 | [diff] [blame] | 108 | UniqueID file_status::getUniqueID() const { |
Rafael Espindola | 7f822a9 | 2013-07-29 21:26:49 +0000 | [diff] [blame] | 109 | // The file is uniquely identified by the volume serial number along |
| 110 | // with the 64-bit file identifier. |
| 111 | uint64_t FileID = (static_cast<uint64_t>(FileIndexHigh) << 32ULL) | |
| 112 | static_cast<uint64_t>(FileIndexLow); |
| 113 | |
| 114 | return UniqueID(VolumeSerialNumber, FileID); |
| 115 | } |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 116 | |
Rafael Espindola | be3ede7 | 2013-06-20 21:51:49 +0000 | [diff] [blame] | 117 | TimeValue file_status::getLastModificationTime() const { |
Rafael Espindola | db5d8fe | 2013-06-20 18:42:04 +0000 | [diff] [blame] | 118 | ULARGE_INTEGER UI; |
| 119 | UI.LowPart = LastWriteTimeLow; |
| 120 | UI.HighPart = LastWriteTimeHigh; |
| 121 | |
| 122 | TimeValue Ret; |
| 123 | Ret.fromWin32Time(UI.QuadPart); |
| 124 | return Ret; |
| 125 | } |
| 126 | |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 127 | error_code current_path(SmallVectorImpl<char> &result) { |
David Majnemer | 61eae2e | 2013-10-07 01:00:07 +0000 | [diff] [blame] | 128 | SmallVector<wchar_t, MAX_PATH> cur_path; |
| 129 | DWORD len = MAX_PATH; |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 130 | |
David Majnemer | 61eae2e | 2013-10-07 01:00:07 +0000 | [diff] [blame] | 131 | do { |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 132 | cur_path.reserve(len); |
David Majnemer | 61eae2e | 2013-10-07 01:00:07 +0000 | [diff] [blame] | 133 | len = ::GetCurrentDirectoryW(cur_path.capacity(), cur_path.data()); |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 134 | |
David Majnemer | 61eae2e | 2013-10-07 01:00:07 +0000 | [diff] [blame] | 135 | // A zero return value indicates a failure other than insufficient space. |
| 136 | if (len == 0) |
| 137 | return windows_error(::GetLastError()); |
| 138 | |
| 139 | // If there's insufficient space, the len returned is larger than the len |
| 140 | // given. |
| 141 | } while (len > cur_path.capacity()); |
| 142 | |
| 143 | // On success, GetCurrentDirectoryW returns the number of characters not |
| 144 | // including the null-terminator. |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 145 | cur_path.set_size(len); |
Aaron Ballman | b16cf53 | 2013-08-16 17:53:28 +0000 | [diff] [blame] | 146 | return UTF16ToUTF8(cur_path.begin(), cur_path.size(), result); |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 149 | error_code create_directory(const Twine &path, bool IgnoreExisting) { |
Michael J. Spencer | 31e310c | 2010-12-03 05:42:11 +0000 | [diff] [blame] | 150 | SmallString<128> path_storage; |
| 151 | SmallVector<wchar_t, 128> path_utf16; |
| 152 | |
| 153 | if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage), |
| 154 | path_utf16)) |
| 155 | return ec; |
| 156 | |
| 157 | if (!::CreateDirectoryW(path_utf16.begin(), NULL)) { |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 158 | DWORD LastError = ::GetLastError(); |
| 159 | if (LastError != ERROR_ALREADY_EXISTS || !IgnoreExisting) |
| 160 | return windows_error(LastError); |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 161 | } |
Michael J. Spencer | 31e310c | 2010-12-03 05:42:11 +0000 | [diff] [blame] | 162 | |
Rafael Espindola | 03bddfe | 2014-05-31 01:37:45 +0000 | [diff] [blame] | 163 | return error_code(); |
Michael J. Spencer | 31e310c | 2010-12-03 05:42:11 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Saleem Abdulrasool | afc50b3ed | 2014-03-11 22:05:42 +0000 | [diff] [blame] | 166 | error_code normalize_separators(SmallVectorImpl<char> &Path) { |
| 167 | (void) Path; |
Rafael Espindola | 03bddfe | 2014-05-31 01:37:45 +0000 | [diff] [blame] | 168 | return error_code(); |
Saleem Abdulrasool | afc50b3ed | 2014-03-11 22:05:42 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Rafael Espindola | 83f858e | 2014-03-11 18:40:24 +0000 | [diff] [blame] | 171 | // We can't use symbolic links for windows. |
| 172 | error_code create_link(const Twine &to, const Twine &from) { |
Michael J. Spencer | e0c4560 | 2010-12-03 05:58:41 +0000 | [diff] [blame] | 173 | // Get arguments. |
| 174 | SmallString<128> from_storage; |
| 175 | SmallString<128> to_storage; |
| 176 | StringRef f = from.toStringRef(from_storage); |
| 177 | StringRef t = to.toStringRef(to_storage); |
| 178 | |
| 179 | // Convert to utf-16. |
| 180 | SmallVector<wchar_t, 128> wide_from; |
| 181 | SmallVector<wchar_t, 128> wide_to; |
| 182 | if (error_code ec = UTF8ToUTF16(f, wide_from)) return ec; |
| 183 | if (error_code ec = UTF8ToUTF16(t, wide_to)) return ec; |
| 184 | |
| 185 | if (!::CreateHardLinkW(wide_from.begin(), wide_to.begin(), NULL)) |
Michael J. Spencer | 66a1f86 | 2010-12-04 18:45:32 +0000 | [diff] [blame] | 186 | return windows_error(::GetLastError()); |
Michael J. Spencer | e0c4560 | 2010-12-03 05:58:41 +0000 | [diff] [blame] | 187 | |
Rafael Espindola | 03bddfe | 2014-05-31 01:37:45 +0000 | [diff] [blame] | 188 | return error_code(); |
Michael J. Spencer | e0c4560 | 2010-12-03 05:58:41 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 191 | error_code remove(const Twine &path, bool IgnoreNonExisting) { |
Michael J. Spencer | 6e74e11 | 2010-12-03 17:53:43 +0000 | [diff] [blame] | 192 | SmallString<128> path_storage; |
| 193 | SmallVector<wchar_t, 128> path_utf16; |
| 194 | |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 195 | file_status ST; |
| 196 | if (error_code EC = status(path, ST)) { |
Rafael Espindola | 5c4f829 | 2014-06-11 19:05:50 +0000 | [diff] [blame] | 197 | if (EC != std::errc::no_such_file_or_directory || !IgnoreNonExisting) |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 198 | return EC; |
Rafael Espindola | 03bddfe | 2014-05-31 01:37:45 +0000 | [diff] [blame] | 199 | return error_code(); |
Rafael Espindola | 107b74c | 2013-07-31 00:10:25 +0000 | [diff] [blame] | 200 | } |
Michael J. Spencer | 153749b | 2011-01-05 16:39:22 +0000 | [diff] [blame] | 201 | |
Michael J. Spencer | 6e74e11 | 2010-12-03 17:53:43 +0000 | [diff] [blame] | 202 | if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage), |
| 203 | path_utf16)) |
| 204 | return ec; |
| 205 | |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 206 | if (ST.type() == file_type::directory_file) { |
Michael J. Spencer | 153749b | 2011-01-05 16:39:22 +0000 | [diff] [blame] | 207 | if (!::RemoveDirectoryW(c_str(path_utf16))) { |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 208 | error_code EC = windows_error(::GetLastError()); |
Rafael Espindola | 5c4f829 | 2014-06-11 19:05:50 +0000 | [diff] [blame] | 209 | if (EC != std::errc::no_such_file_or_directory || !IgnoreNonExisting) |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 210 | return EC; |
| 211 | } |
Rafael Espindola | 03bddfe | 2014-05-31 01:37:45 +0000 | [diff] [blame] | 212 | return error_code(); |
Michael J. Spencer | 153749b | 2011-01-05 16:39:22 +0000 | [diff] [blame] | 213 | } |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 214 | if (!::DeleteFileW(c_str(path_utf16))) { |
| 215 | error_code EC = windows_error(::GetLastError()); |
Rafael Espindola | 5c4f829 | 2014-06-11 19:05:50 +0000 | [diff] [blame] | 216 | if (EC != std::errc::no_such_file_or_directory || !IgnoreNonExisting) |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 217 | return EC; |
| 218 | } |
Rafael Espindola | 03bddfe | 2014-05-31 01:37:45 +0000 | [diff] [blame] | 219 | return error_code(); |
Michael J. Spencer | 6e74e11 | 2010-12-03 17:53:43 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Michael J. Spencer | 409f556 | 2010-12-03 17:53:55 +0000 | [diff] [blame] | 222 | error_code rename(const Twine &from, const Twine &to) { |
| 223 | // Get arguments. |
| 224 | SmallString<128> from_storage; |
| 225 | SmallString<128> to_storage; |
| 226 | StringRef f = from.toStringRef(from_storage); |
| 227 | StringRef t = to.toStringRef(to_storage); |
| 228 | |
| 229 | // Convert to utf-16. |
| 230 | SmallVector<wchar_t, 128> wide_from; |
| 231 | SmallVector<wchar_t, 128> wide_to; |
| 232 | if (error_code ec = UTF8ToUTF16(f, wide_from)) return ec; |
| 233 | if (error_code ec = UTF8ToUTF16(t, wide_to)) return ec; |
| 234 | |
Rafael Espindola | 03bddfe | 2014-05-31 01:37:45 +0000 | [diff] [blame] | 235 | error_code ec = error_code(); |
NAKAMURA Takumi | 3b7f995 | 2012-05-08 14:31:46 +0000 | [diff] [blame] | 236 | for (int i = 0; i < 2000; i++) { |
| 237 | if (::MoveFileExW(wide_from.begin(), wide_to.begin(), |
| 238 | MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING)) |
Rafael Espindola | 03bddfe | 2014-05-31 01:37:45 +0000 | [diff] [blame] | 239 | return error_code(); |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 240 | DWORD LastError = ::GetLastError(); |
| 241 | if (LastError != ERROR_ACCESS_DENIED) |
NAKAMURA Takumi | 3b7f995 | 2012-05-08 14:31:46 +0000 | [diff] [blame] | 242 | break; |
| 243 | // Retry MoveFile() at ACCESS_DENIED. |
| 244 | // System scanners (eg. indexer) might open the source file when |
| 245 | // It is written and closed. |
| 246 | ::Sleep(1); |
| 247 | } |
Michael J. Spencer | 409f556 | 2010-12-03 17:53:55 +0000 | [diff] [blame] | 248 | |
NAKAMURA Takumi | 3b7f995 | 2012-05-08 14:31:46 +0000 | [diff] [blame] | 249 | return ec; |
Michael J. Spencer | 409f556 | 2010-12-03 17:53:55 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Michael J. Spencer | c20a032 | 2010-12-03 17:54:07 +0000 | [diff] [blame] | 252 | error_code resize_file(const Twine &path, uint64_t size) { |
| 253 | SmallString<128> path_storage; |
| 254 | SmallVector<wchar_t, 128> path_utf16; |
| 255 | |
| 256 | if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage), |
| 257 | path_utf16)) |
| 258 | return ec; |
| 259 | |
Michael J. Spencer | 20abb20 | 2012-12-03 22:09:31 +0000 | [diff] [blame] | 260 | 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] | 261 | if (fd == -1) |
Rafael Espindola | 7e577f7 | 2014-06-12 02:00:39 +0000 | [diff] [blame] | 262 | return error_code(errno, std::generic_category()); |
Michael J. Spencer | ca242f2 | 2010-12-03 18:48:56 +0000 | [diff] [blame] | 263 | #ifdef HAVE__CHSIZE_S |
Michael J. Spencer | c20a032 | 2010-12-03 17:54:07 +0000 | [diff] [blame] | 264 | errno_t error = ::_chsize_s(fd, size); |
Michael J. Spencer | ca242f2 | 2010-12-03 18:48:56 +0000 | [diff] [blame] | 265 | #else |
| 266 | errno_t error = ::_chsize(fd, size); |
| 267 | #endif |
Michael J. Spencer | c20a032 | 2010-12-03 17:54:07 +0000 | [diff] [blame] | 268 | ::close(fd); |
Rafael Espindola | 7e577f7 | 2014-06-12 02:00:39 +0000 | [diff] [blame] | 269 | return error_code(error, std::generic_category()); |
Michael J. Spencer | c20a032 | 2010-12-03 17:54:07 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 272 | error_code exists(const Twine &path, bool &result) { |
| 273 | SmallString<128> path_storage; |
| 274 | SmallVector<wchar_t, 128> path_utf16; |
| 275 | |
| 276 | if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage), |
| 277 | path_utf16)) |
| 278 | return ec; |
| 279 | |
| 280 | DWORD attributes = ::GetFileAttributesW(path_utf16.begin()); |
| 281 | |
| 282 | if (attributes == INVALID_FILE_ATTRIBUTES) { |
| 283 | // See if the file didn't actually exist. |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 284 | DWORD LastError = ::GetLastError(); |
| 285 | if (LastError != ERROR_FILE_NOT_FOUND && |
| 286 | LastError != ERROR_PATH_NOT_FOUND) |
| 287 | return windows_error(LastError); |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 288 | result = false; |
| 289 | } else |
| 290 | result = true; |
Rafael Espindola | 03bddfe | 2014-05-31 01:37:45 +0000 | [diff] [blame] | 291 | return error_code(); |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Rafael Espindola | a1280c1 | 2013-06-18 20:56:38 +0000 | [diff] [blame] | 294 | bool can_write(const Twine &Path) { |
| 295 | // FIXME: take security attributes into account. |
| 296 | SmallString<128> PathStorage; |
| 297 | SmallVector<wchar_t, 128> PathUtf16; |
| 298 | |
| 299 | if (UTF8ToUTF16(Path.toStringRef(PathStorage), PathUtf16)) |
| 300 | return false; |
| 301 | |
| 302 | DWORD Attr = ::GetFileAttributesW(PathUtf16.begin()); |
| 303 | return (Attr != INVALID_FILE_ATTRIBUTES) && !(Attr & FILE_ATTRIBUTE_READONLY); |
| 304 | } |
| 305 | |
Rafael Espindola | b0a5c96 | 2013-06-14 19:38:45 +0000 | [diff] [blame] | 306 | bool can_execute(const Twine &Path) { |
| 307 | SmallString<128> PathStorage; |
| 308 | SmallVector<wchar_t, 128> PathUtf16; |
| 309 | |
| 310 | if (UTF8ToUTF16(Path.toStringRef(PathStorage), PathUtf16)) |
| 311 | return false; |
| 312 | |
| 313 | DWORD Attr = ::GetFileAttributesW(PathUtf16.begin()); |
| 314 | return Attr != INVALID_FILE_ATTRIBUTES; |
| 315 | } |
| 316 | |
Michael J. Spencer | 203d780 | 2011-12-12 06:04:28 +0000 | [diff] [blame] | 317 | bool equivalent(file_status A, file_status B) { |
| 318 | assert(status_known(A) && status_known(B)); |
| 319 | return A.FileIndexHigh == B.FileIndexHigh && |
| 320 | A.FileIndexLow == B.FileIndexLow && |
| 321 | A.FileSizeHigh == B.FileSizeHigh && |
| 322 | A.FileSizeLow == B.FileSizeLow && |
| 323 | A.LastWriteTimeHigh == B.LastWriteTimeHigh && |
| 324 | A.LastWriteTimeLow == B.LastWriteTimeLow && |
| 325 | A.VolumeSerialNumber == B.VolumeSerialNumber; |
| 326 | } |
| 327 | |
Michael J. Spencer | 376d387 | 2010-12-03 18:49:13 +0000 | [diff] [blame] | 328 | error_code equivalent(const Twine &A, const Twine &B, bool &result) { |
Michael J. Spencer | 203d780 | 2011-12-12 06:04:28 +0000 | [diff] [blame] | 329 | file_status fsA, fsB; |
| 330 | if (error_code ec = status(A, fsA)) return ec; |
| 331 | if (error_code ec = status(B, fsB)) return ec; |
| 332 | result = equivalent(fsA, fsB); |
Rafael Espindola | 03bddfe | 2014-05-31 01:37:45 +0000 | [diff] [blame] | 333 | return error_code(); |
Michael J. Spencer | 376d387 | 2010-12-03 18:49:13 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Benjamin Kramer | 58994ec | 2011-08-20 21:36:38 +0000 | [diff] [blame] | 336 | static bool isReservedName(StringRef path) { |
| 337 | // This list of reserved names comes from MSDN, at: |
| 338 | // http://msdn.microsoft.com/en-us/library/aa365247%28v=vs.85%29.aspx |
| 339 | static const char *sReservedNames[] = { "nul", "con", "prn", "aux", |
| 340 | "com1", "com2", "com3", "com4", "com5", "com6", |
| 341 | "com7", "com8", "com9", "lpt1", "lpt2", "lpt3", |
| 342 | "lpt4", "lpt5", "lpt6", "lpt7", "lpt8", "lpt9" }; |
| 343 | |
| 344 | // First, check to see if this is a device namespace, which always |
| 345 | // starts with \\.\, since device namespaces are not legal file paths. |
| 346 | if (path.startswith("\\\\.\\")) |
| 347 | return true; |
| 348 | |
| 349 | // Then compare against the list of ancient reserved names |
Craig Topper | 5871321 | 2013-07-15 04:27:47 +0000 | [diff] [blame] | 350 | for (size_t i = 0; i < array_lengthof(sReservedNames); ++i) { |
Benjamin Kramer | 58994ec | 2011-08-20 21:36:38 +0000 | [diff] [blame] | 351 | if (path.equals_lower(sReservedNames[i])) |
| 352 | return true; |
| 353 | } |
| 354 | |
| 355 | // The path isn't what we consider reserved. |
| 356 | return false; |
| 357 | } |
| 358 | |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 359 | static error_code getStatus(HANDLE FileHandle, file_status &Result) { |
| 360 | if (FileHandle == INVALID_HANDLE_VALUE) |
| 361 | goto handle_status_error; |
| 362 | |
NAKAMURA Takumi | 8b01da4 | 2013-07-18 17:00:54 +0000 | [diff] [blame] | 363 | switch (::GetFileType(FileHandle)) { |
| 364 | default: |
Rafael Espindola | 81177c5 | 2013-07-18 18:42:52 +0000 | [diff] [blame] | 365 | llvm_unreachable("Don't know anything about this file type"); |
| 366 | case FILE_TYPE_UNKNOWN: { |
| 367 | DWORD Err = ::GetLastError(); |
| 368 | if (Err != NO_ERROR) |
| 369 | return windows_error(Err); |
| 370 | Result = file_status(file_type::type_unknown); |
Rafael Espindola | 03bddfe | 2014-05-31 01:37:45 +0000 | [diff] [blame] | 371 | return error_code(); |
Rafael Espindola | 81177c5 | 2013-07-18 18:42:52 +0000 | [diff] [blame] | 372 | } |
NAKAMURA Takumi | 8b01da4 | 2013-07-18 17:00:54 +0000 | [diff] [blame] | 373 | case FILE_TYPE_DISK: |
| 374 | break; |
| 375 | case FILE_TYPE_CHAR: |
| 376 | Result = file_status(file_type::character_file); |
Rafael Espindola | 03bddfe | 2014-05-31 01:37:45 +0000 | [diff] [blame] | 377 | return error_code(); |
NAKAMURA Takumi | 8b01da4 | 2013-07-18 17:00:54 +0000 | [diff] [blame] | 378 | case FILE_TYPE_PIPE: |
| 379 | Result = file_status(file_type::fifo_file); |
Rafael Espindola | 03bddfe | 2014-05-31 01:37:45 +0000 | [diff] [blame] | 380 | return error_code(); |
NAKAMURA Takumi | 8b01da4 | 2013-07-18 17:00:54 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 383 | BY_HANDLE_FILE_INFORMATION Info; |
| 384 | if (!::GetFileInformationByHandle(FileHandle, &Info)) |
| 385 | goto handle_status_error; |
| 386 | |
Rafael Espindola | a5932af | 2013-07-30 20:25:53 +0000 | [diff] [blame] | 387 | { |
| 388 | file_type Type = (Info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) |
| 389 | ? file_type::directory_file |
| 390 | : file_type::regular_file; |
| 391 | Result = |
| 392 | file_status(Type, Info.ftLastWriteTime.dwHighDateTime, |
| 393 | Info.ftLastWriteTime.dwLowDateTime, |
| 394 | Info.dwVolumeSerialNumber, Info.nFileSizeHigh, |
| 395 | Info.nFileSizeLow, Info.nFileIndexHigh, Info.nFileIndexLow); |
Rafael Espindola | 03bddfe | 2014-05-31 01:37:45 +0000 | [diff] [blame] | 396 | return error_code(); |
Rafael Espindola | a5932af | 2013-07-30 20:25:53 +0000 | [diff] [blame] | 397 | } |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 398 | |
| 399 | handle_status_error: |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 400 | DWORD LastError = ::GetLastError(); |
| 401 | if (LastError == ERROR_FILE_NOT_FOUND || |
| 402 | LastError == ERROR_PATH_NOT_FOUND) |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 403 | Result = file_status(file_type::file_not_found); |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 404 | else if (LastError == ERROR_SHARING_VIOLATION) |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 405 | Result = file_status(file_type::type_unknown); |
Rafael Espindola | 107b74c | 2013-07-31 00:10:25 +0000 | [diff] [blame] | 406 | else |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 407 | Result = file_status(file_type::status_error); |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 408 | return windows_error(LastError); |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Michael J. Spencer | db5576a | 2010-12-04 00:32:40 +0000 | [diff] [blame] | 411 | error_code status(const Twine &path, file_status &result) { |
| 412 | SmallString<128> path_storage; |
| 413 | SmallVector<wchar_t, 128> path_utf16; |
| 414 | |
NAKAMURA Takumi | a3d4749 | 2011-03-16 02:53:32 +0000 | [diff] [blame] | 415 | StringRef path8 = path.toStringRef(path_storage); |
Benjamin Kramer | 58994ec | 2011-08-20 21:36:38 +0000 | [diff] [blame] | 416 | if (isReservedName(path8)) { |
NAKAMURA Takumi | a3d4749 | 2011-03-16 02:53:32 +0000 | [diff] [blame] | 417 | result = file_status(file_type::character_file); |
Rafael Espindola | 03bddfe | 2014-05-31 01:37:45 +0000 | [diff] [blame] | 418 | return error_code(); |
NAKAMURA Takumi | a3d4749 | 2011-03-16 02:53:32 +0000 | [diff] [blame] | 419 | } |
| 420 | |
Michael J. Spencer | 203d780 | 2011-12-12 06:04:28 +0000 | [diff] [blame] | 421 | if (error_code ec = UTF8ToUTF16(path8, path_utf16)) |
Michael J. Spencer | db5576a | 2010-12-04 00:32:40 +0000 | [diff] [blame] | 422 | return ec; |
| 423 | |
| 424 | DWORD attr = ::GetFileAttributesW(path_utf16.begin()); |
| 425 | if (attr == INVALID_FILE_ATTRIBUTES) |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 426 | return getStatus(INVALID_HANDLE_VALUE, result); |
Michael J. Spencer | db5576a | 2010-12-04 00:32:40 +0000 | [diff] [blame] | 427 | |
| 428 | // Handle reparse points. |
| 429 | if (attr & FILE_ATTRIBUTE_REPARSE_POINT) { |
Michael J. Spencer | 513f1b6 | 2011-12-12 06:03:33 +0000 | [diff] [blame] | 430 | ScopedFileHandle h( |
Michael J. Spencer | db5576a | 2010-12-04 00:32:40 +0000 | [diff] [blame] | 431 | ::CreateFileW(path_utf16.begin(), |
| 432 | 0, // Attributes only. |
| 433 | FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, |
| 434 | NULL, |
| 435 | OPEN_EXISTING, |
| 436 | FILE_FLAG_BACKUP_SEMANTICS, |
| 437 | 0)); |
Michael J. Spencer | 513f1b6 | 2011-12-12 06:03:33 +0000 | [diff] [blame] | 438 | if (!h) |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 439 | return getStatus(INVALID_HANDLE_VALUE, result); |
Michael J. Spencer | db5576a | 2010-12-04 00:32:40 +0000 | [diff] [blame] | 440 | } |
| 441 | |
Rafael Espindola | a5932af | 2013-07-30 20:25:53 +0000 | [diff] [blame] | 442 | ScopedFileHandle h( |
| 443 | ::CreateFileW(path_utf16.begin(), 0, // Attributes only. |
Michael J. Spencer | 203d780 | 2011-12-12 06:04:28 +0000 | [diff] [blame] | 444 | FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, |
Rafael Espindola | a5932af | 2013-07-30 20:25:53 +0000 | [diff] [blame] | 445 | NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0)); |
Michael J. Spencer | 203d780 | 2011-12-12 06:04:28 +0000 | [diff] [blame] | 446 | if (!h) |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 447 | return getStatus(INVALID_HANDLE_VALUE, result); |
Michael J. Spencer | db5576a | 2010-12-04 00:32:40 +0000 | [diff] [blame] | 448 | |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 449 | return getStatus(h, result); |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 450 | } |
Michael J. Spencer | db5576a | 2010-12-04 00:32:40 +0000 | [diff] [blame] | 451 | |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 452 | error_code status(int FD, file_status &Result) { |
| 453 | HANDLE FileHandle = reinterpret_cast<HANDLE>(_get_osfhandle(FD)); |
| 454 | return getStatus(FileHandle, Result); |
Michael J. Spencer | db5576a | 2010-12-04 00:32:40 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Rafael Espindola | 4a3365c | 2013-06-20 20:56:14 +0000 | [diff] [blame] | 457 | error_code setLastModificationAndAccessTime(int FD, TimeValue Time) { |
| 458 | ULARGE_INTEGER UI; |
| 459 | UI.QuadPart = Time.toWin32Time(); |
| 460 | FILETIME FT; |
| 461 | FT.dwLowDateTime = UI.LowPart; |
| 462 | FT.dwHighDateTime = UI.HighPart; |
| 463 | HANDLE FileHandle = reinterpret_cast<HANDLE>(_get_osfhandle(FD)); |
| 464 | if (!SetFileTime(FileHandle, NULL, &FT, &FT)) |
| 465 | return windows_error(::GetLastError()); |
Rafael Espindola | 03bddfe | 2014-05-31 01:37:45 +0000 | [diff] [blame] | 466 | return error_code(); |
Rafael Espindola | 4a3365c | 2013-06-20 20:56:14 +0000 | [diff] [blame] | 467 | } |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 468 | |
Michael J. Spencer | 42ad29f | 2013-03-14 00:20:10 +0000 | [diff] [blame] | 469 | 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] | 470 | FileDescriptor = FD; |
| 471 | // Make sure that the requested size fits within SIZE_T. |
| 472 | if (Size > std::numeric_limits<SIZE_T>::max()) { |
Michael J. Spencer | d932d41 | 2013-03-15 19:25:47 +0000 | [diff] [blame] | 473 | if (FileDescriptor) { |
Michael J. Spencer | 42ad29f | 2013-03-14 00:20:10 +0000 | [diff] [blame] | 474 | if (CloseFD) |
| 475 | _close(FileDescriptor); |
Michael J. Spencer | d932d41 | 2013-03-15 19:25:47 +0000 | [diff] [blame] | 476 | } else |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 477 | ::CloseHandle(FileHandle); |
Rafael Espindola | ed6882b | 2014-06-12 11:58:49 +0000 | [diff] [blame] | 478 | return std::make_error_code(std::errc::invalid_argument); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | DWORD flprotect; |
| 482 | switch (Mode) { |
| 483 | case readonly: flprotect = PAGE_READONLY; break; |
| 484 | case readwrite: flprotect = PAGE_READWRITE; break; |
| 485 | case priv: flprotect = PAGE_WRITECOPY; break; |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 486 | } |
| 487 | |
David Majnemer | 17a4496 | 2013-10-07 09:52:36 +0000 | [diff] [blame] | 488 | FileMappingHandle = |
| 489 | ::CreateFileMappingW(FileHandle, 0, flprotect, |
| 490 | (Offset + Size) >> 32, |
| 491 | (Offset + Size) & 0xffffffff, |
| 492 | 0); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 493 | if (FileMappingHandle == NULL) { |
| 494 | error_code ec = windows_error(GetLastError()); |
Michael J. Spencer | d932d41 | 2013-03-15 19:25:47 +0000 | [diff] [blame] | 495 | if (FileDescriptor) { |
Michael J. Spencer | 42ad29f | 2013-03-14 00:20:10 +0000 | [diff] [blame] | 496 | if (CloseFD) |
| 497 | _close(FileDescriptor); |
Michael J. Spencer | d932d41 | 2013-03-15 19:25:47 +0000 | [diff] [blame] | 498 | } else |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 499 | ::CloseHandle(FileHandle); |
| 500 | return ec; |
| 501 | } |
| 502 | |
| 503 | DWORD dwDesiredAccess; |
| 504 | switch (Mode) { |
| 505 | case readonly: dwDesiredAccess = FILE_MAP_READ; break; |
| 506 | case readwrite: dwDesiredAccess = FILE_MAP_WRITE; break; |
| 507 | case priv: dwDesiredAccess = FILE_MAP_COPY; break; |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 508 | } |
| 509 | Mapping = ::MapViewOfFile(FileMappingHandle, |
| 510 | dwDesiredAccess, |
| 511 | Offset >> 32, |
| 512 | Offset & 0xffffffff, |
| 513 | Size); |
| 514 | if (Mapping == NULL) { |
| 515 | error_code ec = windows_error(GetLastError()); |
| 516 | ::CloseHandle(FileMappingHandle); |
Michael J. Spencer | d932d41 | 2013-03-15 19:25:47 +0000 | [diff] [blame] | 517 | if (FileDescriptor) { |
Michael J. Spencer | 42ad29f | 2013-03-14 00:20:10 +0000 | [diff] [blame] | 518 | if (CloseFD) |
| 519 | _close(FileDescriptor); |
Michael J. Spencer | d932d41 | 2013-03-15 19:25:47 +0000 | [diff] [blame] | 520 | } else |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 521 | ::CloseHandle(FileHandle); |
| 522 | return ec; |
| 523 | } |
| 524 | |
| 525 | if (Size == 0) { |
| 526 | MEMORY_BASIC_INFORMATION mbi; |
| 527 | SIZE_T Result = VirtualQuery(Mapping, &mbi, sizeof(mbi)); |
| 528 | if (Result == 0) { |
| 529 | error_code ec = windows_error(GetLastError()); |
| 530 | ::UnmapViewOfFile(Mapping); |
| 531 | ::CloseHandle(FileMappingHandle); |
Michael J. Spencer | d932d41 | 2013-03-15 19:25:47 +0000 | [diff] [blame] | 532 | if (FileDescriptor) { |
Michael J. Spencer | 42ad29f | 2013-03-14 00:20:10 +0000 | [diff] [blame] | 533 | if (CloseFD) |
| 534 | _close(FileDescriptor); |
Michael J. Spencer | d932d41 | 2013-03-15 19:25:47 +0000 | [diff] [blame] | 535 | } else |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 536 | ::CloseHandle(FileHandle); |
| 537 | return ec; |
| 538 | } |
| 539 | Size = mbi.RegionSize; |
| 540 | } |
Michael J. Spencer | 42ad29f | 2013-03-14 00:20:10 +0000 | [diff] [blame] | 541 | |
| 542 | // Close all the handles except for the view. It will keep the other handles |
| 543 | // alive. |
| 544 | ::CloseHandle(FileMappingHandle); |
Michael J. Spencer | d932d41 | 2013-03-15 19:25:47 +0000 | [diff] [blame] | 545 | if (FileDescriptor) { |
Michael J. Spencer | 42ad29f | 2013-03-14 00:20:10 +0000 | [diff] [blame] | 546 | if (CloseFD) |
| 547 | _close(FileDescriptor); // Also closes FileHandle. |
Michael J. Spencer | d932d41 | 2013-03-15 19:25:47 +0000 | [diff] [blame] | 548 | } else |
Michael J. Spencer | 42ad29f | 2013-03-14 00:20:10 +0000 | [diff] [blame] | 549 | ::CloseHandle(FileHandle); |
Rafael Espindola | 03bddfe | 2014-05-31 01:37:45 +0000 | [diff] [blame] | 550 | return error_code(); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | mapped_file_region::mapped_file_region(const Twine &path, |
| 554 | mapmode mode, |
| 555 | uint64_t length, |
| 556 | uint64_t offset, |
NAKAMURA Takumi | edf7615 | 2013-08-22 15:14:45 +0000 | [diff] [blame] | 557 | error_code &ec) |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 558 | : Mode(mode) |
| 559 | , Size(length) |
| 560 | , Mapping() |
| 561 | , FileDescriptor() |
| 562 | , FileHandle(INVALID_HANDLE_VALUE) |
| 563 | , FileMappingHandle() { |
| 564 | SmallString<128> path_storage; |
| 565 | SmallVector<wchar_t, 128> path_utf16; |
| 566 | |
| 567 | // Convert path to UTF-16. |
Nico Weber | eada11b | 2012-09-25 05:24:16 +0000 | [diff] [blame] | 568 | if ((ec = UTF8ToUTF16(path.toStringRef(path_storage), path_utf16))) |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 569 | return; |
| 570 | |
| 571 | // Get file handle for creating a file mapping. |
| 572 | FileHandle = ::CreateFileW(c_str(path_utf16), |
| 573 | Mode == readonly ? GENERIC_READ |
| 574 | : GENERIC_READ | GENERIC_WRITE, |
| 575 | Mode == readonly ? FILE_SHARE_READ |
| 576 | : 0, |
| 577 | 0, |
| 578 | Mode == readonly ? OPEN_EXISTING |
| 579 | : OPEN_ALWAYS, |
| 580 | Mode == readonly ? FILE_ATTRIBUTE_READONLY |
| 581 | : FILE_ATTRIBUTE_NORMAL, |
| 582 | 0); |
| 583 | if (FileHandle == INVALID_HANDLE_VALUE) { |
| 584 | ec = windows_error(::GetLastError()); |
| 585 | return; |
| 586 | } |
| 587 | |
| 588 | FileDescriptor = 0; |
Michael J. Spencer | 42ad29f | 2013-03-14 00:20:10 +0000 | [diff] [blame] | 589 | ec = init(FileDescriptor, true, offset); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 590 | if (ec) { |
| 591 | Mapping = FileMappingHandle = 0; |
| 592 | FileHandle = INVALID_HANDLE_VALUE; |
| 593 | FileDescriptor = 0; |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | mapped_file_region::mapped_file_region(int fd, |
Michael J. Spencer | 42ad29f | 2013-03-14 00:20:10 +0000 | [diff] [blame] | 598 | bool closefd, |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 599 | mapmode mode, |
| 600 | uint64_t length, |
| 601 | uint64_t offset, |
| 602 | error_code &ec) |
| 603 | : Mode(mode) |
| 604 | , Size(length) |
| 605 | , Mapping() |
| 606 | , FileDescriptor(fd) |
| 607 | , FileHandle(INVALID_HANDLE_VALUE) |
| 608 | , FileMappingHandle() { |
| 609 | FileHandle = reinterpret_cast<HANDLE>(_get_osfhandle(fd)); |
| 610 | if (FileHandle == INVALID_HANDLE_VALUE) { |
Michael J. Spencer | 42ad29f | 2013-03-14 00:20:10 +0000 | [diff] [blame] | 611 | if (closefd) |
| 612 | _close(FileDescriptor); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 613 | FileDescriptor = 0; |
Rafael Espindola | ed6882b | 2014-06-12 11:58:49 +0000 | [diff] [blame] | 614 | ec = std::make_error_code(std::errc::bad_file_descriptor); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 615 | return; |
| 616 | } |
| 617 | |
Michael J. Spencer | 42ad29f | 2013-03-14 00:20:10 +0000 | [diff] [blame] | 618 | ec = init(FileDescriptor, closefd, offset); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 619 | if (ec) { |
| 620 | Mapping = FileMappingHandle = 0; |
| 621 | FileHandle = INVALID_HANDLE_VALUE; |
| 622 | FileDescriptor = 0; |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | mapped_file_region::~mapped_file_region() { |
| 627 | if (Mapping) |
| 628 | ::UnmapViewOfFile(Mapping); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 629 | } |
| 630 | |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 631 | mapped_file_region::mapped_file_region(mapped_file_region &&other) |
| 632 | : Mode(other.Mode) |
| 633 | , Size(other.Size) |
| 634 | , Mapping(other.Mapping) |
| 635 | , FileDescriptor(other.FileDescriptor) |
| 636 | , FileHandle(other.FileHandle) |
| 637 | , FileMappingHandle(other.FileMappingHandle) { |
| 638 | other.Mapping = other.FileMappingHandle = 0; |
| 639 | other.FileHandle = INVALID_HANDLE_VALUE; |
| 640 | other.FileDescriptor = 0; |
| 641 | } |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 642 | |
| 643 | mapped_file_region::mapmode mapped_file_region::flags() const { |
| 644 | assert(Mapping && "Mapping failed but used anyway!"); |
| 645 | return Mode; |
| 646 | } |
| 647 | |
| 648 | uint64_t mapped_file_region::size() const { |
| 649 | assert(Mapping && "Mapping failed but used anyway!"); |
| 650 | return Size; |
| 651 | } |
| 652 | |
| 653 | char *mapped_file_region::data() const { |
Alp Toker | f907b89 | 2013-12-05 05:44:44 +0000 | [diff] [blame] | 654 | assert(Mode != readonly && "Cannot get non-const data for readonly mapping!"); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 655 | assert(Mapping && "Mapping failed but used anyway!"); |
| 656 | return reinterpret_cast<char*>(Mapping); |
| 657 | } |
| 658 | |
| 659 | const char *mapped_file_region::const_data() const { |
| 660 | assert(Mapping && "Mapping failed but used anyway!"); |
| 661 | return reinterpret_cast<const char*>(Mapping); |
| 662 | } |
| 663 | |
| 664 | int mapped_file_region::alignment() { |
| 665 | SYSTEM_INFO SysInfo; |
| 666 | ::GetSystemInfo(&SysInfo); |
| 667 | return SysInfo.dwAllocationGranularity; |
| 668 | } |
| 669 | |
Michael J. Spencer | 0a7625d | 2011-12-08 22:50:09 +0000 | [diff] [blame] | 670 | error_code detail::directory_iterator_construct(detail::DirIterState &it, |
| 671 | StringRef path){ |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 672 | SmallVector<wchar_t, 128> path_utf16; |
| 673 | |
| 674 | if (error_code ec = UTF8ToUTF16(path, |
| 675 | path_utf16)) |
| 676 | return ec; |
| 677 | |
| 678 | // Convert path to the format that Windows is happy with. |
| 679 | if (path_utf16.size() > 0 && |
| 680 | !is_separator(path_utf16[path.size() - 1]) && |
| 681 | path_utf16[path.size() - 1] != L':') { |
| 682 | path_utf16.push_back(L'\\'); |
| 683 | path_utf16.push_back(L'*'); |
| 684 | } else { |
| 685 | path_utf16.push_back(L'*'); |
| 686 | } |
| 687 | |
| 688 | // Get the first directory entry. |
| 689 | WIN32_FIND_DATAW FirstFind; |
Michael J. Spencer | 751e9aa | 2010-12-09 17:37:18 +0000 | [diff] [blame] | 690 | ScopedFindHandle FindHandle(::FindFirstFileW(c_str(path_utf16), &FirstFind)); |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 691 | if (!FindHandle) |
| 692 | return windows_error(::GetLastError()); |
| 693 | |
Michael J. Spencer | 98879d7 | 2011-01-05 16:39:30 +0000 | [diff] [blame] | 694 | size_t FilenameLen = ::wcslen(FirstFind.cFileName); |
| 695 | while ((FilenameLen == 1 && FirstFind.cFileName[0] == L'.') || |
| 696 | (FilenameLen == 2 && FirstFind.cFileName[0] == L'.' && |
| 697 | FirstFind.cFileName[1] == L'.')) |
| 698 | if (!::FindNextFileW(FindHandle, &FirstFind)) { |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 699 | DWORD LastError = ::GetLastError(); |
Michael J. Spencer | 98879d7 | 2011-01-05 16:39:30 +0000 | [diff] [blame] | 700 | // Check for end. |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 701 | if (LastError == ERROR_NO_MORE_FILES) |
Michael J. Spencer | 0a7625d | 2011-12-08 22:50:09 +0000 | [diff] [blame] | 702 | return detail::directory_iterator_destruct(it); |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 703 | return windows_error(LastError); |
Michael J. Spencer | 98879d7 | 2011-01-05 16:39:30 +0000 | [diff] [blame] | 704 | } else |
| 705 | FilenameLen = ::wcslen(FirstFind.cFileName); |
| 706 | |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 707 | // Construct the current directory entry. |
Michael J. Spencer | 98879d7 | 2011-01-05 16:39:30 +0000 | [diff] [blame] | 708 | SmallString<128> directory_entry_name_utf8; |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 709 | if (error_code ec = UTF16ToUTF8(FirstFind.cFileName, |
| 710 | ::wcslen(FirstFind.cFileName), |
Michael J. Spencer | 98879d7 | 2011-01-05 16:39:30 +0000 | [diff] [blame] | 711 | directory_entry_name_utf8)) |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 712 | return ec; |
| 713 | |
| 714 | it.IterationHandle = intptr_t(FindHandle.take()); |
Michael J. Spencer | 98879d7 | 2011-01-05 16:39:30 +0000 | [diff] [blame] | 715 | SmallString<128> directory_entry_path(path); |
| 716 | path::append(directory_entry_path, directory_entry_name_utf8.str()); |
| 717 | it.CurrentEntry = directory_entry(directory_entry_path.str()); |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 718 | |
Rafael Espindola | 03bddfe | 2014-05-31 01:37:45 +0000 | [diff] [blame] | 719 | return error_code(); |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 720 | } |
| 721 | |
Michael J. Spencer | 0a7625d | 2011-12-08 22:50:09 +0000 | [diff] [blame] | 722 | error_code detail::directory_iterator_destruct(detail::DirIterState &it) { |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 723 | if (it.IterationHandle != 0) |
| 724 | // Closes the handle if it's valid. |
| 725 | ScopedFindHandle close(HANDLE(it.IterationHandle)); |
| 726 | it.IterationHandle = 0; |
| 727 | it.CurrentEntry = directory_entry(); |
Rafael Espindola | 03bddfe | 2014-05-31 01:37:45 +0000 | [diff] [blame] | 728 | return error_code(); |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 729 | } |
| 730 | |
Michael J. Spencer | 0a7625d | 2011-12-08 22:50:09 +0000 | [diff] [blame] | 731 | error_code detail::directory_iterator_increment(detail::DirIterState &it) { |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 732 | WIN32_FIND_DATAW FindData; |
| 733 | if (!::FindNextFileW(HANDLE(it.IterationHandle), &FindData)) { |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 734 | DWORD LastError = ::GetLastError(); |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 735 | // Check for end. |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 736 | if (LastError == ERROR_NO_MORE_FILES) |
Michael J. Spencer | 0a7625d | 2011-12-08 22:50:09 +0000 | [diff] [blame] | 737 | return detail::directory_iterator_destruct(it); |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 738 | return windows_error(LastError); |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 739 | } |
| 740 | |
Michael J. Spencer | 98879d7 | 2011-01-05 16:39:30 +0000 | [diff] [blame] | 741 | size_t FilenameLen = ::wcslen(FindData.cFileName); |
| 742 | if ((FilenameLen == 1 && FindData.cFileName[0] == L'.') || |
| 743 | (FilenameLen == 2 && FindData.cFileName[0] == L'.' && |
| 744 | FindData.cFileName[1] == L'.')) |
| 745 | return directory_iterator_increment(it); |
| 746 | |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 747 | SmallString<128> directory_entry_path_utf8; |
| 748 | if (error_code ec = UTF16ToUTF8(FindData.cFileName, |
| 749 | ::wcslen(FindData.cFileName), |
| 750 | directory_entry_path_utf8)) |
| 751 | return ec; |
| 752 | |
| 753 | it.CurrentEntry.replace_filename(Twine(directory_entry_path_utf8)); |
Rafael Espindola | 03bddfe | 2014-05-31 01:37:45 +0000 | [diff] [blame] | 754 | return error_code(); |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 755 | } |
| 756 | |
Rafael Espindola | a0d9b6b | 2013-07-17 14:58:25 +0000 | [diff] [blame] | 757 | error_code openFileForRead(const Twine &Name, int &ResultFD) { |
| 758 | SmallString<128> PathStorage; |
| 759 | SmallVector<wchar_t, 128> PathUTF16; |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 760 | |
Rafael Espindola | a0d9b6b | 2013-07-17 14:58:25 +0000 | [diff] [blame] | 761 | if (error_code EC = UTF8ToUTF16(Name.toStringRef(PathStorage), |
| 762 | PathUTF16)) |
| 763 | return EC; |
| 764 | |
| 765 | HANDLE H = ::CreateFileW(PathUTF16.begin(), GENERIC_READ, |
Rafael Espindola | 16431fe | 2013-07-17 19:44:07 +0000 | [diff] [blame] | 766 | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, |
Rafael Espindola | a0d9b6b | 2013-07-17 14:58:25 +0000 | [diff] [blame] | 767 | OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
| 768 | if (H == INVALID_HANDLE_VALUE) { |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 769 | DWORD LastError = ::GetLastError(); |
| 770 | error_code EC = windows_error(LastError); |
Rafael Espindola | 331aeba | 2013-07-17 19:58:28 +0000 | [diff] [blame] | 771 | // Provide a better error message when trying to open directories. |
Rafael Espindola | a0d9b6b | 2013-07-17 14:58:25 +0000 | [diff] [blame] | 772 | // This only runs if we failed to open the file, so there is probably |
| 773 | // no performances issues. |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 774 | if (LastError != ERROR_ACCESS_DENIED) |
Rafael Espindola | a0d9b6b | 2013-07-17 14:58:25 +0000 | [diff] [blame] | 775 | return EC; |
| 776 | if (is_directory(Name)) |
Rafael Espindola | ed6882b | 2014-06-12 11:58:49 +0000 | [diff] [blame] | 777 | return std::make_error_code(std::errc::is_a_directory); |
Rafael Espindola | a0d9b6b | 2013-07-17 14:58:25 +0000 | [diff] [blame] | 778 | return EC; |
| 779 | } |
| 780 | |
| 781 | int FD = ::_open_osfhandle(intptr_t(H), 0); |
| 782 | if (FD == -1) { |
| 783 | ::CloseHandle(H); |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 784 | return windows_error(ERROR_INVALID_HANDLE); |
Rafael Espindola | a0d9b6b | 2013-07-17 14:58:25 +0000 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | ResultFD = FD; |
Rafael Espindola | 03bddfe | 2014-05-31 01:37:45 +0000 | [diff] [blame] | 788 | return error_code(); |
Rafael Espindola | a0d9b6b | 2013-07-17 14:58:25 +0000 | [diff] [blame] | 789 | } |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 790 | |
Rafael Espindola | 67080ce | 2013-07-19 15:02:03 +0000 | [diff] [blame] | 791 | error_code openFileForWrite(const Twine &Name, int &ResultFD, |
| 792 | sys::fs::OpenFlags Flags, unsigned Mode) { |
| 793 | // Verify that we don't have both "append" and "excl". |
| 794 | assert((!(Flags & sys::fs::F_Excl) || !(Flags & sys::fs::F_Append)) && |
| 795 | "Cannot specify both 'excl' and 'append' file creation flags!"); |
| 796 | |
| 797 | SmallString<128> PathStorage; |
| 798 | SmallVector<wchar_t, 128> PathUTF16; |
| 799 | |
| 800 | if (error_code EC = UTF8ToUTF16(Name.toStringRef(PathStorage), |
| 801 | PathUTF16)) |
| 802 | return EC; |
| 803 | |
| 804 | DWORD CreationDisposition; |
| 805 | if (Flags & F_Excl) |
| 806 | CreationDisposition = CREATE_NEW; |
NAKAMURA Takumi | edf7615 | 2013-08-22 15:14:45 +0000 | [diff] [blame] | 807 | else if (Flags & F_Append) |
Rafael Espindola | 67080ce | 2013-07-19 15:02:03 +0000 | [diff] [blame] | 808 | CreationDisposition = OPEN_ALWAYS; |
| 809 | else |
| 810 | CreationDisposition = CREATE_ALWAYS; |
| 811 | |
Rafael Espindola | 7a0b640 | 2014-02-24 03:07:41 +0000 | [diff] [blame] | 812 | DWORD Access = GENERIC_WRITE; |
| 813 | if (Flags & F_RW) |
| 814 | Access |= GENERIC_READ; |
| 815 | |
| 816 | HANDLE H = ::CreateFileW(PathUTF16.begin(), Access, |
Rafael Espindola | 67080ce | 2013-07-19 15:02:03 +0000 | [diff] [blame] | 817 | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, |
| 818 | CreationDisposition, FILE_ATTRIBUTE_NORMAL, NULL); |
| 819 | |
| 820 | if (H == INVALID_HANDLE_VALUE) { |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 821 | DWORD LastError = ::GetLastError(); |
| 822 | error_code EC = windows_error(LastError); |
Rafael Espindola | 67080ce | 2013-07-19 15:02:03 +0000 | [diff] [blame] | 823 | // Provide a better error message when trying to open directories. |
| 824 | // This only runs if we failed to open the file, so there is probably |
| 825 | // no performances issues. |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 826 | if (LastError != ERROR_ACCESS_DENIED) |
Rafael Espindola | 67080ce | 2013-07-19 15:02:03 +0000 | [diff] [blame] | 827 | return EC; |
| 828 | if (is_directory(Name)) |
Rafael Espindola | ed6882b | 2014-06-12 11:58:49 +0000 | [diff] [blame] | 829 | return std::make_error_code(std::errc::is_a_directory); |
Rafael Espindola | 67080ce | 2013-07-19 15:02:03 +0000 | [diff] [blame] | 830 | return EC; |
| 831 | } |
| 832 | |
| 833 | int OpenFlags = 0; |
| 834 | if (Flags & F_Append) |
| 835 | OpenFlags |= _O_APPEND; |
| 836 | |
Rafael Espindola | 90c7f1c | 2014-02-24 18:20:12 +0000 | [diff] [blame] | 837 | if (Flags & F_Text) |
Rafael Espindola | 67080ce | 2013-07-19 15:02:03 +0000 | [diff] [blame] | 838 | OpenFlags |= _O_TEXT; |
| 839 | |
| 840 | int FD = ::_open_osfhandle(intptr_t(H), OpenFlags); |
| 841 | if (FD == -1) { |
| 842 | ::CloseHandle(H); |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 843 | return windows_error(ERROR_INVALID_HANDLE); |
Rafael Espindola | 67080ce | 2013-07-19 15:02:03 +0000 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | ResultFD = FD; |
Rafael Espindola | 03bddfe | 2014-05-31 01:37:45 +0000 | [diff] [blame] | 847 | return error_code(); |
Rafael Espindola | 67080ce | 2013-07-19 15:02:03 +0000 | [diff] [blame] | 848 | } |
Michael J. Spencer | 9fc1d9d | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 849 | } // end namespace fs |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 850 | |
Peter Collingbourne | f7d4101 | 2014-01-31 23:46:06 +0000 | [diff] [blame] | 851 | namespace path { |
| 852 | |
| 853 | bool home_directory(SmallVectorImpl<char> &result) { |
| 854 | wchar_t Path[MAX_PATH]; |
| 855 | if (::SHGetFolderPathW(0, CSIDL_APPDATA | CSIDL_FLAG_CREATE, 0, |
Peter Collingbourne | ad141ab | 2014-02-01 02:42:20 +0000 | [diff] [blame] | 856 | /*SHGFP_TYPE_CURRENT*/0, Path) != S_OK) |
Peter Collingbourne | f7d4101 | 2014-01-31 23:46:06 +0000 | [diff] [blame] | 857 | return false; |
| 858 | |
| 859 | if (UTF16ToUTF8(Path, ::wcslen(Path), result)) |
| 860 | return false; |
| 861 | |
| 862 | return true; |
| 863 | } |
| 864 | |
| 865 | } // end namespace path |
| 866 | |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 867 | namespace windows { |
Rafael Espindola | 0a5f9cf | 2014-06-12 14:11:22 +0000 | [diff] [blame^] | 868 | std::error_code UTF8ToUTF16(llvm::StringRef utf8, |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 869 | llvm::SmallVectorImpl<wchar_t> &utf16) { |
Michael J. Spencer | 7a3a510 | 2014-02-20 20:46:23 +0000 | [diff] [blame] | 870 | if (!utf8.empty()) { |
| 871 | int len = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, utf8.begin(), |
| 872 | utf8.size(), utf16.begin(), 0); |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 873 | |
Michael J. Spencer | 7a3a510 | 2014-02-20 20:46:23 +0000 | [diff] [blame] | 874 | if (len == 0) |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 875 | return windows_error(::GetLastError()); |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 876 | |
Michael J. Spencer | 7a3a510 | 2014-02-20 20:46:23 +0000 | [diff] [blame] | 877 | utf16.reserve(len + 1); |
| 878 | utf16.set_size(len); |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 879 | |
Michael J. Spencer | 7a3a510 | 2014-02-20 20:46:23 +0000 | [diff] [blame] | 880 | len = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, utf8.begin(), |
| 881 | utf8.size(), utf16.begin(), utf16.size()); |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 882 | |
Michael J. Spencer | 7a3a510 | 2014-02-20 20:46:23 +0000 | [diff] [blame] | 883 | if (len == 0) |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 884 | return windows_error(::GetLastError()); |
Michael J. Spencer | 7a3a510 | 2014-02-20 20:46:23 +0000 | [diff] [blame] | 885 | } |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 886 | |
| 887 | // Make utf16 null terminated. |
| 888 | utf16.push_back(0); |
| 889 | utf16.pop_back(); |
| 890 | |
Rafael Espindola | 0a5f9cf | 2014-06-12 14:11:22 +0000 | [diff] [blame^] | 891 | return std::error_code(); |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 892 | } |
| 893 | |
Rafael Espindola | 0a5f9cf | 2014-06-12 14:11:22 +0000 | [diff] [blame^] | 894 | std::error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len, |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 895 | llvm::SmallVectorImpl<char> &utf8) { |
Michael J. Spencer | 7a3a510 | 2014-02-20 20:46:23 +0000 | [diff] [blame] | 896 | if (utf16_len) { |
| 897 | // Get length. |
| 898 | int len = ::WideCharToMultiByte(CP_UTF8, 0, utf16, utf16_len, utf8.begin(), |
| 899 | 0, NULL, NULL); |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 900 | |
Michael J. Spencer | 7a3a510 | 2014-02-20 20:46:23 +0000 | [diff] [blame] | 901 | if (len == 0) |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 902 | return windows_error(::GetLastError()); |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 903 | |
Michael J. Spencer | 7a3a510 | 2014-02-20 20:46:23 +0000 | [diff] [blame] | 904 | utf8.reserve(len); |
| 905 | utf8.set_size(len); |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 906 | |
Michael J. Spencer | 7a3a510 | 2014-02-20 20:46:23 +0000 | [diff] [blame] | 907 | // Now do the actual conversion. |
| 908 | len = ::WideCharToMultiByte(CP_UTF8, 0, utf16, utf16_len, utf8.data(), |
| 909 | utf8.size(), NULL, NULL); |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 910 | |
Michael J. Spencer | 7a3a510 | 2014-02-20 20:46:23 +0000 | [diff] [blame] | 911 | if (len == 0) |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 912 | return windows_error(::GetLastError()); |
Michael J. Spencer | 7a3a510 | 2014-02-20 20:46:23 +0000 | [diff] [blame] | 913 | } |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 914 | |
| 915 | // Make utf8 null terminated. |
| 916 | utf8.push_back(0); |
| 917 | utf8.pop_back(); |
| 918 | |
Rafael Espindola | 0a5f9cf | 2014-06-12 14:11:22 +0000 | [diff] [blame^] | 919 | return std::error_code(); |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 920 | } |
| 921 | } // end namespace windows |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 922 | } // end namespace sys |
| 923 | } // end namespace llvm |