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 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
Rafael Espindola | f1fc382 | 2013-06-26 19:33:03 +0000 | [diff] [blame] | 9 | // This file implements the Windows specific implementation of the Path API. |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | //=== WARNING: Implementation here must contain only generic Windows code that |
| 15 | //=== is guaranteed to work on *all* Windows variants. |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
Craig Topper | f18edae | 2013-07-15 07:15:05 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/STLExtras.h" |
Zachary Turner | b44d7a0 | 2018-06-01 22:23:46 +0000 | [diff] [blame] | 19 | #include "llvm/Support/ConvertUTF.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" |
Zachary Turner | 260bda3 | 2017-03-08 22:49:32 +0000 | [diff] [blame] | 29 | #include <shellapi.h> |
NAKAMURA Takumi | 04d39d7 | 2014-02-12 11:50:22 +0000 | [diff] [blame] | 30 | #include <shlobj.h> |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 31 | |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 32 | #undef max |
| 33 | |
Michael J. Spencer | 6025247 | 2010-12-03 18:03:28 +0000 | [diff] [blame] | 34 | // MinGW doesn't define this. |
Michael J. Spencer | 521c321 | 2010-12-03 18:04:11 +0000 | [diff] [blame] | 35 | #ifndef _ERRNO_T_DEFINED |
| 36 | #define _ERRNO_T_DEFINED |
| 37 | typedef int errno_t; |
Michael J. Spencer | 6025247 | 2010-12-03 18:03:28 +0000 | [diff] [blame] | 38 | #endif |
| 39 | |
Reid Kleckner | 11da004 | 2013-08-07 20:19:31 +0000 | [diff] [blame] | 40 | #ifdef _MSC_VER |
| 41 | # pragma comment(lib, "advapi32.lib") // This provides CryptAcquireContextW. |
Reid Kleckner | 304af56 | 2016-01-12 18:33:49 +0000 | [diff] [blame] | 42 | # pragma comment(lib, "ole32.lib") // This provides CoTaskMemFree |
Reid Kleckner | 11da004 | 2013-08-07 20:19:31 +0000 | [diff] [blame] | 43 | #endif |
| 44 | |
Michael J. Spencer | 9fc1d9d | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 45 | using namespace llvm; |
| 46 | |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 47 | using llvm::sys::windows::UTF8ToUTF16; |
Aaron Smith | 8a5ea61 | 2018-04-07 00:32:59 +0000 | [diff] [blame] | 48 | using llvm::sys::windows::CurCPToUTF16; |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 49 | using llvm::sys::windows::UTF16ToUTF8; |
Paul Robinson | c38deee | 2014-11-24 18:05:29 +0000 | [diff] [blame] | 50 | using llvm::sys::path::widenPath; |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 51 | |
Rafael Espindola | 37b012d | 2014-02-23 15:16:03 +0000 | [diff] [blame] | 52 | static bool is_separator(const wchar_t value) { |
| 53 | switch (value) { |
| 54 | case L'\\': |
| 55 | case L'/': |
| 56 | return true; |
| 57 | default: |
| 58 | return false; |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 59 | } |
Michael J. Spencer | 9fc1d9d | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Paul Robinson | c38deee | 2014-11-24 18:05:29 +0000 | [diff] [blame] | 62 | namespace llvm { |
| 63 | namespace sys { |
| 64 | namespace path { |
| 65 | |
Aaron Smith | 02caafd7 | 2018-04-18 15:26:26 +0000 | [diff] [blame] | 66 | // Convert a UTF-8 path to UTF-16. Also, if the absolute equivalent of the |
Paul Robinson | d9c4a9a | 2014-11-13 00:12:14 +0000 | [diff] [blame] | 67 | // path is longer than CreateDirectory can tolerate, make it absolute and |
| 68 | // prefixed by '\\?\'. |
Paul Robinson | c38deee | 2014-11-24 18:05:29 +0000 | [diff] [blame] | 69 | std::error_code widenPath(const Twine &Path8, |
| 70 | SmallVectorImpl<wchar_t> &Path16) { |
Paul Robinson | d9c4a9a | 2014-11-13 00:12:14 +0000 | [diff] [blame] | 71 | const size_t MaxDirLen = MAX_PATH - 12; // Must leave room for 8.3 filename. |
| 72 | |
| 73 | // Several operations would convert Path8 to SmallString; more efficient to |
| 74 | // do it once up front. |
Aaron Smith | 02caafd7 | 2018-04-18 15:26:26 +0000 | [diff] [blame] | 75 | SmallString<128> Path8Str; |
Paul Robinson | d9c4a9a | 2014-11-13 00:12:14 +0000 | [diff] [blame] | 76 | Path8.toVector(Path8Str); |
| 77 | |
| 78 | // If we made this path absolute, how much longer would it get? |
| 79 | size_t CurPathLen; |
| 80 | if (llvm::sys::path::is_absolute(Twine(Path8Str))) |
| 81 | CurPathLen = 0; // No contribution from current_path needed. |
| 82 | else { |
| 83 | CurPathLen = ::GetCurrentDirectoryW(0, NULL); |
| 84 | if (CurPathLen == 0) |
Yaron Keren | f8e6517 | 2015-05-04 04:48:10 +0000 | [diff] [blame] | 85 | return mapWindowsError(::GetLastError()); |
Paul Robinson | d9c4a9a | 2014-11-13 00:12:14 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | // Would the absolute path be longer than our limit? |
| 89 | if ((Path8Str.size() + CurPathLen) >= MaxDirLen && |
| 90 | !Path8Str.startswith("\\\\?\\")) { |
| 91 | SmallString<2*MAX_PATH> FullPath("\\\\?\\"); |
| 92 | if (CurPathLen) { |
| 93 | SmallString<80> CurPath; |
| 94 | if (std::error_code EC = llvm::sys::fs::current_path(CurPath)) |
| 95 | return EC; |
| 96 | FullPath.append(CurPath); |
| 97 | } |
Pirama Arumuga Nainar | 3d48bb5 | 2017-08-21 20:49:44 +0000 | [diff] [blame] | 98 | // Traverse the requested path, canonicalizing . and .. (because the \\?\ |
| 99 | // prefix is documented to treat them as real components). Ignore |
| 100 | // separators, which can be returned from the iterator if the path has a |
| 101 | // drive name. We don't need to call native() on the result since append() |
| 102 | // always attaches preferred_separator. |
Paul Robinson | d9c4a9a | 2014-11-13 00:12:14 +0000 | [diff] [blame] | 103 | for (llvm::sys::path::const_iterator I = llvm::sys::path::begin(Path8Str), |
| 104 | E = llvm::sys::path::end(Path8Str); |
| 105 | I != E; ++I) { |
Pirama Arumuga Nainar | 3d48bb5 | 2017-08-21 20:49:44 +0000 | [diff] [blame] | 106 | if (I->size() == 1 && is_separator((*I)[0])) |
| 107 | continue; |
Paul Robinson | d9c4a9a | 2014-11-13 00:12:14 +0000 | [diff] [blame] | 108 | if (I->size() == 1 && *I == ".") |
| 109 | continue; |
| 110 | if (I->size() == 2 && *I == "..") |
| 111 | llvm::sys::path::remove_filename(FullPath); |
| 112 | else |
| 113 | llvm::sys::path::append(FullPath, *I); |
| 114 | } |
Aaron Smith | 02caafd7 | 2018-04-18 15:26:26 +0000 | [diff] [blame] | 115 | return UTF8ToUTF16(FullPath, Path16); |
Paul Robinson | d9c4a9a | 2014-11-13 00:12:14 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Aaron Smith | 02caafd7 | 2018-04-18 15:26:26 +0000 | [diff] [blame] | 118 | // Just use the caller's original path. |
| 119 | return UTF8ToUTF16(Path8Str, Path16); |
Paul Robinson | d9c4a9a | 2014-11-13 00:12:14 +0000 | [diff] [blame] | 120 | } |
Paul Robinson | c38deee | 2014-11-24 18:05:29 +0000 | [diff] [blame] | 121 | } // end namespace path |
Paul Robinson | d9c4a9a | 2014-11-13 00:12:14 +0000 | [diff] [blame] | 122 | |
Michael J. Spencer | 20daa28 | 2010-12-07 01:22:31 +0000 | [diff] [blame] | 123 | namespace fs { |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 124 | |
Zachary Turner | 63db25b | 2018-06-04 19:38:11 +0000 | [diff] [blame] | 125 | const file_t kInvalidFile = INVALID_HANDLE_VALUE; |
| 126 | |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 127 | std::string getMainExecutable(const char *argv0, void *MainExecAddr) { |
David Majnemer | 17a4496 | 2013-10-07 09:52:36 +0000 | [diff] [blame] | 128 | SmallVector<wchar_t, MAX_PATH> PathName; |
| 129 | DWORD Size = ::GetModuleFileNameW(NULL, PathName.data(), PathName.capacity()); |
| 130 | |
| 131 | // A zero return value indicates a failure other than insufficient space. |
| 132 | if (Size == 0) |
| 133 | return ""; |
| 134 | |
| 135 | // Insufficient space is determined by a return value equal to the size of |
| 136 | // the buffer passed in. |
| 137 | if (Size == PathName.capacity()) |
| 138 | return ""; |
| 139 | |
| 140 | // On success, GetModuleFileNameW returns the number of characters written to |
| 141 | // the buffer not including the NULL terminator. |
| 142 | PathName.set_size(Size); |
| 143 | |
| 144 | // Convert the result from UTF-16 to UTF-8. |
| 145 | SmallVector<char, MAX_PATH> PathNameUTF8; |
| 146 | if (UTF16ToUTF8(PathName.data(), PathName.size(), PathNameUTF8)) |
| 147 | return ""; |
| 148 | |
| 149 | return std::string(PathNameUTF8.data()); |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Rafael Espindola | d123099 | 2013-07-29 21:55:38 +0000 | [diff] [blame] | 152 | UniqueID file_status::getUniqueID() const { |
Rafael Espindola | 7f822a9 | 2013-07-29 21:26:49 +0000 | [diff] [blame] | 153 | // The file is uniquely identified by the volume serial number along |
| 154 | // with the 64-bit file identifier. |
| 155 | uint64_t FileID = (static_cast<uint64_t>(FileIndexHigh) << 32ULL) | |
| 156 | static_cast<uint64_t>(FileIndexLow); |
| 157 | |
| 158 | return UniqueID(VolumeSerialNumber, FileID); |
| 159 | } |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 160 | |
Mehdi Amini | e2d8f1b | 2016-04-01 00:18:08 +0000 | [diff] [blame] | 161 | ErrorOr<space_info> disk_space(const Twine &Path) { |
| 162 | ULARGE_INTEGER Avail, Total, Free; |
| 163 | if (!::GetDiskFreeSpaceExA(Path.str().c_str(), &Avail, &Total, &Free)) |
| 164 | return mapWindowsError(::GetLastError()); |
| 165 | space_info SpaceInfo; |
| 166 | SpaceInfo.capacity = |
| 167 | (static_cast<uint64_t>(Total.HighPart) << 32) + Total.LowPart; |
Mehdi Amini | 6471915 | 2016-04-01 00:52:05 +0000 | [diff] [blame] | 168 | SpaceInfo.free = (static_cast<uint64_t>(Free.HighPart) << 32) + Free.LowPart; |
Mehdi Amini | e2d8f1b | 2016-04-01 00:18:08 +0000 | [diff] [blame] | 169 | SpaceInfo.available = |
| 170 | (static_cast<uint64_t>(Avail.HighPart) << 32) + Avail.LowPart; |
| 171 | return SpaceInfo; |
| 172 | } |
| 173 | |
Peter Collingbourne | 0dfdb44 | 2017-10-10 22:19:46 +0000 | [diff] [blame] | 174 | TimePoint<> basic_file_status::getLastAccessedTime() const { |
Pavel Labath | 757ca88 | 2016-10-24 10:59:17 +0000 | [diff] [blame] | 175 | FILETIME Time; |
| 176 | Time.dwLowDateTime = LastAccessedTimeLow; |
| 177 | Time.dwHighDateTime = LastAccessedTimeHigh; |
| 178 | return toTimePoint(Time); |
Mehdi Amini | 1e39ef3 | 2016-03-25 07:30:21 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Peter Collingbourne | 0dfdb44 | 2017-10-10 22:19:46 +0000 | [diff] [blame] | 181 | TimePoint<> basic_file_status::getLastModificationTime() const { |
Pavel Labath | 757ca88 | 2016-10-24 10:59:17 +0000 | [diff] [blame] | 182 | FILETIME Time; |
| 183 | Time.dwLowDateTime = LastWriteTimeLow; |
| 184 | Time.dwHighDateTime = LastWriteTimeHigh; |
| 185 | return toTimePoint(Time); |
Rafael Espindola | db5d8fe | 2013-06-20 18:42:04 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Zachary Turner | 5821a3b | 2017-03-20 23:55:20 +0000 | [diff] [blame] | 188 | uint32_t file_status::getLinkCount() const { |
| 189 | return NumLinks; |
| 190 | } |
| 191 | |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 192 | std::error_code current_path(SmallVectorImpl<char> &result) { |
David Majnemer | 61eae2e | 2013-10-07 01:00:07 +0000 | [diff] [blame] | 193 | SmallVector<wchar_t, MAX_PATH> cur_path; |
| 194 | DWORD len = MAX_PATH; |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 195 | |
David Majnemer | 61eae2e | 2013-10-07 01:00:07 +0000 | [diff] [blame] | 196 | do { |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 197 | cur_path.reserve(len); |
David Majnemer | 61eae2e | 2013-10-07 01:00:07 +0000 | [diff] [blame] | 198 | len = ::GetCurrentDirectoryW(cur_path.capacity(), cur_path.data()); |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 199 | |
David Majnemer | 61eae2e | 2013-10-07 01:00:07 +0000 | [diff] [blame] | 200 | // A zero return value indicates a failure other than insufficient space. |
| 201 | if (len == 0) |
Yaron Keren | f8e6517 | 2015-05-04 04:48:10 +0000 | [diff] [blame] | 202 | return mapWindowsError(::GetLastError()); |
David Majnemer | 61eae2e | 2013-10-07 01:00:07 +0000 | [diff] [blame] | 203 | |
| 204 | // If there's insufficient space, the len returned is larger than the len |
| 205 | // given. |
| 206 | } while (len > cur_path.capacity()); |
| 207 | |
| 208 | // On success, GetCurrentDirectoryW returns the number of characters not |
| 209 | // including the null-terminator. |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 210 | cur_path.set_size(len); |
Aaron Ballman | b16cf53 | 2013-08-16 17:53:28 +0000 | [diff] [blame] | 211 | return UTF16ToUTF8(cur_path.begin(), cur_path.size(), result); |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Pavel Labath | 2f09609 | 2017-01-24 10:32:03 +0000 | [diff] [blame] | 214 | std::error_code set_current_path(const Twine &path) { |
| 215 | // Convert to utf-16. |
| 216 | SmallVector<wchar_t, 128> wide_path; |
| 217 | if (std::error_code ec = widenPath(path, wide_path)) |
| 218 | return ec; |
| 219 | |
| 220 | if (!::SetCurrentDirectoryW(wide_path.begin())) |
| 221 | return mapWindowsError(::GetLastError()); |
| 222 | |
| 223 | return std::error_code(); |
| 224 | } |
| 225 | |
Frederic Riss | 6b9396c | 2015-08-06 21:04:55 +0000 | [diff] [blame] | 226 | std::error_code create_directory(const Twine &path, bool IgnoreExisting, |
| 227 | perms Perms) { |
Michael J. Spencer | 31e310c | 2010-12-03 05:42:11 +0000 | [diff] [blame] | 228 | SmallVector<wchar_t, 128> path_utf16; |
| 229 | |
Paul Robinson | d9c4a9a | 2014-11-13 00:12:14 +0000 | [diff] [blame] | 230 | if (std::error_code ec = widenPath(path, path_utf16)) |
Michael J. Spencer | 31e310c | 2010-12-03 05:42:11 +0000 | [diff] [blame] | 231 | return ec; |
| 232 | |
| 233 | if (!::CreateDirectoryW(path_utf16.begin(), NULL)) { |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 234 | DWORD LastError = ::GetLastError(); |
| 235 | if (LastError != ERROR_ALREADY_EXISTS || !IgnoreExisting) |
Yaron Keren | f8e6517 | 2015-05-04 04:48:10 +0000 | [diff] [blame] | 236 | return mapWindowsError(LastError); |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 237 | } |
Michael J. Spencer | 31e310c | 2010-12-03 05:42:11 +0000 | [diff] [blame] | 238 | |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 239 | return std::error_code(); |
Michael J. Spencer | 31e310c | 2010-12-03 05:42:11 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Rafael Espindola | 83f858e | 2014-03-11 18:40:24 +0000 | [diff] [blame] | 242 | // We can't use symbolic links for windows. |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 243 | std::error_code create_link(const Twine &to, const Twine &from) { |
Michael J. Spencer | e0c4560 | 2010-12-03 05:58:41 +0000 | [diff] [blame] | 244 | // Convert to utf-16. |
| 245 | SmallVector<wchar_t, 128> wide_from; |
| 246 | SmallVector<wchar_t, 128> wide_to; |
Paul Robinson | d9c4a9a | 2014-11-13 00:12:14 +0000 | [diff] [blame] | 247 | if (std::error_code ec = widenPath(from, wide_from)) |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 248 | return ec; |
Paul Robinson | d9c4a9a | 2014-11-13 00:12:14 +0000 | [diff] [blame] | 249 | if (std::error_code ec = widenPath(to, wide_to)) |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 250 | return ec; |
Michael J. Spencer | e0c4560 | 2010-12-03 05:58:41 +0000 | [diff] [blame] | 251 | |
| 252 | if (!::CreateHardLinkW(wide_from.begin(), wide_to.begin(), NULL)) |
Yaron Keren | f8e6517 | 2015-05-04 04:48:10 +0000 | [diff] [blame] | 253 | return mapWindowsError(::GetLastError()); |
Michael J. Spencer | e0c4560 | 2010-12-03 05:58:41 +0000 | [diff] [blame] | 254 | |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 255 | return std::error_code(); |
Michael J. Spencer | e0c4560 | 2010-12-03 05:58:41 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Mehdi Amini | 8e13bc4 | 2016-12-14 04:56:42 +0000 | [diff] [blame] | 258 | std::error_code create_hard_link(const Twine &to, const Twine &from) { |
NAKAMURA Takumi | a1e97a7 | 2017-08-28 06:47:47 +0000 | [diff] [blame] | 259 | return create_link(to, from); |
Mehdi Amini | 8e13bc4 | 2016-12-14 04:56:42 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 262 | std::error_code remove(const Twine &path, bool IgnoreNonExisting) { |
Michael J. Spencer | 6e74e11 | 2010-12-03 17:53:43 +0000 | [diff] [blame] | 263 | SmallVector<wchar_t, 128> path_utf16; |
| 264 | |
Paul Robinson | d9c4a9a | 2014-11-13 00:12:14 +0000 | [diff] [blame] | 265 | if (std::error_code ec = widenPath(path, path_utf16)) |
Michael J. Spencer | 6e74e11 | 2010-12-03 17:53:43 +0000 | [diff] [blame] | 266 | return ec; |
| 267 | |
Peter Collingbourne | 0f9e889 | 2017-10-10 19:39:46 +0000 | [diff] [blame] | 268 | // We don't know whether this is a file or a directory, and remove() can |
| 269 | // accept both. The usual way to delete a file or directory is to use one of |
| 270 | // the DeleteFile or RemoveDirectory functions, but that requires you to know |
| 271 | // which one it is. We could stat() the file to determine that, but that would |
| 272 | // cost us additional system calls, which can be slow in a directory |
| 273 | // containing a large number of files. So instead we call CreateFile directly. |
| 274 | // The important part is the FILE_FLAG_DELETE_ON_CLOSE flag, which causes the |
| 275 | // file to be deleted once it is closed. We also use the flags |
| 276 | // FILE_FLAG_BACKUP_SEMANTICS (which allows us to open directories), and |
| 277 | // FILE_FLAG_OPEN_REPARSE_POINT (don't follow symlinks). |
| 278 | ScopedFileHandle h(::CreateFileW( |
| 279 | c_str(path_utf16), DELETE, |
| 280 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, |
| 281 | OPEN_EXISTING, |
| 282 | FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS | |
| 283 | FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_DELETE_ON_CLOSE, |
| 284 | NULL)); |
| 285 | if (!h) { |
Yaron Keren | f8e6517 | 2015-05-04 04:48:10 +0000 | [diff] [blame] | 286 | std::error_code EC = mapWindowsError(::GetLastError()); |
Rafael Espindola | 2a826e4 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 287 | if (EC != errc::no_such_file_or_directory || !IgnoreNonExisting) |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 288 | return EC; |
| 289 | } |
Peter Collingbourne | 0f9e889 | 2017-10-10 19:39:46 +0000 | [diff] [blame] | 290 | |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 291 | return std::error_code(); |
Michael J. Spencer | 6e74e11 | 2010-12-03 17:53:43 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Zachary Turner | 392ed9d | 2017-02-21 20:55:47 +0000 | [diff] [blame] | 294 | static std::error_code is_local_internal(SmallVectorImpl<wchar_t> &Path, |
| 295 | bool &Result) { |
| 296 | SmallVector<wchar_t, 128> VolumePath; |
| 297 | size_t Len = 128; |
| 298 | while (true) { |
| 299 | VolumePath.resize(Len); |
| 300 | BOOL Success = |
| 301 | ::GetVolumePathNameW(Path.data(), VolumePath.data(), VolumePath.size()); |
| 302 | |
| 303 | if (Success) |
| 304 | break; |
| 305 | |
| 306 | DWORD Err = ::GetLastError(); |
| 307 | if (Err != ERROR_INSUFFICIENT_BUFFER) |
| 308 | return mapWindowsError(Err); |
| 309 | |
| 310 | Len *= 2; |
| 311 | } |
| 312 | // If the output buffer has exactly enough space for the path name, but not |
| 313 | // the null terminator, it will leave the output unterminated. Push a null |
| 314 | // terminator onto the end to ensure that this never happens. |
| 315 | VolumePath.push_back(L'\0'); |
| 316 | VolumePath.set_size(wcslen(VolumePath.data())); |
| 317 | const wchar_t *P = VolumePath.data(); |
| 318 | |
| 319 | UINT Type = ::GetDriveTypeW(P); |
| 320 | switch (Type) { |
| 321 | case DRIVE_FIXED: |
| 322 | Result = true; |
| 323 | return std::error_code(); |
| 324 | case DRIVE_REMOTE: |
| 325 | case DRIVE_CDROM: |
| 326 | case DRIVE_RAMDISK: |
| 327 | case DRIVE_REMOVABLE: |
| 328 | Result = false; |
| 329 | return std::error_code(); |
| 330 | default: |
| 331 | return make_error_code(errc::no_such_file_or_directory); |
| 332 | } |
| 333 | llvm_unreachable("Unreachable!"); |
| 334 | } |
| 335 | |
| 336 | std::error_code is_local(const Twine &path, bool &result) { |
| 337 | if (!llvm::sys::fs::exists(path) || !llvm::sys::path::has_root_path(path)) |
| 338 | return make_error_code(errc::no_such_file_or_directory); |
| 339 | |
| 340 | SmallString<128> Storage; |
| 341 | StringRef P = path.toStringRef(Storage); |
| 342 | |
| 343 | // Convert to utf-16. |
| 344 | SmallVector<wchar_t, 128> WidePath; |
| 345 | if (std::error_code ec = widenPath(P, WidePath)) |
| 346 | return ec; |
| 347 | return is_local_internal(WidePath, result); |
| 348 | } |
| 349 | |
Rafael Espindola | 041299e | 2017-11-18 02:05:59 +0000 | [diff] [blame] | 350 | static std::error_code realPathFromHandle(HANDLE H, |
Rafael Espindola | 8dc0e10 | 2017-11-18 02:12:53 +0000 | [diff] [blame] | 351 | SmallVectorImpl<wchar_t> &Buffer) { |
| 352 | DWORD CountChars = ::GetFinalPathNameByHandleW( |
| 353 | H, Buffer.begin(), Buffer.capacity() - 1, FILE_NAME_NORMALIZED); |
| 354 | if (CountChars > Buffer.capacity()) { |
| 355 | // The buffer wasn't big enough, try again. In this case the return value |
| 356 | // *does* indicate the size of the null terminator. |
| 357 | Buffer.reserve(CountChars); |
| 358 | CountChars = ::GetFinalPathNameByHandleW( |
| 359 | H, Buffer.data(), Buffer.capacity() - 1, FILE_NAME_NORMALIZED); |
| 360 | } |
| 361 | if (CountChars == 0) |
| 362 | return mapWindowsError(GetLastError()); |
| 363 | Buffer.set_size(CountChars); |
| 364 | return std::error_code(); |
| 365 | } |
| 366 | |
| 367 | static std::error_code realPathFromHandle(HANDLE H, |
| 368 | SmallVectorImpl<char> &RealPath) { |
| 369 | RealPath.clear(); |
| 370 | SmallVector<wchar_t, MAX_PATH> Buffer; |
| 371 | if (std::error_code EC = realPathFromHandle(H, Buffer)) |
| 372 | return EC; |
| 373 | |
Hans Wennborg | 91a5a2a | 2019-09-05 09:07:05 +0000 | [diff] [blame] | 374 | // Strip the \\?\ prefix. We don't want it ending up in output, and such |
| 375 | // paths don't get canonicalized by file APIs. |
| 376 | wchar_t *Data = Buffer.data(); |
Rafael Espindola | 8dc0e10 | 2017-11-18 02:12:53 +0000 | [diff] [blame] | 377 | DWORD CountChars = Buffer.size(); |
Hans Wennborg | 91a5a2a | 2019-09-05 09:07:05 +0000 | [diff] [blame] | 378 | if (CountChars >= 8 && ::memcmp(Data, L"\\\\?\\UNC\\", 16) == 0) { |
| 379 | // Convert \\?\UNC\foo\bar to \\foo\bar |
| 380 | CountChars -= 6; |
| 381 | Data += 6; |
| 382 | Data[0] = '\\'; |
| 383 | } else if (CountChars >= 4 && ::memcmp(Data, L"\\\\?\\", 8) == 0) { |
| 384 | // Convert \\?\c:\foo to c:\foo |
| 385 | CountChars -= 4; |
| 386 | Data += 4; |
Rafael Espindola | 8dc0e10 | 2017-11-18 02:12:53 +0000 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | // Convert the result from UTF-16 to UTF-8. |
| 390 | return UTF16ToUTF8(Data, CountChars, RealPath); |
| 391 | } |
Rafael Espindola | 041299e | 2017-11-18 02:05:59 +0000 | [diff] [blame] | 392 | |
Zachary Turner | 392ed9d | 2017-02-21 20:55:47 +0000 | [diff] [blame] | 393 | std::error_code is_local(int FD, bool &Result) { |
| 394 | SmallVector<wchar_t, 128> FinalPath; |
| 395 | HANDLE Handle = reinterpret_cast<HANDLE>(_get_osfhandle(FD)); |
| 396 | |
Rafael Espindola | 041299e | 2017-11-18 02:05:59 +0000 | [diff] [blame] | 397 | if (std::error_code EC = realPathFromHandle(Handle, FinalPath)) |
| 398 | return EC; |
Zachary Turner | 392ed9d | 2017-02-21 20:55:47 +0000 | [diff] [blame] | 399 | |
| 400 | return is_local_internal(FinalPath, Result); |
| 401 | } |
| 402 | |
Rafael Espindola | 20569e9 | 2017-12-05 16:40:56 +0000 | [diff] [blame] | 403 | static std::error_code setDeleteDisposition(HANDLE Handle, bool Delete) { |
| 404 | FILE_DISPOSITION_INFO Disposition; |
| 405 | Disposition.DeleteFile = Delete; |
| 406 | if (!SetFileInformationByHandle(Handle, FileDispositionInfo, &Disposition, |
| 407 | sizeof(Disposition))) |
| 408 | return mapWindowsError(::GetLastError()); |
| 409 | return std::error_code(); |
| 410 | } |
| 411 | |
Peter Collingbourne | 80e31f1 | 2017-10-06 17:14:36 +0000 | [diff] [blame] | 412 | static std::error_code rename_internal(HANDLE FromHandle, const Twine &To, |
| 413 | bool ReplaceIfExists) { |
| 414 | SmallVector<wchar_t, 0> ToWide; |
| 415 | if (auto EC = widenPath(To, ToWide)) |
| 416 | return EC; |
| 417 | |
| 418 | std::vector<char> RenameInfoBuf(sizeof(FILE_RENAME_INFO) - sizeof(wchar_t) + |
| 419 | (ToWide.size() * sizeof(wchar_t))); |
| 420 | FILE_RENAME_INFO &RenameInfo = |
| 421 | *reinterpret_cast<FILE_RENAME_INFO *>(RenameInfoBuf.data()); |
| 422 | RenameInfo.ReplaceIfExists = ReplaceIfExists; |
| 423 | RenameInfo.RootDirectory = 0; |
Shoaib Meenai | 96929fd | 2018-12-13 00:08:25 +0000 | [diff] [blame] | 424 | RenameInfo.FileNameLength = ToWide.size() * sizeof(wchar_t); |
Adrian McCarthy | e6275c6 | 2017-10-09 17:50:01 +0000 | [diff] [blame] | 425 | std::copy(ToWide.begin(), ToWide.end(), &RenameInfo.FileName[0]); |
Peter Collingbourne | 80e31f1 | 2017-10-06 17:14:36 +0000 | [diff] [blame] | 426 | |
Hans Wennborg | 477c974 | 2017-10-12 17:38:22 +0000 | [diff] [blame] | 427 | SetLastError(ERROR_SUCCESS); |
Peter Collingbourne | 80e31f1 | 2017-10-06 17:14:36 +0000 | [diff] [blame] | 428 | if (!SetFileInformationByHandle(FromHandle, FileRenameInfo, &RenameInfo, |
Hans Wennborg | 477c974 | 2017-10-12 17:38:22 +0000 | [diff] [blame] | 429 | RenameInfoBuf.size())) { |
| 430 | unsigned Error = GetLastError(); |
| 431 | if (Error == ERROR_SUCCESS) |
| 432 | Error = ERROR_CALL_NOT_IMPLEMENTED; // Wine doesn't always set error code. |
| 433 | return mapWindowsError(Error); |
| 434 | } |
Peter Collingbourne | 80e31f1 | 2017-10-06 17:14:36 +0000 | [diff] [blame] | 435 | |
| 436 | return std::error_code(); |
| 437 | } |
| 438 | |
Rafael Espindola | 5908aff | 2017-11-21 01:52:44 +0000 | [diff] [blame] | 439 | static std::error_code rename_handle(HANDLE FromHandle, const Twine &To) { |
| 440 | SmallVector<wchar_t, 128> WideTo; |
| 441 | if (std::error_code EC = widenPath(To, WideTo)) |
| 442 | return EC; |
| 443 | |
Peter Collingbourne | 80e31f1 | 2017-10-06 17:14:36 +0000 | [diff] [blame] | 444 | // We normally expect this loop to succeed after a few iterations. If it |
| 445 | // requires more than 200 tries, it's more likely that the failures are due to |
| 446 | // a true error, so stop trying. |
| 447 | for (unsigned Retry = 0; Retry != 200; ++Retry) { |
| 448 | auto EC = rename_internal(FromHandle, To, true); |
Hans Wennborg | 17701ab | 2017-10-11 22:04:14 +0000 | [diff] [blame] | 449 | |
| 450 | if (EC == |
| 451 | std::error_code(ERROR_CALL_NOT_IMPLEMENTED, std::system_category())) { |
| 452 | // Wine doesn't support SetFileInformationByHandle in rename_internal. |
| 453 | // Fall back to MoveFileEx. |
Rafael Espindola | 5908aff | 2017-11-21 01:52:44 +0000 | [diff] [blame] | 454 | SmallVector<wchar_t, MAX_PATH> WideFrom; |
| 455 | if (std::error_code EC2 = realPathFromHandle(FromHandle, WideFrom)) |
| 456 | return EC2; |
Hans Wennborg | 17701ab | 2017-10-11 22:04:14 +0000 | [diff] [blame] | 457 | if (::MoveFileExW(WideFrom.begin(), WideTo.begin(), |
Jeremy Morse | 0194065 | 2018-08-03 10:13:35 +0000 | [diff] [blame] | 458 | MOVEFILE_REPLACE_EXISTING)) |
Hans Wennborg | 17701ab | 2017-10-11 22:04:14 +0000 | [diff] [blame] | 459 | return std::error_code(); |
| 460 | return mapWindowsError(GetLastError()); |
| 461 | } |
| 462 | |
Peter Collingbourne | 80e31f1 | 2017-10-06 17:14:36 +0000 | [diff] [blame] | 463 | if (!EC || EC != errc::permission_denied) |
| 464 | return EC; |
Greg Bedwell | 7f68a71 | 2015-10-12 15:11:47 +0000 | [diff] [blame] | 465 | |
Peter Collingbourne | 80e31f1 | 2017-10-06 17:14:36 +0000 | [diff] [blame] | 466 | // The destination file probably exists and is currently open in another |
| 467 | // process, either because the file was opened without FILE_SHARE_DELETE or |
| 468 | // it is mapped into memory (e.g. using MemoryBuffer). Rename it in order to |
| 469 | // move it out of the way of the source file. Use FILE_FLAG_DELETE_ON_CLOSE |
| 470 | // to arrange for the destination file to be deleted when the other process |
| 471 | // closes it. |
| 472 | ScopedFileHandle ToHandle( |
| 473 | ::CreateFileW(WideTo.begin(), GENERIC_READ | DELETE, |
| 474 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, |
| 475 | NULL, OPEN_EXISTING, |
| 476 | FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE, NULL)); |
| 477 | if (!ToHandle) { |
| 478 | auto EC = mapWindowsError(GetLastError()); |
| 479 | // Another process might have raced with us and moved the existing file |
| 480 | // out of the way before we had a chance to open it. If that happens, try |
| 481 | // to rename the source file again. |
| 482 | if (EC == errc::no_such_file_or_directory) |
Sunil Srivastava | 34fce93 | 2016-03-25 23:41:28 +0000 | [diff] [blame] | 483 | continue; |
Peter Collingbourne | 80e31f1 | 2017-10-06 17:14:36 +0000 | [diff] [blame] | 484 | return EC; |
Sunil Srivastava | 34fce93 | 2016-03-25 23:41:28 +0000 | [diff] [blame] | 485 | } |
Greg Bedwell | 7f68a71 | 2015-10-12 15:11:47 +0000 | [diff] [blame] | 486 | |
Peter Collingbourne | 80e31f1 | 2017-10-06 17:14:36 +0000 | [diff] [blame] | 487 | BY_HANDLE_FILE_INFORMATION FI; |
| 488 | if (!GetFileInformationByHandle(ToHandle, &FI)) |
| 489 | return mapWindowsError(GetLastError()); |
Greg Bedwell | 7f68a71 | 2015-10-12 15:11:47 +0000 | [diff] [blame] | 490 | |
Peter Collingbourne | 80e31f1 | 2017-10-06 17:14:36 +0000 | [diff] [blame] | 491 | // Try to find a unique new name for the destination file. |
| 492 | for (unsigned UniqueId = 0; UniqueId != 200; ++UniqueId) { |
| 493 | std::string TmpFilename = (To + ".tmp" + utostr(UniqueId)).str(); |
| 494 | if (auto EC = rename_internal(ToHandle, TmpFilename, false)) { |
| 495 | if (EC == errc::file_exists || EC == errc::permission_denied) { |
| 496 | // Again, another process might have raced with us and moved the file |
| 497 | // before we could move it. Check whether this is the case, as it |
| 498 | // might have caused the permission denied error. If that was the |
| 499 | // case, we don't need to move it ourselves. |
| 500 | ScopedFileHandle ToHandle2(::CreateFileW( |
| 501 | WideTo.begin(), 0, |
| 502 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, |
| 503 | OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)); |
| 504 | if (!ToHandle2) { |
| 505 | auto EC = mapWindowsError(GetLastError()); |
| 506 | if (EC == errc::no_such_file_or_directory) |
| 507 | break; |
| 508 | return EC; |
| 509 | } |
| 510 | BY_HANDLE_FILE_INFORMATION FI2; |
| 511 | if (!GetFileInformationByHandle(ToHandle2, &FI2)) |
| 512 | return mapWindowsError(GetLastError()); |
| 513 | if (FI.nFileIndexHigh != FI2.nFileIndexHigh || |
| 514 | FI.nFileIndexLow != FI2.nFileIndexLow || |
| 515 | FI.dwVolumeSerialNumber != FI2.dwVolumeSerialNumber) |
| 516 | break; |
| 517 | continue; |
| 518 | } |
| 519 | return EC; |
| 520 | } |
| 521 | break; |
| 522 | } |
| 523 | |
| 524 | // Okay, the old destination file has probably been moved out of the way at |
| 525 | // this point, so try to rename the source file again. Still, another |
| 526 | // process might have raced with us to create and open the destination |
| 527 | // file, so we need to keep doing this until we succeed. |
NAKAMURA Takumi | 3b7f995 | 2012-05-08 14:31:46 +0000 | [diff] [blame] | 528 | } |
Michael J. Spencer | 409f556 | 2010-12-03 17:53:55 +0000 | [diff] [blame] | 529 | |
Peter Collingbourne | 80e31f1 | 2017-10-06 17:14:36 +0000 | [diff] [blame] | 530 | // The most likely root cause. |
| 531 | return errc::permission_denied; |
Michael J. Spencer | 409f556 | 2010-12-03 17:53:55 +0000 | [diff] [blame] | 532 | } |
| 533 | |
Rafael Espindola | 3ecd204 | 2017-11-28 01:41:22 +0000 | [diff] [blame] | 534 | static std::error_code rename_fd(int FromFD, const Twine &To) { |
| 535 | HANDLE FromHandle = reinterpret_cast<HANDLE>(_get_osfhandle(FromFD)); |
| 536 | return rename_handle(FromHandle, To); |
| 537 | } |
| 538 | |
Rafael Espindola | 811d5e8 | 2017-11-21 05:35:45 +0000 | [diff] [blame] | 539 | std::error_code rename(const Twine &From, const Twine &To) { |
| 540 | // Convert to utf-16. |
| 541 | SmallVector<wchar_t, 128> WideFrom; |
| 542 | if (std::error_code EC = widenPath(From, WideFrom)) |
| 543 | return EC; |
| 544 | |
| 545 | ScopedFileHandle FromHandle; |
| 546 | // Retry this a few times to defeat badly behaved file system scanners. |
| 547 | for (unsigned Retry = 0; Retry != 200; ++Retry) { |
| 548 | if (Retry != 0) |
| 549 | ::Sleep(10); |
| 550 | FromHandle = |
| 551 | ::CreateFileW(WideFrom.begin(), GENERIC_READ | DELETE, |
| 552 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, |
| 553 | NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
| 554 | if (FromHandle) |
| 555 | break; |
| 556 | } |
| 557 | if (!FromHandle) |
| 558 | return mapWindowsError(GetLastError()); |
| 559 | |
| 560 | return rename_handle(FromHandle, To); |
| 561 | } |
| 562 | |
Rafael Espindola | 59aaa6c | 2014-12-12 17:55:12 +0000 | [diff] [blame] | 563 | std::error_code resize_file(int FD, uint64_t Size) { |
Michael J. Spencer | ca242f2 | 2010-12-03 18:48:56 +0000 | [diff] [blame] | 564 | #ifdef HAVE__CHSIZE_S |
Rafael Espindola | 59aaa6c | 2014-12-12 17:55:12 +0000 | [diff] [blame] | 565 | errno_t error = ::_chsize_s(FD, Size); |
Michael J. Spencer | ca242f2 | 2010-12-03 18:48:56 +0000 | [diff] [blame] | 566 | #else |
Rafael Espindola | 59aaa6c | 2014-12-12 17:55:12 +0000 | [diff] [blame] | 567 | errno_t error = ::_chsize(FD, Size); |
Michael J. Spencer | ca242f2 | 2010-12-03 18:48:56 +0000 | [diff] [blame] | 568 | #endif |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 569 | return std::error_code(error, std::generic_category()); |
Michael J. Spencer | c20a032 | 2010-12-03 17:54:07 +0000 | [diff] [blame] | 570 | } |
| 571 | |
Rafael Espindola | 281f23a | 2014-09-11 20:30:02 +0000 | [diff] [blame] | 572 | std::error_code access(const Twine &Path, AccessMode Mode) { |
Rafael Espindola | 281f23a | 2014-09-11 20:30:02 +0000 | [diff] [blame] | 573 | SmallVector<wchar_t, 128> PathUtf16; |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 574 | |
Paul Robinson | d9c4a9a | 2014-11-13 00:12:14 +0000 | [diff] [blame] | 575 | if (std::error_code EC = widenPath(Path, PathUtf16)) |
Rafael Espindola | 281f23a | 2014-09-11 20:30:02 +0000 | [diff] [blame] | 576 | return EC; |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 577 | |
Rafael Espindola | 281f23a | 2014-09-11 20:30:02 +0000 | [diff] [blame] | 578 | DWORD Attributes = ::GetFileAttributesW(PathUtf16.begin()); |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 579 | |
Rafael Espindola | 281f23a | 2014-09-11 20:30:02 +0000 | [diff] [blame] | 580 | if (Attributes == INVALID_FILE_ATTRIBUTES) { |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 581 | // See if the file didn't actually exist. |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 582 | DWORD LastError = ::GetLastError(); |
| 583 | if (LastError != ERROR_FILE_NOT_FOUND && |
| 584 | LastError != ERROR_PATH_NOT_FOUND) |
Yaron Keren | f8e6517 | 2015-05-04 04:48:10 +0000 | [diff] [blame] | 585 | return mapWindowsError(LastError); |
Rafael Espindola | 281f23a | 2014-09-11 20:30:02 +0000 | [diff] [blame] | 586 | return errc::no_such_file_or_directory; |
| 587 | } |
| 588 | |
| 589 | if (Mode == AccessMode::Write && (Attributes & FILE_ATTRIBUTE_READONLY)) |
| 590 | return errc::permission_denied; |
| 591 | |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 592 | return std::error_code(); |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 593 | } |
| 594 | |
Reid Kleckner | 89d4b1a | 2015-09-10 23:28:06 +0000 | [diff] [blame] | 595 | bool can_execute(const Twine &Path) { |
| 596 | return !access(Path, AccessMode::Execute) || |
| 597 | !access(Path + ".exe", AccessMode::Execute); |
| 598 | } |
| 599 | |
Michael J. Spencer | 203d780 | 2011-12-12 06:04:28 +0000 | [diff] [blame] | 600 | bool equivalent(file_status A, file_status B) { |
| 601 | assert(status_known(A) && status_known(B)); |
Mehdi Amini | 1e39ef3 | 2016-03-25 07:30:21 +0000 | [diff] [blame] | 602 | return A.FileIndexHigh == B.FileIndexHigh && |
| 603 | A.FileIndexLow == B.FileIndexLow && |
| 604 | A.FileSizeHigh == B.FileSizeHigh && |
| 605 | A.FileSizeLow == B.FileSizeLow && |
| 606 | A.LastAccessedTimeHigh == B.LastAccessedTimeHigh && |
| 607 | A.LastAccessedTimeLow == B.LastAccessedTimeLow && |
| 608 | A.LastWriteTimeHigh == B.LastWriteTimeHigh && |
| 609 | A.LastWriteTimeLow == B.LastWriteTimeLow && |
| 610 | A.VolumeSerialNumber == B.VolumeSerialNumber; |
Michael J. Spencer | 203d780 | 2011-12-12 06:04:28 +0000 | [diff] [blame] | 611 | } |
| 612 | |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 613 | 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] | 614 | file_status fsA, fsB; |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 615 | if (std::error_code ec = status(A, fsA)) |
| 616 | return ec; |
| 617 | if (std::error_code ec = status(B, fsB)) |
| 618 | return ec; |
Michael J. Spencer | 203d780 | 2011-12-12 06:04:28 +0000 | [diff] [blame] | 619 | result = equivalent(fsA, fsB); |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 620 | return std::error_code(); |
Michael J. Spencer | 376d387 | 2010-12-03 18:49:13 +0000 | [diff] [blame] | 621 | } |
| 622 | |
Benjamin Kramer | 58994ec | 2011-08-20 21:36:38 +0000 | [diff] [blame] | 623 | static bool isReservedName(StringRef path) { |
| 624 | // This list of reserved names comes from MSDN, at: |
| 625 | // http://msdn.microsoft.com/en-us/library/aa365247%28v=vs.85%29.aspx |
Craig Topper | 2626094 | 2015-10-18 05:15:34 +0000 | [diff] [blame] | 626 | static const char *const sReservedNames[] = { "nul", "con", "prn", "aux", |
| 627 | "com1", "com2", "com3", "com4", |
| 628 | "com5", "com6", "com7", "com8", |
| 629 | "com9", "lpt1", "lpt2", "lpt3", |
| 630 | "lpt4", "lpt5", "lpt6", "lpt7", |
| 631 | "lpt8", "lpt9" }; |
Benjamin Kramer | 58994ec | 2011-08-20 21:36:38 +0000 | [diff] [blame] | 632 | |
| 633 | // First, check to see if this is a device namespace, which always |
| 634 | // starts with \\.\, since device namespaces are not legal file paths. |
| 635 | if (path.startswith("\\\\.\\")) |
| 636 | return true; |
| 637 | |
Douglas Yung | 091d8fd | 2016-05-03 00:12:59 +0000 | [diff] [blame] | 638 | // Then compare against the list of ancient reserved names. |
Craig Topper | 5871321 | 2013-07-15 04:27:47 +0000 | [diff] [blame] | 639 | for (size_t i = 0; i < array_lengthof(sReservedNames); ++i) { |
Benjamin Kramer | 58994ec | 2011-08-20 21:36:38 +0000 | [diff] [blame] | 640 | if (path.equals_lower(sReservedNames[i])) |
| 641 | return true; |
| 642 | } |
| 643 | |
| 644 | // The path isn't what we consider reserved. |
| 645 | return false; |
| 646 | } |
| 647 | |
Peter Collingbourne | 0dfdb44 | 2017-10-10 22:19:46 +0000 | [diff] [blame] | 648 | static file_type file_type_from_attrs(DWORD Attrs) { |
| 649 | return (Attrs & FILE_ATTRIBUTE_DIRECTORY) ? file_type::directory_file |
| 650 | : file_type::regular_file; |
| 651 | } |
| 652 | |
| 653 | static perms perms_from_attrs(DWORD Attrs) { |
| 654 | return (Attrs & FILE_ATTRIBUTE_READONLY) ? (all_read | all_exe) : all_all; |
| 655 | } |
| 656 | |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 657 | static std::error_code getStatus(HANDLE FileHandle, file_status &Result) { |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 658 | if (FileHandle == INVALID_HANDLE_VALUE) |
| 659 | goto handle_status_error; |
| 660 | |
NAKAMURA Takumi | 8b01da4 | 2013-07-18 17:00:54 +0000 | [diff] [blame] | 661 | switch (::GetFileType(FileHandle)) { |
| 662 | default: |
Rafael Espindola | 81177c5 | 2013-07-18 18:42:52 +0000 | [diff] [blame] | 663 | llvm_unreachable("Don't know anything about this file type"); |
| 664 | case FILE_TYPE_UNKNOWN: { |
| 665 | DWORD Err = ::GetLastError(); |
| 666 | if (Err != NO_ERROR) |
Yaron Keren | f8e6517 | 2015-05-04 04:48:10 +0000 | [diff] [blame] | 667 | return mapWindowsError(Err); |
Rafael Espindola | 81177c5 | 2013-07-18 18:42:52 +0000 | [diff] [blame] | 668 | Result = file_status(file_type::type_unknown); |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 669 | return std::error_code(); |
Rafael Espindola | 81177c5 | 2013-07-18 18:42:52 +0000 | [diff] [blame] | 670 | } |
NAKAMURA Takumi | 8b01da4 | 2013-07-18 17:00:54 +0000 | [diff] [blame] | 671 | case FILE_TYPE_DISK: |
| 672 | break; |
| 673 | case FILE_TYPE_CHAR: |
| 674 | Result = file_status(file_type::character_file); |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 675 | return std::error_code(); |
NAKAMURA Takumi | 8b01da4 | 2013-07-18 17:00:54 +0000 | [diff] [blame] | 676 | case FILE_TYPE_PIPE: |
| 677 | Result = file_status(file_type::fifo_file); |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 678 | return std::error_code(); |
NAKAMURA Takumi | 8b01da4 | 2013-07-18 17:00:54 +0000 | [diff] [blame] | 679 | } |
| 680 | |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 681 | BY_HANDLE_FILE_INFORMATION Info; |
| 682 | if (!::GetFileInformationByHandle(FileHandle, &Info)) |
| 683 | goto handle_status_error; |
| 684 | |
Peter Collingbourne | 0dfdb44 | 2017-10-10 22:19:46 +0000 | [diff] [blame] | 685 | Result = file_status( |
| 686 | file_type_from_attrs(Info.dwFileAttributes), |
| 687 | perms_from_attrs(Info.dwFileAttributes), Info.nNumberOfLinks, |
| 688 | Info.ftLastAccessTime.dwHighDateTime, Info.ftLastAccessTime.dwLowDateTime, |
| 689 | Info.ftLastWriteTime.dwHighDateTime, Info.ftLastWriteTime.dwLowDateTime, |
| 690 | Info.dwVolumeSerialNumber, Info.nFileSizeHigh, Info.nFileSizeLow, |
| 691 | Info.nFileIndexHigh, Info.nFileIndexLow); |
| 692 | return std::error_code(); |
Aaron Ballman | 345012d | 2017-03-13 12:24:51 +0000 | [diff] [blame] | 693 | |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 694 | handle_status_error: |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 695 | DWORD LastError = ::GetLastError(); |
| 696 | if (LastError == ERROR_FILE_NOT_FOUND || |
| 697 | LastError == ERROR_PATH_NOT_FOUND) |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 698 | Result = file_status(file_type::file_not_found); |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 699 | else if (LastError == ERROR_SHARING_VIOLATION) |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 700 | Result = file_status(file_type::type_unknown); |
Rafael Espindola | 107b74c | 2013-07-31 00:10:25 +0000 | [diff] [blame] | 701 | else |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 702 | Result = file_status(file_type::status_error); |
Yaron Keren | f8e6517 | 2015-05-04 04:48:10 +0000 | [diff] [blame] | 703 | return mapWindowsError(LastError); |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 704 | } |
| 705 | |
Zachary Turner | 82dd542 | 2017-03-07 16:10:10 +0000 | [diff] [blame] | 706 | std::error_code status(const Twine &path, file_status &result, bool Follow) { |
Michael J. Spencer | db5576a | 2010-12-04 00:32:40 +0000 | [diff] [blame] | 707 | SmallString<128> path_storage; |
| 708 | SmallVector<wchar_t, 128> path_utf16; |
| 709 | |
NAKAMURA Takumi | a3d4749 | 2011-03-16 02:53:32 +0000 | [diff] [blame] | 710 | StringRef path8 = path.toStringRef(path_storage); |
Benjamin Kramer | 58994ec | 2011-08-20 21:36:38 +0000 | [diff] [blame] | 711 | if (isReservedName(path8)) { |
NAKAMURA Takumi | a3d4749 | 2011-03-16 02:53:32 +0000 | [diff] [blame] | 712 | result = file_status(file_type::character_file); |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 713 | return std::error_code(); |
NAKAMURA Takumi | a3d4749 | 2011-03-16 02:53:32 +0000 | [diff] [blame] | 714 | } |
| 715 | |
Paul Robinson | d9c4a9a | 2014-11-13 00:12:14 +0000 | [diff] [blame] | 716 | if (std::error_code ec = widenPath(path8, path_utf16)) |
Michael J. Spencer | db5576a | 2010-12-04 00:32:40 +0000 | [diff] [blame] | 717 | return ec; |
| 718 | |
| 719 | DWORD attr = ::GetFileAttributesW(path_utf16.begin()); |
| 720 | if (attr == INVALID_FILE_ATTRIBUTES) |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 721 | return getStatus(INVALID_HANDLE_VALUE, result); |
Michael J. Spencer | db5576a | 2010-12-04 00:32:40 +0000 | [diff] [blame] | 722 | |
Zachary Turner | 82dd542 | 2017-03-07 16:10:10 +0000 | [diff] [blame] | 723 | DWORD Flags = FILE_FLAG_BACKUP_SEMANTICS; |
Michael J. Spencer | db5576a | 2010-12-04 00:32:40 +0000 | [diff] [blame] | 724 | // Handle reparse points. |
Zachary Turner | 82dd542 | 2017-03-07 16:10:10 +0000 | [diff] [blame] | 725 | if (!Follow && (attr & FILE_ATTRIBUTE_REPARSE_POINT)) |
| 726 | Flags |= FILE_FLAG_OPEN_REPARSE_POINT; |
Michael J. Spencer | db5576a | 2010-12-04 00:32:40 +0000 | [diff] [blame] | 727 | |
Rafael Espindola | a5932af | 2013-07-30 20:25:53 +0000 | [diff] [blame] | 728 | ScopedFileHandle h( |
| 729 | ::CreateFileW(path_utf16.begin(), 0, // Attributes only. |
Michael J. Spencer | 203d780 | 2011-12-12 06:04:28 +0000 | [diff] [blame] | 730 | FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, |
Zachary Turner | 82dd542 | 2017-03-07 16:10:10 +0000 | [diff] [blame] | 731 | NULL, OPEN_EXISTING, Flags, 0)); |
| 732 | if (!h) |
| 733 | return getStatus(INVALID_HANDLE_VALUE, result); |
Michael J. Spencer | db5576a | 2010-12-04 00:32:40 +0000 | [diff] [blame] | 734 | |
Zachary Turner | 82dd542 | 2017-03-07 16:10:10 +0000 | [diff] [blame] | 735 | return getStatus(h, result); |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 736 | } |
Michael J. Spencer | db5576a | 2010-12-04 00:32:40 +0000 | [diff] [blame] | 737 | |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 738 | std::error_code status(int FD, file_status &Result) { |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 739 | HANDLE FileHandle = reinterpret_cast<HANDLE>(_get_osfhandle(FD)); |
Aaron Ballman | 345012d | 2017-03-13 12:24:51 +0000 | [diff] [blame] | 740 | return getStatus(FileHandle, Result); |
| 741 | } |
| 742 | |
Reid Kleckner | cc418a3 | 2019-07-10 00:34:13 +0000 | [diff] [blame] | 743 | std::error_code status(file_t FileHandle, file_status &Result) { |
| 744 | return getStatus(FileHandle, Result); |
| 745 | } |
| 746 | |
Alex Brachet | 3b715d6 | 2019-06-28 03:21:00 +0000 | [diff] [blame] | 747 | unsigned getUmask() { |
| 748 | return 0; |
| 749 | } |
| 750 | |
Fangrui Song | 6dc5962 | 2019-07-11 10:17:59 +0000 | [diff] [blame] | 751 | std::error_code setPermissions(const Twine &Path, perms Permissions) { |
James Henderson | 566fdf4 | 2017-03-16 11:22:09 +0000 | [diff] [blame] | 752 | SmallVector<wchar_t, 128> PathUTF16; |
| 753 | if (std::error_code EC = widenPath(Path, PathUTF16)) |
| 754 | return EC; |
| 755 | |
| 756 | DWORD Attributes = ::GetFileAttributesW(PathUTF16.begin()); |
| 757 | if (Attributes == INVALID_FILE_ATTRIBUTES) |
| 758 | return mapWindowsError(GetLastError()); |
| 759 | |
| 760 | // There are many Windows file attributes that are not to do with the file |
| 761 | // permissions (e.g. FILE_ATTRIBUTE_HIDDEN). We need to be careful to preserve |
| 762 | // them. |
| 763 | if (Permissions & all_write) { |
| 764 | Attributes &= ~FILE_ATTRIBUTE_READONLY; |
| 765 | if (Attributes == 0) |
| 766 | // FILE_ATTRIBUTE_NORMAL indicates no other attributes are set. |
| 767 | Attributes |= FILE_ATTRIBUTE_NORMAL; |
| 768 | } |
| 769 | else { |
| 770 | Attributes |= FILE_ATTRIBUTE_READONLY; |
| 771 | // FILE_ATTRIBUTE_NORMAL is not compatible with any other attributes, so |
| 772 | // remove it, if it is present. |
| 773 | Attributes &= ~FILE_ATTRIBUTE_NORMAL; |
| 774 | } |
| 775 | |
| 776 | if (!::SetFileAttributesW(PathUTF16.begin(), Attributes)) |
| 777 | return mapWindowsError(GetLastError()); |
| 778 | |
| 779 | return std::error_code(); |
| 780 | } |
| 781 | |
Fangrui Song | 6dc5962 | 2019-07-11 10:17:59 +0000 | [diff] [blame] | 782 | std::error_code setPermissions(int FD, perms Permissions) { |
| 783 | // FIXME Not implemented. |
| 784 | return std::make_error_code(std::errc::not_supported); |
| 785 | } |
| 786 | |
Jordan Rupprecht | 97ea485 | 2018-08-13 23:03:45 +0000 | [diff] [blame] | 787 | std::error_code setLastAccessAndModificationTime(int FD, TimePoint<> AccessTime, |
| 788 | TimePoint<> ModificationTime) { |
| 789 | FILETIME AccessFT = toFILETIME(AccessTime); |
| 790 | FILETIME ModifyFT = toFILETIME(ModificationTime); |
Aaron Ballman | 345012d | 2017-03-13 12:24:51 +0000 | [diff] [blame] | 791 | HANDLE FileHandle = reinterpret_cast<HANDLE>(_get_osfhandle(FD)); |
Jordan Rupprecht | 97ea485 | 2018-08-13 23:03:45 +0000 | [diff] [blame] | 792 | if (!SetFileTime(FileHandle, NULL, &AccessFT, &ModifyFT)) |
Yaron Keren | f8e6517 | 2015-05-04 04:48:10 +0000 | [diff] [blame] | 793 | return mapWindowsError(::GetLastError()); |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 794 | return std::error_code(); |
Rafael Espindola | 4a3365c | 2013-06-20 20:56:14 +0000 | [diff] [blame] | 795 | } |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 796 | |
Reid Kleckner | cc418a3 | 2019-07-10 00:34:13 +0000 | [diff] [blame] | 797 | std::error_code mapped_file_region::init(sys::fs::file_t OrigFileHandle, |
| 798 | uint64_t Offset, mapmode Mode) { |
Zachary Turner | acd8791 | 2018-02-15 18:36:10 +0000 | [diff] [blame] | 799 | this->Mode = Mode; |
Peter Collingbourne | 881ba10 | 2018-06-13 18:03:14 +0000 | [diff] [blame] | 800 | if (OrigFileHandle == INVALID_HANDLE_VALUE) |
Rafael Espindola | a23008a | 2014-12-16 03:10:29 +0000 | [diff] [blame] | 801 | return make_error_code(errc::bad_file_descriptor); |
| 802 | |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 803 | DWORD flprotect; |
| 804 | switch (Mode) { |
| 805 | case readonly: flprotect = PAGE_READONLY; break; |
| 806 | case readwrite: flprotect = PAGE_READWRITE; break; |
| 807 | case priv: flprotect = PAGE_WRITECOPY; break; |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 808 | } |
| 809 | |
Rafael Espindola | 369d514 | 2014-12-16 02:53:35 +0000 | [diff] [blame] | 810 | HANDLE FileMappingHandle = |
Peter Collingbourne | 881ba10 | 2018-06-13 18:03:14 +0000 | [diff] [blame] | 811 | ::CreateFileMappingW(OrigFileHandle, 0, flprotect, |
Zachary Turner | ab1ade4 | 2017-11-16 22:39:55 +0000 | [diff] [blame] | 812 | Hi_32(Size), |
| 813 | Lo_32(Size), |
David Majnemer | 17a4496 | 2013-10-07 09:52:36 +0000 | [diff] [blame] | 814 | 0); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 815 | if (FileMappingHandle == NULL) { |
Yaron Keren | f8e6517 | 2015-05-04 04:48:10 +0000 | [diff] [blame] | 816 | std::error_code ec = mapWindowsError(GetLastError()); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 817 | return ec; |
| 818 | } |
| 819 | |
| 820 | DWORD dwDesiredAccess; |
| 821 | switch (Mode) { |
| 822 | case readonly: dwDesiredAccess = FILE_MAP_READ; break; |
| 823 | case readwrite: dwDesiredAccess = FILE_MAP_WRITE; break; |
| 824 | case priv: dwDesiredAccess = FILE_MAP_COPY; break; |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 825 | } |
| 826 | Mapping = ::MapViewOfFile(FileMappingHandle, |
| 827 | dwDesiredAccess, |
| 828 | Offset >> 32, |
| 829 | Offset & 0xffffffff, |
| 830 | Size); |
| 831 | if (Mapping == NULL) { |
Yaron Keren | f8e6517 | 2015-05-04 04:48:10 +0000 | [diff] [blame] | 832 | std::error_code ec = mapWindowsError(GetLastError()); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 833 | ::CloseHandle(FileMappingHandle); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 834 | return ec; |
| 835 | } |
| 836 | |
| 837 | if (Size == 0) { |
| 838 | MEMORY_BASIC_INFORMATION mbi; |
| 839 | SIZE_T Result = VirtualQuery(Mapping, &mbi, sizeof(mbi)); |
| 840 | if (Result == 0) { |
Yaron Keren | f8e6517 | 2015-05-04 04:48:10 +0000 | [diff] [blame] | 841 | std::error_code ec = mapWindowsError(GetLastError()); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 842 | ::UnmapViewOfFile(Mapping); |
| 843 | ::CloseHandle(FileMappingHandle); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 844 | return ec; |
| 845 | } |
| 846 | Size = mbi.RegionSize; |
| 847 | } |
Michael J. Spencer | 42ad29f | 2013-03-14 00:20:10 +0000 | [diff] [blame] | 848 | |
Peter Collingbourne | 881ba10 | 2018-06-13 18:03:14 +0000 | [diff] [blame] | 849 | // Close the file mapping handle, as it's kept alive by the file mapping. But |
| 850 | // neither the file mapping nor the file mapping handle keep the file handle |
| 851 | // alive, so we need to keep a reference to the file in case all other handles |
| 852 | // are closed and the file is deleted, which may cause invalid data to be read |
| 853 | // from the file. |
Michael J. Spencer | 42ad29f | 2013-03-14 00:20:10 +0000 | [diff] [blame] | 854 | ::CloseHandle(FileMappingHandle); |
Peter Collingbourne | 881ba10 | 2018-06-13 18:03:14 +0000 | [diff] [blame] | 855 | if (!::DuplicateHandle(::GetCurrentProcess(), OrigFileHandle, |
| 856 | ::GetCurrentProcess(), &FileHandle, 0, 0, |
| 857 | DUPLICATE_SAME_ACCESS)) { |
| 858 | std::error_code ec = mapWindowsError(GetLastError()); |
| 859 | ::UnmapViewOfFile(Mapping); |
| 860 | return ec; |
| 861 | } |
| 862 | |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 863 | return std::error_code(); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 864 | } |
| 865 | |
Reid Kleckner | cc418a3 | 2019-07-10 00:34:13 +0000 | [diff] [blame] | 866 | mapped_file_region::mapped_file_region(sys::fs::file_t fd, mapmode mode, |
| 867 | size_t length, uint64_t offset, |
| 868 | std::error_code &ec) |
Rafael Espindola | a23008a | 2014-12-16 03:10:29 +0000 | [diff] [blame] | 869 | : Size(length), Mapping() { |
Rafael Espindola | 986f5ad | 2014-12-16 02:19:26 +0000 | [diff] [blame] | 870 | ec = init(fd, offset, mode); |
Rafael Espindola | a23008a | 2014-12-16 03:10:29 +0000 | [diff] [blame] | 871 | if (ec) |
Rafael Espindola | 369d514 | 2014-12-16 02:53:35 +0000 | [diff] [blame] | 872 | Mapping = 0; |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 873 | } |
| 874 | |
Alexandre Ganea | 3b9b4d2 | 2018-11-05 19:14:10 +0000 | [diff] [blame] | 875 | static bool hasFlushBufferKernelBug() { |
| 876 | static bool Ret{GetWindowsOSVersion() < llvm::VersionTuple(10, 0, 0, 17763)}; |
| 877 | return Ret; |
| 878 | } |
| 879 | |
| 880 | static bool isEXE(StringRef Magic) { |
| 881 | static const char PEMagic[] = {'P', 'E', '\0', '\0'}; |
| 882 | if (Magic.startswith(StringRef("MZ")) && Magic.size() >= 0x3c + 4) { |
| 883 | uint32_t off = read32le(Magic.data() + 0x3c); |
| 884 | // PE/COFF file, either EXE or DLL. |
| 885 | if (Magic.substr(off).startswith(StringRef(PEMagic, sizeof(PEMagic)))) |
| 886 | return true; |
| 887 | } |
| 888 | return false; |
| 889 | } |
| 890 | |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 891 | mapped_file_region::~mapped_file_region() { |
Zachary Turner | acd8791 | 2018-02-15 18:36:10 +0000 | [diff] [blame] | 892 | if (Mapping) { |
Alexandre Ganea | 3b9b4d2 | 2018-11-05 19:14:10 +0000 | [diff] [blame] | 893 | |
| 894 | bool Exe = isEXE(StringRef((char *)Mapping, Size)); |
| 895 | |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 896 | ::UnmapViewOfFile(Mapping); |
Zachary Turner | acd8791 | 2018-02-15 18:36:10 +0000 | [diff] [blame] | 897 | |
Alexandre Ganea | 3b9b4d2 | 2018-11-05 19:14:10 +0000 | [diff] [blame] | 898 | if (Mode == mapmode::readwrite && Exe && hasFlushBufferKernelBug()) { |
Zachary Turner | acd8791 | 2018-02-15 18:36:10 +0000 | [diff] [blame] | 899 | // There is a Windows kernel bug, the exact trigger conditions of which |
| 900 | // are not well understood. When triggered, dirty pages are not properly |
| 901 | // flushed and subsequent process's attempts to read a file can return |
| 902 | // invalid data. Calling FlushFileBuffers on the write handle is |
| 903 | // sufficient to ensure that this bug is not triggered. |
Alexandre Ganea | 3b9b4d2 | 2018-11-05 19:14:10 +0000 | [diff] [blame] | 904 | // The bug only occurs when writing an executable and executing it right |
| 905 | // after, under high I/O pressure. |
Peter Collingbourne | 881ba10 | 2018-06-13 18:03:14 +0000 | [diff] [blame] | 906 | ::FlushFileBuffers(FileHandle); |
Zachary Turner | acd8791 | 2018-02-15 18:36:10 +0000 | [diff] [blame] | 907 | } |
Peter Collingbourne | 881ba10 | 2018-06-13 18:03:14 +0000 | [diff] [blame] | 908 | |
| 909 | ::CloseHandle(FileHandle); |
Zachary Turner | acd8791 | 2018-02-15 18:36:10 +0000 | [diff] [blame] | 910 | } |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 911 | } |
| 912 | |
Roman Lebedev | 1e053ab | 2017-09-27 17:24:34 +0000 | [diff] [blame] | 913 | size_t mapped_file_region::size() const { |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 914 | assert(Mapping && "Mapping failed but used anyway!"); |
| 915 | return Size; |
| 916 | } |
| 917 | |
| 918 | char *mapped_file_region::data() const { |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 919 | assert(Mapping && "Mapping failed but used anyway!"); |
| 920 | return reinterpret_cast<char*>(Mapping); |
| 921 | } |
| 922 | |
| 923 | const char *mapped_file_region::const_data() const { |
| 924 | assert(Mapping && "Mapping failed but used anyway!"); |
| 925 | return reinterpret_cast<const char*>(Mapping); |
| 926 | } |
| 927 | |
| 928 | int mapped_file_region::alignment() { |
| 929 | SYSTEM_INFO SysInfo; |
| 930 | ::GetSystemInfo(&SysInfo); |
| 931 | return SysInfo.dwAllocationGranularity; |
| 932 | } |
| 933 | |
Peter Collingbourne | b4f1b88 | 2017-10-11 02:09:06 +0000 | [diff] [blame] | 934 | static basic_file_status status_from_find_data(WIN32_FIND_DATAW *FindData) { |
Peter Collingbourne | 0dfdb44 | 2017-10-10 22:19:46 +0000 | [diff] [blame] | 935 | return basic_file_status(file_type_from_attrs(FindData->dwFileAttributes), |
| 936 | perms_from_attrs(FindData->dwFileAttributes), |
| 937 | FindData->ftLastAccessTime.dwHighDateTime, |
| 938 | FindData->ftLastAccessTime.dwLowDateTime, |
| 939 | FindData->ftLastWriteTime.dwHighDateTime, |
| 940 | FindData->ftLastWriteTime.dwLowDateTime, |
| 941 | FindData->nFileSizeHigh, FindData->nFileSizeLow); |
| 942 | } |
| 943 | |
Kristina Brooks | 3a55d1e | 2018-09-12 22:08:10 +0000 | [diff] [blame] | 944 | std::error_code detail::directory_iterator_construct(detail::DirIterState &IT, |
| 945 | StringRef Path, |
| 946 | bool FollowSymlinks) { |
| 947 | SmallVector<wchar_t, 128> PathUTF16; |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 948 | |
Kristina Brooks | 3a55d1e | 2018-09-12 22:08:10 +0000 | [diff] [blame] | 949 | if (std::error_code EC = widenPath(Path, PathUTF16)) |
| 950 | return EC; |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 951 | |
| 952 | // Convert path to the format that Windows is happy with. |
Kristina Brooks | 3a55d1e | 2018-09-12 22:08:10 +0000 | [diff] [blame] | 953 | if (PathUTF16.size() > 0 && |
| 954 | !is_separator(PathUTF16[Path.size() - 1]) && |
| 955 | PathUTF16[Path.size() - 1] != L':') { |
| 956 | PathUTF16.push_back(L'\\'); |
| 957 | PathUTF16.push_back(L'*'); |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 958 | } else { |
Kristina Brooks | 3a55d1e | 2018-09-12 22:08:10 +0000 | [diff] [blame] | 959 | PathUTF16.push_back(L'*'); |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | // Get the first directory entry. |
| 963 | WIN32_FIND_DATAW FirstFind; |
Peter Collingbourne | 0dfdb44 | 2017-10-10 22:19:46 +0000 | [diff] [blame] | 964 | ScopedFindHandle FindHandle(::FindFirstFileExW( |
Kristina Brooks | 3a55d1e | 2018-09-12 22:08:10 +0000 | [diff] [blame] | 965 | c_str(PathUTF16), FindExInfoBasic, &FirstFind, FindExSearchNameMatch, |
Peter Collingbourne | 0dfdb44 | 2017-10-10 22:19:46 +0000 | [diff] [blame] | 966 | NULL, FIND_FIRST_EX_LARGE_FETCH)); |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 967 | if (!FindHandle) |
Yaron Keren | f8e6517 | 2015-05-04 04:48:10 +0000 | [diff] [blame] | 968 | return mapWindowsError(::GetLastError()); |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 969 | |
Michael J. Spencer | 98879d7 | 2011-01-05 16:39:30 +0000 | [diff] [blame] | 970 | size_t FilenameLen = ::wcslen(FirstFind.cFileName); |
| 971 | while ((FilenameLen == 1 && FirstFind.cFileName[0] == L'.') || |
| 972 | (FilenameLen == 2 && FirstFind.cFileName[0] == L'.' && |
| 973 | FirstFind.cFileName[1] == L'.')) |
| 974 | if (!::FindNextFileW(FindHandle, &FirstFind)) { |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 975 | DWORD LastError = ::GetLastError(); |
Michael J. Spencer | 98879d7 | 2011-01-05 16:39:30 +0000 | [diff] [blame] | 976 | // Check for end. |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 977 | if (LastError == ERROR_NO_MORE_FILES) |
Kristina Brooks | 3a55d1e | 2018-09-12 22:08:10 +0000 | [diff] [blame] | 978 | return detail::directory_iterator_destruct(IT); |
Yaron Keren | f8e6517 | 2015-05-04 04:48:10 +0000 | [diff] [blame] | 979 | return mapWindowsError(LastError); |
Michael J. Spencer | 98879d7 | 2011-01-05 16:39:30 +0000 | [diff] [blame] | 980 | } else |
| 981 | FilenameLen = ::wcslen(FirstFind.cFileName); |
| 982 | |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 983 | // Construct the current directory entry. |
Kristina Brooks | 3a55d1e | 2018-09-12 22:08:10 +0000 | [diff] [blame] | 984 | SmallString<128> DirectoryEntryNameUTF8; |
| 985 | if (std::error_code EC = |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 986 | UTF16ToUTF8(FirstFind.cFileName, ::wcslen(FirstFind.cFileName), |
Kristina Brooks | 3a55d1e | 2018-09-12 22:08:10 +0000 | [diff] [blame] | 987 | DirectoryEntryNameUTF8)) |
| 988 | return EC; |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 989 | |
Kristina Brooks | 3a55d1e | 2018-09-12 22:08:10 +0000 | [diff] [blame] | 990 | IT.IterationHandle = intptr_t(FindHandle.take()); |
| 991 | SmallString<128> DirectoryEntryPath(Path); |
| 992 | path::append(DirectoryEntryPath, DirectoryEntryNameUTF8); |
| 993 | IT.CurrentEntry = |
| 994 | directory_entry(DirectoryEntryPath, FollowSymlinks, |
| 995 | file_type_from_attrs(FirstFind.dwFileAttributes), |
| 996 | status_from_find_data(&FirstFind)); |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 997 | |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 998 | return std::error_code(); |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
Kristina Brooks | 3a55d1e | 2018-09-12 22:08:10 +0000 | [diff] [blame] | 1001 | std::error_code detail::directory_iterator_destruct(detail::DirIterState &IT) { |
| 1002 | if (IT.IterationHandle != 0) |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 1003 | // Closes the handle if it's valid. |
Kristina Brooks | 3a55d1e | 2018-09-12 22:08:10 +0000 | [diff] [blame] | 1004 | ScopedFindHandle close(HANDLE(IT.IterationHandle)); |
| 1005 | IT.IterationHandle = 0; |
| 1006 | IT.CurrentEntry = directory_entry(); |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 1007 | return std::error_code(); |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 1008 | } |
| 1009 | |
Kristina Brooks | 3a55d1e | 2018-09-12 22:08:10 +0000 | [diff] [blame] | 1010 | std::error_code detail::directory_iterator_increment(detail::DirIterState &IT) { |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 1011 | WIN32_FIND_DATAW FindData; |
Kristina Brooks | 3a55d1e | 2018-09-12 22:08:10 +0000 | [diff] [blame] | 1012 | if (!::FindNextFileW(HANDLE(IT.IterationHandle), &FindData)) { |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 1013 | DWORD LastError = ::GetLastError(); |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 1014 | // Check for end. |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 1015 | if (LastError == ERROR_NO_MORE_FILES) |
Kristina Brooks | 3a55d1e | 2018-09-12 22:08:10 +0000 | [diff] [blame] | 1016 | return detail::directory_iterator_destruct(IT); |
Yaron Keren | f8e6517 | 2015-05-04 04:48:10 +0000 | [diff] [blame] | 1017 | return mapWindowsError(LastError); |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
Michael J. Spencer | 98879d7 | 2011-01-05 16:39:30 +0000 | [diff] [blame] | 1020 | size_t FilenameLen = ::wcslen(FindData.cFileName); |
| 1021 | if ((FilenameLen == 1 && FindData.cFileName[0] == L'.') || |
| 1022 | (FilenameLen == 2 && FindData.cFileName[0] == L'.' && |
| 1023 | FindData.cFileName[1] == L'.')) |
Kristina Brooks | 3a55d1e | 2018-09-12 22:08:10 +0000 | [diff] [blame] | 1024 | return directory_iterator_increment(IT); |
Michael J. Spencer | 98879d7 | 2011-01-05 16:39:30 +0000 | [diff] [blame] | 1025 | |
Kristina Brooks | 3a55d1e | 2018-09-12 22:08:10 +0000 | [diff] [blame] | 1026 | SmallString<128> DirectoryEntryPathUTF8; |
| 1027 | if (std::error_code EC = |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 1028 | UTF16ToUTF8(FindData.cFileName, ::wcslen(FindData.cFileName), |
Kristina Brooks | 3a55d1e | 2018-09-12 22:08:10 +0000 | [diff] [blame] | 1029 | DirectoryEntryPathUTF8)) |
| 1030 | return EC; |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 1031 | |
Kristina Brooks | 3a55d1e | 2018-09-12 22:08:10 +0000 | [diff] [blame] | 1032 | IT.CurrentEntry.replace_filename( |
| 1033 | Twine(DirectoryEntryPathUTF8), |
| 1034 | file_type_from_attrs(FindData.dwFileAttributes), |
| 1035 | status_from_find_data(&FindData)); |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 1036 | return std::error_code(); |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
Peter Collingbourne | 0dfdb44 | 2017-10-10 22:19:46 +0000 | [diff] [blame] | 1039 | ErrorOr<basic_file_status> directory_entry::status() const { |
| 1040 | return Status; |
| 1041 | } |
| 1042 | |
Zachary Turner | 63db25b | 2018-06-04 19:38:11 +0000 | [diff] [blame] | 1043 | static std::error_code nativeFileToFd(Expected<HANDLE> H, int &ResultFD, |
Zachary Turner | 1f67a3c | 2018-06-07 19:58:58 +0000 | [diff] [blame] | 1044 | OpenFlags Flags) { |
| 1045 | int CrtOpenFlags = 0; |
| 1046 | if (Flags & OF_Append) |
| 1047 | CrtOpenFlags |= _O_APPEND; |
| 1048 | |
| 1049 | if (Flags & OF_Text) |
| 1050 | CrtOpenFlags |= _O_TEXT; |
| 1051 | |
Zachary Turner | 63db25b | 2018-06-04 19:38:11 +0000 | [diff] [blame] | 1052 | ResultFD = -1; |
| 1053 | if (!H) |
| 1054 | return errorToErrorCode(H.takeError()); |
| 1055 | |
Zachary Turner | 1f67a3c | 2018-06-07 19:58:58 +0000 | [diff] [blame] | 1056 | ResultFD = ::_open_osfhandle(intptr_t(*H), CrtOpenFlags); |
Zachary Turner | 63db25b | 2018-06-04 19:38:11 +0000 | [diff] [blame] | 1057 | if (ResultFD == -1) { |
| 1058 | ::CloseHandle(*H); |
| 1059 | return mapWindowsError(ERROR_INVALID_HANDLE); |
| 1060 | } |
| 1061 | return std::error_code(); |
| 1062 | } |
| 1063 | |
Zachary Turner | 1f67a3c | 2018-06-07 19:58:58 +0000 | [diff] [blame] | 1064 | static DWORD nativeDisposition(CreationDisposition Disp, OpenFlags Flags) { |
| 1065 | // This is a compatibility hack. Really we should respect the creation |
| 1066 | // disposition, but a lot of old code relied on the implicit assumption that |
| 1067 | // OF_Append implied it would open an existing file. Since the disposition is |
| 1068 | // now explicit and defaults to CD_CreateAlways, this assumption would cause |
| 1069 | // any usage of OF_Append to append to a new file, even if the file already |
| 1070 | // existed. A better solution might have two new creation dispositions: |
| 1071 | // CD_AppendAlways and CD_AppendNew. This would also address the problem of |
| 1072 | // OF_Append being used on a read-only descriptor, which doesn't make sense. |
| 1073 | if (Flags & OF_Append) |
| 1074 | return OPEN_ALWAYS; |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 1075 | |
Zachary Turner | 1f67a3c | 2018-06-07 19:58:58 +0000 | [diff] [blame] | 1076 | switch (Disp) { |
| 1077 | case CD_CreateAlways: |
| 1078 | return CREATE_ALWAYS; |
| 1079 | case CD_CreateNew: |
| 1080 | return CREATE_NEW; |
| 1081 | case CD_OpenAlways: |
| 1082 | return OPEN_ALWAYS; |
| 1083 | case CD_OpenExisting: |
| 1084 | return OPEN_EXISTING; |
| 1085 | } |
| 1086 | llvm_unreachable("unreachable!"); |
| 1087 | } |
| 1088 | |
| 1089 | static DWORD nativeAccess(FileAccess Access, OpenFlags Flags) { |
| 1090 | DWORD Result = 0; |
| 1091 | if (Access & FA_Read) |
| 1092 | Result |= GENERIC_READ; |
| 1093 | if (Access & FA_Write) |
| 1094 | Result |= GENERIC_WRITE; |
| 1095 | if (Flags & OF_Delete) |
| 1096 | Result |= DELETE; |
Andrew Ng | 089303d | 2018-07-04 14:17:10 +0000 | [diff] [blame] | 1097 | if (Flags & OF_UpdateAtime) |
| 1098 | Result |= FILE_WRITE_ATTRIBUTES; |
Zachary Turner | 1f67a3c | 2018-06-07 19:58:58 +0000 | [diff] [blame] | 1099 | return Result; |
| 1100 | } |
| 1101 | |
Zachary Turner | 9d2cfa6 | 2018-06-07 23:25:13 +0000 | [diff] [blame] | 1102 | static std::error_code openNativeFileInternal(const Twine &Name, |
| 1103 | file_t &ResultFile, DWORD Disp, |
Zachary Turner | 6edfecb | 2018-06-08 15:15:56 +0000 | [diff] [blame] | 1104 | DWORD Access, DWORD Flags, |
| 1105 | bool Inherit = false) { |
Zachary Turner | 1f67a3c | 2018-06-07 19:58:58 +0000 | [diff] [blame] | 1106 | SmallVector<wchar_t, 128> PathUTF16; |
Paul Robinson | d9c4a9a | 2014-11-13 00:12:14 +0000 | [diff] [blame] | 1107 | if (std::error_code EC = widenPath(Name, PathUTF16)) |
Zachary Turner | 9d2cfa6 | 2018-06-07 23:25:13 +0000 | [diff] [blame] | 1108 | return EC; |
Rafael Espindola | a0d9b6b | 2013-07-17 14:58:25 +0000 | [diff] [blame] | 1109 | |
Zachary Turner | 6edfecb | 2018-06-08 15:15:56 +0000 | [diff] [blame] | 1110 | SECURITY_ATTRIBUTES SA; |
| 1111 | SA.nLength = sizeof(SA); |
| 1112 | SA.lpSecurityDescriptor = nullptr; |
| 1113 | SA.bInheritHandle = Inherit; |
| 1114 | |
Greg Bedwell | 7f68a71 | 2015-10-12 15:11:47 +0000 | [diff] [blame] | 1115 | HANDLE H = |
Zachary Turner | 1f67a3c | 2018-06-07 19:58:58 +0000 | [diff] [blame] | 1116 | ::CreateFileW(PathUTF16.begin(), Access, |
Zachary Turner | 6edfecb | 2018-06-08 15:15:56 +0000 | [diff] [blame] | 1117 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, &SA, |
| 1118 | Disp, Flags, NULL); |
Rafael Espindola | a0d9b6b | 2013-07-17 14:58:25 +0000 | [diff] [blame] | 1119 | if (H == INVALID_HANDLE_VALUE) { |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 1120 | DWORD LastError = ::GetLastError(); |
Yaron Keren | f8e6517 | 2015-05-04 04:48:10 +0000 | [diff] [blame] | 1121 | std::error_code EC = mapWindowsError(LastError); |
Rafael Espindola | 331aeba | 2013-07-17 19:58:28 +0000 | [diff] [blame] | 1122 | // Provide a better error message when trying to open directories. |
Rafael Espindola | a0d9b6b | 2013-07-17 14:58:25 +0000 | [diff] [blame] | 1123 | // This only runs if we failed to open the file, so there is probably |
| 1124 | // no performances issues. |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 1125 | if (LastError != ERROR_ACCESS_DENIED) |
Zachary Turner | 9d2cfa6 | 2018-06-07 23:25:13 +0000 | [diff] [blame] | 1126 | return EC; |
Rafael Espindola | a0d9b6b | 2013-07-17 14:58:25 +0000 | [diff] [blame] | 1127 | if (is_directory(Name)) |
Zachary Turner | 9d2cfa6 | 2018-06-07 23:25:13 +0000 | [diff] [blame] | 1128 | return make_error_code(errc::is_a_directory); |
| 1129 | return EC; |
Rafael Espindola | a0d9b6b | 2013-07-17 14:58:25 +0000 | [diff] [blame] | 1130 | } |
Zachary Turner | 9d2cfa6 | 2018-06-07 23:25:13 +0000 | [diff] [blame] | 1131 | ResultFile = H; |
| 1132 | return std::error_code(); |
Zachary Turner | 1f67a3c | 2018-06-07 19:58:58 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
Zachary Turner | 9d2cfa6 | 2018-06-07 23:25:13 +0000 | [diff] [blame] | 1135 | Expected<file_t> openNativeFile(const Twine &Name, CreationDisposition Disp, |
| 1136 | FileAccess Access, OpenFlags Flags, |
| 1137 | unsigned Mode) { |
Zachary Turner | 1f67a3c | 2018-06-07 19:58:58 +0000 | [diff] [blame] | 1138 | // Verify that we don't have both "append" and "excl". |
| 1139 | assert((!(Disp == CD_CreateNew) || !(Flags & OF_Append)) && |
| 1140 | "Cannot specify both 'CreateNew' and 'Append' file creation flags!"); |
| 1141 | |
Zachary Turner | 1f67a3c | 2018-06-07 19:58:58 +0000 | [diff] [blame] | 1142 | DWORD NativeDisp = nativeDisposition(Disp, Flags); |
| 1143 | DWORD NativeAccess = nativeAccess(Access, Flags); |
| 1144 | |
Zachary Turner | 6edfecb | 2018-06-08 15:15:56 +0000 | [diff] [blame] | 1145 | bool Inherit = false; |
| 1146 | if (Flags & OF_ChildInherit) |
| 1147 | Inherit = true; |
| 1148 | |
Zachary Turner | 9d2cfa6 | 2018-06-07 23:25:13 +0000 | [diff] [blame] | 1149 | file_t Result; |
Zachary Turner | 6edfecb | 2018-06-08 15:15:56 +0000 | [diff] [blame] | 1150 | std::error_code EC = openNativeFileInternal( |
Peter Collingbourne | 881ba10 | 2018-06-13 18:03:14 +0000 | [diff] [blame] | 1151 | Name, Result, NativeDisp, NativeAccess, FILE_ATTRIBUTE_NORMAL, Inherit); |
Zachary Turner | 9d2cfa6 | 2018-06-07 23:25:13 +0000 | [diff] [blame] | 1152 | if (EC) |
| 1153 | return errorCodeToError(EC); |
Andrew Ng | 089303d | 2018-07-04 14:17:10 +0000 | [diff] [blame] | 1154 | |
| 1155 | if (Flags & OF_UpdateAtime) { |
| 1156 | FILETIME FileTime; |
| 1157 | SYSTEMTIME SystemTime; |
| 1158 | GetSystemTime(&SystemTime); |
| 1159 | if (SystemTimeToFileTime(&SystemTime, &FileTime) == 0 || |
| 1160 | SetFileTime(Result, NULL, &FileTime, NULL) == 0) { |
| 1161 | DWORD LastError = ::GetLastError(); |
| 1162 | ::CloseHandle(Result); |
| 1163 | return errorCodeToError(mapWindowsError(LastError)); |
| 1164 | } |
| 1165 | } |
| 1166 | |
Peter Collingbourne | 881ba10 | 2018-06-13 18:03:14 +0000 | [diff] [blame] | 1167 | if (Flags & OF_Delete) { |
| 1168 | if ((EC = setDeleteDisposition(Result, true))) { |
| 1169 | ::CloseHandle(Result); |
| 1170 | return errorCodeToError(EC); |
| 1171 | } |
| 1172 | } |
Zachary Turner | 9d2cfa6 | 2018-06-07 23:25:13 +0000 | [diff] [blame] | 1173 | return Result; |
| 1174 | } |
| 1175 | |
| 1176 | std::error_code openFile(const Twine &Name, int &ResultFD, |
| 1177 | CreationDisposition Disp, FileAccess Access, |
| 1178 | OpenFlags Flags, unsigned int Mode) { |
| 1179 | Expected<file_t> Result = openNativeFile(Name, Disp, Access, Flags); |
| 1180 | if (!Result) |
| 1181 | return errorToErrorCode(Result.takeError()); |
| 1182 | |
| 1183 | return nativeFileToFd(*Result, ResultFD, Flags); |
Zachary Turner | 1f67a3c | 2018-06-07 19:58:58 +0000 | [diff] [blame] | 1184 | } |
| 1185 | |
| 1186 | static std::error_code directoryRealPath(const Twine &Name, |
| 1187 | SmallVectorImpl<char> &RealPath) { |
Zachary Turner | 9d2cfa6 | 2018-06-07 23:25:13 +0000 | [diff] [blame] | 1188 | file_t File; |
| 1189 | std::error_code EC = openNativeFileInternal( |
| 1190 | Name, File, OPEN_EXISTING, GENERIC_READ, FILE_FLAG_BACKUP_SEMANTICS); |
| 1191 | if (EC) |
| 1192 | return EC; |
Zachary Turner | 1f67a3c | 2018-06-07 19:58:58 +0000 | [diff] [blame] | 1193 | |
Zachary Turner | 9d2cfa6 | 2018-06-07 23:25:13 +0000 | [diff] [blame] | 1194 | EC = realPathFromHandle(File, RealPath); |
| 1195 | ::CloseHandle(File); |
Zachary Turner | 1f67a3c | 2018-06-07 19:58:58 +0000 | [diff] [blame] | 1196 | return EC; |
| 1197 | } |
| 1198 | |
| 1199 | std::error_code openFileForRead(const Twine &Name, int &ResultFD, |
| 1200 | OpenFlags Flags, |
| 1201 | SmallVectorImpl<char> *RealPath) { |
| 1202 | Expected<HANDLE> NativeFile = openNativeFileForRead(Name, Flags, RealPath); |
| 1203 | return nativeFileToFd(std::move(NativeFile), ResultFD, OF_None); |
| 1204 | } |
| 1205 | |
| 1206 | Expected<file_t> openNativeFileForRead(const Twine &Name, OpenFlags Flags, |
| 1207 | SmallVectorImpl<char> *RealPath) { |
Zachary Turner | 9d2cfa6 | 2018-06-07 23:25:13 +0000 | [diff] [blame] | 1208 | Expected<file_t> Result = |
| 1209 | openNativeFile(Name, CD_OpenExisting, FA_Read, Flags); |
Rafael Espindola | a0d9b6b | 2013-07-17 14:58:25 +0000 | [diff] [blame] | 1210 | |
Taewook Oh | d915327 | 2016-06-13 15:54:56 +0000 | [diff] [blame] | 1211 | // Fetch the real name of the file, if the user asked |
Zachary Turner | 1f67a3c | 2018-06-07 19:58:58 +0000 | [diff] [blame] | 1212 | if (Result && RealPath) |
| 1213 | realPathFromHandle(*Result, *RealPath); |
Taewook Oh | d915327 | 2016-06-13 15:54:56 +0000 | [diff] [blame] | 1214 | |
Zachary Turner | 15243d5 | 2018-06-10 20:57:14 +0000 | [diff] [blame] | 1215 | return Result; |
Rafael Espindola | a0d9b6b | 2013-07-17 14:58:25 +0000 | [diff] [blame] | 1216 | } |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 1217 | |
Reid Kleckner | cc418a3 | 2019-07-10 00:34:13 +0000 | [diff] [blame] | 1218 | file_t convertFDToNativeFile(int FD) { |
| 1219 | return reinterpret_cast<HANDLE>(::_get_osfhandle(FD)); |
| 1220 | } |
| 1221 | |
| 1222 | file_t getStdinHandle() { return ::GetStdHandle(STD_INPUT_HANDLE); } |
| 1223 | file_t getStdoutHandle() { return ::GetStdHandle(STD_OUTPUT_HANDLE); } |
| 1224 | file_t getStderrHandle() { return ::GetStdHandle(STD_ERROR_HANDLE); } |
| 1225 | |
Pavel Labath | 1b30ea2 | 2019-08-22 08:13:30 +0000 | [diff] [blame] | 1226 | Expected<size_t> readNativeFileImpl(file_t FileHandle, |
| 1227 | MutableArrayRef<char> Buf, |
| 1228 | OVERLAPPED *Overlap) { |
Reid Kleckner | cc418a3 | 2019-07-10 00:34:13 +0000 | [diff] [blame] | 1229 | // ReadFile can only read 2GB at a time. The caller should check the number of |
| 1230 | // bytes and read in a loop until termination. |
Pavel Labath | 1b30ea2 | 2019-08-22 08:13:30 +0000 | [diff] [blame] | 1231 | DWORD BytesToRead = |
| 1232 | std::min(size_t(std::numeric_limits<DWORD>::max()), Buf.size()); |
| 1233 | DWORD BytesRead = 0; |
| 1234 | if (::ReadFile(FileHandle, Buf.data(), BytesToRead, &BytesRead, Overlap)) |
| 1235 | return BytesRead; |
| 1236 | DWORD Err = ::GetLastError(); |
| 1237 | // EOF is not an error. |
| 1238 | if (Err == ERROR_BROKEN_PIPE || Err == ERROR_HANDLE_EOF) |
| 1239 | return BytesRead; |
| 1240 | return errorCodeToError(mapWindowsError(Err)); |
Reid Kleckner | cc418a3 | 2019-07-10 00:34:13 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
Pavel Labath | 1b30ea2 | 2019-08-22 08:13:30 +0000 | [diff] [blame] | 1243 | Expected<size_t> readNativeFile(file_t FileHandle, MutableArrayRef<char> Buf) { |
| 1244 | return readNativeFileImpl(FileHandle, Buf, /*Overlap=*/nullptr); |
Reid Kleckner | cc418a3 | 2019-07-10 00:34:13 +0000 | [diff] [blame] | 1245 | } |
| 1246 | |
Pavel Labath | 1b30ea2 | 2019-08-22 08:13:30 +0000 | [diff] [blame] | 1247 | Expected<size_t> readNativeFileSlice(file_t FileHandle, |
| 1248 | MutableArrayRef<char> Buf, |
| 1249 | uint64_t Offset) { |
| 1250 | OVERLAPPED Overlapped = {}; |
| 1251 | Overlapped.Offset = uint32_t(Offset); |
| 1252 | Overlapped.OffsetHigh = uint32_t(Offset >> 32); |
| 1253 | return readNativeFileImpl(FileHandle, Buf, &Overlapped); |
Reid Kleckner | cc418a3 | 2019-07-10 00:34:13 +0000 | [diff] [blame] | 1254 | } |
| 1255 | |
| 1256 | std::error_code closeFile(file_t &F) { |
| 1257 | file_t TmpF = F; |
Zachary Turner | 63db25b | 2018-06-04 19:38:11 +0000 | [diff] [blame] | 1258 | F = kInvalidFile; |
Reid Kleckner | cc418a3 | 2019-07-10 00:34:13 +0000 | [diff] [blame] | 1259 | if (!::CloseHandle(TmpF)) |
| 1260 | return mapWindowsError(::GetLastError()); |
| 1261 | return std::error_code(); |
Rafael Espindola | 67080ce | 2013-07-19 15:02:03 +0000 | [diff] [blame] | 1262 | } |
Taewook Oh | d915327 | 2016-06-13 15:54:56 +0000 | [diff] [blame] | 1263 | |
Zachary Turner | 260bda3 | 2017-03-08 22:49:32 +0000 | [diff] [blame] | 1264 | std::error_code remove_directories(const Twine &path, bool IgnoreErrors) { |
| 1265 | // Convert to utf-16. |
| 1266 | SmallVector<wchar_t, 128> Path16; |
| 1267 | std::error_code EC = widenPath(path, Path16); |
| 1268 | if (EC && !IgnoreErrors) |
| 1269 | return EC; |
| 1270 | |
| 1271 | // SHFileOperation() accepts a list of paths, and so must be double null- |
| 1272 | // terminated to indicate the end of the list. The buffer is already null |
| 1273 | // terminated, but since that null character is not considered part of the |
| 1274 | // vector's size, pushing another one will just consume that byte. So we |
| 1275 | // need to push 2 null terminators. |
| 1276 | Path16.push_back(0); |
| 1277 | Path16.push_back(0); |
| 1278 | |
| 1279 | SHFILEOPSTRUCTW shfos = {}; |
| 1280 | shfos.wFunc = FO_DELETE; |
| 1281 | shfos.pFrom = Path16.data(); |
| 1282 | shfos.fFlags = FOF_NO_UI; |
| 1283 | |
| 1284 | int result = ::SHFileOperationW(&shfos); |
| 1285 | if (result != 0 && !IgnoreErrors) |
| 1286 | return mapWindowsError(result); |
| 1287 | return std::error_code(); |
| 1288 | } |
| 1289 | |
Zachary Turner | e48ace6 | 2017-03-10 17:39:21 +0000 | [diff] [blame] | 1290 | static void expandTildeExpr(SmallVectorImpl<char> &Path) { |
| 1291 | // Path does not begin with a tilde expression. |
| 1292 | if (Path.empty() || Path[0] != '~') |
| 1293 | return; |
| 1294 | |
| 1295 | StringRef PathStr(Path.begin(), Path.size()); |
| 1296 | PathStr = PathStr.drop_front(); |
Zachary Turner | 5c5091f | 2017-03-16 22:28:04 +0000 | [diff] [blame] | 1297 | StringRef Expr = PathStr.take_until([](char c) { return path::is_separator(c); }); |
Zachary Turner | e48ace6 | 2017-03-10 17:39:21 +0000 | [diff] [blame] | 1298 | |
| 1299 | if (!Expr.empty()) { |
| 1300 | // This is probably a ~username/ expression. Don't support this on Windows. |
| 1301 | return; |
| 1302 | } |
| 1303 | |
| 1304 | SmallString<128> HomeDir; |
| 1305 | if (!path::home_directory(HomeDir)) { |
| 1306 | // For some reason we couldn't get the home directory. Just exit. |
| 1307 | return; |
| 1308 | } |
| 1309 | |
| 1310 | // Overwrite the first character and insert the rest. |
| 1311 | Path[0] = HomeDir[0]; |
| 1312 | Path.insert(Path.begin() + 1, HomeDir.begin() + 1, HomeDir.end()); |
| 1313 | } |
| 1314 | |
Jonas Devlieghere | b23f430 | 2018-11-13 18:23:32 +0000 | [diff] [blame] | 1315 | void expand_tilde(const Twine &path, SmallVectorImpl<char> &dest) { |
| 1316 | dest.clear(); |
| 1317 | if (path.isTriviallyEmpty()) |
| 1318 | return; |
| 1319 | |
| 1320 | path.toVector(dest); |
| 1321 | expandTildeExpr(dest); |
| 1322 | |
| 1323 | return; |
| 1324 | } |
| 1325 | |
Zachary Turner | e48ace6 | 2017-03-10 17:39:21 +0000 | [diff] [blame] | 1326 | std::error_code real_path(const Twine &path, SmallVectorImpl<char> &dest, |
| 1327 | bool expand_tilde) { |
| 1328 | dest.clear(); |
| 1329 | if (path.isTriviallyEmpty()) |
| 1330 | return std::error_code(); |
| 1331 | |
| 1332 | if (expand_tilde) { |
| 1333 | SmallString<128> Storage; |
| 1334 | path.toVector(Storage); |
| 1335 | expandTildeExpr(Storage); |
| 1336 | return real_path(Storage, dest, false); |
| 1337 | } |
| 1338 | |
| 1339 | if (is_directory(path)) |
| 1340 | return directoryRealPath(path, dest); |
| 1341 | |
| 1342 | int fd; |
Zachary Turner | 1f67a3c | 2018-06-07 19:58:58 +0000 | [diff] [blame] | 1343 | if (std::error_code EC = |
| 1344 | llvm::sys::fs::openFileForRead(path, fd, OF_None, &dest)) |
Zachary Turner | e48ace6 | 2017-03-10 17:39:21 +0000 | [diff] [blame] | 1345 | return EC; |
| 1346 | ::close(fd); |
| 1347 | return std::error_code(); |
| 1348 | } |
| 1349 | |
Michael J. Spencer | 9fc1d9d | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 1350 | } // end namespace fs |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 1351 | |
Peter Collingbourne | f7d4101 | 2014-01-31 23:46:06 +0000 | [diff] [blame] | 1352 | namespace path { |
Pawel Bylica | 7c1f36a | 2015-11-02 14:57:24 +0000 | [diff] [blame] | 1353 | static bool getKnownFolderPath(KNOWNFOLDERID folderId, |
| 1354 | SmallVectorImpl<char> &result) { |
Pawel Bylica | 7187e4b | 2015-10-16 09:08:59 +0000 | [diff] [blame] | 1355 | wchar_t *path = nullptr; |
| 1356 | if (::SHGetKnownFolderPath(folderId, KF_FLAG_CREATE, nullptr, &path) != S_OK) |
| 1357 | return false; |
| 1358 | |
| 1359 | bool ok = !UTF16ToUTF8(path, ::wcslen(path), result); |
| 1360 | ::CoTaskMemFree(path); |
| 1361 | return ok; |
| 1362 | } |
Pawel Bylica | 0e97e5c | 2015-11-02 09:49:17 +0000 | [diff] [blame] | 1363 | |
Peter Collingbourne | f7d4101 | 2014-01-31 23:46:06 +0000 | [diff] [blame] | 1364 | bool home_directory(SmallVectorImpl<char> &result) { |
Pawel Bylica | 7187e4b | 2015-10-16 09:08:59 +0000 | [diff] [blame] | 1365 | return getKnownFolderPath(FOLDERID_Profile, result); |
Peter Collingbourne | f7d4101 | 2014-01-31 23:46:06 +0000 | [diff] [blame] | 1366 | } |
| 1367 | |
Pawel Bylica | a90e745 | 2015-11-17 16:54:32 +0000 | [diff] [blame] | 1368 | static bool getTempDirEnvVar(const wchar_t *Var, SmallVectorImpl<char> &Res) { |
Pawel Bylica | 6e680b2 | 2015-11-06 23:44:23 +0000 | [diff] [blame] | 1369 | SmallVector<wchar_t, 1024> Buf; |
| 1370 | size_t Size = 1024; |
| 1371 | do { |
| 1372 | Buf.reserve(Size); |
Pawel Bylica | a90e745 | 2015-11-17 16:54:32 +0000 | [diff] [blame] | 1373 | Size = GetEnvironmentVariableW(Var, Buf.data(), Buf.capacity()); |
Pawel Bylica | 6e680b2 | 2015-11-06 23:44:23 +0000 | [diff] [blame] | 1374 | if (Size == 0) |
| 1375 | return false; |
| 1376 | |
| 1377 | // Try again with larger buffer. |
| 1378 | } while (Size > Buf.capacity()); |
| 1379 | Buf.set_size(Size); |
| 1380 | |
Pawel Bylica | a90e745 | 2015-11-17 16:54:32 +0000 | [diff] [blame] | 1381 | return !windows::UTF16ToUTF8(Buf.data(), Size, Res); |
Pawel Bylica | 6e680b2 | 2015-11-06 23:44:23 +0000 | [diff] [blame] | 1382 | } |
| 1383 | |
| 1384 | static bool getTempDirEnvVar(SmallVectorImpl<char> &Res) { |
Pawel Bylica | a90e745 | 2015-11-17 16:54:32 +0000 | [diff] [blame] | 1385 | const wchar_t *EnvironmentVariables[] = {L"TMP", L"TEMP", L"USERPROFILE"}; |
| 1386 | for (auto *Env : EnvironmentVariables) { |
Pawel Bylica | 6e680b2 | 2015-11-06 23:44:23 +0000 | [diff] [blame] | 1387 | if (getTempDirEnvVar(Env, Res)) |
| 1388 | return true; |
| 1389 | } |
| 1390 | return false; |
| 1391 | } |
| 1392 | |
Rafael Espindola | 016a6d5 | 2014-08-26 14:47:52 +0000 | [diff] [blame] | 1393 | void system_temp_directory(bool ErasedOnReboot, SmallVectorImpl<char> &Result) { |
| 1394 | (void)ErasedOnReboot; |
Pawel Bylica | 6e680b2 | 2015-11-06 23:44:23 +0000 | [diff] [blame] | 1395 | Result.clear(); |
Rafael Espindola | 016a6d5 | 2014-08-26 14:47:52 +0000 | [diff] [blame] | 1396 | |
Pawel Bylica | a90e745 | 2015-11-17 16:54:32 +0000 | [diff] [blame] | 1397 | // Check whether the temporary directory is specified by an environment var. |
| 1398 | // This matches GetTempPath logic to some degree. GetTempPath is not used |
| 1399 | // directly as it cannot handle evn var longer than 130 chars on Windows 7 |
| 1400 | // (fixed on Windows 8). |
| 1401 | if (getTempDirEnvVar(Result)) { |
| 1402 | assert(!Result.empty() && "Unexpected empty path"); |
| 1403 | native(Result); // Some Unix-like shells use Unix path separator in $TMP. |
| 1404 | fs::make_absolute(Result); // Make it absolute if not already. |
Pawel Bylica | 6e680b2 | 2015-11-06 23:44:23 +0000 | [diff] [blame] | 1405 | return; |
Pawel Bylica | a90e745 | 2015-11-17 16:54:32 +0000 | [diff] [blame] | 1406 | } |
Rafael Espindola | 016a6d5 | 2014-08-26 14:47:52 +0000 | [diff] [blame] | 1407 | |
| 1408 | // Fall back to a system default. |
Pawel Bylica | a90e745 | 2015-11-17 16:54:32 +0000 | [diff] [blame] | 1409 | const char *DefaultResult = "C:\\Temp"; |
Rafael Espindola | 016a6d5 | 2014-08-26 14:47:52 +0000 | [diff] [blame] | 1410 | Result.append(DefaultResult, DefaultResult + strlen(DefaultResult)); |
| 1411 | } |
Peter Collingbourne | f7d4101 | 2014-01-31 23:46:06 +0000 | [diff] [blame] | 1412 | } // end namespace path |
| 1413 | |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 1414 | namespace windows { |
Aaron Smith | 8a5ea61 | 2018-04-07 00:32:59 +0000 | [diff] [blame] | 1415 | std::error_code CodePageToUTF16(unsigned codepage, |
| 1416 | llvm::StringRef original, |
| 1417 | llvm::SmallVectorImpl<wchar_t> &utf16) { |
| 1418 | if (!original.empty()) { |
| 1419 | int len = ::MultiByteToWideChar(codepage, MB_ERR_INVALID_CHARS, original.begin(), |
| 1420 | original.size(), utf16.begin(), 0); |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 1421 | |
Aaron Smith | 8a5ea61 | 2018-04-07 00:32:59 +0000 | [diff] [blame] | 1422 | if (len == 0) { |
Yaron Keren | f8e6517 | 2015-05-04 04:48:10 +0000 | [diff] [blame] | 1423 | return mapWindowsError(::GetLastError()); |
Aaron Smith | 8a5ea61 | 2018-04-07 00:32:59 +0000 | [diff] [blame] | 1424 | } |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 1425 | |
Michael J. Spencer | 7a3a510 | 2014-02-20 20:46:23 +0000 | [diff] [blame] | 1426 | utf16.reserve(len + 1); |
| 1427 | utf16.set_size(len); |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 1428 | |
Aaron Smith | 8a5ea61 | 2018-04-07 00:32:59 +0000 | [diff] [blame] | 1429 | len = ::MultiByteToWideChar(codepage, MB_ERR_INVALID_CHARS, original.begin(), |
| 1430 | original.size(), utf16.begin(), utf16.size()); |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 1431 | |
Aaron Smith | 8a5ea61 | 2018-04-07 00:32:59 +0000 | [diff] [blame] | 1432 | if (len == 0) { |
Yaron Keren | f8e6517 | 2015-05-04 04:48:10 +0000 | [diff] [blame] | 1433 | return mapWindowsError(::GetLastError()); |
Aaron Smith | 8a5ea61 | 2018-04-07 00:32:59 +0000 | [diff] [blame] | 1434 | } |
Michael J. Spencer | 7a3a510 | 2014-02-20 20:46:23 +0000 | [diff] [blame] | 1435 | } |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 1436 | |
| 1437 | // Make utf16 null terminated. |
| 1438 | utf16.push_back(0); |
| 1439 | utf16.pop_back(); |
| 1440 | |
Rafael Espindola | 0a5f9cf | 2014-06-12 14:11:22 +0000 | [diff] [blame] | 1441 | return std::error_code(); |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 1442 | } |
| 1443 | |
Aaron Smith | 8a5ea61 | 2018-04-07 00:32:59 +0000 | [diff] [blame] | 1444 | std::error_code UTF8ToUTF16(llvm::StringRef utf8, |
| 1445 | llvm::SmallVectorImpl<wchar_t> &utf16) { |
| 1446 | return CodePageToUTF16(CP_UTF8, utf8, utf16); |
| 1447 | } |
| 1448 | |
| 1449 | std::error_code CurCPToUTF16(llvm::StringRef curcp, |
| 1450 | llvm::SmallVectorImpl<wchar_t> &utf16) { |
| 1451 | return CodePageToUTF16(CP_ACP, curcp, utf16); |
| 1452 | } |
| 1453 | |
Rafael Espindola | 9c35966 | 2014-09-03 20:02:00 +0000 | [diff] [blame] | 1454 | static |
| 1455 | std::error_code UTF16ToCodePage(unsigned codepage, const wchar_t *utf16, |
| 1456 | size_t utf16_len, |
Aaron Smith | 8a5ea61 | 2018-04-07 00:32:59 +0000 | [diff] [blame] | 1457 | llvm::SmallVectorImpl<char> &converted) { |
Michael J. Spencer | 7a3a510 | 2014-02-20 20:46:23 +0000 | [diff] [blame] | 1458 | if (utf16_len) { |
| 1459 | // Get length. |
Aaron Smith | 8a5ea61 | 2018-04-07 00:32:59 +0000 | [diff] [blame] | 1460 | int len = ::WideCharToMultiByte(codepage, 0, utf16, utf16_len, converted.begin(), |
Michael J. Spencer | 7a3a510 | 2014-02-20 20:46:23 +0000 | [diff] [blame] | 1461 | 0, NULL, NULL); |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 1462 | |
Aaron Smith | 8a5ea61 | 2018-04-07 00:32:59 +0000 | [diff] [blame] | 1463 | if (len == 0) { |
Yaron Keren | f8e6517 | 2015-05-04 04:48:10 +0000 | [diff] [blame] | 1464 | return mapWindowsError(::GetLastError()); |
Aaron Smith | 8a5ea61 | 2018-04-07 00:32:59 +0000 | [diff] [blame] | 1465 | } |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 1466 | |
Aaron Smith | 8a5ea61 | 2018-04-07 00:32:59 +0000 | [diff] [blame] | 1467 | converted.reserve(len); |
| 1468 | converted.set_size(len); |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 1469 | |
Michael J. Spencer | 7a3a510 | 2014-02-20 20:46:23 +0000 | [diff] [blame] | 1470 | // Now do the actual conversion. |
Aaron Smith | 8a5ea61 | 2018-04-07 00:32:59 +0000 | [diff] [blame] | 1471 | len = ::WideCharToMultiByte(codepage, 0, utf16, utf16_len, converted.data(), |
| 1472 | converted.size(), NULL, NULL); |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 1473 | |
Aaron Smith | 8a5ea61 | 2018-04-07 00:32:59 +0000 | [diff] [blame] | 1474 | if (len == 0) { |
Yaron Keren | f8e6517 | 2015-05-04 04:48:10 +0000 | [diff] [blame] | 1475 | return mapWindowsError(::GetLastError()); |
Aaron Smith | 8a5ea61 | 2018-04-07 00:32:59 +0000 | [diff] [blame] | 1476 | } |
Michael J. Spencer | 7a3a510 | 2014-02-20 20:46:23 +0000 | [diff] [blame] | 1477 | } |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 1478 | |
Aaron Smith | 8a5ea61 | 2018-04-07 00:32:59 +0000 | [diff] [blame] | 1479 | // Make the new string null terminated. |
| 1480 | converted.push_back(0); |
| 1481 | converted.pop_back(); |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 1482 | |
Rafael Espindola | 0a5f9cf | 2014-06-12 14:11:22 +0000 | [diff] [blame] | 1483 | return std::error_code(); |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 1484 | } |
Rafael Espindola | 9c35966 | 2014-09-03 20:02:00 +0000 | [diff] [blame] | 1485 | |
| 1486 | std::error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len, |
| 1487 | llvm::SmallVectorImpl<char> &utf8) { |
| 1488 | return UTF16ToCodePage(CP_UTF8, utf16, utf16_len, utf8); |
| 1489 | } |
| 1490 | |
| 1491 | std::error_code UTF16ToCurCP(const wchar_t *utf16, size_t utf16_len, |
Aaron Smith | 8a5ea61 | 2018-04-07 00:32:59 +0000 | [diff] [blame] | 1492 | llvm::SmallVectorImpl<char> &curcp) { |
| 1493 | return UTF16ToCodePage(CP_ACP, utf16, utf16_len, curcp); |
Rafael Espindola | 9c35966 | 2014-09-03 20:02:00 +0000 | [diff] [blame] | 1494 | } |
Taewook Oh | d915327 | 2016-06-13 15:54:56 +0000 | [diff] [blame] | 1495 | |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 1496 | } // end namespace windows |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 1497 | } // end namespace sys |
| 1498 | } // end namespace llvm |