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