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"
24 #include <sys/types.h>
28 // MinGW doesn't define this.
29 #ifndef _ERRNO_T_DEFINED
30 #define _ERRNO_T_DEFINED
35 # pragma comment(lib, "advapi32.lib") // This provides CryptAcquireContextW.
41 typedef BOOLEAN (WINAPI *PtrCreateSymbolicLinkW)(
42 /*__in*/ LPCWSTR lpSymlinkFileName,
43 /*__in*/ LPCWSTR lpTargetFileName,
44 /*__in*/ DWORD dwFlags);
46 PtrCreateSymbolicLinkW create_symbolic_link_api = PtrCreateSymbolicLinkW(
47 ::GetProcAddress(::GetModuleHandleA("kernel32.dll"),
48 "CreateSymbolicLinkW"));
50 error_code UTF8ToUTF16(StringRef utf8, SmallVectorImpl<wchar_t> &utf16) {
51 int len = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
52 utf8.begin(), utf8.size(),
56 return windows_error(::GetLastError());
58 utf16.reserve(len + 1);
61 len = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
62 utf8.begin(), utf8.size(),
63 utf16.begin(), utf16.size());
66 return windows_error(::GetLastError());
68 // Make utf16 null terminated.
72 return error_code::success();
75 error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len,
76 SmallVectorImpl<char> &utf8) {
78 int len = ::WideCharToMultiByte(CP_UTF8, 0,
84 return windows_error(::GetLastError());
89 // Now do the actual conversion.
90 len = ::WideCharToMultiByte(CP_UTF8, 0,
92 utf8.data(), utf8.size(),
96 return windows_error(::GetLastError());
98 // Make utf8 null terminated.
102 return error_code::success();
105 error_code TempDir(SmallVectorImpl<wchar_t> &result) {
107 DWORD len = ::GetTempPathW(result.capacity(), result.begin());
110 return windows_error(::GetLastError());
112 if (len > result.capacity()) {
117 result.set_size(len);
118 return error_code::success();
121 bool is_separator(const wchar_t value) {
132 // FIXME: mode should be used here and default to user r/w only,
133 // it currently comes in as a UNIX mode.
134 static error_code createUniqueEntity(const Twine &model, int &result_fd,
135 SmallVectorImpl<char> &result_path,
136 bool makeAbsolute, unsigned mode,
138 // Use result_path as temp storage.
139 result_path.set_size(0);
140 StringRef m = model.toStringRef(result_path);
142 SmallVector<wchar_t, 128> model_utf16;
143 if (error_code ec = UTF8ToUTF16(m, model_utf16)) return ec;
146 // Make model absolute by prepending a temp directory if it's not already.
147 bool absolute = sys::path::is_absolute(m);
150 SmallVector<wchar_t, 64> temp_dir;
151 if (error_code ec = TempDir(temp_dir)) return ec;
152 // Handle c: by removing it.
153 if (model_utf16.size() > 2 && model_utf16[1] == L':') {
154 model_utf16.erase(model_utf16.begin(), model_utf16.begin() + 2);
156 model_utf16.insert(model_utf16.begin(), temp_dir.begin(), temp_dir.end());
160 // Replace '%' with random chars. From here on, DO NOT modify model. It may be
161 // needed if the randomly chosen path already exists.
162 SmallVector<wchar_t, 128> random_path_utf16;
164 // Get a Crypto Provider for CryptGenRandom.
166 if (!::CryptAcquireContextW(&HCPC,
170 CRYPT_VERIFYCONTEXT))
171 return windows_error(::GetLastError());
172 ScopedCryptContext CryptoProvider(HCPC);
175 random_path_utf16.set_size(0);
176 for (SmallVectorImpl<wchar_t>::const_iterator i = model_utf16.begin(),
177 e = model_utf16.end();
181 if (!::CryptGenRandom(CryptoProvider, 1, &val))
182 return windows_error(::GetLastError());
183 random_path_utf16.push_back("0123456789abcdef"[val & 15]);
186 random_path_utf16.push_back(*i);
188 // Make random_path_utf16 null terminated.
189 random_path_utf16.push_back(0);
190 random_path_utf16.pop_back();
192 HANDLE TempFileHandle = INVALID_HANDLE_VALUE;
196 // Try to create + open the path.
198 ::CreateFileW(random_path_utf16.begin(), GENERIC_READ | GENERIC_WRITE,
199 FILE_SHARE_READ, NULL,
200 // Return ERROR_FILE_EXISTS if the file
202 CREATE_NEW, FILE_ATTRIBUTE_TEMPORARY, NULL);
203 if (TempFileHandle == INVALID_HANDLE_VALUE) {
204 // If the file existed, try again, otherwise, error.
205 error_code ec = windows_error(::GetLastError());
206 if (ec == windows_error::file_exists)
207 goto retry_random_path;
212 // Convert the Windows API file handle into a C-runtime handle.
213 int fd = ::_open_osfhandle(intptr_t(TempFileHandle), 0);
215 ::CloseHandle(TempFileHandle);
216 ::DeleteFileW(random_path_utf16.begin());
217 // MSDN doesn't say anything about _open_osfhandle setting errno or
218 // GetLastError(), so just return invalid_handle.
219 return windows_error::invalid_handle;
227 DWORD attributes = ::GetFileAttributesW(random_path_utf16.begin());
228 if (attributes != INVALID_FILE_ATTRIBUTES)
229 goto retry_random_path;
230 error_code EC = make_error_code(windows_error(::GetLastError()));
231 if (EC != windows_error::file_not_found &&
232 EC != windows_error::path_not_found)
238 if (!::CreateDirectoryW(random_path_utf16.begin(), NULL)) {
239 error_code EC = windows_error(::GetLastError());
240 if (EC != windows_error::already_exists)
242 goto retry_random_path;
247 // Set result_path to the utf-8 representation of the path.
248 if (error_code ec = UTF16ToUTF8(random_path_utf16.begin(),
249 random_path_utf16.size(), result_path)) {
252 ::CloseHandle(TempFileHandle);
253 ::DeleteFileW(random_path_utf16.begin());
257 ::RemoveDirectoryW(random_path_utf16.begin());
263 return error_code::success();
270 std::string getMainExecutable(const char *argv0, void *MainExecAddr) {
271 char pathname[MAX_PATH];
272 DWORD ret = ::GetModuleFileNameA(NULL, pathname, MAX_PATH);
273 return ret != MAX_PATH ? pathname : "";
276 UniqueID file_status::getUniqueID() const {
277 // The file is uniquely identified by the volume serial number along
278 // with the 64-bit file identifier.
279 uint64_t FileID = (static_cast<uint64_t>(FileIndexHigh) << 32ULL) |
280 static_cast<uint64_t>(FileIndexLow);
282 return UniqueID(VolumeSerialNumber, FileID);
285 TimeValue file_status::getLastModificationTime() const {
287 UI.LowPart = LastWriteTimeLow;
288 UI.HighPart = LastWriteTimeHigh;
291 Ret.fromWin32Time(UI.QuadPart);
295 error_code current_path(SmallVectorImpl<char> &result) {
296 SmallVector<wchar_t, 128> cur_path;
297 cur_path.reserve(128);
299 DWORD len = ::GetCurrentDirectoryW(cur_path.capacity(), cur_path.data());
301 // A zero return value indicates a failure other than insufficient space.
303 return windows_error(::GetLastError());
305 // If there's insufficient space, the len returned is larger than the len
307 if (len > cur_path.capacity()) {
308 cur_path.reserve(len);
312 cur_path.set_size(len);
313 return UTF16ToUTF8(cur_path.begin(), cur_path.size(), result);
316 error_code create_directory(const Twine &path, bool &existed) {
317 SmallString<128> path_storage;
318 SmallVector<wchar_t, 128> path_utf16;
320 if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage),
324 if (!::CreateDirectoryW(path_utf16.begin(), NULL)) {
325 error_code ec = windows_error(::GetLastError());
326 if (ec == windows_error::already_exists)
333 return error_code::success();
336 error_code create_hard_link(const Twine &to, const Twine &from) {
338 SmallString<128> from_storage;
339 SmallString<128> to_storage;
340 StringRef f = from.toStringRef(from_storage);
341 StringRef t = to.toStringRef(to_storage);
343 // Convert to utf-16.
344 SmallVector<wchar_t, 128> wide_from;
345 SmallVector<wchar_t, 128> wide_to;
346 if (error_code ec = UTF8ToUTF16(f, wide_from)) return ec;
347 if (error_code ec = UTF8ToUTF16(t, wide_to)) return ec;
349 if (!::CreateHardLinkW(wide_from.begin(), wide_to.begin(), NULL))
350 return windows_error(::GetLastError());
352 return error_code::success();
355 error_code create_symlink(const Twine &to, const Twine &from) {
356 // Only do it if the function is available at runtime.
357 if (!create_symbolic_link_api)
358 return make_error_code(errc::function_not_supported);
361 SmallString<128> from_storage;
362 SmallString<128> to_storage;
363 StringRef f = from.toStringRef(from_storage);
364 StringRef t = to.toStringRef(to_storage);
366 // Convert to utf-16.
367 SmallVector<wchar_t, 128> wide_from;
368 SmallVector<wchar_t, 128> wide_to;
369 if (error_code ec = UTF8ToUTF16(f, wide_from)) return ec;
370 if (error_code ec = UTF8ToUTF16(t, wide_to)) return ec;
372 if (!create_symbolic_link_api(wide_from.begin(), wide_to.begin(), 0))
373 return windows_error(::GetLastError());
375 return error_code::success();
378 error_code remove(const Twine &path, bool &existed) {
379 SmallString<128> path_storage;
380 SmallVector<wchar_t, 128> path_utf16;
383 error_code EC = status(path, st);
385 if (EC == windows_error::file_not_found ||
386 EC == windows_error::path_not_found) {
388 return error_code::success();
393 if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage),
397 if (st.type() == file_type::directory_file) {
398 if (!::RemoveDirectoryW(c_str(path_utf16))) {
399 error_code ec = windows_error(::GetLastError());
400 if (ec != windows_error::file_not_found)
406 if (!::DeleteFileW(c_str(path_utf16))) {
407 error_code ec = windows_error(::GetLastError());
408 if (ec != windows_error::file_not_found)
415 return error_code::success();
418 error_code rename(const Twine &from, const Twine &to) {
420 SmallString<128> from_storage;
421 SmallString<128> to_storage;
422 StringRef f = from.toStringRef(from_storage);
423 StringRef t = to.toStringRef(to_storage);
425 // Convert to utf-16.
426 SmallVector<wchar_t, 128> wide_from;
427 SmallVector<wchar_t, 128> wide_to;
428 if (error_code ec = UTF8ToUTF16(f, wide_from)) return ec;
429 if (error_code ec = UTF8ToUTF16(t, wide_to)) return ec;
431 error_code ec = error_code::success();
432 for (int i = 0; i < 2000; i++) {
433 if (::MoveFileExW(wide_from.begin(), wide_to.begin(),
434 MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING))
435 return error_code::success();
436 ec = windows_error(::GetLastError());
437 if (ec != windows_error::access_denied)
439 // Retry MoveFile() at ACCESS_DENIED.
440 // System scanners (eg. indexer) might open the source file when
441 // It is written and closed.
448 error_code resize_file(const Twine &path, uint64_t size) {
449 SmallString<128> path_storage;
450 SmallVector<wchar_t, 128> path_utf16;
452 if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage),
456 int fd = ::_wopen(path_utf16.begin(), O_BINARY | _O_RDWR, S_IWRITE);
458 return error_code(errno, generic_category());
459 #ifdef HAVE__CHSIZE_S
460 errno_t error = ::_chsize_s(fd, size);
462 errno_t error = ::_chsize(fd, size);
465 return error_code(error, generic_category());
468 error_code exists(const Twine &path, bool &result) {
469 SmallString<128> path_storage;
470 SmallVector<wchar_t, 128> path_utf16;
472 if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage),
476 DWORD attributes = ::GetFileAttributesW(path_utf16.begin());
478 if (attributes == INVALID_FILE_ATTRIBUTES) {
479 // See if the file didn't actually exist.
480 error_code ec = make_error_code(windows_error(::GetLastError()));
481 if (ec != windows_error::file_not_found &&
482 ec != windows_error::path_not_found)
487 return error_code::success();
490 bool can_write(const Twine &Path) {
491 // FIXME: take security attributes into account.
492 SmallString<128> PathStorage;
493 SmallVector<wchar_t, 128> PathUtf16;
495 if (UTF8ToUTF16(Path.toStringRef(PathStorage), PathUtf16))
498 DWORD Attr = ::GetFileAttributesW(PathUtf16.begin());
499 return (Attr != INVALID_FILE_ATTRIBUTES) && !(Attr & FILE_ATTRIBUTE_READONLY);
502 bool can_execute(const Twine &Path) {
503 SmallString<128> PathStorage;
504 SmallVector<wchar_t, 128> PathUtf16;
506 if (UTF8ToUTF16(Path.toStringRef(PathStorage), PathUtf16))
509 DWORD Attr = ::GetFileAttributesW(PathUtf16.begin());
510 return Attr != INVALID_FILE_ATTRIBUTES;
513 bool equivalent(file_status A, file_status B) {
514 assert(status_known(A) && status_known(B));
515 return A.FileIndexHigh == B.FileIndexHigh &&
516 A.FileIndexLow == B.FileIndexLow &&
517 A.FileSizeHigh == B.FileSizeHigh &&
518 A.FileSizeLow == B.FileSizeLow &&
519 A.LastWriteTimeHigh == B.LastWriteTimeHigh &&
520 A.LastWriteTimeLow == B.LastWriteTimeLow &&
521 A.VolumeSerialNumber == B.VolumeSerialNumber;
524 error_code equivalent(const Twine &A, const Twine &B, bool &result) {
525 file_status fsA, fsB;
526 if (error_code ec = status(A, fsA)) return ec;
527 if (error_code ec = status(B, fsB)) return ec;
528 result = equivalent(fsA, fsB);
529 return error_code::success();
532 static bool isReservedName(StringRef path) {
533 // This list of reserved names comes from MSDN, at:
534 // http://msdn.microsoft.com/en-us/library/aa365247%28v=vs.85%29.aspx
535 static const char *sReservedNames[] = { "nul", "con", "prn", "aux",
536 "com1", "com2", "com3", "com4", "com5", "com6",
537 "com7", "com8", "com9", "lpt1", "lpt2", "lpt3",
538 "lpt4", "lpt5", "lpt6", "lpt7", "lpt8", "lpt9" };
540 // First, check to see if this is a device namespace, which always
541 // starts with \\.\, since device namespaces are not legal file paths.
542 if (path.startswith("\\\\.\\"))
545 // Then compare against the list of ancient reserved names
546 for (size_t i = 0; i < array_lengthof(sReservedNames); ++i) {
547 if (path.equals_lower(sReservedNames[i]))
551 // The path isn't what we consider reserved.
555 static error_code getStatus(HANDLE FileHandle, file_status &Result) {
556 if (FileHandle == INVALID_HANDLE_VALUE)
557 goto handle_status_error;
559 switch (::GetFileType(FileHandle)) {
561 llvm_unreachable("Don't know anything about this file type");
562 case FILE_TYPE_UNKNOWN: {
563 DWORD Err = ::GetLastError();
565 return windows_error(Err);
566 Result = file_status(file_type::type_unknown);
567 return error_code::success();
572 Result = file_status(file_type::character_file);
573 return error_code::success();
575 Result = file_status(file_type::fifo_file);
576 return error_code::success();
579 BY_HANDLE_FILE_INFORMATION Info;
580 if (!::GetFileInformationByHandle(FileHandle, &Info))
581 goto handle_status_error;
584 file_type Type = (Info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
585 ? file_type::directory_file
586 : file_type::regular_file;
588 file_status(Type, Info.ftLastWriteTime.dwHighDateTime,
589 Info.ftLastWriteTime.dwLowDateTime,
590 Info.dwVolumeSerialNumber, Info.nFileSizeHigh,
591 Info.nFileSizeLow, Info.nFileIndexHigh, Info.nFileIndexLow);
592 return error_code::success();
596 error_code EC = windows_error(::GetLastError());
597 if (EC == windows_error::file_not_found ||
598 EC == windows_error::path_not_found)
599 Result = file_status(file_type::file_not_found);
600 else if (EC == windows_error::sharing_violation)
601 Result = file_status(file_type::type_unknown);
603 Result = file_status(file_type::status_error);
607 error_code status(const Twine &path, file_status &result) {
608 SmallString<128> path_storage;
609 SmallVector<wchar_t, 128> path_utf16;
611 StringRef path8 = path.toStringRef(path_storage);
612 if (isReservedName(path8)) {
613 result = file_status(file_type::character_file);
614 return error_code::success();
617 if (error_code ec = UTF8ToUTF16(path8, path_utf16))
620 DWORD attr = ::GetFileAttributesW(path_utf16.begin());
621 if (attr == INVALID_FILE_ATTRIBUTES)
622 return getStatus(INVALID_HANDLE_VALUE, result);
624 // Handle reparse points.
625 if (attr & FILE_ATTRIBUTE_REPARSE_POINT) {
627 ::CreateFileW(path_utf16.begin(),
628 0, // Attributes only.
629 FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
632 FILE_FLAG_BACKUP_SEMANTICS,
635 return getStatus(INVALID_HANDLE_VALUE, result);
639 ::CreateFileW(path_utf16.begin(), 0, // Attributes only.
640 FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
641 NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0));
643 return getStatus(INVALID_HANDLE_VALUE, result);
645 return getStatus(h, result);
648 error_code status(int FD, file_status &Result) {
649 HANDLE FileHandle = reinterpret_cast<HANDLE>(_get_osfhandle(FD));
650 return getStatus(FileHandle, Result);
653 error_code setLastModificationAndAccessTime(int FD, TimeValue Time) {
655 UI.QuadPart = Time.toWin32Time();
657 FT.dwLowDateTime = UI.LowPart;
658 FT.dwHighDateTime = UI.HighPart;
659 HANDLE FileHandle = reinterpret_cast<HANDLE>(_get_osfhandle(FD));
660 if (!SetFileTime(FileHandle, NULL, &FT, &FT))
661 return windows_error(::GetLastError());
662 return error_code::success();
665 error_code get_magic(const Twine &path, uint32_t len,
666 SmallVectorImpl<char> &result) {
667 SmallString<128> path_storage;
668 SmallVector<wchar_t, 128> path_utf16;
671 // Convert path to UTF-16.
672 if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage),
677 HANDLE file = ::CreateFileW(c_str(path_utf16),
682 FILE_ATTRIBUTE_READONLY,
684 if (file == INVALID_HANDLE_VALUE)
685 return windows_error(::GetLastError());
691 DWORD bytes_read = 0;
692 BOOL read_success = ::ReadFile(file, result.data(), len, &bytes_read, NULL);
693 error_code ec = windows_error(::GetLastError());
695 if (!read_success || (bytes_read != len)) {
696 // Set result size to the number of bytes read if it's valid.
697 if (bytes_read <= len)
698 result.set_size(bytes_read);
699 // ERROR_HANDLE_EOF is mapped to errc::value_too_large.
703 result.set_size(len);
704 return error_code::success();
707 error_code mapped_file_region::init(int FD, bool CloseFD, uint64_t Offset) {
709 // Make sure that the requested size fits within SIZE_T.
710 if (Size > std::numeric_limits<SIZE_T>::max()) {
711 if (FileDescriptor) {
713 _close(FileDescriptor);
715 ::CloseHandle(FileHandle);
716 return make_error_code(errc::invalid_argument);
721 case readonly: flprotect = PAGE_READONLY; break;
722 case readwrite: flprotect = PAGE_READWRITE; break;
723 case priv: flprotect = PAGE_WRITECOPY; break;
726 FileMappingHandle = ::CreateFileMapping(FileHandle,
732 if (FileMappingHandle == NULL) {
733 error_code ec = windows_error(GetLastError());
734 if (FileDescriptor) {
736 _close(FileDescriptor);
738 ::CloseHandle(FileHandle);
742 DWORD dwDesiredAccess;
744 case readonly: dwDesiredAccess = FILE_MAP_READ; break;
745 case readwrite: dwDesiredAccess = FILE_MAP_WRITE; break;
746 case priv: dwDesiredAccess = FILE_MAP_COPY; break;
748 Mapping = ::MapViewOfFile(FileMappingHandle,
753 if (Mapping == NULL) {
754 error_code ec = windows_error(GetLastError());
755 ::CloseHandle(FileMappingHandle);
756 if (FileDescriptor) {
758 _close(FileDescriptor);
760 ::CloseHandle(FileHandle);
765 MEMORY_BASIC_INFORMATION mbi;
766 SIZE_T Result = VirtualQuery(Mapping, &mbi, sizeof(mbi));
768 error_code ec = windows_error(GetLastError());
769 ::UnmapViewOfFile(Mapping);
770 ::CloseHandle(FileMappingHandle);
771 if (FileDescriptor) {
773 _close(FileDescriptor);
775 ::CloseHandle(FileHandle);
778 Size = mbi.RegionSize;
781 // Close all the handles except for the view. It will keep the other handles
783 ::CloseHandle(FileMappingHandle);
784 if (FileDescriptor) {
786 _close(FileDescriptor); // Also closes FileHandle.
788 ::CloseHandle(FileHandle);
789 return error_code::success();
792 mapped_file_region::mapped_file_region(const Twine &path,
801 , FileHandle(INVALID_HANDLE_VALUE)
802 , FileMappingHandle() {
803 SmallString<128> path_storage;
804 SmallVector<wchar_t, 128> path_utf16;
806 // Convert path to UTF-16.
807 if ((ec = UTF8ToUTF16(path.toStringRef(path_storage), path_utf16)))
810 // Get file handle for creating a file mapping.
811 FileHandle = ::CreateFileW(c_str(path_utf16),
812 Mode == readonly ? GENERIC_READ
813 : GENERIC_READ | GENERIC_WRITE,
814 Mode == readonly ? FILE_SHARE_READ
817 Mode == readonly ? OPEN_EXISTING
819 Mode == readonly ? FILE_ATTRIBUTE_READONLY
820 : FILE_ATTRIBUTE_NORMAL,
822 if (FileHandle == INVALID_HANDLE_VALUE) {
823 ec = windows_error(::GetLastError());
828 ec = init(FileDescriptor, true, offset);
830 Mapping = FileMappingHandle = 0;
831 FileHandle = INVALID_HANDLE_VALUE;
836 mapped_file_region::mapped_file_region(int fd,
846 , FileHandle(INVALID_HANDLE_VALUE)
847 , FileMappingHandle() {
848 FileHandle = reinterpret_cast<HANDLE>(_get_osfhandle(fd));
849 if (FileHandle == INVALID_HANDLE_VALUE) {
851 _close(FileDescriptor);
853 ec = make_error_code(errc::bad_file_descriptor);
857 ec = init(FileDescriptor, closefd, offset);
859 Mapping = FileMappingHandle = 0;
860 FileHandle = INVALID_HANDLE_VALUE;
865 mapped_file_region::~mapped_file_region() {
867 ::UnmapViewOfFile(Mapping);
870 #if LLVM_HAS_RVALUE_REFERENCES
871 mapped_file_region::mapped_file_region(mapped_file_region &&other)
874 , Mapping(other.Mapping)
875 , FileDescriptor(other.FileDescriptor)
876 , FileHandle(other.FileHandle)
877 , FileMappingHandle(other.FileMappingHandle) {
878 other.Mapping = other.FileMappingHandle = 0;
879 other.FileHandle = INVALID_HANDLE_VALUE;
880 other.FileDescriptor = 0;
884 mapped_file_region::mapmode mapped_file_region::flags() const {
885 assert(Mapping && "Mapping failed but used anyway!");
889 uint64_t mapped_file_region::size() const {
890 assert(Mapping && "Mapping failed but used anyway!");
894 char *mapped_file_region::data() const {
895 assert(Mode != readonly && "Cannot get non const data for readonly mapping!");
896 assert(Mapping && "Mapping failed but used anyway!");
897 return reinterpret_cast<char*>(Mapping);
900 const char *mapped_file_region::const_data() const {
901 assert(Mapping && "Mapping failed but used anyway!");
902 return reinterpret_cast<const char*>(Mapping);
905 int mapped_file_region::alignment() {
907 ::GetSystemInfo(&SysInfo);
908 return SysInfo.dwAllocationGranularity;
911 error_code detail::directory_iterator_construct(detail::DirIterState &it,
913 SmallVector<wchar_t, 128> path_utf16;
915 if (error_code ec = UTF8ToUTF16(path,
919 // Convert path to the format that Windows is happy with.
920 if (path_utf16.size() > 0 &&
921 !is_separator(path_utf16[path.size() - 1]) &&
922 path_utf16[path.size() - 1] != L':') {
923 path_utf16.push_back(L'\\');
924 path_utf16.push_back(L'*');
926 path_utf16.push_back(L'*');
929 // Get the first directory entry.
930 WIN32_FIND_DATAW FirstFind;
931 ScopedFindHandle FindHandle(::FindFirstFileW(c_str(path_utf16), &FirstFind));
933 return windows_error(::GetLastError());
935 size_t FilenameLen = ::wcslen(FirstFind.cFileName);
936 while ((FilenameLen == 1 && FirstFind.cFileName[0] == L'.') ||
937 (FilenameLen == 2 && FirstFind.cFileName[0] == L'.' &&
938 FirstFind.cFileName[1] == L'.'))
939 if (!::FindNextFileW(FindHandle, &FirstFind)) {
940 error_code ec = windows_error(::GetLastError());
942 if (ec == windows_error::no_more_files)
943 return detail::directory_iterator_destruct(it);
946 FilenameLen = ::wcslen(FirstFind.cFileName);
948 // Construct the current directory entry.
949 SmallString<128> directory_entry_name_utf8;
950 if (error_code ec = UTF16ToUTF8(FirstFind.cFileName,
951 ::wcslen(FirstFind.cFileName),
952 directory_entry_name_utf8))
955 it.IterationHandle = intptr_t(FindHandle.take());
956 SmallString<128> directory_entry_path(path);
957 path::append(directory_entry_path, directory_entry_name_utf8.str());
958 it.CurrentEntry = directory_entry(directory_entry_path.str());
960 return error_code::success();
963 error_code detail::directory_iterator_destruct(detail::DirIterState &it) {
964 if (it.IterationHandle != 0)
965 // Closes the handle if it's valid.
966 ScopedFindHandle close(HANDLE(it.IterationHandle));
967 it.IterationHandle = 0;
968 it.CurrentEntry = directory_entry();
969 return error_code::success();
972 error_code detail::directory_iterator_increment(detail::DirIterState &it) {
973 WIN32_FIND_DATAW FindData;
974 if (!::FindNextFileW(HANDLE(it.IterationHandle), &FindData)) {
975 error_code ec = windows_error(::GetLastError());
977 if (ec == windows_error::no_more_files)
978 return detail::directory_iterator_destruct(it);
982 size_t FilenameLen = ::wcslen(FindData.cFileName);
983 if ((FilenameLen == 1 && FindData.cFileName[0] == L'.') ||
984 (FilenameLen == 2 && FindData.cFileName[0] == L'.' &&
985 FindData.cFileName[1] == L'.'))
986 return directory_iterator_increment(it);
988 SmallString<128> directory_entry_path_utf8;
989 if (error_code ec = UTF16ToUTF8(FindData.cFileName,
990 ::wcslen(FindData.cFileName),
991 directory_entry_path_utf8))
994 it.CurrentEntry.replace_filename(Twine(directory_entry_path_utf8));
995 return error_code::success();
998 error_code map_file_pages(const Twine &path, off_t file_offset, size_t size,
999 bool map_writable, void *&result) {
1000 assert(0 && "NOT IMPLEMENTED");
1001 return windows_error::invalid_function;
1004 error_code unmap_file_pages(void *base, size_t size) {
1005 assert(0 && "NOT IMPLEMENTED");
1006 return windows_error::invalid_function;
1009 error_code openFileForRead(const Twine &Name, int &ResultFD) {
1010 SmallString<128> PathStorage;
1011 SmallVector<wchar_t, 128> PathUTF16;
1013 if (error_code EC = UTF8ToUTF16(Name.toStringRef(PathStorage),
1017 HANDLE H = ::CreateFileW(PathUTF16.begin(), GENERIC_READ,
1018 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1019 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1020 if (H == INVALID_HANDLE_VALUE) {
1021 error_code EC = windows_error(::GetLastError());
1022 // Provide a better error message when trying to open directories.
1023 // This only runs if we failed to open the file, so there is probably
1024 // no performances issues.
1025 if (EC != windows_error::access_denied)
1027 if (is_directory(Name))
1028 return error_code(errc::is_a_directory, posix_category());
1032 int FD = ::_open_osfhandle(intptr_t(H), 0);
1035 return windows_error::invalid_handle;
1039 return error_code::success();
1042 error_code openFileForWrite(const Twine &Name, int &ResultFD,
1043 sys::fs::OpenFlags Flags, unsigned Mode) {
1044 // Verify that we don't have both "append" and "excl".
1045 assert((!(Flags & sys::fs::F_Excl) || !(Flags & sys::fs::F_Append)) &&
1046 "Cannot specify both 'excl' and 'append' file creation flags!");
1048 SmallString<128> PathStorage;
1049 SmallVector<wchar_t, 128> PathUTF16;
1051 if (error_code EC = UTF8ToUTF16(Name.toStringRef(PathStorage),
1055 DWORD CreationDisposition;
1057 CreationDisposition = CREATE_NEW;
1058 else if (Flags & F_Append)
1059 CreationDisposition = OPEN_ALWAYS;
1061 CreationDisposition = CREATE_ALWAYS;
1063 HANDLE H = ::CreateFileW(PathUTF16.begin(), GENERIC_WRITE,
1064 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1065 CreationDisposition, FILE_ATTRIBUTE_NORMAL, NULL);
1067 if (H == INVALID_HANDLE_VALUE) {
1068 error_code EC = windows_error(::GetLastError());
1069 // Provide a better error message when trying to open directories.
1070 // This only runs if we failed to open the file, so there is probably
1071 // no performances issues.
1072 if (EC != windows_error::access_denied)
1074 if (is_directory(Name))
1075 return error_code(errc::is_a_directory, posix_category());
1080 if (Flags & F_Append)
1081 OpenFlags |= _O_APPEND;
1083 if (!(Flags & F_Binary))
1084 OpenFlags |= _O_TEXT;
1086 int FD = ::_open_osfhandle(intptr_t(H), OpenFlags);
1089 return windows_error::invalid_handle;
1093 return error_code::success();
1096 } // end namespace fs
1097 } // end namespace sys
1098 } // end namespace llvm