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