1 //===- llvm/Support/Windows/Path.inc - Windows Path Impl --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Windows specific implementation of the Path API.
12 //===----------------------------------------------------------------------===//
14 //===----------------------------------------------------------------------===//
15 //=== WARNING: Implementation here must contain only generic Windows code that
16 //=== is guaranteed to work on *all* Windows variants.
17 //===----------------------------------------------------------------------===//
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/Support/WindowsError.h"
24 #include <sys/types.h>
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
28 #include "WindowsSupport.h"
33 // MinGW doesn't define this.
34 #ifndef _ERRNO_T_DEFINED
35 #define _ERRNO_T_DEFINED
40 # pragma comment(lib, "advapi32.lib") // This provides CryptAcquireContextW.
45 using llvm::sys::windows::UTF8ToUTF16;
46 using llvm::sys::windows::UTF16ToUTF8;
48 static std::error_code windows_error(DWORD E) {
49 return mapWindowsError(E);
52 static bool is_separator(const wchar_t value) {
66 std::string getMainExecutable(const char *argv0, void *MainExecAddr) {
67 SmallVector<wchar_t, MAX_PATH> PathName;
68 DWORD Size = ::GetModuleFileNameW(NULL, PathName.data(), PathName.capacity());
70 // A zero return value indicates a failure other than insufficient space.
74 // Insufficient space is determined by a return value equal to the size of
75 // the buffer passed in.
76 if (Size == PathName.capacity())
79 // On success, GetModuleFileNameW returns the number of characters written to
80 // the buffer not including the NULL terminator.
81 PathName.set_size(Size);
83 // Convert the result from UTF-16 to UTF-8.
84 SmallVector<char, MAX_PATH> PathNameUTF8;
85 if (UTF16ToUTF8(PathName.data(), PathName.size(), PathNameUTF8))
88 return std::string(PathNameUTF8.data());
91 UniqueID file_status::getUniqueID() const {
92 // The file is uniquely identified by the volume serial number along
93 // with the 64-bit file identifier.
94 uint64_t FileID = (static_cast<uint64_t>(FileIndexHigh) << 32ULL) |
95 static_cast<uint64_t>(FileIndexLow);
97 return UniqueID(VolumeSerialNumber, FileID);
100 TimeValue file_status::getLastModificationTime() const {
102 UI.LowPart = LastWriteTimeLow;
103 UI.HighPart = LastWriteTimeHigh;
106 Ret.fromWin32Time(UI.QuadPart);
110 std::error_code current_path(SmallVectorImpl<char> &result) {
111 SmallVector<wchar_t, MAX_PATH> cur_path;
112 DWORD len = MAX_PATH;
115 cur_path.reserve(len);
116 len = ::GetCurrentDirectoryW(cur_path.capacity(), cur_path.data());
118 // A zero return value indicates a failure other than insufficient space.
120 return windows_error(::GetLastError());
122 // If there's insufficient space, the len returned is larger than the len
124 } while (len > cur_path.capacity());
126 // On success, GetCurrentDirectoryW returns the number of characters not
127 // including the null-terminator.
128 cur_path.set_size(len);
129 return UTF16ToUTF8(cur_path.begin(), cur_path.size(), result);
132 std::error_code create_directory(const Twine &path, bool IgnoreExisting) {
133 SmallString<128> path_storage;
134 SmallVector<wchar_t, 128> path_utf16;
136 if (std::error_code ec =
137 UTF8ToUTF16(path.toStringRef(path_storage), path_utf16))
140 if (!::CreateDirectoryW(path_utf16.begin(), NULL)) {
141 DWORD LastError = ::GetLastError();
142 if (LastError != ERROR_ALREADY_EXISTS || !IgnoreExisting)
143 return windows_error(LastError);
146 return std::error_code();
149 // We can't use symbolic links for windows.
150 std::error_code create_link(const Twine &to, const Twine &from) {
152 SmallString<128> from_storage;
153 SmallString<128> to_storage;
154 StringRef f = from.toStringRef(from_storage);
155 StringRef t = to.toStringRef(to_storage);
157 // Convert to utf-16.
158 SmallVector<wchar_t, 128> wide_from;
159 SmallVector<wchar_t, 128> wide_to;
160 if (std::error_code ec = UTF8ToUTF16(f, wide_from))
162 if (std::error_code ec = UTF8ToUTF16(t, wide_to))
165 if (!::CreateHardLinkW(wide_from.begin(), wide_to.begin(), NULL))
166 return windows_error(::GetLastError());
168 return std::error_code();
171 std::error_code remove(const Twine &path, bool IgnoreNonExisting) {
172 SmallString<128> path_storage;
173 SmallVector<wchar_t, 128> path_utf16;
176 if (std::error_code EC = status(path, ST)) {
177 if (EC != errc::no_such_file_or_directory || !IgnoreNonExisting)
179 return std::error_code();
182 if (std::error_code ec =
183 UTF8ToUTF16(path.toStringRef(path_storage), path_utf16))
186 if (ST.type() == file_type::directory_file) {
187 if (!::RemoveDirectoryW(c_str(path_utf16))) {
188 std::error_code EC = windows_error(::GetLastError());
189 if (EC != errc::no_such_file_or_directory || !IgnoreNonExisting)
192 return std::error_code();
194 if (!::DeleteFileW(c_str(path_utf16))) {
195 std::error_code EC = windows_error(::GetLastError());
196 if (EC != errc::no_such_file_or_directory || !IgnoreNonExisting)
199 return std::error_code();
202 std::error_code rename(const Twine &from, const Twine &to) {
204 SmallString<128> from_storage;
205 SmallString<128> to_storage;
206 StringRef f = from.toStringRef(from_storage);
207 StringRef t = to.toStringRef(to_storage);
209 // Convert to utf-16.
210 SmallVector<wchar_t, 128> wide_from;
211 SmallVector<wchar_t, 128> wide_to;
212 if (std::error_code ec = UTF8ToUTF16(f, wide_from))
214 if (std::error_code ec = UTF8ToUTF16(t, wide_to))
217 std::error_code ec = std::error_code();
218 for (int i = 0; i < 2000; i++) {
219 if (::MoveFileExW(wide_from.begin(), wide_to.begin(),
220 MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING))
221 return std::error_code();
222 DWORD LastError = ::GetLastError();
223 if (LastError != ERROR_ACCESS_DENIED)
225 // Retry MoveFile() at ACCESS_DENIED.
226 // System scanners (eg. indexer) might open the source file when
227 // It is written and closed.
234 std::error_code resize_file(const Twine &path, uint64_t size) {
235 SmallString<128> path_storage;
236 SmallVector<wchar_t, 128> path_utf16;
238 if (std::error_code ec =
239 UTF8ToUTF16(path.toStringRef(path_storage), path_utf16))
242 int fd = ::_wopen(path_utf16.begin(), O_BINARY | _O_RDWR, S_IWRITE);
244 return std::error_code(errno, std::generic_category());
245 #ifdef HAVE__CHSIZE_S
246 errno_t error = ::_chsize_s(fd, size);
248 errno_t error = ::_chsize(fd, size);
251 return std::error_code(error, std::generic_category());
254 std::error_code exists(const Twine &path, bool &result) {
255 SmallString<128> path_storage;
256 SmallVector<wchar_t, 128> path_utf16;
258 if (std::error_code ec =
259 UTF8ToUTF16(path.toStringRef(path_storage), path_utf16))
262 DWORD attributes = ::GetFileAttributesW(path_utf16.begin());
264 if (attributes == INVALID_FILE_ATTRIBUTES) {
265 // See if the file didn't actually exist.
266 DWORD LastError = ::GetLastError();
267 if (LastError != ERROR_FILE_NOT_FOUND &&
268 LastError != ERROR_PATH_NOT_FOUND)
269 return windows_error(LastError);
273 return std::error_code();
276 bool can_write(const Twine &Path) {
277 // FIXME: take security attributes into account.
278 SmallString<128> PathStorage;
279 SmallVector<wchar_t, 128> PathUtf16;
281 if (UTF8ToUTF16(Path.toStringRef(PathStorage), PathUtf16))
284 DWORD Attr = ::GetFileAttributesW(PathUtf16.begin());
285 return (Attr != INVALID_FILE_ATTRIBUTES) && !(Attr & FILE_ATTRIBUTE_READONLY);
288 bool can_execute(const Twine &Path) {
289 SmallString<128> PathStorage;
290 SmallVector<wchar_t, 128> PathUtf16;
292 if (UTF8ToUTF16(Path.toStringRef(PathStorage), PathUtf16))
295 DWORD Attr = ::GetFileAttributesW(PathUtf16.begin());
296 return Attr != INVALID_FILE_ATTRIBUTES;
299 bool equivalent(file_status A, file_status B) {
300 assert(status_known(A) && status_known(B));
301 return A.FileIndexHigh == B.FileIndexHigh &&
302 A.FileIndexLow == B.FileIndexLow &&
303 A.FileSizeHigh == B.FileSizeHigh &&
304 A.FileSizeLow == B.FileSizeLow &&
305 A.LastWriteTimeHigh == B.LastWriteTimeHigh &&
306 A.LastWriteTimeLow == B.LastWriteTimeLow &&
307 A.VolumeSerialNumber == B.VolumeSerialNumber;
310 std::error_code equivalent(const Twine &A, const Twine &B, bool &result) {
311 file_status fsA, fsB;
312 if (std::error_code ec = status(A, fsA))
314 if (std::error_code ec = status(B, fsB))
316 result = equivalent(fsA, fsB);
317 return std::error_code();
320 static bool isReservedName(StringRef path) {
321 // This list of reserved names comes from MSDN, at:
322 // http://msdn.microsoft.com/en-us/library/aa365247%28v=vs.85%29.aspx
323 static const char *sReservedNames[] = { "nul", "con", "prn", "aux",
324 "com1", "com2", "com3", "com4", "com5", "com6",
325 "com7", "com8", "com9", "lpt1", "lpt2", "lpt3",
326 "lpt4", "lpt5", "lpt6", "lpt7", "lpt8", "lpt9" };
328 // First, check to see if this is a device namespace, which always
329 // starts with \\.\, since device namespaces are not legal file paths.
330 if (path.startswith("\\\\.\\"))
333 // Then compare against the list of ancient reserved names
334 for (size_t i = 0; i < array_lengthof(sReservedNames); ++i) {
335 if (path.equals_lower(sReservedNames[i]))
339 // The path isn't what we consider reserved.
343 static std::error_code getStatus(HANDLE FileHandle, file_status &Result) {
344 if (FileHandle == INVALID_HANDLE_VALUE)
345 goto handle_status_error;
347 switch (::GetFileType(FileHandle)) {
349 llvm_unreachable("Don't know anything about this file type");
350 case FILE_TYPE_UNKNOWN: {
351 DWORD Err = ::GetLastError();
353 return windows_error(Err);
354 Result = file_status(file_type::type_unknown);
355 return std::error_code();
360 Result = file_status(file_type::character_file);
361 return std::error_code();
363 Result = file_status(file_type::fifo_file);
364 return std::error_code();
367 BY_HANDLE_FILE_INFORMATION Info;
368 if (!::GetFileInformationByHandle(FileHandle, &Info))
369 goto handle_status_error;
372 file_type Type = (Info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
373 ? file_type::directory_file
374 : file_type::regular_file;
376 file_status(Type, Info.ftLastWriteTime.dwHighDateTime,
377 Info.ftLastWriteTime.dwLowDateTime,
378 Info.dwVolumeSerialNumber, Info.nFileSizeHigh,
379 Info.nFileSizeLow, Info.nFileIndexHigh, Info.nFileIndexLow);
380 return std::error_code();
384 DWORD LastError = ::GetLastError();
385 if (LastError == ERROR_FILE_NOT_FOUND ||
386 LastError == ERROR_PATH_NOT_FOUND)
387 Result = file_status(file_type::file_not_found);
388 else if (LastError == ERROR_SHARING_VIOLATION)
389 Result = file_status(file_type::type_unknown);
391 Result = file_status(file_type::status_error);
392 return windows_error(LastError);
395 std::error_code status(const Twine &path, file_status &result) {
396 SmallString<128> path_storage;
397 SmallVector<wchar_t, 128> path_utf16;
399 StringRef path8 = path.toStringRef(path_storage);
400 if (isReservedName(path8)) {
401 result = file_status(file_type::character_file);
402 return std::error_code();
405 if (std::error_code ec = UTF8ToUTF16(path8, path_utf16))
408 DWORD attr = ::GetFileAttributesW(path_utf16.begin());
409 if (attr == INVALID_FILE_ATTRIBUTES)
410 return getStatus(INVALID_HANDLE_VALUE, result);
412 // Handle reparse points.
413 if (attr & FILE_ATTRIBUTE_REPARSE_POINT) {
415 ::CreateFileW(path_utf16.begin(),
416 0, // Attributes only.
417 FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
420 FILE_FLAG_BACKUP_SEMANTICS,
423 return getStatus(INVALID_HANDLE_VALUE, result);
427 ::CreateFileW(path_utf16.begin(), 0, // Attributes only.
428 FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
429 NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0));
431 return getStatus(INVALID_HANDLE_VALUE, result);
433 return getStatus(h, result);
436 std::error_code status(int FD, file_status &Result) {
437 HANDLE FileHandle = reinterpret_cast<HANDLE>(_get_osfhandle(FD));
438 return getStatus(FileHandle, Result);
441 std::error_code setLastModificationAndAccessTime(int FD, TimeValue Time) {
443 UI.QuadPart = Time.toWin32Time();
445 FT.dwLowDateTime = UI.LowPart;
446 FT.dwHighDateTime = UI.HighPart;
447 HANDLE FileHandle = reinterpret_cast<HANDLE>(_get_osfhandle(FD));
448 if (!SetFileTime(FileHandle, NULL, &FT, &FT))
449 return windows_error(::GetLastError());
450 return std::error_code();
453 std::error_code mapped_file_region::init(int FD, bool CloseFD, uint64_t Offset) {
455 // Make sure that the requested size fits within SIZE_T.
456 if (Size > std::numeric_limits<SIZE_T>::max()) {
457 if (FileDescriptor) {
459 _close(FileDescriptor);
461 ::CloseHandle(FileHandle);
462 return make_error_code(errc::invalid_argument);
467 case readonly: flprotect = PAGE_READONLY; break;
468 case readwrite: flprotect = PAGE_READWRITE; break;
469 case priv: flprotect = PAGE_WRITECOPY; break;
473 ::CreateFileMappingW(FileHandle, 0, flprotect,
474 (Offset + Size) >> 32,
475 (Offset + Size) & 0xffffffff,
477 if (FileMappingHandle == NULL) {
478 std::error_code ec = windows_error(GetLastError());
479 if (FileDescriptor) {
481 _close(FileDescriptor);
483 ::CloseHandle(FileHandle);
487 DWORD dwDesiredAccess;
489 case readonly: dwDesiredAccess = FILE_MAP_READ; break;
490 case readwrite: dwDesiredAccess = FILE_MAP_WRITE; break;
491 case priv: dwDesiredAccess = FILE_MAP_COPY; break;
493 Mapping = ::MapViewOfFile(FileMappingHandle,
498 if (Mapping == NULL) {
499 std::error_code ec = windows_error(GetLastError());
500 ::CloseHandle(FileMappingHandle);
501 if (FileDescriptor) {
503 _close(FileDescriptor);
505 ::CloseHandle(FileHandle);
510 MEMORY_BASIC_INFORMATION mbi;
511 SIZE_T Result = VirtualQuery(Mapping, &mbi, sizeof(mbi));
513 std::error_code ec = windows_error(GetLastError());
514 ::UnmapViewOfFile(Mapping);
515 ::CloseHandle(FileMappingHandle);
516 if (FileDescriptor) {
518 _close(FileDescriptor);
520 ::CloseHandle(FileHandle);
523 Size = mbi.RegionSize;
526 // Close all the handles except for the view. It will keep the other handles
528 ::CloseHandle(FileMappingHandle);
529 if (FileDescriptor) {
531 _close(FileDescriptor); // Also closes FileHandle.
533 ::CloseHandle(FileHandle);
534 return std::error_code();
537 mapped_file_region::mapped_file_region(const Twine &path,
546 , FileHandle(INVALID_HANDLE_VALUE)
547 , FileMappingHandle() {
548 SmallString<128> path_storage;
549 SmallVector<wchar_t, 128> path_utf16;
551 // Convert path to UTF-16.
552 if ((ec = UTF8ToUTF16(path.toStringRef(path_storage), path_utf16)))
555 // Get file handle for creating a file mapping.
556 FileHandle = ::CreateFileW(c_str(path_utf16),
557 Mode == readonly ? GENERIC_READ
558 : GENERIC_READ | GENERIC_WRITE,
559 Mode == readonly ? FILE_SHARE_READ
562 Mode == readonly ? OPEN_EXISTING
564 Mode == readonly ? FILE_ATTRIBUTE_READONLY
565 : FILE_ATTRIBUTE_NORMAL,
567 if (FileHandle == INVALID_HANDLE_VALUE) {
568 ec = windows_error(::GetLastError());
573 ec = init(FileDescriptor, true, offset);
575 Mapping = FileMappingHandle = 0;
576 FileHandle = INVALID_HANDLE_VALUE;
581 mapped_file_region::mapped_file_region(int fd,
591 , FileHandle(INVALID_HANDLE_VALUE)
592 , FileMappingHandle() {
593 FileHandle = reinterpret_cast<HANDLE>(_get_osfhandle(fd));
594 if (FileHandle == INVALID_HANDLE_VALUE) {
596 _close(FileDescriptor);
598 ec = make_error_code(errc::bad_file_descriptor);
602 ec = init(FileDescriptor, closefd, offset);
604 Mapping = FileMappingHandle = 0;
605 FileHandle = INVALID_HANDLE_VALUE;
610 mapped_file_region::~mapped_file_region() {
612 ::UnmapViewOfFile(Mapping);
615 mapped_file_region::mapped_file_region(mapped_file_region &&other)
618 , Mapping(other.Mapping)
619 , FileDescriptor(other.FileDescriptor)
620 , FileHandle(other.FileHandle)
621 , FileMappingHandle(other.FileMappingHandle) {
622 other.Mapping = other.FileMappingHandle = 0;
623 other.FileHandle = INVALID_HANDLE_VALUE;
624 other.FileDescriptor = 0;
627 mapped_file_region::mapmode mapped_file_region::flags() const {
628 assert(Mapping && "Mapping failed but used anyway!");
632 uint64_t mapped_file_region::size() const {
633 assert(Mapping && "Mapping failed but used anyway!");
637 char *mapped_file_region::data() const {
638 assert(Mode != readonly && "Cannot get non-const data for readonly mapping!");
639 assert(Mapping && "Mapping failed but used anyway!");
640 return reinterpret_cast<char*>(Mapping);
643 const char *mapped_file_region::const_data() const {
644 assert(Mapping && "Mapping failed but used anyway!");
645 return reinterpret_cast<const char*>(Mapping);
648 int mapped_file_region::alignment() {
650 ::GetSystemInfo(&SysInfo);
651 return SysInfo.dwAllocationGranularity;
654 std::error_code detail::directory_iterator_construct(detail::DirIterState &it,
656 SmallVector<wchar_t, 128> path_utf16;
658 if (std::error_code ec = UTF8ToUTF16(path, path_utf16))
661 // Convert path to the format that Windows is happy with.
662 if (path_utf16.size() > 0 &&
663 !is_separator(path_utf16[path.size() - 1]) &&
664 path_utf16[path.size() - 1] != L':') {
665 path_utf16.push_back(L'\\');
666 path_utf16.push_back(L'*');
668 path_utf16.push_back(L'*');
671 // Get the first directory entry.
672 WIN32_FIND_DATAW FirstFind;
673 ScopedFindHandle FindHandle(::FindFirstFileW(c_str(path_utf16), &FirstFind));
675 return windows_error(::GetLastError());
677 size_t FilenameLen = ::wcslen(FirstFind.cFileName);
678 while ((FilenameLen == 1 && FirstFind.cFileName[0] == L'.') ||
679 (FilenameLen == 2 && FirstFind.cFileName[0] == L'.' &&
680 FirstFind.cFileName[1] == L'.'))
681 if (!::FindNextFileW(FindHandle, &FirstFind)) {
682 DWORD LastError = ::GetLastError();
684 if (LastError == ERROR_NO_MORE_FILES)
685 return detail::directory_iterator_destruct(it);
686 return windows_error(LastError);
688 FilenameLen = ::wcslen(FirstFind.cFileName);
690 // Construct the current directory entry.
691 SmallString<128> directory_entry_name_utf8;
692 if (std::error_code ec =
693 UTF16ToUTF8(FirstFind.cFileName, ::wcslen(FirstFind.cFileName),
694 directory_entry_name_utf8))
697 it.IterationHandle = intptr_t(FindHandle.take());
698 SmallString<128> directory_entry_path(path);
699 path::append(directory_entry_path, directory_entry_name_utf8.str());
700 it.CurrentEntry = directory_entry(directory_entry_path.str());
702 return std::error_code();
705 std::error_code detail::directory_iterator_destruct(detail::DirIterState &it) {
706 if (it.IterationHandle != 0)
707 // Closes the handle if it's valid.
708 ScopedFindHandle close(HANDLE(it.IterationHandle));
709 it.IterationHandle = 0;
710 it.CurrentEntry = directory_entry();
711 return std::error_code();
714 std::error_code detail::directory_iterator_increment(detail::DirIterState &it) {
715 WIN32_FIND_DATAW FindData;
716 if (!::FindNextFileW(HANDLE(it.IterationHandle), &FindData)) {
717 DWORD LastError = ::GetLastError();
719 if (LastError == ERROR_NO_MORE_FILES)
720 return detail::directory_iterator_destruct(it);
721 return windows_error(LastError);
724 size_t FilenameLen = ::wcslen(FindData.cFileName);
725 if ((FilenameLen == 1 && FindData.cFileName[0] == L'.') ||
726 (FilenameLen == 2 && FindData.cFileName[0] == L'.' &&
727 FindData.cFileName[1] == L'.'))
728 return directory_iterator_increment(it);
730 SmallString<128> directory_entry_path_utf8;
731 if (std::error_code ec =
732 UTF16ToUTF8(FindData.cFileName, ::wcslen(FindData.cFileName),
733 directory_entry_path_utf8))
736 it.CurrentEntry.replace_filename(Twine(directory_entry_path_utf8));
737 return std::error_code();
740 std::error_code openFileForRead(const Twine &Name, int &ResultFD) {
741 SmallString<128> PathStorage;
742 SmallVector<wchar_t, 128> PathUTF16;
744 if (std::error_code EC =
745 UTF8ToUTF16(Name.toStringRef(PathStorage), PathUTF16))
748 HANDLE H = ::CreateFileW(PathUTF16.begin(), GENERIC_READ,
749 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
750 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
751 if (H == INVALID_HANDLE_VALUE) {
752 DWORD LastError = ::GetLastError();
753 std::error_code EC = windows_error(LastError);
754 // Provide a better error message when trying to open directories.
755 // This only runs if we failed to open the file, so there is probably
756 // no performances issues.
757 if (LastError != ERROR_ACCESS_DENIED)
759 if (is_directory(Name))
760 return make_error_code(errc::is_a_directory);
764 int FD = ::_open_osfhandle(intptr_t(H), 0);
767 return windows_error(ERROR_INVALID_HANDLE);
771 return std::error_code();
774 std::error_code openFileForWrite(const Twine &Name, int &ResultFD,
775 sys::fs::OpenFlags Flags, unsigned Mode) {
776 // Verify that we don't have both "append" and "excl".
777 assert((!(Flags & sys::fs::F_Excl) || !(Flags & sys::fs::F_Append)) &&
778 "Cannot specify both 'excl' and 'append' file creation flags!");
780 SmallString<128> PathStorage;
781 SmallVector<wchar_t, 128> PathUTF16;
783 if (std::error_code EC =
784 UTF8ToUTF16(Name.toStringRef(PathStorage), PathUTF16))
787 DWORD CreationDisposition;
789 CreationDisposition = CREATE_NEW;
790 else if (Flags & F_Append)
791 CreationDisposition = OPEN_ALWAYS;
793 CreationDisposition = CREATE_ALWAYS;
795 DWORD Access = GENERIC_WRITE;
797 Access |= GENERIC_READ;
799 HANDLE H = ::CreateFileW(PathUTF16.begin(), Access,
800 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
801 CreationDisposition, FILE_ATTRIBUTE_NORMAL, NULL);
803 if (H == INVALID_HANDLE_VALUE) {
804 DWORD LastError = ::GetLastError();
805 std::error_code EC = windows_error(LastError);
806 // Provide a better error message when trying to open directories.
807 // This only runs if we failed to open the file, so there is probably
808 // no performances issues.
809 if (LastError != ERROR_ACCESS_DENIED)
811 if (is_directory(Name))
812 return make_error_code(errc::is_a_directory);
817 if (Flags & F_Append)
818 OpenFlags |= _O_APPEND;
821 OpenFlags |= _O_TEXT;
823 int FD = ::_open_osfhandle(intptr_t(H), OpenFlags);
826 return windows_error(ERROR_INVALID_HANDLE);
830 return std::error_code();
832 } // end namespace fs
836 bool home_directory(SmallVectorImpl<char> &result) {
837 wchar_t Path[MAX_PATH];
838 if (::SHGetFolderPathW(0, CSIDL_APPDATA | CSIDL_FLAG_CREATE, 0,
839 /*SHGFP_TYPE_CURRENT*/0, Path) != S_OK)
842 if (UTF16ToUTF8(Path, ::wcslen(Path), result))
848 static bool getTempDirEnvVar(const char *Var, SmallVectorImpl<char> &Res) {
849 SmallVector<wchar_t, 128> NameUTF16;
850 if (windows::UTF8ToUTF16(Var, NameUTF16))
853 SmallVector<wchar_t, 1024> Buf;
858 GetEnvironmentVariableW(NameUTF16.data(), Buf.data(), Buf.capacity());
862 // Try again with larger buffer.
863 } while (Size > Buf.capacity());
866 if (windows::UTF16ToUTF8(Buf.data(), Size, Res))
871 static bool getTempDirEnvVar(SmallVectorImpl<char> &Res) {
872 const char *EnvironmentVariables[] = {"TMP", "TEMP", "USERPROFILE"};
873 for (const char *Env : EnvironmentVariables) {
874 if (getTempDirEnvVar(Env, Res))
880 void system_temp_directory(bool ErasedOnReboot, SmallVectorImpl<char> &Result) {
881 (void)ErasedOnReboot;
884 // Check whether the temporary directory is specified by an environment
886 if (getTempDirEnvVar(Result))
889 // Fall back to a system default.
890 const char *DefaultResult = "C:\\TEMP";
891 Result.append(DefaultResult, DefaultResult + strlen(DefaultResult));
893 } // end namespace path
896 std::error_code UTF8ToUTF16(llvm::StringRef utf8,
897 llvm::SmallVectorImpl<wchar_t> &utf16) {
899 int len = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, utf8.begin(),
900 utf8.size(), utf16.begin(), 0);
903 return windows_error(::GetLastError());
905 utf16.reserve(len + 1);
908 len = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, utf8.begin(),
909 utf8.size(), utf16.begin(), utf16.size());
912 return windows_error(::GetLastError());
915 // Make utf16 null terminated.
919 return std::error_code();
923 std::error_code UTF16ToCodePage(unsigned codepage, const wchar_t *utf16,
925 llvm::SmallVectorImpl<char> &utf8) {
928 int len = ::WideCharToMultiByte(codepage, 0, utf16, utf16_len, utf8.begin(),
932 return windows_error(::GetLastError());
937 // Now do the actual conversion.
938 len = ::WideCharToMultiByte(codepage, 0, utf16, utf16_len, utf8.data(),
939 utf8.size(), NULL, NULL);
942 return windows_error(::GetLastError());
945 // Make utf8 null terminated.
949 return std::error_code();
952 std::error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len,
953 llvm::SmallVectorImpl<char> &utf8) {
954 return UTF16ToCodePage(CP_UTF8, utf16, utf16_len, utf8);
957 std::error_code UTF16ToCurCP(const wchar_t *utf16, size_t utf16_len,
958 llvm::SmallVectorImpl<char> &utf8) {
959 return UTF16ToCodePage(CP_ACP, utf16, utf16_len, utf8);
961 } // end namespace windows
962 } // end namespace sys
963 } // end namespace llvm