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"
23 #include <sys/types.h>
25 // These two headers must be included last, and make sure shlobj is required
26 // after Windows.h to make sure it picks up our definition of _WIN32_WINNT
27 #include "WindowsSupport.h"
32 // MinGW doesn't define this.
33 #ifndef _ERRNO_T_DEFINED
34 #define _ERRNO_T_DEFINED
39 # pragma comment(lib, "advapi32.lib") // This provides CryptAcquireContextW.
44 using llvm::sys::windows::UTF8ToUTF16;
45 using llvm::sys::windows::UTF16ToUTF8;
47 static error_code TempDir(SmallVectorImpl<wchar_t> &result) {
49 DWORD len = ::GetTempPathW(result.capacity(), result.begin());
52 return windows_error(::GetLastError());
54 if (len > result.capacity()) {
60 return error_code::success();
63 static bool is_separator(const wchar_t value) {
73 // FIXME: mode should be used here and default to user r/w only,
74 // it currently comes in as a UNIX mode.
75 static error_code createUniqueEntity(const Twine &model, int &result_fd,
76 SmallVectorImpl<char> &result_path,
77 bool makeAbsolute, unsigned mode,
79 // Use result_path as temp storage.
80 result_path.set_size(0);
81 StringRef m = model.toStringRef(result_path);
83 SmallVector<wchar_t, 128> model_utf16;
84 if (error_code ec = UTF8ToUTF16(m, model_utf16)) return ec;
87 // Make model absolute by prepending a temp directory if it's not already.
88 bool absolute = sys::path::is_absolute(m);
91 SmallVector<wchar_t, 64> temp_dir;
92 if (error_code ec = TempDir(temp_dir)) return ec;
93 // Handle c: by removing it.
94 if (model_utf16.size() > 2 && model_utf16[1] == L':') {
95 model_utf16.erase(model_utf16.begin(), model_utf16.begin() + 2);
97 model_utf16.insert(model_utf16.begin(), temp_dir.begin(), temp_dir.end());
101 // Replace '%' with random chars. From here on, DO NOT modify model. It may be
102 // needed if the randomly chosen path already exists.
103 SmallVector<wchar_t, 128> random_path_utf16;
106 random_path_utf16.set_size(0);
107 for (SmallVectorImpl<wchar_t>::const_iterator i = model_utf16.begin(),
108 e = model_utf16.end();
111 unsigned val = sys::Process::GetRandomNumber();
112 random_path_utf16.push_back(L"0123456789abcdef"[val & 15]);
115 random_path_utf16.push_back(*i);
117 // Make random_path_utf16 null terminated.
118 random_path_utf16.push_back(0);
119 random_path_utf16.pop_back();
121 HANDLE TempFileHandle = INVALID_HANDLE_VALUE;
125 // Try to create + open the path.
127 ::CreateFileW(random_path_utf16.begin(), GENERIC_READ | GENERIC_WRITE,
128 FILE_SHARE_READ, NULL,
129 // Return ERROR_FILE_EXISTS if the file
131 CREATE_NEW, FILE_ATTRIBUTE_TEMPORARY, NULL);
132 if (TempFileHandle == INVALID_HANDLE_VALUE) {
133 // If the file existed, try again, otherwise, error.
134 error_code ec = windows_error(::GetLastError());
135 if (ec == windows_error::file_exists)
136 goto retry_random_path;
141 // Convert the Windows API file handle into a C-runtime handle.
142 int fd = ::_open_osfhandle(intptr_t(TempFileHandle), 0);
144 ::CloseHandle(TempFileHandle);
145 ::DeleteFileW(random_path_utf16.begin());
146 // MSDN doesn't say anything about _open_osfhandle setting errno or
147 // GetLastError(), so just return invalid_handle.
148 return windows_error::invalid_handle;
156 DWORD attributes = ::GetFileAttributesW(random_path_utf16.begin());
157 if (attributes != INVALID_FILE_ATTRIBUTES)
158 goto retry_random_path;
159 error_code EC = make_error_code(windows_error(::GetLastError()));
160 if (EC != windows_error::file_not_found &&
161 EC != windows_error::path_not_found)
167 if (!::CreateDirectoryW(random_path_utf16.begin(), NULL)) {
168 error_code EC = windows_error(::GetLastError());
169 if (EC != windows_error::already_exists)
171 goto retry_random_path;
176 // Set result_path to the utf-8 representation of the path.
177 if (error_code ec = UTF16ToUTF8(random_path_utf16.begin(),
178 random_path_utf16.size(), result_path)) {
181 ::CloseHandle(TempFileHandle);
182 ::DeleteFileW(random_path_utf16.begin());
186 ::RemoveDirectoryW(random_path_utf16.begin());
192 return error_code::success();
199 std::string getMainExecutable(const char *argv0, void *MainExecAddr) {
200 SmallVector<wchar_t, MAX_PATH> PathName;
201 DWORD Size = ::GetModuleFileNameW(NULL, PathName.data(), PathName.capacity());
203 // A zero return value indicates a failure other than insufficient space.
207 // Insufficient space is determined by a return value equal to the size of
208 // the buffer passed in.
209 if (Size == PathName.capacity())
212 // On success, GetModuleFileNameW returns the number of characters written to
213 // the buffer not including the NULL terminator.
214 PathName.set_size(Size);
216 // Convert the result from UTF-16 to UTF-8.
217 SmallVector<char, MAX_PATH> PathNameUTF8;
218 if (UTF16ToUTF8(PathName.data(), PathName.size(), PathNameUTF8))
221 return std::string(PathNameUTF8.data());
224 UniqueID file_status::getUniqueID() const {
225 // The file is uniquely identified by the volume serial number along
226 // with the 64-bit file identifier.
227 uint64_t FileID = (static_cast<uint64_t>(FileIndexHigh) << 32ULL) |
228 static_cast<uint64_t>(FileIndexLow);
230 return UniqueID(VolumeSerialNumber, FileID);
233 TimeValue file_status::getLastModificationTime() const {
235 UI.LowPart = LastWriteTimeLow;
236 UI.HighPart = LastWriteTimeHigh;
239 Ret.fromWin32Time(UI.QuadPart);
243 error_code current_path(SmallVectorImpl<char> &result) {
244 SmallVector<wchar_t, MAX_PATH> cur_path;
245 DWORD len = MAX_PATH;
248 cur_path.reserve(len);
249 len = ::GetCurrentDirectoryW(cur_path.capacity(), cur_path.data());
251 // A zero return value indicates a failure other than insufficient space.
253 return windows_error(::GetLastError());
255 // If there's insufficient space, the len returned is larger than the len
257 } while (len > cur_path.capacity());
259 // On success, GetCurrentDirectoryW returns the number of characters not
260 // including the null-terminator.
261 cur_path.set_size(len);
262 return UTF16ToUTF8(cur_path.begin(), cur_path.size(), result);
265 error_code create_directory(const Twine &path, bool IgnoreExisting) {
266 SmallString<128> path_storage;
267 SmallVector<wchar_t, 128> path_utf16;
269 if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage),
273 if (!::CreateDirectoryW(path_utf16.begin(), NULL)) {
274 error_code ec = windows_error(::GetLastError());
275 if (ec != windows_error::already_exists || !IgnoreExisting)
279 return error_code::success();
282 error_code create_hard_link(const Twine &to, const Twine &from) {
284 SmallString<128> from_storage;
285 SmallString<128> to_storage;
286 StringRef f = from.toStringRef(from_storage);
287 StringRef t = to.toStringRef(to_storage);
289 // Convert to utf-16.
290 SmallVector<wchar_t, 128> wide_from;
291 SmallVector<wchar_t, 128> wide_to;
292 if (error_code ec = UTF8ToUTF16(f, wide_from)) return ec;
293 if (error_code ec = UTF8ToUTF16(t, wide_to)) return ec;
295 if (!::CreateHardLinkW(wide_from.begin(), wide_to.begin(), NULL))
296 return windows_error(::GetLastError());
298 return error_code::success();
301 error_code remove(const Twine &path, bool IgnoreNonExisting) {
302 SmallString<128> path_storage;
303 SmallVector<wchar_t, 128> path_utf16;
306 if (error_code EC = status(path, ST)) {
307 if (EC != errc::no_such_file_or_directory || !IgnoreNonExisting)
309 return error_code::success();
312 if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage),
316 if (ST.type() == file_type::directory_file) {
317 if (!::RemoveDirectoryW(c_str(path_utf16))) {
318 error_code EC = windows_error(::GetLastError());
319 if (EC != errc::no_such_file_or_directory || !IgnoreNonExisting)
322 return error_code::success();
324 if (!::DeleteFileW(c_str(path_utf16))) {
325 error_code EC = windows_error(::GetLastError());
326 if (EC != errc::no_such_file_or_directory || !IgnoreNonExisting)
329 return error_code::success();
332 error_code rename(const Twine &from, const Twine &to) {
334 SmallString<128> from_storage;
335 SmallString<128> to_storage;
336 StringRef f = from.toStringRef(from_storage);
337 StringRef t = to.toStringRef(to_storage);
339 // Convert to utf-16.
340 SmallVector<wchar_t, 128> wide_from;
341 SmallVector<wchar_t, 128> wide_to;
342 if (error_code ec = UTF8ToUTF16(f, wide_from)) return ec;
343 if (error_code ec = UTF8ToUTF16(t, wide_to)) return ec;
345 error_code ec = error_code::success();
346 for (int i = 0; i < 2000; i++) {
347 if (::MoveFileExW(wide_from.begin(), wide_to.begin(),
348 MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING))
349 return error_code::success();
350 ec = windows_error(::GetLastError());
351 if (ec != windows_error::access_denied)
353 // Retry MoveFile() at ACCESS_DENIED.
354 // System scanners (eg. indexer) might open the source file when
355 // It is written and closed.
362 error_code resize_file(const Twine &path, uint64_t size) {
363 SmallString<128> path_storage;
364 SmallVector<wchar_t, 128> path_utf16;
366 if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage),
370 int fd = ::_wopen(path_utf16.begin(), O_BINARY | _O_RDWR, S_IWRITE);
372 return error_code(errno, generic_category());
373 #ifdef HAVE__CHSIZE_S
374 errno_t error = ::_chsize_s(fd, size);
376 errno_t error = ::_chsize(fd, size);
379 return error_code(error, generic_category());
382 error_code exists(const Twine &path, bool &result) {
383 SmallString<128> path_storage;
384 SmallVector<wchar_t, 128> path_utf16;
386 if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage),
390 DWORD attributes = ::GetFileAttributesW(path_utf16.begin());
392 if (attributes == INVALID_FILE_ATTRIBUTES) {
393 // See if the file didn't actually exist.
394 error_code ec = make_error_code(windows_error(::GetLastError()));
395 if (ec != windows_error::file_not_found &&
396 ec != windows_error::path_not_found)
401 return error_code::success();
404 bool can_write(const Twine &Path) {
405 // FIXME: take security attributes into account.
406 SmallString<128> PathStorage;
407 SmallVector<wchar_t, 128> PathUtf16;
409 if (UTF8ToUTF16(Path.toStringRef(PathStorage), PathUtf16))
412 DWORD Attr = ::GetFileAttributesW(PathUtf16.begin());
413 return (Attr != INVALID_FILE_ATTRIBUTES) && !(Attr & FILE_ATTRIBUTE_READONLY);
416 bool can_execute(const Twine &Path) {
417 SmallString<128> PathStorage;
418 SmallVector<wchar_t, 128> PathUtf16;
420 if (UTF8ToUTF16(Path.toStringRef(PathStorage), PathUtf16))
423 DWORD Attr = ::GetFileAttributesW(PathUtf16.begin());
424 return Attr != INVALID_FILE_ATTRIBUTES;
427 bool equivalent(file_status A, file_status B) {
428 assert(status_known(A) && status_known(B));
429 return A.FileIndexHigh == B.FileIndexHigh &&
430 A.FileIndexLow == B.FileIndexLow &&
431 A.FileSizeHigh == B.FileSizeHigh &&
432 A.FileSizeLow == B.FileSizeLow &&
433 A.LastWriteTimeHigh == B.LastWriteTimeHigh &&
434 A.LastWriteTimeLow == B.LastWriteTimeLow &&
435 A.VolumeSerialNumber == B.VolumeSerialNumber;
438 error_code equivalent(const Twine &A, const Twine &B, bool &result) {
439 file_status fsA, fsB;
440 if (error_code ec = status(A, fsA)) return ec;
441 if (error_code ec = status(B, fsB)) return ec;
442 result = equivalent(fsA, fsB);
443 return error_code::success();
446 static bool isReservedName(StringRef path) {
447 // This list of reserved names comes from MSDN, at:
448 // http://msdn.microsoft.com/en-us/library/aa365247%28v=vs.85%29.aspx
449 static const char *sReservedNames[] = { "nul", "con", "prn", "aux",
450 "com1", "com2", "com3", "com4", "com5", "com6",
451 "com7", "com8", "com9", "lpt1", "lpt2", "lpt3",
452 "lpt4", "lpt5", "lpt6", "lpt7", "lpt8", "lpt9" };
454 // First, check to see if this is a device namespace, which always
455 // starts with \\.\, since device namespaces are not legal file paths.
456 if (path.startswith("\\\\.\\"))
459 // Then compare against the list of ancient reserved names
460 for (size_t i = 0; i < array_lengthof(sReservedNames); ++i) {
461 if (path.equals_lower(sReservedNames[i]))
465 // The path isn't what we consider reserved.
469 static error_code getStatus(HANDLE FileHandle, file_status &Result) {
470 if (FileHandle == INVALID_HANDLE_VALUE)
471 goto handle_status_error;
473 switch (::GetFileType(FileHandle)) {
475 llvm_unreachable("Don't know anything about this file type");
476 case FILE_TYPE_UNKNOWN: {
477 DWORD Err = ::GetLastError();
479 return windows_error(Err);
480 Result = file_status(file_type::type_unknown);
481 return error_code::success();
486 Result = file_status(file_type::character_file);
487 return error_code::success();
489 Result = file_status(file_type::fifo_file);
490 return error_code::success();
493 BY_HANDLE_FILE_INFORMATION Info;
494 if (!::GetFileInformationByHandle(FileHandle, &Info))
495 goto handle_status_error;
498 file_type Type = (Info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
499 ? file_type::directory_file
500 : file_type::regular_file;
502 file_status(Type, Info.ftLastWriteTime.dwHighDateTime,
503 Info.ftLastWriteTime.dwLowDateTime,
504 Info.dwVolumeSerialNumber, Info.nFileSizeHigh,
505 Info.nFileSizeLow, Info.nFileIndexHigh, Info.nFileIndexLow);
506 return error_code::success();
510 error_code EC = windows_error(::GetLastError());
511 if (EC == windows_error::file_not_found ||
512 EC == windows_error::path_not_found)
513 Result = file_status(file_type::file_not_found);
514 else if (EC == windows_error::sharing_violation)
515 Result = file_status(file_type::type_unknown);
517 Result = file_status(file_type::status_error);
521 error_code status(const Twine &path, file_status &result) {
522 SmallString<128> path_storage;
523 SmallVector<wchar_t, 128> path_utf16;
525 StringRef path8 = path.toStringRef(path_storage);
526 if (isReservedName(path8)) {
527 result = file_status(file_type::character_file);
528 return error_code::success();
531 if (error_code ec = UTF8ToUTF16(path8, path_utf16))
534 DWORD attr = ::GetFileAttributesW(path_utf16.begin());
535 if (attr == INVALID_FILE_ATTRIBUTES)
536 return getStatus(INVALID_HANDLE_VALUE, result);
538 // Handle reparse points.
539 if (attr & FILE_ATTRIBUTE_REPARSE_POINT) {
541 ::CreateFileW(path_utf16.begin(),
542 0, // Attributes only.
543 FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
546 FILE_FLAG_BACKUP_SEMANTICS,
549 return getStatus(INVALID_HANDLE_VALUE, result);
553 ::CreateFileW(path_utf16.begin(), 0, // Attributes only.
554 FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
555 NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0));
557 return getStatus(INVALID_HANDLE_VALUE, result);
559 return getStatus(h, result);
562 error_code status(int FD, file_status &Result) {
563 HANDLE FileHandle = reinterpret_cast<HANDLE>(_get_osfhandle(FD));
564 return getStatus(FileHandle, Result);
567 error_code setLastModificationAndAccessTime(int FD, TimeValue Time) {
569 UI.QuadPart = Time.toWin32Time();
571 FT.dwLowDateTime = UI.LowPart;
572 FT.dwHighDateTime = UI.HighPart;
573 HANDLE FileHandle = reinterpret_cast<HANDLE>(_get_osfhandle(FD));
574 if (!SetFileTime(FileHandle, NULL, &FT, &FT))
575 return windows_error(::GetLastError());
576 return error_code::success();
579 error_code get_magic(const Twine &path, uint32_t len,
580 SmallVectorImpl<char> &result) {
581 SmallString<128> path_storage;
582 SmallVector<wchar_t, 128> path_utf16;
585 // Convert path to UTF-16.
586 if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage),
591 HANDLE file = ::CreateFileW(c_str(path_utf16),
596 FILE_ATTRIBUTE_READONLY,
598 if (file == INVALID_HANDLE_VALUE)
599 return windows_error(::GetLastError());
605 DWORD bytes_read = 0;
606 BOOL read_success = ::ReadFile(file, result.data(), len, &bytes_read, NULL);
607 error_code ec = windows_error(::GetLastError());
609 if (!read_success || (bytes_read != len)) {
610 // Set result size to the number of bytes read if it's valid.
611 if (bytes_read <= len)
612 result.set_size(bytes_read);
613 // ERROR_HANDLE_EOF is mapped to errc::value_too_large.
617 result.set_size(len);
618 return error_code::success();
621 error_code mapped_file_region::init(int FD, bool CloseFD, uint64_t Offset) {
623 // Make sure that the requested size fits within SIZE_T.
624 if (Size > std::numeric_limits<SIZE_T>::max()) {
625 if (FileDescriptor) {
627 _close(FileDescriptor);
629 ::CloseHandle(FileHandle);
630 return make_error_code(errc::invalid_argument);
635 case readonly: flprotect = PAGE_READONLY; break;
636 case readwrite: flprotect = PAGE_READWRITE; break;
637 case priv: flprotect = PAGE_WRITECOPY; break;
641 ::CreateFileMappingW(FileHandle, 0, flprotect,
642 (Offset + Size) >> 32,
643 (Offset + Size) & 0xffffffff,
645 if (FileMappingHandle == NULL) {
646 error_code ec = windows_error(GetLastError());
647 if (FileDescriptor) {
649 _close(FileDescriptor);
651 ::CloseHandle(FileHandle);
655 DWORD dwDesiredAccess;
657 case readonly: dwDesiredAccess = FILE_MAP_READ; break;
658 case readwrite: dwDesiredAccess = FILE_MAP_WRITE; break;
659 case priv: dwDesiredAccess = FILE_MAP_COPY; break;
661 Mapping = ::MapViewOfFile(FileMappingHandle,
666 if (Mapping == NULL) {
667 error_code ec = windows_error(GetLastError());
668 ::CloseHandle(FileMappingHandle);
669 if (FileDescriptor) {
671 _close(FileDescriptor);
673 ::CloseHandle(FileHandle);
678 MEMORY_BASIC_INFORMATION mbi;
679 SIZE_T Result = VirtualQuery(Mapping, &mbi, sizeof(mbi));
681 error_code ec = windows_error(GetLastError());
682 ::UnmapViewOfFile(Mapping);
683 ::CloseHandle(FileMappingHandle);
684 if (FileDescriptor) {
686 _close(FileDescriptor);
688 ::CloseHandle(FileHandle);
691 Size = mbi.RegionSize;
694 // Close all the handles except for the view. It will keep the other handles
696 ::CloseHandle(FileMappingHandle);
697 if (FileDescriptor) {
699 _close(FileDescriptor); // Also closes FileHandle.
701 ::CloseHandle(FileHandle);
702 return error_code::success();
705 mapped_file_region::mapped_file_region(const Twine &path,
714 , FileHandle(INVALID_HANDLE_VALUE)
715 , FileMappingHandle() {
716 SmallString<128> path_storage;
717 SmallVector<wchar_t, 128> path_utf16;
719 // Convert path to UTF-16.
720 if ((ec = UTF8ToUTF16(path.toStringRef(path_storage), path_utf16)))
723 // Get file handle for creating a file mapping.
724 FileHandle = ::CreateFileW(c_str(path_utf16),
725 Mode == readonly ? GENERIC_READ
726 : GENERIC_READ | GENERIC_WRITE,
727 Mode == readonly ? FILE_SHARE_READ
730 Mode == readonly ? OPEN_EXISTING
732 Mode == readonly ? FILE_ATTRIBUTE_READONLY
733 : FILE_ATTRIBUTE_NORMAL,
735 if (FileHandle == INVALID_HANDLE_VALUE) {
736 ec = windows_error(::GetLastError());
741 ec = init(FileDescriptor, true, offset);
743 Mapping = FileMappingHandle = 0;
744 FileHandle = INVALID_HANDLE_VALUE;
749 mapped_file_region::mapped_file_region(int fd,
759 , FileHandle(INVALID_HANDLE_VALUE)
760 , FileMappingHandle() {
761 FileHandle = reinterpret_cast<HANDLE>(_get_osfhandle(fd));
762 if (FileHandle == INVALID_HANDLE_VALUE) {
764 _close(FileDescriptor);
766 ec = make_error_code(errc::bad_file_descriptor);
770 ec = init(FileDescriptor, closefd, offset);
772 Mapping = FileMappingHandle = 0;
773 FileHandle = INVALID_HANDLE_VALUE;
778 mapped_file_region::~mapped_file_region() {
780 ::UnmapViewOfFile(Mapping);
783 #if LLVM_HAS_RVALUE_REFERENCES
784 mapped_file_region::mapped_file_region(mapped_file_region &&other)
787 , Mapping(other.Mapping)
788 , FileDescriptor(other.FileDescriptor)
789 , FileHandle(other.FileHandle)
790 , FileMappingHandle(other.FileMappingHandle) {
791 other.Mapping = other.FileMappingHandle = 0;
792 other.FileHandle = INVALID_HANDLE_VALUE;
793 other.FileDescriptor = 0;
797 mapped_file_region::mapmode mapped_file_region::flags() const {
798 assert(Mapping && "Mapping failed but used anyway!");
802 uint64_t mapped_file_region::size() const {
803 assert(Mapping && "Mapping failed but used anyway!");
807 char *mapped_file_region::data() const {
808 assert(Mode != readonly && "Cannot get non-const data for readonly mapping!");
809 assert(Mapping && "Mapping failed but used anyway!");
810 return reinterpret_cast<char*>(Mapping);
813 const char *mapped_file_region::const_data() const {
814 assert(Mapping && "Mapping failed but used anyway!");
815 return reinterpret_cast<const char*>(Mapping);
818 int mapped_file_region::alignment() {
820 ::GetSystemInfo(&SysInfo);
821 return SysInfo.dwAllocationGranularity;
824 error_code detail::directory_iterator_construct(detail::DirIterState &it,
826 SmallVector<wchar_t, 128> path_utf16;
828 if (error_code ec = UTF8ToUTF16(path,
832 // Convert path to the format that Windows is happy with.
833 if (path_utf16.size() > 0 &&
834 !is_separator(path_utf16[path.size() - 1]) &&
835 path_utf16[path.size() - 1] != L':') {
836 path_utf16.push_back(L'\\');
837 path_utf16.push_back(L'*');
839 path_utf16.push_back(L'*');
842 // Get the first directory entry.
843 WIN32_FIND_DATAW FirstFind;
844 ScopedFindHandle FindHandle(::FindFirstFileW(c_str(path_utf16), &FirstFind));
846 return windows_error(::GetLastError());
848 size_t FilenameLen = ::wcslen(FirstFind.cFileName);
849 while ((FilenameLen == 1 && FirstFind.cFileName[0] == L'.') ||
850 (FilenameLen == 2 && FirstFind.cFileName[0] == L'.' &&
851 FirstFind.cFileName[1] == L'.'))
852 if (!::FindNextFileW(FindHandle, &FirstFind)) {
853 error_code ec = windows_error(::GetLastError());
855 if (ec == windows_error::no_more_files)
856 return detail::directory_iterator_destruct(it);
859 FilenameLen = ::wcslen(FirstFind.cFileName);
861 // Construct the current directory entry.
862 SmallString<128> directory_entry_name_utf8;
863 if (error_code ec = UTF16ToUTF8(FirstFind.cFileName,
864 ::wcslen(FirstFind.cFileName),
865 directory_entry_name_utf8))
868 it.IterationHandle = intptr_t(FindHandle.take());
869 SmallString<128> directory_entry_path(path);
870 path::append(directory_entry_path, directory_entry_name_utf8.str());
871 it.CurrentEntry = directory_entry(directory_entry_path.str());
873 return error_code::success();
876 error_code detail::directory_iterator_destruct(detail::DirIterState &it) {
877 if (it.IterationHandle != 0)
878 // Closes the handle if it's valid.
879 ScopedFindHandle close(HANDLE(it.IterationHandle));
880 it.IterationHandle = 0;
881 it.CurrentEntry = directory_entry();
882 return error_code::success();
885 error_code detail::directory_iterator_increment(detail::DirIterState &it) {
886 WIN32_FIND_DATAW FindData;
887 if (!::FindNextFileW(HANDLE(it.IterationHandle), &FindData)) {
888 error_code ec = windows_error(::GetLastError());
890 if (ec == windows_error::no_more_files)
891 return detail::directory_iterator_destruct(it);
895 size_t FilenameLen = ::wcslen(FindData.cFileName);
896 if ((FilenameLen == 1 && FindData.cFileName[0] == L'.') ||
897 (FilenameLen == 2 && FindData.cFileName[0] == L'.' &&
898 FindData.cFileName[1] == L'.'))
899 return directory_iterator_increment(it);
901 SmallString<128> directory_entry_path_utf8;
902 if (error_code ec = UTF16ToUTF8(FindData.cFileName,
903 ::wcslen(FindData.cFileName),
904 directory_entry_path_utf8))
907 it.CurrentEntry.replace_filename(Twine(directory_entry_path_utf8));
908 return error_code::success();
911 error_code map_file_pages(const Twine &path, off_t file_offset, size_t size,
912 bool map_writable, void *&result) {
913 assert(0 && "NOT IMPLEMENTED");
914 return windows_error::invalid_function;
917 error_code unmap_file_pages(void *base, size_t size) {
918 assert(0 && "NOT IMPLEMENTED");
919 return windows_error::invalid_function;
922 error_code openFileForRead(const Twine &Name, int &ResultFD) {
923 SmallString<128> PathStorage;
924 SmallVector<wchar_t, 128> PathUTF16;
926 if (error_code EC = UTF8ToUTF16(Name.toStringRef(PathStorage),
930 HANDLE H = ::CreateFileW(PathUTF16.begin(), GENERIC_READ,
931 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
932 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
933 if (H == INVALID_HANDLE_VALUE) {
934 error_code EC = windows_error(::GetLastError());
935 // Provide a better error message when trying to open directories.
936 // This only runs if we failed to open the file, so there is probably
937 // no performances issues.
938 if (EC != windows_error::access_denied)
940 if (is_directory(Name))
941 return error_code(errc::is_a_directory, posix_category());
945 int FD = ::_open_osfhandle(intptr_t(H), 0);
948 return windows_error::invalid_handle;
952 return error_code::success();
955 error_code openFileForWrite(const Twine &Name, int &ResultFD,
956 sys::fs::OpenFlags Flags, unsigned Mode) {
957 // Verify that we don't have both "append" and "excl".
958 assert((!(Flags & sys::fs::F_Excl) || !(Flags & sys::fs::F_Append)) &&
959 "Cannot specify both 'excl' and 'append' file creation flags!");
961 SmallString<128> PathStorage;
962 SmallVector<wchar_t, 128> PathUTF16;
964 if (error_code EC = UTF8ToUTF16(Name.toStringRef(PathStorage),
968 DWORD CreationDisposition;
970 CreationDisposition = CREATE_NEW;
971 else if (Flags & F_Append)
972 CreationDisposition = OPEN_ALWAYS;
974 CreationDisposition = CREATE_ALWAYS;
976 HANDLE H = ::CreateFileW(PathUTF16.begin(), GENERIC_WRITE,
977 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
978 CreationDisposition, FILE_ATTRIBUTE_NORMAL, NULL);
980 if (H == INVALID_HANDLE_VALUE) {
981 error_code EC = windows_error(::GetLastError());
982 // Provide a better error message when trying to open directories.
983 // This only runs if we failed to open the file, so there is probably
984 // no performances issues.
985 if (EC != windows_error::access_denied)
987 if (is_directory(Name))
988 return error_code(errc::is_a_directory, posix_category());
993 if (Flags & F_Append)
994 OpenFlags |= _O_APPEND;
996 if (!(Flags & F_Binary))
997 OpenFlags |= _O_TEXT;
999 int FD = ::_open_osfhandle(intptr_t(H), OpenFlags);
1002 return windows_error::invalid_handle;
1006 return error_code::success();
1008 } // end namespace fs
1012 bool home_directory(SmallVectorImpl<char> &result) {
1013 wchar_t Path[MAX_PATH];
1014 if (::SHGetFolderPathW(0, CSIDL_APPDATA | CSIDL_FLAG_CREATE, 0,
1015 /*SHGFP_TYPE_CURRENT*/0, Path) != S_OK)
1018 if (UTF16ToUTF8(Path, ::wcslen(Path), result))
1024 } // end namespace path
1027 llvm::error_code UTF8ToUTF16(llvm::StringRef utf8,
1028 llvm::SmallVectorImpl<wchar_t> &utf16) {
1029 if (!utf8.empty()) {
1030 int len = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, utf8.begin(),
1031 utf8.size(), utf16.begin(), 0);
1034 return llvm::windows_error(::GetLastError());
1036 utf16.reserve(len + 1);
1037 utf16.set_size(len);
1039 len = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, utf8.begin(),
1040 utf8.size(), utf16.begin(), utf16.size());
1043 return llvm::windows_error(::GetLastError());
1046 // Make utf16 null terminated.
1050 return llvm::error_code::success();
1053 llvm::error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len,
1054 llvm::SmallVectorImpl<char> &utf8) {
1057 int len = ::WideCharToMultiByte(CP_UTF8, 0, utf16, utf16_len, utf8.begin(),
1061 return llvm::windows_error(::GetLastError());
1066 // Now do the actual conversion.
1067 len = ::WideCharToMultiByte(CP_UTF8, 0, utf16, utf16_len, utf8.data(),
1068 utf8.size(), NULL, NULL);
1071 return llvm::windows_error(::GetLastError());
1074 // Make utf8 null terminated.
1078 return llvm::error_code::success();
1080 } // end namespace windows
1081 } // end namespace sys
1082 } // end namespace llvm