Rafael Espindola | f1fc382 | 2013-06-26 19:33:03 +0000 | [diff] [blame] | 1 | //===- llvm/Support/Windows/Path.inc - Windows Path Impl --------*- C++ -*-===// |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Rafael Espindola | f1fc382 | 2013-06-26 19:33:03 +0000 | [diff] [blame] | 10 | // This file implements the Windows specific implementation of the Path API. |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | //=== WARNING: Implementation here must contain only generic Windows code that |
| 16 | //=== is guaranteed to work on *all* Windows variants. |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
Craig Topper | f18edae | 2013-07-15 07:15:05 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/STLExtras.h" |
Rafael Espindola | 5c4f829 | 2014-06-11 19:05:50 +0000 | [diff] [blame] | 20 | #include "llvm/Support/WindowsError.h" |
Michael J. Spencer | c20a032 | 2010-12-03 17:54:07 +0000 | [diff] [blame] | 21 | #include <fcntl.h> |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 22 | #include <io.h> |
Michael J. Spencer | c20a032 | 2010-12-03 17:54:07 +0000 | [diff] [blame] | 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 25 | |
NAKAMURA Takumi | 04d39d7 | 2014-02-12 11:50:22 +0000 | [diff] [blame] | 26 | // These two headers must be included last, and make sure shlobj is required |
| 27 | // after Windows.h to make sure it picks up our definition of _WIN32_WINNT |
Reid Kleckner | d59e2fa | 2014-02-12 21:26:20 +0000 | [diff] [blame] | 28 | #include "WindowsSupport.h" |
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; |
| 48 | using llvm::sys::windows::UTF16ToUTF8; |
Paul Robinson | c38deee | 2014-11-24 18:05:29 +0000 | [diff] [blame] | 49 | using llvm::sys::path::widenPath; |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 50 | |
Rafael Espindola | 37b012d | 2014-02-23 15:16:03 +0000 | [diff] [blame] | 51 | static bool is_separator(const wchar_t value) { |
| 52 | switch (value) { |
| 53 | case L'\\': |
| 54 | case L'/': |
| 55 | return true; |
| 56 | default: |
| 57 | return false; |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 58 | } |
Michael J. Spencer | 9fc1d9d | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Paul Robinson | c38deee | 2014-11-24 18:05:29 +0000 | [diff] [blame] | 61 | namespace llvm { |
| 62 | namespace sys { |
| 63 | namespace path { |
| 64 | |
Paul Robinson | d9c4a9a | 2014-11-13 00:12:14 +0000 | [diff] [blame] | 65 | // Convert a UTF-8 path to UTF-16. Also, if the absolute equivalent of the |
| 66 | // path is longer than CreateDirectory can tolerate, make it absolute and |
| 67 | // prefixed by '\\?\'. |
Paul Robinson | c38deee | 2014-11-24 18:05:29 +0000 | [diff] [blame] | 68 | std::error_code widenPath(const Twine &Path8, |
| 69 | SmallVectorImpl<wchar_t> &Path16) { |
Paul Robinson | d9c4a9a | 2014-11-13 00:12:14 +0000 | [diff] [blame] | 70 | const size_t MaxDirLen = MAX_PATH - 12; // Must leave room for 8.3 filename. |
| 71 | |
| 72 | // Several operations would convert Path8 to SmallString; more efficient to |
| 73 | // do it once up front. |
| 74 | SmallString<128> Path8Str; |
| 75 | Path8.toVector(Path8Str); |
| 76 | |
| 77 | // If we made this path absolute, how much longer would it get? |
| 78 | size_t CurPathLen; |
| 79 | if (llvm::sys::path::is_absolute(Twine(Path8Str))) |
| 80 | CurPathLen = 0; // No contribution from current_path needed. |
| 81 | else { |
| 82 | CurPathLen = ::GetCurrentDirectoryW(0, NULL); |
| 83 | if (CurPathLen == 0) |
Yaron Keren | f8e6517 | 2015-05-04 04:48:10 +0000 | [diff] [blame] | 84 | return mapWindowsError(::GetLastError()); |
Paul Robinson | d9c4a9a | 2014-11-13 00:12:14 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | // Would the absolute path be longer than our limit? |
| 88 | if ((Path8Str.size() + CurPathLen) >= MaxDirLen && |
| 89 | !Path8Str.startswith("\\\\?\\")) { |
| 90 | SmallString<2*MAX_PATH> FullPath("\\\\?\\"); |
| 91 | if (CurPathLen) { |
| 92 | SmallString<80> CurPath; |
| 93 | if (std::error_code EC = llvm::sys::fs::current_path(CurPath)) |
| 94 | return EC; |
| 95 | FullPath.append(CurPath); |
| 96 | } |
Pirama Arumuga Nainar | 3d48bb5 | 2017-08-21 20:49:44 +0000 | [diff] [blame] | 97 | // Traverse the requested path, canonicalizing . and .. (because the \\?\ |
| 98 | // prefix is documented to treat them as real components). Ignore |
| 99 | // separators, which can be returned from the iterator if the path has a |
| 100 | // drive name. We don't need to call native() on the result since append() |
| 101 | // always attaches preferred_separator. |
Paul Robinson | d9c4a9a | 2014-11-13 00:12:14 +0000 | [diff] [blame] | 102 | for (llvm::sys::path::const_iterator I = llvm::sys::path::begin(Path8Str), |
| 103 | E = llvm::sys::path::end(Path8Str); |
| 104 | I != E; ++I) { |
Pirama Arumuga Nainar | 3d48bb5 | 2017-08-21 20:49:44 +0000 | [diff] [blame] | 105 | if (I->size() == 1 && is_separator((*I)[0])) |
| 106 | continue; |
Paul Robinson | d9c4a9a | 2014-11-13 00:12:14 +0000 | [diff] [blame] | 107 | if (I->size() == 1 && *I == ".") |
| 108 | continue; |
| 109 | if (I->size() == 2 && *I == "..") |
| 110 | llvm::sys::path::remove_filename(FullPath); |
| 111 | else |
| 112 | llvm::sys::path::append(FullPath, *I); |
| 113 | } |
| 114 | return UTF8ToUTF16(FullPath, Path16); |
| 115 | } |
| 116 | |
| 117 | // Just use the caller's original path. |
| 118 | return UTF8ToUTF16(Path8Str, Path16); |
| 119 | } |
Paul Robinson | c38deee | 2014-11-24 18:05:29 +0000 | [diff] [blame] | 120 | } // end namespace path |
Paul Robinson | d9c4a9a | 2014-11-13 00:12:14 +0000 | [diff] [blame] | 121 | |
Michael J. Spencer | 20daa28 | 2010-12-07 01:22:31 +0000 | [diff] [blame] | 122 | namespace fs { |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 123 | |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 124 | std::string getMainExecutable(const char *argv0, void *MainExecAddr) { |
David Majnemer | 17a4496 | 2013-10-07 09:52:36 +0000 | [diff] [blame] | 125 | SmallVector<wchar_t, MAX_PATH> PathName; |
| 126 | DWORD Size = ::GetModuleFileNameW(NULL, PathName.data(), PathName.capacity()); |
| 127 | |
| 128 | // A zero return value indicates a failure other than insufficient space. |
| 129 | if (Size == 0) |
| 130 | return ""; |
| 131 | |
| 132 | // Insufficient space is determined by a return value equal to the size of |
| 133 | // the buffer passed in. |
| 134 | if (Size == PathName.capacity()) |
| 135 | return ""; |
| 136 | |
| 137 | // On success, GetModuleFileNameW returns the number of characters written to |
| 138 | // the buffer not including the NULL terminator. |
| 139 | PathName.set_size(Size); |
| 140 | |
| 141 | // Convert the result from UTF-16 to UTF-8. |
| 142 | SmallVector<char, MAX_PATH> PathNameUTF8; |
| 143 | if (UTF16ToUTF8(PathName.data(), PathName.size(), PathNameUTF8)) |
| 144 | return ""; |
| 145 | |
| 146 | return std::string(PathNameUTF8.data()); |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Rafael Espindola | d123099 | 2013-07-29 21:55:38 +0000 | [diff] [blame] | 149 | UniqueID file_status::getUniqueID() const { |
Rafael Espindola | 7f822a9 | 2013-07-29 21:26:49 +0000 | [diff] [blame] | 150 | // The file is uniquely identified by the volume serial number along |
| 151 | // with the 64-bit file identifier. |
| 152 | uint64_t FileID = (static_cast<uint64_t>(FileIndexHigh) << 32ULL) | |
| 153 | static_cast<uint64_t>(FileIndexLow); |
| 154 | |
| 155 | return UniqueID(VolumeSerialNumber, FileID); |
| 156 | } |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 157 | |
Mehdi Amini | e2d8f1b | 2016-04-01 00:18:08 +0000 | [diff] [blame] | 158 | ErrorOr<space_info> disk_space(const Twine &Path) { |
| 159 | ULARGE_INTEGER Avail, Total, Free; |
| 160 | if (!::GetDiskFreeSpaceExA(Path.str().c_str(), &Avail, &Total, &Free)) |
| 161 | return mapWindowsError(::GetLastError()); |
| 162 | space_info SpaceInfo; |
| 163 | SpaceInfo.capacity = |
| 164 | (static_cast<uint64_t>(Total.HighPart) << 32) + Total.LowPart; |
Mehdi Amini | 6471915 | 2016-04-01 00:52:05 +0000 | [diff] [blame] | 165 | SpaceInfo.free = (static_cast<uint64_t>(Free.HighPart) << 32) + Free.LowPart; |
Mehdi Amini | e2d8f1b | 2016-04-01 00:18:08 +0000 | [diff] [blame] | 166 | SpaceInfo.available = |
| 167 | (static_cast<uint64_t>(Avail.HighPart) << 32) + Avail.LowPart; |
| 168 | return SpaceInfo; |
| 169 | } |
| 170 | |
Pavel Labath | 757ca88 | 2016-10-24 10:59:17 +0000 | [diff] [blame] | 171 | TimePoint<> file_status::getLastAccessedTime() const { |
| 172 | FILETIME Time; |
| 173 | Time.dwLowDateTime = LastAccessedTimeLow; |
| 174 | Time.dwHighDateTime = LastAccessedTimeHigh; |
| 175 | return toTimePoint(Time); |
Mehdi Amini | 1e39ef3 | 2016-03-25 07:30:21 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Pavel Labath | 757ca88 | 2016-10-24 10:59:17 +0000 | [diff] [blame] | 178 | TimePoint<> file_status::getLastModificationTime() const { |
| 179 | FILETIME Time; |
| 180 | Time.dwLowDateTime = LastWriteTimeLow; |
| 181 | Time.dwHighDateTime = LastWriteTimeHigh; |
| 182 | return toTimePoint(Time); |
Rafael Espindola | db5d8fe | 2013-06-20 18:42:04 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Zachary Turner | 5821a3b | 2017-03-20 23:55:20 +0000 | [diff] [blame] | 185 | uint32_t file_status::getLinkCount() const { |
| 186 | return NumLinks; |
| 187 | } |
| 188 | |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 189 | std::error_code current_path(SmallVectorImpl<char> &result) { |
David Majnemer | 61eae2e | 2013-10-07 01:00:07 +0000 | [diff] [blame] | 190 | SmallVector<wchar_t, MAX_PATH> cur_path; |
| 191 | DWORD len = MAX_PATH; |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 192 | |
David Majnemer | 61eae2e | 2013-10-07 01:00:07 +0000 | [diff] [blame] | 193 | do { |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 194 | cur_path.reserve(len); |
David Majnemer | 61eae2e | 2013-10-07 01:00:07 +0000 | [diff] [blame] | 195 | len = ::GetCurrentDirectoryW(cur_path.capacity(), cur_path.data()); |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 196 | |
David Majnemer | 61eae2e | 2013-10-07 01:00:07 +0000 | [diff] [blame] | 197 | // A zero return value indicates a failure other than insufficient space. |
| 198 | if (len == 0) |
Yaron Keren | f8e6517 | 2015-05-04 04:48:10 +0000 | [diff] [blame] | 199 | return mapWindowsError(::GetLastError()); |
David Majnemer | 61eae2e | 2013-10-07 01:00:07 +0000 | [diff] [blame] | 200 | |
| 201 | // If there's insufficient space, the len returned is larger than the len |
| 202 | // given. |
| 203 | } while (len > cur_path.capacity()); |
| 204 | |
| 205 | // On success, GetCurrentDirectoryW returns the number of characters not |
| 206 | // including the null-terminator. |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 207 | cur_path.set_size(len); |
Aaron Ballman | b16cf53 | 2013-08-16 17:53:28 +0000 | [diff] [blame] | 208 | return UTF16ToUTF8(cur_path.begin(), cur_path.size(), result); |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Pavel Labath | 2f09609 | 2017-01-24 10:32:03 +0000 | [diff] [blame] | 211 | std::error_code set_current_path(const Twine &path) { |
| 212 | // Convert to utf-16. |
| 213 | SmallVector<wchar_t, 128> wide_path; |
| 214 | if (std::error_code ec = widenPath(path, wide_path)) |
| 215 | return ec; |
| 216 | |
| 217 | if (!::SetCurrentDirectoryW(wide_path.begin())) |
| 218 | return mapWindowsError(::GetLastError()); |
| 219 | |
| 220 | return std::error_code(); |
| 221 | } |
| 222 | |
Frederic Riss | 6b9396c | 2015-08-06 21:04:55 +0000 | [diff] [blame] | 223 | std::error_code create_directory(const Twine &path, bool IgnoreExisting, |
| 224 | perms Perms) { |
Michael J. Spencer | 31e310c | 2010-12-03 05:42:11 +0000 | [diff] [blame] | 225 | SmallVector<wchar_t, 128> path_utf16; |
| 226 | |
Paul Robinson | d9c4a9a | 2014-11-13 00:12:14 +0000 | [diff] [blame] | 227 | if (std::error_code ec = widenPath(path, path_utf16)) |
Michael J. Spencer | 31e310c | 2010-12-03 05:42:11 +0000 | [diff] [blame] | 228 | return ec; |
| 229 | |
| 230 | if (!::CreateDirectoryW(path_utf16.begin(), NULL)) { |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 231 | DWORD LastError = ::GetLastError(); |
| 232 | if (LastError != ERROR_ALREADY_EXISTS || !IgnoreExisting) |
Yaron Keren | f8e6517 | 2015-05-04 04:48:10 +0000 | [diff] [blame] | 233 | return mapWindowsError(LastError); |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 234 | } |
Michael J. Spencer | 31e310c | 2010-12-03 05:42:11 +0000 | [diff] [blame] | 235 | |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 236 | return std::error_code(); |
Michael J. Spencer | 31e310c | 2010-12-03 05:42:11 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Rafael Espindola | 83f858e | 2014-03-11 18:40:24 +0000 | [diff] [blame] | 239 | // We can't use symbolic links for windows. |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 240 | std::error_code create_link(const Twine &to, const Twine &from) { |
Michael J. Spencer | e0c4560 | 2010-12-03 05:58:41 +0000 | [diff] [blame] | 241 | // Convert to utf-16. |
| 242 | SmallVector<wchar_t, 128> wide_from; |
| 243 | SmallVector<wchar_t, 128> wide_to; |
Paul Robinson | d9c4a9a | 2014-11-13 00:12:14 +0000 | [diff] [blame] | 244 | if (std::error_code ec = widenPath(from, wide_from)) |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 245 | return ec; |
Paul Robinson | d9c4a9a | 2014-11-13 00:12:14 +0000 | [diff] [blame] | 246 | if (std::error_code ec = widenPath(to, wide_to)) |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 247 | return ec; |
Michael J. Spencer | e0c4560 | 2010-12-03 05:58:41 +0000 | [diff] [blame] | 248 | |
| 249 | if (!::CreateHardLinkW(wide_from.begin(), wide_to.begin(), NULL)) |
Yaron Keren | f8e6517 | 2015-05-04 04:48:10 +0000 | [diff] [blame] | 250 | return mapWindowsError(::GetLastError()); |
Michael J. Spencer | e0c4560 | 2010-12-03 05:58:41 +0000 | [diff] [blame] | 251 | |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 252 | return std::error_code(); |
Michael J. Spencer | e0c4560 | 2010-12-03 05:58:41 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Mehdi Amini | 8e13bc4 | 2016-12-14 04:56:42 +0000 | [diff] [blame] | 255 | std::error_code create_hard_link(const Twine &to, const Twine &from) { |
NAKAMURA Takumi | a1e97a7 | 2017-08-28 06:47:47 +0000 | [diff] [blame] | 256 | return create_link(to, from); |
Mehdi Amini | 8e13bc4 | 2016-12-14 04:56:42 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 259 | std::error_code remove(const Twine &path, bool IgnoreNonExisting) { |
Michael J. Spencer | 6e74e11 | 2010-12-03 17:53:43 +0000 | [diff] [blame] | 260 | SmallVector<wchar_t, 128> path_utf16; |
| 261 | |
Paul Robinson | d9c4a9a | 2014-11-13 00:12:14 +0000 | [diff] [blame] | 262 | if (std::error_code ec = widenPath(path, path_utf16)) |
Michael J. Spencer | 6e74e11 | 2010-12-03 17:53:43 +0000 | [diff] [blame] | 263 | return ec; |
| 264 | |
Peter Collingbourne | 0f9e889 | 2017-10-10 19:39:46 +0000 | [diff] [blame^] | 265 | // We don't know whether this is a file or a directory, and remove() can |
| 266 | // accept both. The usual way to delete a file or directory is to use one of |
| 267 | // the DeleteFile or RemoveDirectory functions, but that requires you to know |
| 268 | // which one it is. We could stat() the file to determine that, but that would |
| 269 | // cost us additional system calls, which can be slow in a directory |
| 270 | // containing a large number of files. So instead we call CreateFile directly. |
| 271 | // The important part is the FILE_FLAG_DELETE_ON_CLOSE flag, which causes the |
| 272 | // file to be deleted once it is closed. We also use the flags |
| 273 | // FILE_FLAG_BACKUP_SEMANTICS (which allows us to open directories), and |
| 274 | // FILE_FLAG_OPEN_REPARSE_POINT (don't follow symlinks). |
| 275 | ScopedFileHandle h(::CreateFileW( |
| 276 | c_str(path_utf16), DELETE, |
| 277 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, |
| 278 | OPEN_EXISTING, |
| 279 | FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS | |
| 280 | FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_DELETE_ON_CLOSE, |
| 281 | NULL)); |
| 282 | if (!h) { |
Yaron Keren | f8e6517 | 2015-05-04 04:48:10 +0000 | [diff] [blame] | 283 | std::error_code EC = mapWindowsError(::GetLastError()); |
Rafael Espindola | 2a826e4 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 284 | if (EC != errc::no_such_file_or_directory || !IgnoreNonExisting) |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 285 | return EC; |
| 286 | } |
Peter Collingbourne | 0f9e889 | 2017-10-10 19:39:46 +0000 | [diff] [blame^] | 287 | |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 288 | return std::error_code(); |
Michael J. Spencer | 6e74e11 | 2010-12-03 17:53:43 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Zachary Turner | 392ed9d | 2017-02-21 20:55:47 +0000 | [diff] [blame] | 291 | static std::error_code is_local_internal(SmallVectorImpl<wchar_t> &Path, |
| 292 | bool &Result) { |
| 293 | SmallVector<wchar_t, 128> VolumePath; |
| 294 | size_t Len = 128; |
| 295 | while (true) { |
| 296 | VolumePath.resize(Len); |
| 297 | BOOL Success = |
| 298 | ::GetVolumePathNameW(Path.data(), VolumePath.data(), VolumePath.size()); |
| 299 | |
| 300 | if (Success) |
| 301 | break; |
| 302 | |
| 303 | DWORD Err = ::GetLastError(); |
| 304 | if (Err != ERROR_INSUFFICIENT_BUFFER) |
| 305 | return mapWindowsError(Err); |
| 306 | |
| 307 | Len *= 2; |
| 308 | } |
| 309 | // If the output buffer has exactly enough space for the path name, but not |
| 310 | // the null terminator, it will leave the output unterminated. Push a null |
| 311 | // terminator onto the end to ensure that this never happens. |
| 312 | VolumePath.push_back(L'\0'); |
| 313 | VolumePath.set_size(wcslen(VolumePath.data())); |
| 314 | const wchar_t *P = VolumePath.data(); |
| 315 | |
| 316 | UINT Type = ::GetDriveTypeW(P); |
| 317 | switch (Type) { |
| 318 | case DRIVE_FIXED: |
| 319 | Result = true; |
| 320 | return std::error_code(); |
| 321 | case DRIVE_REMOTE: |
| 322 | case DRIVE_CDROM: |
| 323 | case DRIVE_RAMDISK: |
| 324 | case DRIVE_REMOVABLE: |
| 325 | Result = false; |
| 326 | return std::error_code(); |
| 327 | default: |
| 328 | return make_error_code(errc::no_such_file_or_directory); |
| 329 | } |
| 330 | llvm_unreachable("Unreachable!"); |
| 331 | } |
| 332 | |
| 333 | std::error_code is_local(const Twine &path, bool &result) { |
| 334 | if (!llvm::sys::fs::exists(path) || !llvm::sys::path::has_root_path(path)) |
| 335 | return make_error_code(errc::no_such_file_or_directory); |
| 336 | |
| 337 | SmallString<128> Storage; |
| 338 | StringRef P = path.toStringRef(Storage); |
| 339 | |
| 340 | // Convert to utf-16. |
| 341 | SmallVector<wchar_t, 128> WidePath; |
| 342 | if (std::error_code ec = widenPath(P, WidePath)) |
| 343 | return ec; |
| 344 | return is_local_internal(WidePath, result); |
| 345 | } |
| 346 | |
| 347 | std::error_code is_local(int FD, bool &Result) { |
| 348 | SmallVector<wchar_t, 128> FinalPath; |
| 349 | HANDLE Handle = reinterpret_cast<HANDLE>(_get_osfhandle(FD)); |
| 350 | |
| 351 | size_t Len = 128; |
| 352 | do { |
| 353 | FinalPath.reserve(Len); |
| 354 | Len = ::GetFinalPathNameByHandleW(Handle, FinalPath.data(), |
| 355 | FinalPath.capacity() - 1, VOLUME_NAME_NT); |
| 356 | if (Len == 0) |
| 357 | return mapWindowsError(::GetLastError()); |
| 358 | } while (Len > FinalPath.capacity()); |
| 359 | |
| 360 | FinalPath.set_size(Len); |
| 361 | |
| 362 | return is_local_internal(FinalPath, Result); |
| 363 | } |
| 364 | |
Peter Collingbourne | 80e31f1 | 2017-10-06 17:14:36 +0000 | [diff] [blame] | 365 | static std::error_code rename_internal(HANDLE FromHandle, const Twine &To, |
| 366 | bool ReplaceIfExists) { |
| 367 | SmallVector<wchar_t, 0> ToWide; |
| 368 | if (auto EC = widenPath(To, ToWide)) |
| 369 | return EC; |
| 370 | |
| 371 | std::vector<char> RenameInfoBuf(sizeof(FILE_RENAME_INFO) - sizeof(wchar_t) + |
| 372 | (ToWide.size() * sizeof(wchar_t))); |
| 373 | FILE_RENAME_INFO &RenameInfo = |
| 374 | *reinterpret_cast<FILE_RENAME_INFO *>(RenameInfoBuf.data()); |
| 375 | RenameInfo.ReplaceIfExists = ReplaceIfExists; |
| 376 | RenameInfo.RootDirectory = 0; |
| 377 | RenameInfo.FileNameLength = ToWide.size(); |
Adrian McCarthy | e6275c6 | 2017-10-09 17:50:01 +0000 | [diff] [blame] | 378 | std::copy(ToWide.begin(), ToWide.end(), &RenameInfo.FileName[0]); |
Peter Collingbourne | 80e31f1 | 2017-10-06 17:14:36 +0000 | [diff] [blame] | 379 | |
| 380 | if (!SetFileInformationByHandle(FromHandle, FileRenameInfo, &RenameInfo, |
| 381 | RenameInfoBuf.size())) |
| 382 | return mapWindowsError(GetLastError()); |
| 383 | |
| 384 | return std::error_code(); |
| 385 | } |
| 386 | |
| 387 | std::error_code rename(const Twine &From, const Twine &To) { |
Michael J. Spencer | 409f556 | 2010-12-03 17:53:55 +0000 | [diff] [blame] | 388 | // Convert to utf-16. |
Peter Collingbourne | 80e31f1 | 2017-10-06 17:14:36 +0000 | [diff] [blame] | 389 | SmallVector<wchar_t, 128> WideFrom; |
| 390 | SmallVector<wchar_t, 128> WideTo; |
| 391 | if (std::error_code EC = widenPath(From, WideFrom)) |
| 392 | return EC; |
| 393 | if (std::error_code EC = widenPath(To, WideTo)) |
| 394 | return EC; |
Michael J. Spencer | 409f556 | 2010-12-03 17:53:55 +0000 | [diff] [blame] | 395 | |
Peter Collingbourne | 80e31f1 | 2017-10-06 17:14:36 +0000 | [diff] [blame] | 396 | ScopedFileHandle FromHandle; |
| 397 | // Retry this a few times to defeat badly behaved file system scanners. |
| 398 | for (unsigned Retry = 0; Retry != 200; ++Retry) { |
| 399 | if (Retry != 0) |
| 400 | ::Sleep(10); |
| 401 | FromHandle = |
| 402 | ::CreateFileW(WideFrom.begin(), GENERIC_READ | DELETE, |
| 403 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, |
| 404 | NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
| 405 | if (FromHandle) |
| 406 | break; |
| 407 | } |
| 408 | if (!FromHandle) |
| 409 | return mapWindowsError(GetLastError()); |
Greg Bedwell | 7f68a71 | 2015-10-12 15:11:47 +0000 | [diff] [blame] | 410 | |
Peter Collingbourne | 80e31f1 | 2017-10-06 17:14:36 +0000 | [diff] [blame] | 411 | // We normally expect this loop to succeed after a few iterations. If it |
| 412 | // requires more than 200 tries, it's more likely that the failures are due to |
| 413 | // a true error, so stop trying. |
| 414 | for (unsigned Retry = 0; Retry != 200; ++Retry) { |
| 415 | auto EC = rename_internal(FromHandle, To, true); |
| 416 | if (!EC || EC != errc::permission_denied) |
| 417 | return EC; |
Greg Bedwell | 7f68a71 | 2015-10-12 15:11:47 +0000 | [diff] [blame] | 418 | |
Peter Collingbourne | 80e31f1 | 2017-10-06 17:14:36 +0000 | [diff] [blame] | 419 | // The destination file probably exists and is currently open in another |
| 420 | // process, either because the file was opened without FILE_SHARE_DELETE or |
| 421 | // it is mapped into memory (e.g. using MemoryBuffer). Rename it in order to |
| 422 | // move it out of the way of the source file. Use FILE_FLAG_DELETE_ON_CLOSE |
| 423 | // to arrange for the destination file to be deleted when the other process |
| 424 | // closes it. |
| 425 | ScopedFileHandle ToHandle( |
| 426 | ::CreateFileW(WideTo.begin(), GENERIC_READ | DELETE, |
| 427 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, |
| 428 | NULL, OPEN_EXISTING, |
| 429 | FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE, NULL)); |
| 430 | if (!ToHandle) { |
| 431 | auto EC = mapWindowsError(GetLastError()); |
| 432 | // Another process might have raced with us and moved the existing file |
| 433 | // out of the way before we had a chance to open it. If that happens, try |
| 434 | // to rename the source file again. |
| 435 | if (EC == errc::no_such_file_or_directory) |
Sunil Srivastava | 34fce93 | 2016-03-25 23:41:28 +0000 | [diff] [blame] | 436 | continue; |
Peter Collingbourne | 80e31f1 | 2017-10-06 17:14:36 +0000 | [diff] [blame] | 437 | return EC; |
Sunil Srivastava | 34fce93 | 2016-03-25 23:41:28 +0000 | [diff] [blame] | 438 | } |
Greg Bedwell | 7f68a71 | 2015-10-12 15:11:47 +0000 | [diff] [blame] | 439 | |
Peter Collingbourne | 80e31f1 | 2017-10-06 17:14:36 +0000 | [diff] [blame] | 440 | BY_HANDLE_FILE_INFORMATION FI; |
| 441 | if (!GetFileInformationByHandle(ToHandle, &FI)) |
| 442 | return mapWindowsError(GetLastError()); |
Greg Bedwell | 7f68a71 | 2015-10-12 15:11:47 +0000 | [diff] [blame] | 443 | |
Peter Collingbourne | 80e31f1 | 2017-10-06 17:14:36 +0000 | [diff] [blame] | 444 | // Try to find a unique new name for the destination file. |
| 445 | for (unsigned UniqueId = 0; UniqueId != 200; ++UniqueId) { |
| 446 | std::string TmpFilename = (To + ".tmp" + utostr(UniqueId)).str(); |
| 447 | if (auto EC = rename_internal(ToHandle, TmpFilename, false)) { |
| 448 | if (EC == errc::file_exists || EC == errc::permission_denied) { |
| 449 | // Again, another process might have raced with us and moved the file |
| 450 | // before we could move it. Check whether this is the case, as it |
| 451 | // might have caused the permission denied error. If that was the |
| 452 | // case, we don't need to move it ourselves. |
| 453 | ScopedFileHandle ToHandle2(::CreateFileW( |
| 454 | WideTo.begin(), 0, |
| 455 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, |
| 456 | OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)); |
| 457 | if (!ToHandle2) { |
| 458 | auto EC = mapWindowsError(GetLastError()); |
| 459 | if (EC == errc::no_such_file_or_directory) |
| 460 | break; |
| 461 | return EC; |
| 462 | } |
| 463 | BY_HANDLE_FILE_INFORMATION FI2; |
| 464 | if (!GetFileInformationByHandle(ToHandle2, &FI2)) |
| 465 | return mapWindowsError(GetLastError()); |
| 466 | if (FI.nFileIndexHigh != FI2.nFileIndexHigh || |
| 467 | FI.nFileIndexLow != FI2.nFileIndexLow || |
| 468 | FI.dwVolumeSerialNumber != FI2.dwVolumeSerialNumber) |
| 469 | break; |
| 470 | continue; |
| 471 | } |
| 472 | return EC; |
| 473 | } |
| 474 | break; |
| 475 | } |
| 476 | |
| 477 | // Okay, the old destination file has probably been moved out of the way at |
| 478 | // this point, so try to rename the source file again. Still, another |
| 479 | // process might have raced with us to create and open the destination |
| 480 | // file, so we need to keep doing this until we succeed. |
NAKAMURA Takumi | 3b7f995 | 2012-05-08 14:31:46 +0000 | [diff] [blame] | 481 | } |
Michael J. Spencer | 409f556 | 2010-12-03 17:53:55 +0000 | [diff] [blame] | 482 | |
Peter Collingbourne | 80e31f1 | 2017-10-06 17:14:36 +0000 | [diff] [blame] | 483 | // The most likely root cause. |
| 484 | return errc::permission_denied; |
Michael J. Spencer | 409f556 | 2010-12-03 17:53:55 +0000 | [diff] [blame] | 485 | } |
| 486 | |
Rafael Espindola | 59aaa6c | 2014-12-12 17:55:12 +0000 | [diff] [blame] | 487 | std::error_code resize_file(int FD, uint64_t Size) { |
Michael J. Spencer | ca242f2 | 2010-12-03 18:48:56 +0000 | [diff] [blame] | 488 | #ifdef HAVE__CHSIZE_S |
Rafael Espindola | 59aaa6c | 2014-12-12 17:55:12 +0000 | [diff] [blame] | 489 | errno_t error = ::_chsize_s(FD, Size); |
Michael J. Spencer | ca242f2 | 2010-12-03 18:48:56 +0000 | [diff] [blame] | 490 | #else |
Rafael Espindola | 59aaa6c | 2014-12-12 17:55:12 +0000 | [diff] [blame] | 491 | errno_t error = ::_chsize(FD, Size); |
Michael J. Spencer | ca242f2 | 2010-12-03 18:48:56 +0000 | [diff] [blame] | 492 | #endif |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 493 | return std::error_code(error, std::generic_category()); |
Michael J. Spencer | c20a032 | 2010-12-03 17:54:07 +0000 | [diff] [blame] | 494 | } |
| 495 | |
Rafael Espindola | 281f23a | 2014-09-11 20:30:02 +0000 | [diff] [blame] | 496 | std::error_code access(const Twine &Path, AccessMode Mode) { |
Rafael Espindola | 281f23a | 2014-09-11 20:30:02 +0000 | [diff] [blame] | 497 | SmallVector<wchar_t, 128> PathUtf16; |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 498 | |
Paul Robinson | d9c4a9a | 2014-11-13 00:12:14 +0000 | [diff] [blame] | 499 | if (std::error_code EC = widenPath(Path, PathUtf16)) |
Rafael Espindola | 281f23a | 2014-09-11 20:30:02 +0000 | [diff] [blame] | 500 | return EC; |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 501 | |
Rafael Espindola | 281f23a | 2014-09-11 20:30:02 +0000 | [diff] [blame] | 502 | DWORD Attributes = ::GetFileAttributesW(PathUtf16.begin()); |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 503 | |
Rafael Espindola | 281f23a | 2014-09-11 20:30:02 +0000 | [diff] [blame] | 504 | if (Attributes == INVALID_FILE_ATTRIBUTES) { |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 505 | // See if the file didn't actually exist. |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 506 | DWORD LastError = ::GetLastError(); |
| 507 | if (LastError != ERROR_FILE_NOT_FOUND && |
| 508 | LastError != ERROR_PATH_NOT_FOUND) |
Yaron Keren | f8e6517 | 2015-05-04 04:48:10 +0000 | [diff] [blame] | 509 | return mapWindowsError(LastError); |
Rafael Espindola | 281f23a | 2014-09-11 20:30:02 +0000 | [diff] [blame] | 510 | return errc::no_such_file_or_directory; |
| 511 | } |
| 512 | |
| 513 | if (Mode == AccessMode::Write && (Attributes & FILE_ATTRIBUTE_READONLY)) |
| 514 | return errc::permission_denied; |
| 515 | |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 516 | return std::error_code(); |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Reid Kleckner | 89d4b1a | 2015-09-10 23:28:06 +0000 | [diff] [blame] | 519 | bool can_execute(const Twine &Path) { |
| 520 | return !access(Path, AccessMode::Execute) || |
| 521 | !access(Path + ".exe", AccessMode::Execute); |
| 522 | } |
| 523 | |
Michael J. Spencer | 203d780 | 2011-12-12 06:04:28 +0000 | [diff] [blame] | 524 | bool equivalent(file_status A, file_status B) { |
| 525 | assert(status_known(A) && status_known(B)); |
Mehdi Amini | 1e39ef3 | 2016-03-25 07:30:21 +0000 | [diff] [blame] | 526 | return A.FileIndexHigh == B.FileIndexHigh && |
| 527 | A.FileIndexLow == B.FileIndexLow && |
| 528 | A.FileSizeHigh == B.FileSizeHigh && |
| 529 | A.FileSizeLow == B.FileSizeLow && |
| 530 | A.LastAccessedTimeHigh == B.LastAccessedTimeHigh && |
| 531 | A.LastAccessedTimeLow == B.LastAccessedTimeLow && |
| 532 | A.LastWriteTimeHigh == B.LastWriteTimeHigh && |
| 533 | A.LastWriteTimeLow == B.LastWriteTimeLow && |
| 534 | A.VolumeSerialNumber == B.VolumeSerialNumber; |
Michael J. Spencer | 203d780 | 2011-12-12 06:04:28 +0000 | [diff] [blame] | 535 | } |
| 536 | |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 537 | 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] | 538 | file_status fsA, fsB; |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 539 | if (std::error_code ec = status(A, fsA)) |
| 540 | return ec; |
| 541 | if (std::error_code ec = status(B, fsB)) |
| 542 | return ec; |
Michael J. Spencer | 203d780 | 2011-12-12 06:04:28 +0000 | [diff] [blame] | 543 | result = equivalent(fsA, fsB); |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 544 | return std::error_code(); |
Michael J. Spencer | 376d387 | 2010-12-03 18:49:13 +0000 | [diff] [blame] | 545 | } |
| 546 | |
Benjamin Kramer | 58994ec | 2011-08-20 21:36:38 +0000 | [diff] [blame] | 547 | static bool isReservedName(StringRef path) { |
| 548 | // This list of reserved names comes from MSDN, at: |
| 549 | // 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] | 550 | static const char *const sReservedNames[] = { "nul", "con", "prn", "aux", |
| 551 | "com1", "com2", "com3", "com4", |
| 552 | "com5", "com6", "com7", "com8", |
| 553 | "com9", "lpt1", "lpt2", "lpt3", |
| 554 | "lpt4", "lpt5", "lpt6", "lpt7", |
| 555 | "lpt8", "lpt9" }; |
Benjamin Kramer | 58994ec | 2011-08-20 21:36:38 +0000 | [diff] [blame] | 556 | |
| 557 | // First, check to see if this is a device namespace, which always |
| 558 | // starts with \\.\, since device namespaces are not legal file paths. |
| 559 | if (path.startswith("\\\\.\\")) |
| 560 | return true; |
| 561 | |
Douglas Yung | 091d8fd | 2016-05-03 00:12:59 +0000 | [diff] [blame] | 562 | // Then compare against the list of ancient reserved names. |
Craig Topper | 5871321 | 2013-07-15 04:27:47 +0000 | [diff] [blame] | 563 | for (size_t i = 0; i < array_lengthof(sReservedNames); ++i) { |
Benjamin Kramer | 58994ec | 2011-08-20 21:36:38 +0000 | [diff] [blame] | 564 | if (path.equals_lower(sReservedNames[i])) |
| 565 | return true; |
| 566 | } |
| 567 | |
| 568 | // The path isn't what we consider reserved. |
| 569 | return false; |
| 570 | } |
| 571 | |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 572 | static std::error_code getStatus(HANDLE FileHandle, file_status &Result) { |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 573 | if (FileHandle == INVALID_HANDLE_VALUE) |
| 574 | goto handle_status_error; |
| 575 | |
NAKAMURA Takumi | 8b01da4 | 2013-07-18 17:00:54 +0000 | [diff] [blame] | 576 | switch (::GetFileType(FileHandle)) { |
| 577 | default: |
Rafael Espindola | 81177c5 | 2013-07-18 18:42:52 +0000 | [diff] [blame] | 578 | llvm_unreachable("Don't know anything about this file type"); |
| 579 | case FILE_TYPE_UNKNOWN: { |
| 580 | DWORD Err = ::GetLastError(); |
| 581 | if (Err != NO_ERROR) |
Yaron Keren | f8e6517 | 2015-05-04 04:48:10 +0000 | [diff] [blame] | 582 | return mapWindowsError(Err); |
Rafael Espindola | 81177c5 | 2013-07-18 18:42:52 +0000 | [diff] [blame] | 583 | Result = file_status(file_type::type_unknown); |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 584 | return std::error_code(); |
Rafael Espindola | 81177c5 | 2013-07-18 18:42:52 +0000 | [diff] [blame] | 585 | } |
NAKAMURA Takumi | 8b01da4 | 2013-07-18 17:00:54 +0000 | [diff] [blame] | 586 | case FILE_TYPE_DISK: |
| 587 | break; |
| 588 | case FILE_TYPE_CHAR: |
| 589 | Result = file_status(file_type::character_file); |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 590 | return std::error_code(); |
NAKAMURA Takumi | 8b01da4 | 2013-07-18 17:00:54 +0000 | [diff] [blame] | 591 | case FILE_TYPE_PIPE: |
| 592 | Result = file_status(file_type::fifo_file); |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 593 | return std::error_code(); |
NAKAMURA Takumi | 8b01da4 | 2013-07-18 17:00:54 +0000 | [diff] [blame] | 594 | } |
| 595 | |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 596 | BY_HANDLE_FILE_INFORMATION Info; |
| 597 | if (!::GetFileInformationByHandle(FileHandle, &Info)) |
| 598 | goto handle_status_error; |
| 599 | |
Rafael Espindola | a5932af | 2013-07-30 20:25:53 +0000 | [diff] [blame] | 600 | { |
Aaron Ballman | 345012d | 2017-03-13 12:24:51 +0000 | [diff] [blame] | 601 | file_type Type = (Info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) |
| 602 | ? file_type::directory_file |
| 603 | : file_type::regular_file; |
James Henderson | 566fdf4 | 2017-03-16 11:22:09 +0000 | [diff] [blame] | 604 | perms Permissions = (Info.dwFileAttributes & FILE_ATTRIBUTE_READONLY) |
| 605 | ? (all_read | all_exe) |
| 606 | : all_all; |
| 607 | Result = file_status( |
Zachary Turner | 5821a3b | 2017-03-20 23:55:20 +0000 | [diff] [blame] | 608 | Type, Permissions, Info.nNumberOfLinks, |
| 609 | Info.ftLastAccessTime.dwHighDateTime, |
James Henderson | 566fdf4 | 2017-03-16 11:22:09 +0000 | [diff] [blame] | 610 | Info.ftLastAccessTime.dwLowDateTime, |
| 611 | Info.ftLastWriteTime.dwHighDateTime, Info.ftLastWriteTime.dwLowDateTime, |
| 612 | Info.dwVolumeSerialNumber, Info.nFileSizeHigh, Info.nFileSizeLow, |
| 613 | Info.nFileIndexHigh, Info.nFileIndexLow); |
Aaron Ballman | 345012d | 2017-03-13 12:24:51 +0000 | [diff] [blame] | 614 | return std::error_code(); |
| 615 | } |
| 616 | |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 617 | handle_status_error: |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 618 | DWORD LastError = ::GetLastError(); |
| 619 | if (LastError == ERROR_FILE_NOT_FOUND || |
| 620 | LastError == ERROR_PATH_NOT_FOUND) |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 621 | Result = file_status(file_type::file_not_found); |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 622 | else if (LastError == ERROR_SHARING_VIOLATION) |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 623 | Result = file_status(file_type::type_unknown); |
Rafael Espindola | 107b74c | 2013-07-31 00:10:25 +0000 | [diff] [blame] | 624 | else |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 625 | Result = file_status(file_type::status_error); |
Yaron Keren | f8e6517 | 2015-05-04 04:48:10 +0000 | [diff] [blame] | 626 | return mapWindowsError(LastError); |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 627 | } |
| 628 | |
Zachary Turner | 82dd542 | 2017-03-07 16:10:10 +0000 | [diff] [blame] | 629 | 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] | 630 | SmallString<128> path_storage; |
| 631 | SmallVector<wchar_t, 128> path_utf16; |
| 632 | |
NAKAMURA Takumi | a3d4749 | 2011-03-16 02:53:32 +0000 | [diff] [blame] | 633 | StringRef path8 = path.toStringRef(path_storage); |
Benjamin Kramer | 58994ec | 2011-08-20 21:36:38 +0000 | [diff] [blame] | 634 | if (isReservedName(path8)) { |
NAKAMURA Takumi | a3d4749 | 2011-03-16 02:53:32 +0000 | [diff] [blame] | 635 | result = file_status(file_type::character_file); |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 636 | return std::error_code(); |
NAKAMURA Takumi | a3d4749 | 2011-03-16 02:53:32 +0000 | [diff] [blame] | 637 | } |
| 638 | |
Paul Robinson | d9c4a9a | 2014-11-13 00:12:14 +0000 | [diff] [blame] | 639 | if (std::error_code ec = widenPath(path8, path_utf16)) |
Michael J. Spencer | db5576a | 2010-12-04 00:32:40 +0000 | [diff] [blame] | 640 | return ec; |
| 641 | |
| 642 | DWORD attr = ::GetFileAttributesW(path_utf16.begin()); |
| 643 | if (attr == INVALID_FILE_ATTRIBUTES) |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 644 | return getStatus(INVALID_HANDLE_VALUE, result); |
Michael J. Spencer | db5576a | 2010-12-04 00:32:40 +0000 | [diff] [blame] | 645 | |
Zachary Turner | 82dd542 | 2017-03-07 16:10:10 +0000 | [diff] [blame] | 646 | DWORD Flags = FILE_FLAG_BACKUP_SEMANTICS; |
Michael J. Spencer | db5576a | 2010-12-04 00:32:40 +0000 | [diff] [blame] | 647 | // Handle reparse points. |
Zachary Turner | 82dd542 | 2017-03-07 16:10:10 +0000 | [diff] [blame] | 648 | if (!Follow && (attr & FILE_ATTRIBUTE_REPARSE_POINT)) |
| 649 | Flags |= FILE_FLAG_OPEN_REPARSE_POINT; |
Michael J. Spencer | db5576a | 2010-12-04 00:32:40 +0000 | [diff] [blame] | 650 | |
Rafael Espindola | a5932af | 2013-07-30 20:25:53 +0000 | [diff] [blame] | 651 | ScopedFileHandle h( |
| 652 | ::CreateFileW(path_utf16.begin(), 0, // Attributes only. |
Michael J. Spencer | 203d780 | 2011-12-12 06:04:28 +0000 | [diff] [blame] | 653 | FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, |
Zachary Turner | 82dd542 | 2017-03-07 16:10:10 +0000 | [diff] [blame] | 654 | NULL, OPEN_EXISTING, Flags, 0)); |
| 655 | if (!h) |
| 656 | return getStatus(INVALID_HANDLE_VALUE, result); |
Michael J. Spencer | db5576a | 2010-12-04 00:32:40 +0000 | [diff] [blame] | 657 | |
Zachary Turner | 82dd542 | 2017-03-07 16:10:10 +0000 | [diff] [blame] | 658 | return getStatus(h, result); |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 659 | } |
Michael J. Spencer | db5576a | 2010-12-04 00:32:40 +0000 | [diff] [blame] | 660 | |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 661 | std::error_code status(int FD, file_status &Result) { |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 662 | HANDLE FileHandle = reinterpret_cast<HANDLE>(_get_osfhandle(FD)); |
Aaron Ballman | 345012d | 2017-03-13 12:24:51 +0000 | [diff] [blame] | 663 | return getStatus(FileHandle, Result); |
| 664 | } |
| 665 | |
James Henderson | 566fdf4 | 2017-03-16 11:22:09 +0000 | [diff] [blame] | 666 | std::error_code setPermissions(const Twine &Path, perms Permissions) { |
| 667 | SmallVector<wchar_t, 128> PathUTF16; |
| 668 | if (std::error_code EC = widenPath(Path, PathUTF16)) |
| 669 | return EC; |
| 670 | |
| 671 | DWORD Attributes = ::GetFileAttributesW(PathUTF16.begin()); |
| 672 | if (Attributes == INVALID_FILE_ATTRIBUTES) |
| 673 | return mapWindowsError(GetLastError()); |
| 674 | |
| 675 | // There are many Windows file attributes that are not to do with the file |
| 676 | // permissions (e.g. FILE_ATTRIBUTE_HIDDEN). We need to be careful to preserve |
| 677 | // them. |
| 678 | if (Permissions & all_write) { |
| 679 | Attributes &= ~FILE_ATTRIBUTE_READONLY; |
| 680 | if (Attributes == 0) |
| 681 | // FILE_ATTRIBUTE_NORMAL indicates no other attributes are set. |
| 682 | Attributes |= FILE_ATTRIBUTE_NORMAL; |
| 683 | } |
| 684 | else { |
| 685 | Attributes |= FILE_ATTRIBUTE_READONLY; |
| 686 | // FILE_ATTRIBUTE_NORMAL is not compatible with any other attributes, so |
| 687 | // remove it, if it is present. |
| 688 | Attributes &= ~FILE_ATTRIBUTE_NORMAL; |
| 689 | } |
| 690 | |
| 691 | if (!::SetFileAttributesW(PathUTF16.begin(), Attributes)) |
| 692 | return mapWindowsError(GetLastError()); |
| 693 | |
| 694 | return std::error_code(); |
| 695 | } |
| 696 | |
Aaron Ballman | 345012d | 2017-03-13 12:24:51 +0000 | [diff] [blame] | 697 | std::error_code setLastModificationAndAccessTime(int FD, TimePoint<> Time) { |
| 698 | FILETIME FT = toFILETIME(Time); |
| 699 | HANDLE FileHandle = reinterpret_cast<HANDLE>(_get_osfhandle(FD)); |
Rafael Espindola | 4a3365c | 2013-06-20 20:56:14 +0000 | [diff] [blame] | 700 | if (!SetFileTime(FileHandle, NULL, &FT, &FT)) |
Yaron Keren | f8e6517 | 2015-05-04 04:48:10 +0000 | [diff] [blame] | 701 | return mapWindowsError(::GetLastError()); |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 702 | return std::error_code(); |
Rafael Espindola | 4a3365c | 2013-06-20 20:56:14 +0000 | [diff] [blame] | 703 | } |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 704 | |
Rafael Espindola | 7eb1f18 | 2014-12-11 20:12:55 +0000 | [diff] [blame] | 705 | std::error_code mapped_file_region::init(int FD, uint64_t Offset, |
| 706 | mapmode Mode) { |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 707 | // Make sure that the requested size fits within SIZE_T. |
Rafael Espindola | 986f5ad | 2014-12-16 02:19:26 +0000 | [diff] [blame] | 708 | if (Size > std::numeric_limits<SIZE_T>::max()) |
Rafael Espindola | 2a826e4 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 709 | return make_error_code(errc::invalid_argument); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 710 | |
Rafael Espindola | a23008a | 2014-12-16 03:10:29 +0000 | [diff] [blame] | 711 | HANDLE FileHandle = reinterpret_cast<HANDLE>(_get_osfhandle(FD)); |
| 712 | if (FileHandle == INVALID_HANDLE_VALUE) |
| 713 | return make_error_code(errc::bad_file_descriptor); |
| 714 | |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 715 | DWORD flprotect; |
| 716 | switch (Mode) { |
| 717 | case readonly: flprotect = PAGE_READONLY; break; |
| 718 | case readwrite: flprotect = PAGE_READWRITE; break; |
| 719 | case priv: flprotect = PAGE_WRITECOPY; break; |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 720 | } |
| 721 | |
Rafael Espindola | 369d514 | 2014-12-16 02:53:35 +0000 | [diff] [blame] | 722 | HANDLE FileMappingHandle = |
David Majnemer | 17a4496 | 2013-10-07 09:52:36 +0000 | [diff] [blame] | 723 | ::CreateFileMappingW(FileHandle, 0, flprotect, |
| 724 | (Offset + Size) >> 32, |
| 725 | (Offset + Size) & 0xffffffff, |
| 726 | 0); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 727 | if (FileMappingHandle == NULL) { |
Yaron Keren | f8e6517 | 2015-05-04 04:48:10 +0000 | [diff] [blame] | 728 | std::error_code ec = mapWindowsError(GetLastError()); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 729 | return ec; |
| 730 | } |
| 731 | |
| 732 | DWORD dwDesiredAccess; |
| 733 | switch (Mode) { |
| 734 | case readonly: dwDesiredAccess = FILE_MAP_READ; break; |
| 735 | case readwrite: dwDesiredAccess = FILE_MAP_WRITE; break; |
| 736 | case priv: dwDesiredAccess = FILE_MAP_COPY; break; |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 737 | } |
| 738 | Mapping = ::MapViewOfFile(FileMappingHandle, |
| 739 | dwDesiredAccess, |
| 740 | Offset >> 32, |
| 741 | Offset & 0xffffffff, |
| 742 | Size); |
| 743 | if (Mapping == NULL) { |
Yaron Keren | f8e6517 | 2015-05-04 04:48:10 +0000 | [diff] [blame] | 744 | std::error_code ec = mapWindowsError(GetLastError()); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 745 | ::CloseHandle(FileMappingHandle); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 746 | return ec; |
| 747 | } |
| 748 | |
| 749 | if (Size == 0) { |
| 750 | MEMORY_BASIC_INFORMATION mbi; |
| 751 | SIZE_T Result = VirtualQuery(Mapping, &mbi, sizeof(mbi)); |
| 752 | if (Result == 0) { |
Yaron Keren | f8e6517 | 2015-05-04 04:48:10 +0000 | [diff] [blame] | 753 | std::error_code ec = mapWindowsError(GetLastError()); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 754 | ::UnmapViewOfFile(Mapping); |
| 755 | ::CloseHandle(FileMappingHandle); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 756 | return ec; |
| 757 | } |
| 758 | Size = mbi.RegionSize; |
| 759 | } |
Michael J. Spencer | 42ad29f | 2013-03-14 00:20:10 +0000 | [diff] [blame] | 760 | |
| 761 | // Close all the handles except for the view. It will keep the other handles |
| 762 | // alive. |
| 763 | ::CloseHandle(FileMappingHandle); |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 764 | return std::error_code(); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 765 | } |
| 766 | |
Roman Lebedev | 1e053ab | 2017-09-27 17:24:34 +0000 | [diff] [blame] | 767 | mapped_file_region::mapped_file_region(int fd, mapmode mode, size_t length, |
Rafael Espindola | 7eb1f18 | 2014-12-11 20:12:55 +0000 | [diff] [blame] | 768 | uint64_t offset, std::error_code &ec) |
Rafael Espindola | a23008a | 2014-12-16 03:10:29 +0000 | [diff] [blame] | 769 | : Size(length), Mapping() { |
Rafael Espindola | 986f5ad | 2014-12-16 02:19:26 +0000 | [diff] [blame] | 770 | ec = init(fd, offset, mode); |
Rafael Espindola | a23008a | 2014-12-16 03:10:29 +0000 | [diff] [blame] | 771 | if (ec) |
Rafael Espindola | 369d514 | 2014-12-16 02:53:35 +0000 | [diff] [blame] | 772 | Mapping = 0; |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | mapped_file_region::~mapped_file_region() { |
| 776 | if (Mapping) |
| 777 | ::UnmapViewOfFile(Mapping); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 778 | } |
| 779 | |
Roman Lebedev | 1e053ab | 2017-09-27 17:24:34 +0000 | [diff] [blame] | 780 | size_t mapped_file_region::size() const { |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 781 | assert(Mapping && "Mapping failed but used anyway!"); |
| 782 | return Size; |
| 783 | } |
| 784 | |
| 785 | char *mapped_file_region::data() const { |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 786 | assert(Mapping && "Mapping failed but used anyway!"); |
| 787 | return reinterpret_cast<char*>(Mapping); |
| 788 | } |
| 789 | |
| 790 | const char *mapped_file_region::const_data() const { |
| 791 | assert(Mapping && "Mapping failed but used anyway!"); |
| 792 | return reinterpret_cast<const char*>(Mapping); |
| 793 | } |
| 794 | |
| 795 | int mapped_file_region::alignment() { |
| 796 | SYSTEM_INFO SysInfo; |
| 797 | ::GetSystemInfo(&SysInfo); |
| 798 | return SysInfo.dwAllocationGranularity; |
| 799 | } |
| 800 | |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 801 | std::error_code detail::directory_iterator_construct(detail::DirIterState &it, |
Zachary Turner | 260bda3 | 2017-03-08 22:49:32 +0000 | [diff] [blame] | 802 | StringRef path, |
| 803 | bool follow_symlinks) { |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 804 | SmallVector<wchar_t, 128> path_utf16; |
| 805 | |
Paul Robinson | d9c4a9a | 2014-11-13 00:12:14 +0000 | [diff] [blame] | 806 | if (std::error_code ec = widenPath(path, path_utf16)) |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 807 | return ec; |
| 808 | |
| 809 | // Convert path to the format that Windows is happy with. |
| 810 | if (path_utf16.size() > 0 && |
| 811 | !is_separator(path_utf16[path.size() - 1]) && |
| 812 | path_utf16[path.size() - 1] != L':') { |
| 813 | path_utf16.push_back(L'\\'); |
| 814 | path_utf16.push_back(L'*'); |
| 815 | } else { |
| 816 | path_utf16.push_back(L'*'); |
| 817 | } |
| 818 | |
| 819 | // Get the first directory entry. |
| 820 | WIN32_FIND_DATAW FirstFind; |
Michael J. Spencer | 751e9aa | 2010-12-09 17:37:18 +0000 | [diff] [blame] | 821 | ScopedFindHandle FindHandle(::FindFirstFileW(c_str(path_utf16), &FirstFind)); |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 822 | if (!FindHandle) |
Yaron Keren | f8e6517 | 2015-05-04 04:48:10 +0000 | [diff] [blame] | 823 | return mapWindowsError(::GetLastError()); |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 824 | |
Michael J. Spencer | 98879d7 | 2011-01-05 16:39:30 +0000 | [diff] [blame] | 825 | size_t FilenameLen = ::wcslen(FirstFind.cFileName); |
| 826 | while ((FilenameLen == 1 && FirstFind.cFileName[0] == L'.') || |
| 827 | (FilenameLen == 2 && FirstFind.cFileName[0] == L'.' && |
| 828 | FirstFind.cFileName[1] == L'.')) |
| 829 | if (!::FindNextFileW(FindHandle, &FirstFind)) { |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 830 | DWORD LastError = ::GetLastError(); |
Michael J. Spencer | 98879d7 | 2011-01-05 16:39:30 +0000 | [diff] [blame] | 831 | // Check for end. |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 832 | if (LastError == ERROR_NO_MORE_FILES) |
Michael J. Spencer | 0a7625d | 2011-12-08 22:50:09 +0000 | [diff] [blame] | 833 | return detail::directory_iterator_destruct(it); |
Yaron Keren | f8e6517 | 2015-05-04 04:48:10 +0000 | [diff] [blame] | 834 | return mapWindowsError(LastError); |
Michael J. Spencer | 98879d7 | 2011-01-05 16:39:30 +0000 | [diff] [blame] | 835 | } else |
| 836 | FilenameLen = ::wcslen(FirstFind.cFileName); |
| 837 | |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 838 | // Construct the current directory entry. |
Michael J. Spencer | 98879d7 | 2011-01-05 16:39:30 +0000 | [diff] [blame] | 839 | SmallString<128> directory_entry_name_utf8; |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 840 | if (std::error_code ec = |
| 841 | UTF16ToUTF8(FirstFind.cFileName, ::wcslen(FirstFind.cFileName), |
| 842 | directory_entry_name_utf8)) |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 843 | return ec; |
| 844 | |
| 845 | it.IterationHandle = intptr_t(FindHandle.take()); |
Michael J. Spencer | 98879d7 | 2011-01-05 16:39:30 +0000 | [diff] [blame] | 846 | SmallString<128> directory_entry_path(path); |
Yaron Keren | 92e1b62 | 2015-03-18 10:17:07 +0000 | [diff] [blame] | 847 | path::append(directory_entry_path, directory_entry_name_utf8); |
Zachary Turner | 260bda3 | 2017-03-08 22:49:32 +0000 | [diff] [blame] | 848 | it.CurrentEntry = directory_entry(directory_entry_path, follow_symlinks); |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 849 | |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 850 | return std::error_code(); |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 851 | } |
| 852 | |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 853 | std::error_code detail::directory_iterator_destruct(detail::DirIterState &it) { |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 854 | if (it.IterationHandle != 0) |
| 855 | // Closes the handle if it's valid. |
| 856 | ScopedFindHandle close(HANDLE(it.IterationHandle)); |
| 857 | it.IterationHandle = 0; |
| 858 | it.CurrentEntry = directory_entry(); |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 859 | return std::error_code(); |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 860 | } |
| 861 | |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 862 | std::error_code detail::directory_iterator_increment(detail::DirIterState &it) { |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 863 | WIN32_FIND_DATAW FindData; |
| 864 | if (!::FindNextFileW(HANDLE(it.IterationHandle), &FindData)) { |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 865 | DWORD LastError = ::GetLastError(); |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 866 | // Check for end. |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 867 | if (LastError == ERROR_NO_MORE_FILES) |
Michael J. Spencer | 0a7625d | 2011-12-08 22:50:09 +0000 | [diff] [blame] | 868 | return detail::directory_iterator_destruct(it); |
Yaron Keren | f8e6517 | 2015-05-04 04:48:10 +0000 | [diff] [blame] | 869 | return mapWindowsError(LastError); |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 870 | } |
| 871 | |
Michael J. Spencer | 98879d7 | 2011-01-05 16:39:30 +0000 | [diff] [blame] | 872 | size_t FilenameLen = ::wcslen(FindData.cFileName); |
| 873 | if ((FilenameLen == 1 && FindData.cFileName[0] == L'.') || |
| 874 | (FilenameLen == 2 && FindData.cFileName[0] == L'.' && |
| 875 | FindData.cFileName[1] == L'.')) |
| 876 | return directory_iterator_increment(it); |
| 877 | |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 878 | SmallString<128> directory_entry_path_utf8; |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 879 | if (std::error_code ec = |
| 880 | UTF16ToUTF8(FindData.cFileName, ::wcslen(FindData.cFileName), |
| 881 | directory_entry_path_utf8)) |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 882 | return ec; |
| 883 | |
| 884 | it.CurrentEntry.replace_filename(Twine(directory_entry_path_utf8)); |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 885 | return std::error_code(); |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 886 | } |
| 887 | |
Zachary Turner | e48ace6 | 2017-03-10 17:39:21 +0000 | [diff] [blame] | 888 | static std::error_code realPathFromHandle(HANDLE H, |
| 889 | SmallVectorImpl<char> &RealPath) { |
| 890 | RealPath.clear(); |
| 891 | llvm::SmallVector<wchar_t, MAX_PATH> Buffer; |
| 892 | DWORD CountChars = ::GetFinalPathNameByHandleW( |
| 893 | H, Buffer.begin(), Buffer.capacity() - 1, FILE_NAME_NORMALIZED); |
| 894 | if (CountChars > Buffer.capacity()) { |
| 895 | // The buffer wasn't big enough, try again. In this case the return value |
| 896 | // *does* indicate the size of the null terminator. |
| 897 | Buffer.reserve(CountChars); |
| 898 | CountChars = ::GetFinalPathNameByHandleW( |
| 899 | H, Buffer.data(), Buffer.capacity() - 1, FILE_NAME_NORMALIZED); |
| 900 | } |
| 901 | if (CountChars == 0) |
| 902 | return mapWindowsError(GetLastError()); |
| 903 | |
| 904 | const wchar_t *Data = Buffer.data(); |
| 905 | if (CountChars >= 4) { |
| 906 | if (0 == ::memcmp(Data, L"\\\\?\\", 8)) { |
| 907 | CountChars -= 4; |
| 908 | Data += 4; |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | // Convert the result from UTF-16 to UTF-8. |
| 913 | return UTF16ToUTF8(Data, CountChars, RealPath); |
| 914 | } |
| 915 | |
| 916 | static std::error_code directoryRealPath(const Twine &Name, |
| 917 | SmallVectorImpl<char> &RealPath) { |
| 918 | SmallVector<wchar_t, 128> PathUTF16; |
| 919 | |
| 920 | if (std::error_code EC = widenPath(Name, PathUTF16)) |
| 921 | return EC; |
| 922 | |
| 923 | HANDLE H = |
| 924 | ::CreateFileW(PathUTF16.begin(), GENERIC_READ, |
| 925 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, |
| 926 | NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| 927 | if (H == INVALID_HANDLE_VALUE) |
| 928 | return mapWindowsError(GetLastError()); |
| 929 | std::error_code EC = realPathFromHandle(H, RealPath); |
| 930 | ::CloseHandle(H); |
| 931 | return EC; |
| 932 | } |
| 933 | |
Taewook Oh | d915327 | 2016-06-13 15:54:56 +0000 | [diff] [blame] | 934 | std::error_code openFileForRead(const Twine &Name, int &ResultFD, |
| 935 | SmallVectorImpl<char> *RealPath) { |
Rafael Espindola | a0d9b6b | 2013-07-17 14:58:25 +0000 | [diff] [blame] | 936 | SmallVector<wchar_t, 128> PathUTF16; |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 937 | |
Paul Robinson | d9c4a9a | 2014-11-13 00:12:14 +0000 | [diff] [blame] | 938 | if (std::error_code EC = widenPath(Name, PathUTF16)) |
Rafael Espindola | a0d9b6b | 2013-07-17 14:58:25 +0000 | [diff] [blame] | 939 | return EC; |
| 940 | |
Greg Bedwell | 7f68a71 | 2015-10-12 15:11:47 +0000 | [diff] [blame] | 941 | HANDLE H = |
| 942 | ::CreateFileW(PathUTF16.begin(), GENERIC_READ, |
| 943 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, |
| 944 | NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
Rafael Espindola | a0d9b6b | 2013-07-17 14:58:25 +0000 | [diff] [blame] | 945 | if (H == INVALID_HANDLE_VALUE) { |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 946 | DWORD LastError = ::GetLastError(); |
Yaron Keren | f8e6517 | 2015-05-04 04:48:10 +0000 | [diff] [blame] | 947 | std::error_code EC = mapWindowsError(LastError); |
Rafael Espindola | 331aeba | 2013-07-17 19:58:28 +0000 | [diff] [blame] | 948 | // Provide a better error message when trying to open directories. |
Rafael Espindola | a0d9b6b | 2013-07-17 14:58:25 +0000 | [diff] [blame] | 949 | // This only runs if we failed to open the file, so there is probably |
| 950 | // no performances issues. |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 951 | if (LastError != ERROR_ACCESS_DENIED) |
Rafael Espindola | a0d9b6b | 2013-07-17 14:58:25 +0000 | [diff] [blame] | 952 | return EC; |
| 953 | if (is_directory(Name)) |
Rafael Espindola | 2a826e4 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 954 | return make_error_code(errc::is_a_directory); |
Rafael Espindola | a0d9b6b | 2013-07-17 14:58:25 +0000 | [diff] [blame] | 955 | return EC; |
| 956 | } |
| 957 | |
| 958 | int FD = ::_open_osfhandle(intptr_t(H), 0); |
| 959 | if (FD == -1) { |
| 960 | ::CloseHandle(H); |
Yaron Keren | f8e6517 | 2015-05-04 04:48:10 +0000 | [diff] [blame] | 961 | return mapWindowsError(ERROR_INVALID_HANDLE); |
Rafael Espindola | a0d9b6b | 2013-07-17 14:58:25 +0000 | [diff] [blame] | 962 | } |
| 963 | |
Taewook Oh | d915327 | 2016-06-13 15:54:56 +0000 | [diff] [blame] | 964 | // Fetch the real name of the file, if the user asked |
Zachary Turner | e48ace6 | 2017-03-10 17:39:21 +0000 | [diff] [blame] | 965 | if (RealPath) |
Zachary Turner | 3c0dc33 | 2017-03-10 18:33:41 +0000 | [diff] [blame] | 966 | realPathFromHandle(H, *RealPath); |
Taewook Oh | d915327 | 2016-06-13 15:54:56 +0000 | [diff] [blame] | 967 | |
Rafael Espindola | a0d9b6b | 2013-07-17 14:58:25 +0000 | [diff] [blame] | 968 | ResultFD = FD; |
Zachary Turner | 3c0dc33 | 2017-03-10 18:33:41 +0000 | [diff] [blame] | 969 | return std::error_code(); |
Rafael Espindola | a0d9b6b | 2013-07-17 14:58:25 +0000 | [diff] [blame] | 970 | } |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 971 | |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 972 | std::error_code openFileForWrite(const Twine &Name, int &ResultFD, |
Rafael Espindola | 67080ce | 2013-07-19 15:02:03 +0000 | [diff] [blame] | 973 | sys::fs::OpenFlags Flags, unsigned Mode) { |
| 974 | // Verify that we don't have both "append" and "excl". |
| 975 | assert((!(Flags & sys::fs::F_Excl) || !(Flags & sys::fs::F_Append)) && |
| 976 | "Cannot specify both 'excl' and 'append' file creation flags!"); |
| 977 | |
Rafael Espindola | 67080ce | 2013-07-19 15:02:03 +0000 | [diff] [blame] | 978 | SmallVector<wchar_t, 128> PathUTF16; |
| 979 | |
Paul Robinson | d9c4a9a | 2014-11-13 00:12:14 +0000 | [diff] [blame] | 980 | if (std::error_code EC = widenPath(Name, PathUTF16)) |
Rafael Espindola | 67080ce | 2013-07-19 15:02:03 +0000 | [diff] [blame] | 981 | return EC; |
| 982 | |
| 983 | DWORD CreationDisposition; |
| 984 | if (Flags & F_Excl) |
| 985 | CreationDisposition = CREATE_NEW; |
NAKAMURA Takumi | edf7615 | 2013-08-22 15:14:45 +0000 | [diff] [blame] | 986 | else if (Flags & F_Append) |
Rafael Espindola | 67080ce | 2013-07-19 15:02:03 +0000 | [diff] [blame] | 987 | CreationDisposition = OPEN_ALWAYS; |
| 988 | else |
| 989 | CreationDisposition = CREATE_ALWAYS; |
| 990 | |
Rafael Espindola | 7a0b640 | 2014-02-24 03:07:41 +0000 | [diff] [blame] | 991 | DWORD Access = GENERIC_WRITE; |
| 992 | if (Flags & F_RW) |
| 993 | Access |= GENERIC_READ; |
| 994 | |
Reid Kleckner | cefb333 | 2017-08-04 21:52:00 +0000 | [diff] [blame] | 995 | HANDLE H = |
| 996 | ::CreateFileW(PathUTF16.begin(), Access, |
| 997 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, |
| 998 | NULL, CreationDisposition, FILE_ATTRIBUTE_NORMAL, NULL); |
Rafael Espindola | 67080ce | 2013-07-19 15:02:03 +0000 | [diff] [blame] | 999 | |
| 1000 | if (H == INVALID_HANDLE_VALUE) { |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 1001 | DWORD LastError = ::GetLastError(); |
Yaron Keren | f8e6517 | 2015-05-04 04:48:10 +0000 | [diff] [blame] | 1002 | std::error_code EC = mapWindowsError(LastError); |
Rafael Espindola | 67080ce | 2013-07-19 15:02:03 +0000 | [diff] [blame] | 1003 | // Provide a better error message when trying to open directories. |
| 1004 | // This only runs if we failed to open the file, so there is probably |
| 1005 | // no performances issues. |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 1006 | if (LastError != ERROR_ACCESS_DENIED) |
Rafael Espindola | 67080ce | 2013-07-19 15:02:03 +0000 | [diff] [blame] | 1007 | return EC; |
| 1008 | if (is_directory(Name)) |
Rafael Espindola | 2a826e4 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 1009 | return make_error_code(errc::is_a_directory); |
Rafael Espindola | 67080ce | 2013-07-19 15:02:03 +0000 | [diff] [blame] | 1010 | return EC; |
| 1011 | } |
| 1012 | |
| 1013 | int OpenFlags = 0; |
| 1014 | if (Flags & F_Append) |
| 1015 | OpenFlags |= _O_APPEND; |
| 1016 | |
Rafael Espindola | 90c7f1c | 2014-02-24 18:20:12 +0000 | [diff] [blame] | 1017 | if (Flags & F_Text) |
Rafael Espindola | 67080ce | 2013-07-19 15:02:03 +0000 | [diff] [blame] | 1018 | OpenFlags |= _O_TEXT; |
| 1019 | |
| 1020 | int FD = ::_open_osfhandle(intptr_t(H), OpenFlags); |
| 1021 | if (FD == -1) { |
| 1022 | ::CloseHandle(H); |
Yaron Keren | f8e6517 | 2015-05-04 04:48:10 +0000 | [diff] [blame] | 1023 | return mapWindowsError(ERROR_INVALID_HANDLE); |
Rafael Espindola | 67080ce | 2013-07-19 15:02:03 +0000 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | ResultFD = FD; |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 1027 | return std::error_code(); |
Rafael Espindola | 67080ce | 2013-07-19 15:02:03 +0000 | [diff] [blame] | 1028 | } |
Taewook Oh | d915327 | 2016-06-13 15:54:56 +0000 | [diff] [blame] | 1029 | |
Zachary Turner | 260bda3 | 2017-03-08 22:49:32 +0000 | [diff] [blame] | 1030 | std::error_code remove_directories(const Twine &path, bool IgnoreErrors) { |
| 1031 | // Convert to utf-16. |
| 1032 | SmallVector<wchar_t, 128> Path16; |
| 1033 | std::error_code EC = widenPath(path, Path16); |
| 1034 | if (EC && !IgnoreErrors) |
| 1035 | return EC; |
| 1036 | |
| 1037 | // SHFileOperation() accepts a list of paths, and so must be double null- |
| 1038 | // terminated to indicate the end of the list. The buffer is already null |
| 1039 | // terminated, but since that null character is not considered part of the |
| 1040 | // vector's size, pushing another one will just consume that byte. So we |
| 1041 | // need to push 2 null terminators. |
| 1042 | Path16.push_back(0); |
| 1043 | Path16.push_back(0); |
| 1044 | |
| 1045 | SHFILEOPSTRUCTW shfos = {}; |
| 1046 | shfos.wFunc = FO_DELETE; |
| 1047 | shfos.pFrom = Path16.data(); |
| 1048 | shfos.fFlags = FOF_NO_UI; |
| 1049 | |
| 1050 | int result = ::SHFileOperationW(&shfos); |
| 1051 | if (result != 0 && !IgnoreErrors) |
| 1052 | return mapWindowsError(result); |
| 1053 | return std::error_code(); |
| 1054 | } |
| 1055 | |
Zachary Turner | e48ace6 | 2017-03-10 17:39:21 +0000 | [diff] [blame] | 1056 | static void expandTildeExpr(SmallVectorImpl<char> &Path) { |
| 1057 | // Path does not begin with a tilde expression. |
| 1058 | if (Path.empty() || Path[0] != '~') |
| 1059 | return; |
| 1060 | |
| 1061 | StringRef PathStr(Path.begin(), Path.size()); |
| 1062 | PathStr = PathStr.drop_front(); |
Zachary Turner | 5c5091f | 2017-03-16 22:28:04 +0000 | [diff] [blame] | 1063 | StringRef Expr = PathStr.take_until([](char c) { return path::is_separator(c); }); |
Zachary Turner | e48ace6 | 2017-03-10 17:39:21 +0000 | [diff] [blame] | 1064 | |
| 1065 | if (!Expr.empty()) { |
| 1066 | // This is probably a ~username/ expression. Don't support this on Windows. |
| 1067 | return; |
| 1068 | } |
| 1069 | |
| 1070 | SmallString<128> HomeDir; |
| 1071 | if (!path::home_directory(HomeDir)) { |
| 1072 | // For some reason we couldn't get the home directory. Just exit. |
| 1073 | return; |
| 1074 | } |
| 1075 | |
| 1076 | // Overwrite the first character and insert the rest. |
| 1077 | Path[0] = HomeDir[0]; |
| 1078 | Path.insert(Path.begin() + 1, HomeDir.begin() + 1, HomeDir.end()); |
| 1079 | } |
| 1080 | |
| 1081 | std::error_code real_path(const Twine &path, SmallVectorImpl<char> &dest, |
| 1082 | bool expand_tilde) { |
| 1083 | dest.clear(); |
| 1084 | if (path.isTriviallyEmpty()) |
| 1085 | return std::error_code(); |
| 1086 | |
| 1087 | if (expand_tilde) { |
| 1088 | SmallString<128> Storage; |
| 1089 | path.toVector(Storage); |
| 1090 | expandTildeExpr(Storage); |
| 1091 | return real_path(Storage, dest, false); |
| 1092 | } |
| 1093 | |
| 1094 | if (is_directory(path)) |
| 1095 | return directoryRealPath(path, dest); |
| 1096 | |
| 1097 | int fd; |
| 1098 | if (std::error_code EC = llvm::sys::fs::openFileForRead(path, fd, &dest)) |
| 1099 | return EC; |
| 1100 | ::close(fd); |
| 1101 | return std::error_code(); |
| 1102 | } |
| 1103 | |
Michael J. Spencer | 9fc1d9d | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 1104 | } // end namespace fs |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 1105 | |
Peter Collingbourne | f7d4101 | 2014-01-31 23:46:06 +0000 | [diff] [blame] | 1106 | namespace path { |
Pawel Bylica | 7c1f36a | 2015-11-02 14:57:24 +0000 | [diff] [blame] | 1107 | static bool getKnownFolderPath(KNOWNFOLDERID folderId, |
| 1108 | SmallVectorImpl<char> &result) { |
Pawel Bylica | 7187e4b | 2015-10-16 09:08:59 +0000 | [diff] [blame] | 1109 | wchar_t *path = nullptr; |
| 1110 | if (::SHGetKnownFolderPath(folderId, KF_FLAG_CREATE, nullptr, &path) != S_OK) |
| 1111 | return false; |
| 1112 | |
| 1113 | bool ok = !UTF16ToUTF8(path, ::wcslen(path), result); |
| 1114 | ::CoTaskMemFree(path); |
| 1115 | return ok; |
| 1116 | } |
Pawel Bylica | 0e97e5c | 2015-11-02 09:49:17 +0000 | [diff] [blame] | 1117 | |
| 1118 | bool getUserCacheDir(SmallVectorImpl<char> &Result) { |
| 1119 | return getKnownFolderPath(FOLDERID_LocalAppData, Result); |
| 1120 | } |
Pawel Bylica | 7187e4b | 2015-10-16 09:08:59 +0000 | [diff] [blame] | 1121 | |
Peter Collingbourne | f7d4101 | 2014-01-31 23:46:06 +0000 | [diff] [blame] | 1122 | bool home_directory(SmallVectorImpl<char> &result) { |
Pawel Bylica | 7187e4b | 2015-10-16 09:08:59 +0000 | [diff] [blame] | 1123 | return getKnownFolderPath(FOLDERID_Profile, result); |
Peter Collingbourne | f7d4101 | 2014-01-31 23:46:06 +0000 | [diff] [blame] | 1124 | } |
| 1125 | |
Pawel Bylica | a90e745 | 2015-11-17 16:54:32 +0000 | [diff] [blame] | 1126 | static bool getTempDirEnvVar(const wchar_t *Var, SmallVectorImpl<char> &Res) { |
Pawel Bylica | 6e680b2 | 2015-11-06 23:44:23 +0000 | [diff] [blame] | 1127 | SmallVector<wchar_t, 1024> Buf; |
| 1128 | size_t Size = 1024; |
| 1129 | do { |
| 1130 | Buf.reserve(Size); |
Pawel Bylica | a90e745 | 2015-11-17 16:54:32 +0000 | [diff] [blame] | 1131 | Size = GetEnvironmentVariableW(Var, Buf.data(), Buf.capacity()); |
Pawel Bylica | 6e680b2 | 2015-11-06 23:44:23 +0000 | [diff] [blame] | 1132 | if (Size == 0) |
| 1133 | return false; |
| 1134 | |
| 1135 | // Try again with larger buffer. |
| 1136 | } while (Size > Buf.capacity()); |
| 1137 | Buf.set_size(Size); |
| 1138 | |
Pawel Bylica | a90e745 | 2015-11-17 16:54:32 +0000 | [diff] [blame] | 1139 | return !windows::UTF16ToUTF8(Buf.data(), Size, Res); |
Pawel Bylica | 6e680b2 | 2015-11-06 23:44:23 +0000 | [diff] [blame] | 1140 | } |
| 1141 | |
| 1142 | static bool getTempDirEnvVar(SmallVectorImpl<char> &Res) { |
Pawel Bylica | a90e745 | 2015-11-17 16:54:32 +0000 | [diff] [blame] | 1143 | const wchar_t *EnvironmentVariables[] = {L"TMP", L"TEMP", L"USERPROFILE"}; |
| 1144 | for (auto *Env : EnvironmentVariables) { |
Pawel Bylica | 6e680b2 | 2015-11-06 23:44:23 +0000 | [diff] [blame] | 1145 | if (getTempDirEnvVar(Env, Res)) |
| 1146 | return true; |
| 1147 | } |
| 1148 | return false; |
| 1149 | } |
| 1150 | |
Rafael Espindola | 016a6d5 | 2014-08-26 14:47:52 +0000 | [diff] [blame] | 1151 | void system_temp_directory(bool ErasedOnReboot, SmallVectorImpl<char> &Result) { |
| 1152 | (void)ErasedOnReboot; |
Pawel Bylica | 6e680b2 | 2015-11-06 23:44:23 +0000 | [diff] [blame] | 1153 | Result.clear(); |
Rafael Espindola | 016a6d5 | 2014-08-26 14:47:52 +0000 | [diff] [blame] | 1154 | |
Pawel Bylica | a90e745 | 2015-11-17 16:54:32 +0000 | [diff] [blame] | 1155 | // Check whether the temporary directory is specified by an environment var. |
| 1156 | // This matches GetTempPath logic to some degree. GetTempPath is not used |
| 1157 | // directly as it cannot handle evn var longer than 130 chars on Windows 7 |
| 1158 | // (fixed on Windows 8). |
| 1159 | if (getTempDirEnvVar(Result)) { |
| 1160 | assert(!Result.empty() && "Unexpected empty path"); |
| 1161 | native(Result); // Some Unix-like shells use Unix path separator in $TMP. |
| 1162 | fs::make_absolute(Result); // Make it absolute if not already. |
Pawel Bylica | 6e680b2 | 2015-11-06 23:44:23 +0000 | [diff] [blame] | 1163 | return; |
Pawel Bylica | a90e745 | 2015-11-17 16:54:32 +0000 | [diff] [blame] | 1164 | } |
Rafael Espindola | 016a6d5 | 2014-08-26 14:47:52 +0000 | [diff] [blame] | 1165 | |
| 1166 | // Fall back to a system default. |
Pawel Bylica | a90e745 | 2015-11-17 16:54:32 +0000 | [diff] [blame] | 1167 | const char *DefaultResult = "C:\\Temp"; |
Rafael Espindola | 016a6d5 | 2014-08-26 14:47:52 +0000 | [diff] [blame] | 1168 | Result.append(DefaultResult, DefaultResult + strlen(DefaultResult)); |
| 1169 | } |
Peter Collingbourne | f7d4101 | 2014-01-31 23:46:06 +0000 | [diff] [blame] | 1170 | } // end namespace path |
| 1171 | |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 1172 | namespace windows { |
Rafael Espindola | 0a5f9cf | 2014-06-12 14:11:22 +0000 | [diff] [blame] | 1173 | std::error_code UTF8ToUTF16(llvm::StringRef utf8, |
Rafael Espindola | b4ad29b | 2014-06-13 02:36:09 +0000 | [diff] [blame] | 1174 | llvm::SmallVectorImpl<wchar_t> &utf16) { |
Michael J. Spencer | 7a3a510 | 2014-02-20 20:46:23 +0000 | [diff] [blame] | 1175 | if (!utf8.empty()) { |
| 1176 | int len = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, utf8.begin(), |
| 1177 | utf8.size(), utf16.begin(), 0); |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 1178 | |
Michael J. Spencer | 7a3a510 | 2014-02-20 20:46:23 +0000 | [diff] [blame] | 1179 | if (len == 0) |
Yaron Keren | f8e6517 | 2015-05-04 04:48:10 +0000 | [diff] [blame] | 1180 | return mapWindowsError(::GetLastError()); |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 1181 | |
Michael J. Spencer | 7a3a510 | 2014-02-20 20:46:23 +0000 | [diff] [blame] | 1182 | utf16.reserve(len + 1); |
| 1183 | utf16.set_size(len); |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 1184 | |
Michael J. Spencer | 7a3a510 | 2014-02-20 20:46:23 +0000 | [diff] [blame] | 1185 | len = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, utf8.begin(), |
| 1186 | utf8.size(), utf16.begin(), utf16.size()); |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 1187 | |
Michael J. Spencer | 7a3a510 | 2014-02-20 20:46:23 +0000 | [diff] [blame] | 1188 | if (len == 0) |
Yaron Keren | f8e6517 | 2015-05-04 04:48:10 +0000 | [diff] [blame] | 1189 | return mapWindowsError(::GetLastError()); |
Michael J. Spencer | 7a3a510 | 2014-02-20 20:46:23 +0000 | [diff] [blame] | 1190 | } |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 1191 | |
| 1192 | // Make utf16 null terminated. |
| 1193 | utf16.push_back(0); |
| 1194 | utf16.pop_back(); |
| 1195 | |
Rafael Espindola | 0a5f9cf | 2014-06-12 14:11:22 +0000 | [diff] [blame] | 1196 | return std::error_code(); |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
Rafael Espindola | 9c35966 | 2014-09-03 20:02:00 +0000 | [diff] [blame] | 1199 | static |
| 1200 | std::error_code UTF16ToCodePage(unsigned codepage, const wchar_t *utf16, |
| 1201 | size_t utf16_len, |
| 1202 | llvm::SmallVectorImpl<char> &utf8) { |
Michael J. Spencer | 7a3a510 | 2014-02-20 20:46:23 +0000 | [diff] [blame] | 1203 | if (utf16_len) { |
| 1204 | // Get length. |
Rafael Espindola | 9c35966 | 2014-09-03 20:02:00 +0000 | [diff] [blame] | 1205 | int len = ::WideCharToMultiByte(codepage, 0, utf16, utf16_len, utf8.begin(), |
Michael J. Spencer | 7a3a510 | 2014-02-20 20:46:23 +0000 | [diff] [blame] | 1206 | 0, NULL, NULL); |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 1207 | |
Michael J. Spencer | 7a3a510 | 2014-02-20 20:46:23 +0000 | [diff] [blame] | 1208 | if (len == 0) |
Yaron Keren | f8e6517 | 2015-05-04 04:48:10 +0000 | [diff] [blame] | 1209 | return mapWindowsError(::GetLastError()); |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 1210 | |
Michael J. Spencer | 7a3a510 | 2014-02-20 20:46:23 +0000 | [diff] [blame] | 1211 | utf8.reserve(len); |
| 1212 | utf8.set_size(len); |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 1213 | |
Michael J. Spencer | 7a3a510 | 2014-02-20 20:46:23 +0000 | [diff] [blame] | 1214 | // Now do the actual conversion. |
Rafael Espindola | 9c35966 | 2014-09-03 20:02:00 +0000 | [diff] [blame] | 1215 | len = ::WideCharToMultiByte(codepage, 0, utf16, utf16_len, utf8.data(), |
Michael J. Spencer | 7a3a510 | 2014-02-20 20:46:23 +0000 | [diff] [blame] | 1216 | utf8.size(), NULL, NULL); |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 1217 | |
Michael J. Spencer | 7a3a510 | 2014-02-20 20:46:23 +0000 | [diff] [blame] | 1218 | if (len == 0) |
Yaron Keren | f8e6517 | 2015-05-04 04:48:10 +0000 | [diff] [blame] | 1219 | return mapWindowsError(::GetLastError()); |
Michael J. Spencer | 7a3a510 | 2014-02-20 20:46:23 +0000 | [diff] [blame] | 1220 | } |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 1221 | |
| 1222 | // Make utf8 null terminated. |
| 1223 | utf8.push_back(0); |
| 1224 | utf8.pop_back(); |
| 1225 | |
Rafael Espindola | 0a5f9cf | 2014-06-12 14:11:22 +0000 | [diff] [blame] | 1226 | return std::error_code(); |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 1227 | } |
Rafael Espindola | 9c35966 | 2014-09-03 20:02:00 +0000 | [diff] [blame] | 1228 | |
| 1229 | std::error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len, |
| 1230 | llvm::SmallVectorImpl<char> &utf8) { |
| 1231 | return UTF16ToCodePage(CP_UTF8, utf16, utf16_len, utf8); |
| 1232 | } |
| 1233 | |
| 1234 | std::error_code UTF16ToCurCP(const wchar_t *utf16, size_t utf16_len, |
| 1235 | llvm::SmallVectorImpl<char> &utf8) { |
| 1236 | return UTF16ToCodePage(CP_ACP, utf16, utf16_len, utf8); |
| 1237 | } |
Taewook Oh | d915327 | 2016-06-13 15:54:56 +0000 | [diff] [blame] | 1238 | |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 1239 | } // end namespace windows |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 1240 | } // end namespace sys |
| 1241 | } // end namespace llvm |