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 std::error_code TempDir(SmallVectorImpl<char> &Result) {
53 SmallVector<wchar_t, 64> Res;
55 DWORD Len = ::GetTempPathW(Res.capacity(), Res.begin());
58 return windows_error(::GetLastError());
60 if (Len > Res.capacity()) {
66 return UTF16ToUTF8(Res.begin(), Res.size(), Result);
69 static bool is_separator(const wchar_t value) {
83 std::string getMainExecutable(const char *argv0, void *MainExecAddr) {
84 SmallVector<wchar_t, MAX_PATH> PathName;
85 DWORD Size = ::GetModuleFileNameW(NULL, PathName.data(), PathName.capacity());
87 // A zero return value indicates a failure other than insufficient space.
91 // Insufficient space is determined by a return value equal to the size of
92 // the buffer passed in.
93 if (Size == PathName.capacity())
96 // On success, GetModuleFileNameW returns the number of characters written to
97 // the buffer not including the NULL terminator.
98 PathName.set_size(Size);
100 // Convert the result from UTF-16 to UTF-8.
101 SmallVector<char, MAX_PATH> PathNameUTF8;
102 if (UTF16ToUTF8(PathName.data(), PathName.size(), PathNameUTF8))
105 return std::string(PathNameUTF8.data());
108 UniqueID file_status::getUniqueID() const {
109 // The file is uniquely identified by the volume serial number along
110 // with the 64-bit file identifier.
111 uint64_t FileID = (static_cast<uint64_t>(FileIndexHigh) << 32ULL) |
112 static_cast<uint64_t>(FileIndexLow);
114 return UniqueID(VolumeSerialNumber, FileID);
117 TimeValue file_status::getLastModificationTime() const {
119 UI.LowPart = LastWriteTimeLow;
120 UI.HighPart = LastWriteTimeHigh;
123 Ret.fromWin32Time(UI.QuadPart);
127 std::error_code current_path(SmallVectorImpl<char> &result) {
128 SmallVector<wchar_t, MAX_PATH> cur_path;
129 DWORD len = MAX_PATH;
132 cur_path.reserve(len);
133 len = ::GetCurrentDirectoryW(cur_path.capacity(), cur_path.data());
135 // A zero return value indicates a failure other than insufficient space.
137 return windows_error(::GetLastError());
139 // If there's insufficient space, the len returned is larger than the len
141 } while (len > cur_path.capacity());
143 // On success, GetCurrentDirectoryW returns the number of characters not
144 // including the null-terminator.
145 cur_path.set_size(len);
146 return UTF16ToUTF8(cur_path.begin(), cur_path.size(), result);
149 std::error_code create_directory(const Twine &path, bool IgnoreExisting) {
150 SmallString<128> path_storage;
151 SmallVector<wchar_t, 128> path_utf16;
153 if (std::error_code ec =
154 UTF8ToUTF16(path.toStringRef(path_storage), path_utf16))
157 if (!::CreateDirectoryW(path_utf16.begin(), NULL)) {
158 DWORD LastError = ::GetLastError();
159 if (LastError != ERROR_ALREADY_EXISTS || !IgnoreExisting)
160 return windows_error(LastError);
163 return std::error_code();
166 std::error_code normalize_separators(SmallVectorImpl<char> &Path) {
168 return std::error_code();
171 // We can't use symbolic links for windows.
172 std::error_code create_link(const Twine &to, const Twine &from) {
174 SmallString<128> from_storage;
175 SmallString<128> to_storage;
176 StringRef f = from.toStringRef(from_storage);
177 StringRef t = to.toStringRef(to_storage);
179 // Convert to utf-16.
180 SmallVector<wchar_t, 128> wide_from;
181 SmallVector<wchar_t, 128> wide_to;
182 if (std::error_code ec = UTF8ToUTF16(f, wide_from))
184 if (std::error_code ec = UTF8ToUTF16(t, wide_to))
187 if (!::CreateHardLinkW(wide_from.begin(), wide_to.begin(), NULL))
188 return windows_error(::GetLastError());
190 return std::error_code();
193 std::error_code remove(const Twine &path, bool IgnoreNonExisting) {
194 SmallString<128> path_storage;
195 SmallVector<wchar_t, 128> path_utf16;
198 if (std::error_code EC = status(path, ST)) {
199 if (EC != std::errc::no_such_file_or_directory || !IgnoreNonExisting)
201 return std::error_code();
204 if (std::error_code ec =
205 UTF8ToUTF16(path.toStringRef(path_storage), path_utf16))
208 if (ST.type() == file_type::directory_file) {
209 if (!::RemoveDirectoryW(c_str(path_utf16))) {
210 std::error_code EC = windows_error(::GetLastError());
211 if (EC != std::errc::no_such_file_or_directory || !IgnoreNonExisting)
214 return std::error_code();
216 if (!::DeleteFileW(c_str(path_utf16))) {
217 std::error_code EC = windows_error(::GetLastError());
218 if (EC != std::errc::no_such_file_or_directory || !IgnoreNonExisting)
221 return std::error_code();
224 std::error_code rename(const Twine &from, const Twine &to) {
226 SmallString<128> from_storage;
227 SmallString<128> to_storage;
228 StringRef f = from.toStringRef(from_storage);
229 StringRef t = to.toStringRef(to_storage);
231 // Convert to utf-16.
232 SmallVector<wchar_t, 128> wide_from;
233 SmallVector<wchar_t, 128> wide_to;
234 if (std::error_code ec = UTF8ToUTF16(f, wide_from))
236 if (std::error_code ec = UTF8ToUTF16(t, wide_to))
239 std::error_code ec = std::error_code();
240 for (int i = 0; i < 2000; i++) {
241 if (::MoveFileExW(wide_from.begin(), wide_to.begin(),
242 MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING))
243 return std::error_code();
244 DWORD LastError = ::GetLastError();
245 if (LastError != ERROR_ACCESS_DENIED)
247 // Retry MoveFile() at ACCESS_DENIED.
248 // System scanners (eg. indexer) might open the source file when
249 // It is written and closed.
256 std::error_code resize_file(const Twine &path, uint64_t size) {
257 SmallString<128> path_storage;
258 SmallVector<wchar_t, 128> path_utf16;
260 if (std::error_code ec =
261 UTF8ToUTF16(path.toStringRef(path_storage), path_utf16))
264 int fd = ::_wopen(path_utf16.begin(), O_BINARY | _O_RDWR, S_IWRITE);
266 return std::error_code(errno, std::generic_category());
267 #ifdef HAVE__CHSIZE_S
268 errno_t error = ::_chsize_s(fd, size);
270 errno_t error = ::_chsize(fd, size);
273 return std::error_code(error, std::generic_category());
276 std::error_code exists(const Twine &path, bool &result) {
277 SmallString<128> path_storage;
278 SmallVector<wchar_t, 128> path_utf16;
280 if (std::error_code ec =
281 UTF8ToUTF16(path.toStringRef(path_storage), path_utf16))
284 DWORD attributes = ::GetFileAttributesW(path_utf16.begin());
286 if (attributes == INVALID_FILE_ATTRIBUTES) {
287 // See if the file didn't actually exist.
288 DWORD LastError = ::GetLastError();
289 if (LastError != ERROR_FILE_NOT_FOUND &&
290 LastError != ERROR_PATH_NOT_FOUND)
291 return windows_error(LastError);
295 return std::error_code();
298 bool can_write(const Twine &Path) {
299 // FIXME: take security attributes into account.
300 SmallString<128> PathStorage;
301 SmallVector<wchar_t, 128> PathUtf16;
303 if (UTF8ToUTF16(Path.toStringRef(PathStorage), PathUtf16))
306 DWORD Attr = ::GetFileAttributesW(PathUtf16.begin());
307 return (Attr != INVALID_FILE_ATTRIBUTES) && !(Attr & FILE_ATTRIBUTE_READONLY);
310 bool can_execute(const Twine &Path) {
311 SmallString<128> PathStorage;
312 SmallVector<wchar_t, 128> PathUtf16;
314 if (UTF8ToUTF16(Path.toStringRef(PathStorage), PathUtf16))
317 DWORD Attr = ::GetFileAttributesW(PathUtf16.begin());
318 return Attr != INVALID_FILE_ATTRIBUTES;
321 bool equivalent(file_status A, file_status B) {
322 assert(status_known(A) && status_known(B));
323 return A.FileIndexHigh == B.FileIndexHigh &&
324 A.FileIndexLow == B.FileIndexLow &&
325 A.FileSizeHigh == B.FileSizeHigh &&
326 A.FileSizeLow == B.FileSizeLow &&
327 A.LastWriteTimeHigh == B.LastWriteTimeHigh &&
328 A.LastWriteTimeLow == B.LastWriteTimeLow &&
329 A.VolumeSerialNumber == B.VolumeSerialNumber;
332 std::error_code equivalent(const Twine &A, const Twine &B, bool &result) {
333 file_status fsA, fsB;
334 if (std::error_code ec = status(A, fsA))
336 if (std::error_code ec = status(B, fsB))
338 result = equivalent(fsA, fsB);
339 return std::error_code();
342 static bool isReservedName(StringRef path) {
343 // This list of reserved names comes from MSDN, at:
344 // http://msdn.microsoft.com/en-us/library/aa365247%28v=vs.85%29.aspx
345 static const char *sReservedNames[] = { "nul", "con", "prn", "aux",
346 "com1", "com2", "com3", "com4", "com5", "com6",
347 "com7", "com8", "com9", "lpt1", "lpt2", "lpt3",
348 "lpt4", "lpt5", "lpt6", "lpt7", "lpt8", "lpt9" };
350 // First, check to see if this is a device namespace, which always
351 // starts with \\.\, since device namespaces are not legal file paths.
352 if (path.startswith("\\\\.\\"))
355 // Then compare against the list of ancient reserved names
356 for (size_t i = 0; i < array_lengthof(sReservedNames); ++i) {
357 if (path.equals_lower(sReservedNames[i]))
361 // The path isn't what we consider reserved.
365 static std::error_code getStatus(HANDLE FileHandle, file_status &Result) {
366 if (FileHandle == INVALID_HANDLE_VALUE)
367 goto handle_status_error;
369 switch (::GetFileType(FileHandle)) {
371 llvm_unreachable("Don't know anything about this file type");
372 case FILE_TYPE_UNKNOWN: {
373 DWORD Err = ::GetLastError();
375 return windows_error(Err);
376 Result = file_status(file_type::type_unknown);
377 return std::error_code();
382 Result = file_status(file_type::character_file);
383 return std::error_code();
385 Result = file_status(file_type::fifo_file);
386 return std::error_code();
389 BY_HANDLE_FILE_INFORMATION Info;
390 if (!::GetFileInformationByHandle(FileHandle, &Info))
391 goto handle_status_error;
394 file_type Type = (Info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
395 ? file_type::directory_file
396 : file_type::regular_file;
398 file_status(Type, Info.ftLastWriteTime.dwHighDateTime,
399 Info.ftLastWriteTime.dwLowDateTime,
400 Info.dwVolumeSerialNumber, Info.nFileSizeHigh,
401 Info.nFileSizeLow, Info.nFileIndexHigh, Info.nFileIndexLow);
402 return std::error_code();
406 DWORD LastError = ::GetLastError();
407 if (LastError == ERROR_FILE_NOT_FOUND ||
408 LastError == ERROR_PATH_NOT_FOUND)
409 Result = file_status(file_type::file_not_found);
410 else if (LastError == ERROR_SHARING_VIOLATION)
411 Result = file_status(file_type::type_unknown);
413 Result = file_status(file_type::status_error);
414 return windows_error(LastError);
417 std::error_code status(const Twine &path, file_status &result) {
418 SmallString<128> path_storage;
419 SmallVector<wchar_t, 128> path_utf16;
421 StringRef path8 = path.toStringRef(path_storage);
422 if (isReservedName(path8)) {
423 result = file_status(file_type::character_file);
424 return std::error_code();
427 if (std::error_code ec = UTF8ToUTF16(path8, path_utf16))
430 DWORD attr = ::GetFileAttributesW(path_utf16.begin());
431 if (attr == INVALID_FILE_ATTRIBUTES)
432 return getStatus(INVALID_HANDLE_VALUE, result);
434 // Handle reparse points.
435 if (attr & FILE_ATTRIBUTE_REPARSE_POINT) {
437 ::CreateFileW(path_utf16.begin(),
438 0, // Attributes only.
439 FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
442 FILE_FLAG_BACKUP_SEMANTICS,
445 return getStatus(INVALID_HANDLE_VALUE, result);
449 ::CreateFileW(path_utf16.begin(), 0, // Attributes only.
450 FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
451 NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0));
453 return getStatus(INVALID_HANDLE_VALUE, result);
455 return getStatus(h, result);
458 std::error_code status(int FD, file_status &Result) {
459 HANDLE FileHandle = reinterpret_cast<HANDLE>(_get_osfhandle(FD));
460 return getStatus(FileHandle, Result);
463 std::error_code setLastModificationAndAccessTime(int FD, TimeValue Time) {
465 UI.QuadPart = Time.toWin32Time();
467 FT.dwLowDateTime = UI.LowPart;
468 FT.dwHighDateTime = UI.HighPart;
469 HANDLE FileHandle = reinterpret_cast<HANDLE>(_get_osfhandle(FD));
470 if (!SetFileTime(FileHandle, NULL, &FT, &FT))
471 return windows_error(::GetLastError());
472 return std::error_code();
475 std::error_code mapped_file_region::init(int FD, bool CloseFD, uint64_t Offset) {
477 // Make sure that the requested size fits within SIZE_T.
478 if (Size > std::numeric_limits<SIZE_T>::max()) {
479 if (FileDescriptor) {
481 _close(FileDescriptor);
483 ::CloseHandle(FileHandle);
484 return std::make_error_code(std::errc::invalid_argument);
489 case readonly: flprotect = PAGE_READONLY; break;
490 case readwrite: flprotect = PAGE_READWRITE; break;
491 case priv: flprotect = PAGE_WRITECOPY; break;
495 ::CreateFileMappingW(FileHandle, 0, flprotect,
496 (Offset + Size) >> 32,
497 (Offset + Size) & 0xffffffff,
499 if (FileMappingHandle == NULL) {
500 std::error_code ec = windows_error(GetLastError());
501 if (FileDescriptor) {
503 _close(FileDescriptor);
505 ::CloseHandle(FileHandle);
509 DWORD dwDesiredAccess;
511 case readonly: dwDesiredAccess = FILE_MAP_READ; break;
512 case readwrite: dwDesiredAccess = FILE_MAP_WRITE; break;
513 case priv: dwDesiredAccess = FILE_MAP_COPY; break;
515 Mapping = ::MapViewOfFile(FileMappingHandle,
520 if (Mapping == NULL) {
521 std::error_code ec = windows_error(GetLastError());
522 ::CloseHandle(FileMappingHandle);
523 if (FileDescriptor) {
525 _close(FileDescriptor);
527 ::CloseHandle(FileHandle);
532 MEMORY_BASIC_INFORMATION mbi;
533 SIZE_T Result = VirtualQuery(Mapping, &mbi, sizeof(mbi));
535 std::error_code ec = windows_error(GetLastError());
536 ::UnmapViewOfFile(Mapping);
537 ::CloseHandle(FileMappingHandle);
538 if (FileDescriptor) {
540 _close(FileDescriptor);
542 ::CloseHandle(FileHandle);
545 Size = mbi.RegionSize;
548 // Close all the handles except for the view. It will keep the other handles
550 ::CloseHandle(FileMappingHandle);
551 if (FileDescriptor) {
553 _close(FileDescriptor); // Also closes FileHandle.
555 ::CloseHandle(FileHandle);
556 return std::error_code();
559 mapped_file_region::mapped_file_region(const Twine &path,
568 , FileHandle(INVALID_HANDLE_VALUE)
569 , FileMappingHandle() {
570 SmallString<128> path_storage;
571 SmallVector<wchar_t, 128> path_utf16;
573 // Convert path to UTF-16.
574 if ((ec = UTF8ToUTF16(path.toStringRef(path_storage), path_utf16)))
577 // Get file handle for creating a file mapping.
578 FileHandle = ::CreateFileW(c_str(path_utf16),
579 Mode == readonly ? GENERIC_READ
580 : GENERIC_READ | GENERIC_WRITE,
581 Mode == readonly ? FILE_SHARE_READ
584 Mode == readonly ? OPEN_EXISTING
586 Mode == readonly ? FILE_ATTRIBUTE_READONLY
587 : FILE_ATTRIBUTE_NORMAL,
589 if (FileHandle == INVALID_HANDLE_VALUE) {
590 ec = windows_error(::GetLastError());
595 ec = init(FileDescriptor, true, offset);
597 Mapping = FileMappingHandle = 0;
598 FileHandle = INVALID_HANDLE_VALUE;
603 mapped_file_region::mapped_file_region(int fd,
613 , FileHandle(INVALID_HANDLE_VALUE)
614 , FileMappingHandle() {
615 FileHandle = reinterpret_cast<HANDLE>(_get_osfhandle(fd));
616 if (FileHandle == INVALID_HANDLE_VALUE) {
618 _close(FileDescriptor);
620 ec = std::make_error_code(std::errc::bad_file_descriptor);
624 ec = init(FileDescriptor, closefd, offset);
626 Mapping = FileMappingHandle = 0;
627 FileHandle = INVALID_HANDLE_VALUE;
632 mapped_file_region::~mapped_file_region() {
634 ::UnmapViewOfFile(Mapping);
637 mapped_file_region::mapped_file_region(mapped_file_region &&other)
640 , Mapping(other.Mapping)
641 , FileDescriptor(other.FileDescriptor)
642 , FileHandle(other.FileHandle)
643 , FileMappingHandle(other.FileMappingHandle) {
644 other.Mapping = other.FileMappingHandle = 0;
645 other.FileHandle = INVALID_HANDLE_VALUE;
646 other.FileDescriptor = 0;
649 mapped_file_region::mapmode mapped_file_region::flags() const {
650 assert(Mapping && "Mapping failed but used anyway!");
654 uint64_t mapped_file_region::size() const {
655 assert(Mapping && "Mapping failed but used anyway!");
659 char *mapped_file_region::data() const {
660 assert(Mode != readonly && "Cannot get non-const data for readonly mapping!");
661 assert(Mapping && "Mapping failed but used anyway!");
662 return reinterpret_cast<char*>(Mapping);
665 const char *mapped_file_region::const_data() const {
666 assert(Mapping && "Mapping failed but used anyway!");
667 return reinterpret_cast<const char*>(Mapping);
670 int mapped_file_region::alignment() {
672 ::GetSystemInfo(&SysInfo);
673 return SysInfo.dwAllocationGranularity;
676 std::error_code detail::directory_iterator_construct(detail::DirIterState &it,
678 SmallVector<wchar_t, 128> path_utf16;
680 if (std::error_code ec = UTF8ToUTF16(path, path_utf16))
683 // Convert path to the format that Windows is happy with.
684 if (path_utf16.size() > 0 &&
685 !is_separator(path_utf16[path.size() - 1]) &&
686 path_utf16[path.size() - 1] != L':') {
687 path_utf16.push_back(L'\\');
688 path_utf16.push_back(L'*');
690 path_utf16.push_back(L'*');
693 // Get the first directory entry.
694 WIN32_FIND_DATAW FirstFind;
695 ScopedFindHandle FindHandle(::FindFirstFileW(c_str(path_utf16), &FirstFind));
697 return windows_error(::GetLastError());
699 size_t FilenameLen = ::wcslen(FirstFind.cFileName);
700 while ((FilenameLen == 1 && FirstFind.cFileName[0] == L'.') ||
701 (FilenameLen == 2 && FirstFind.cFileName[0] == L'.' &&
702 FirstFind.cFileName[1] == L'.'))
703 if (!::FindNextFileW(FindHandle, &FirstFind)) {
704 DWORD LastError = ::GetLastError();
706 if (LastError == ERROR_NO_MORE_FILES)
707 return detail::directory_iterator_destruct(it);
708 return windows_error(LastError);
710 FilenameLen = ::wcslen(FirstFind.cFileName);
712 // Construct the current directory entry.
713 SmallString<128> directory_entry_name_utf8;
714 if (std::error_code ec =
715 UTF16ToUTF8(FirstFind.cFileName, ::wcslen(FirstFind.cFileName),
716 directory_entry_name_utf8))
719 it.IterationHandle = intptr_t(FindHandle.take());
720 SmallString<128> directory_entry_path(path);
721 path::append(directory_entry_path, directory_entry_name_utf8.str());
722 it.CurrentEntry = directory_entry(directory_entry_path.str());
724 return std::error_code();
727 std::error_code detail::directory_iterator_destruct(detail::DirIterState &it) {
728 if (it.IterationHandle != 0)
729 // Closes the handle if it's valid.
730 ScopedFindHandle close(HANDLE(it.IterationHandle));
731 it.IterationHandle = 0;
732 it.CurrentEntry = directory_entry();
733 return std::error_code();
736 std::error_code detail::directory_iterator_increment(detail::DirIterState &it) {
737 WIN32_FIND_DATAW FindData;
738 if (!::FindNextFileW(HANDLE(it.IterationHandle), &FindData)) {
739 DWORD LastError = ::GetLastError();
741 if (LastError == ERROR_NO_MORE_FILES)
742 return detail::directory_iterator_destruct(it);
743 return windows_error(LastError);
746 size_t FilenameLen = ::wcslen(FindData.cFileName);
747 if ((FilenameLen == 1 && FindData.cFileName[0] == L'.') ||
748 (FilenameLen == 2 && FindData.cFileName[0] == L'.' &&
749 FindData.cFileName[1] == L'.'))
750 return directory_iterator_increment(it);
752 SmallString<128> directory_entry_path_utf8;
753 if (std::error_code ec =
754 UTF16ToUTF8(FindData.cFileName, ::wcslen(FindData.cFileName),
755 directory_entry_path_utf8))
758 it.CurrentEntry.replace_filename(Twine(directory_entry_path_utf8));
759 return std::error_code();
762 std::error_code openFileForRead(const Twine &Name, int &ResultFD) {
763 SmallString<128> PathStorage;
764 SmallVector<wchar_t, 128> PathUTF16;
766 if (std::error_code EC =
767 UTF8ToUTF16(Name.toStringRef(PathStorage), PathUTF16))
770 HANDLE H = ::CreateFileW(PathUTF16.begin(), GENERIC_READ,
771 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
772 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
773 if (H == INVALID_HANDLE_VALUE) {
774 DWORD LastError = ::GetLastError();
775 std::error_code EC = windows_error(LastError);
776 // Provide a better error message when trying to open directories.
777 // This only runs if we failed to open the file, so there is probably
778 // no performances issues.
779 if (LastError != ERROR_ACCESS_DENIED)
781 if (is_directory(Name))
782 return std::make_error_code(std::errc::is_a_directory);
786 int FD = ::_open_osfhandle(intptr_t(H), 0);
789 return windows_error(ERROR_INVALID_HANDLE);
793 return std::error_code();
796 std::error_code openFileForWrite(const Twine &Name, int &ResultFD,
797 sys::fs::OpenFlags Flags, unsigned Mode) {
798 // Verify that we don't have both "append" and "excl".
799 assert((!(Flags & sys::fs::F_Excl) || !(Flags & sys::fs::F_Append)) &&
800 "Cannot specify both 'excl' and 'append' file creation flags!");
802 SmallString<128> PathStorage;
803 SmallVector<wchar_t, 128> PathUTF16;
805 if (std::error_code EC =
806 UTF8ToUTF16(Name.toStringRef(PathStorage), PathUTF16))
809 DWORD CreationDisposition;
811 CreationDisposition = CREATE_NEW;
812 else if (Flags & F_Append)
813 CreationDisposition = OPEN_ALWAYS;
815 CreationDisposition = CREATE_ALWAYS;
817 DWORD Access = GENERIC_WRITE;
819 Access |= GENERIC_READ;
821 HANDLE H = ::CreateFileW(PathUTF16.begin(), Access,
822 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
823 CreationDisposition, FILE_ATTRIBUTE_NORMAL, NULL);
825 if (H == INVALID_HANDLE_VALUE) {
826 DWORD LastError = ::GetLastError();
827 std::error_code EC = windows_error(LastError);
828 // Provide a better error message when trying to open directories.
829 // This only runs if we failed to open the file, so there is probably
830 // no performances issues.
831 if (LastError != ERROR_ACCESS_DENIED)
833 if (is_directory(Name))
834 return std::make_error_code(std::errc::is_a_directory);
839 if (Flags & F_Append)
840 OpenFlags |= _O_APPEND;
843 OpenFlags |= _O_TEXT;
845 int FD = ::_open_osfhandle(intptr_t(H), OpenFlags);
848 return windows_error(ERROR_INVALID_HANDLE);
852 return std::error_code();
854 } // end namespace fs
858 bool home_directory(SmallVectorImpl<char> &result) {
859 wchar_t Path[MAX_PATH];
860 if (::SHGetFolderPathW(0, CSIDL_APPDATA | CSIDL_FLAG_CREATE, 0,
861 /*SHGFP_TYPE_CURRENT*/0, Path) != S_OK)
864 if (UTF16ToUTF8(Path, ::wcslen(Path), result))
870 } // end namespace path
873 std::error_code UTF8ToUTF16(llvm::StringRef utf8,
874 llvm::SmallVectorImpl<wchar_t> &utf16) {
876 int len = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, utf8.begin(),
877 utf8.size(), utf16.begin(), 0);
880 return windows_error(::GetLastError());
882 utf16.reserve(len + 1);
885 len = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, utf8.begin(),
886 utf8.size(), utf16.begin(), utf16.size());
889 return windows_error(::GetLastError());
892 // Make utf16 null terminated.
896 return std::error_code();
899 std::error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len,
900 llvm::SmallVectorImpl<char> &utf8) {
903 int len = ::WideCharToMultiByte(CP_UTF8, 0, utf16, utf16_len, utf8.begin(),
907 return windows_error(::GetLastError());
912 // Now do the actual conversion.
913 len = ::WideCharToMultiByte(CP_UTF8, 0, utf16, utf16_len, utf8.data(),
914 utf8.size(), NULL, NULL);
917 return windows_error(::GetLastError());
920 // Make utf8 null terminated.
924 return std::error_code();
926 } // end namespace windows
927 } // end namespace sys
928 } // end namespace llvm