1 //===- llvm/Support/Windows/PathV2.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 PathV2 API.
12 //===----------------------------------------------------------------------===//
14 //===----------------------------------------------------------------------===//
15 //=== WARNING: Implementation here must contain only generic Windows code that
16 //=== is guaranteed to work on *all* Windows variants.
17 //===----------------------------------------------------------------------===//
23 #include <sys/types.h>
27 // MinGW doesn't define this.
28 #ifndef _ERRNO_T_DEFINED
29 #define _ERRNO_T_DEFINED
36 typedef BOOLEAN (WINAPI *PtrCreateSymbolicLinkW)(
37 /*__in*/ LPCWSTR lpSymlinkFileName,
38 /*__in*/ LPCWSTR lpTargetFileName,
39 /*__in*/ DWORD dwFlags);
41 PtrCreateSymbolicLinkW create_symbolic_link_api = PtrCreateSymbolicLinkW(
42 ::GetProcAddress(::GetModuleHandleA("kernel32.dll"),
43 "CreateSymbolicLinkW"));
45 error_code UTF8ToUTF16(StringRef utf8, SmallVectorImpl<wchar_t> &utf16) {
46 int len = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
47 utf8.begin(), utf8.size(),
51 return windows_error(::GetLastError());
53 utf16.reserve(len + 1);
56 len = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
57 utf8.begin(), utf8.size(),
58 utf16.begin(), utf16.size());
61 return windows_error(::GetLastError());
63 // Make utf16 null terminated.
67 return error_code::success();
70 error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len,
71 SmallVectorImpl<char> &utf8) {
73 int len = ::WideCharToMultiByte(CP_UTF8, 0,
79 return windows_error(::GetLastError());
84 // Now do the actual conversion.
85 len = ::WideCharToMultiByte(CP_UTF8, 0,
87 utf8.data(), utf8.size(),
91 return windows_error(::GetLastError());
93 // Make utf8 null terminated.
97 return error_code::success();
100 error_code TempDir(SmallVectorImpl<wchar_t> &result) {
102 DWORD len = ::GetTempPathW(result.capacity(), result.begin());
105 return windows_error(::GetLastError());
107 if (len > result.capacity()) {
112 result.set_size(len);
113 return error_code::success();
116 bool is_separator(const wchar_t value) {
131 error_code current_path(SmallVectorImpl<char> &result) {
132 SmallVector<wchar_t, 128> cur_path;
133 cur_path.reserve(128);
135 DWORD len = ::GetCurrentDirectoryW(cur_path.capacity(), cur_path.data());
137 // A zero return value indicates a failure other than insufficient space.
139 return windows_error(::GetLastError());
141 // If there's insufficient space, the len returned is larger than the len
143 if (len > cur_path.capacity()) {
144 cur_path.reserve(len);
148 cur_path.set_size(len);
149 // cur_path now holds the current directory in utf-16. Convert to utf-8.
151 // Find out how much space we need. Sadly, this function doesn't return the
152 // size needed unless you tell it the result size is 0, which means you
153 // _always_ have to call it twice.
154 len = ::WideCharToMultiByte(CP_UTF8, 0,
155 cur_path.data(), cur_path.size(),
160 return make_error_code(windows_error(::GetLastError()));
163 result.set_size(len);
164 // Now do the actual conversion.
165 len = ::WideCharToMultiByte(CP_UTF8, 0,
166 cur_path.data(), cur_path.size(),
167 result.data(), result.size(),
170 return windows_error(::GetLastError());
172 return error_code::success();
175 error_code copy_file(const Twine &from, const Twine &to, copy_option copt) {
177 SmallString<128> from_storage;
178 SmallString<128> to_storage;
179 StringRef f = from.toStringRef(from_storage);
180 StringRef t = to.toStringRef(to_storage);
182 // Convert to utf-16.
183 SmallVector<wchar_t, 128> wide_from;
184 SmallVector<wchar_t, 128> wide_to;
185 if (error_code ec = UTF8ToUTF16(f, wide_from)) return ec;
186 if (error_code ec = UTF8ToUTF16(t, wide_to)) return ec;
189 BOOL res = ::CopyFileW(wide_from.begin(), wide_to.begin(),
190 copt != copy_option::overwrite_if_exists);
193 return windows_error(::GetLastError());
195 return error_code::success();
198 error_code create_directory(const Twine &path, bool &existed) {
199 SmallString<128> path_storage;
200 SmallVector<wchar_t, 128> path_utf16;
202 if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage),
206 if (!::CreateDirectoryW(path_utf16.begin(), NULL)) {
207 error_code ec = windows_error(::GetLastError());
208 if (ec == windows_error::already_exists)
215 return error_code::success();
218 error_code create_hard_link(const Twine &to, const Twine &from) {
220 SmallString<128> from_storage;
221 SmallString<128> to_storage;
222 StringRef f = from.toStringRef(from_storage);
223 StringRef t = to.toStringRef(to_storage);
225 // Convert to utf-16.
226 SmallVector<wchar_t, 128> wide_from;
227 SmallVector<wchar_t, 128> wide_to;
228 if (error_code ec = UTF8ToUTF16(f, wide_from)) return ec;
229 if (error_code ec = UTF8ToUTF16(t, wide_to)) return ec;
231 if (!::CreateHardLinkW(wide_from.begin(), wide_to.begin(), NULL))
232 return windows_error(::GetLastError());
234 return error_code::success();
237 error_code create_symlink(const Twine &to, const Twine &from) {
238 // Only do it if the function is available at runtime.
239 if (!create_symbolic_link_api)
240 return make_error_code(errc::function_not_supported);
243 SmallString<128> from_storage;
244 SmallString<128> to_storage;
245 StringRef f = from.toStringRef(from_storage);
246 StringRef t = to.toStringRef(to_storage);
248 // Convert to utf-16.
249 SmallVector<wchar_t, 128> wide_from;
250 SmallVector<wchar_t, 128> wide_to;
251 if (error_code ec = UTF8ToUTF16(f, wide_from)) return ec;
252 if (error_code ec = UTF8ToUTF16(t, wide_to)) return ec;
254 if (!create_symbolic_link_api(wide_from.begin(), wide_to.begin(), 0))
255 return windows_error(::GetLastError());
257 return error_code::success();
260 error_code remove(const Twine &path, bool &existed) {
261 SmallString<128> path_storage;
262 SmallVector<wchar_t, 128> path_utf16;
265 if (error_code ec = status(path, st))
268 if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage),
272 if (st.type() == file_type::directory_file) {
273 if (!::RemoveDirectoryW(c_str(path_utf16))) {
274 error_code ec = windows_error(::GetLastError());
275 if (ec != windows_error::file_not_found)
281 if (!::DeleteFileW(c_str(path_utf16))) {
282 error_code ec = windows_error(::GetLastError());
283 if (ec != windows_error::file_not_found)
290 return error_code::success();
293 error_code rename(const Twine &from, const Twine &to) {
295 SmallString<128> from_storage;
296 SmallString<128> to_storage;
297 StringRef f = from.toStringRef(from_storage);
298 StringRef t = to.toStringRef(to_storage);
300 // Convert to utf-16.
301 SmallVector<wchar_t, 128> wide_from;
302 SmallVector<wchar_t, 128> wide_to;
303 if (error_code ec = UTF8ToUTF16(f, wide_from)) return ec;
304 if (error_code ec = UTF8ToUTF16(t, wide_to)) return ec;
306 error_code ec = error_code::success();
307 for (int i = 0; i < 2000; i++) {
308 if (::MoveFileExW(wide_from.begin(), wide_to.begin(),
309 MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING))
310 return error_code::success();
311 ec = windows_error(::GetLastError());
312 if (ec != windows_error::access_denied)
314 // Retry MoveFile() at ACCESS_DENIED.
315 // System scanners (eg. indexer) might open the source file when
316 // It is written and closed.
323 error_code resize_file(const Twine &path, uint64_t size) {
324 SmallString<128> path_storage;
325 SmallVector<wchar_t, 128> path_utf16;
327 if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage),
331 int fd = ::_wopen(path_utf16.begin(), O_BINARY | _O_RDWR, S_IWRITE);
333 return error_code(errno, generic_category());
334 #ifdef HAVE__CHSIZE_S
335 errno_t error = ::_chsize_s(fd, size);
337 errno_t error = ::_chsize(fd, size);
340 return error_code(error, generic_category());
343 error_code exists(const Twine &path, bool &result) {
344 SmallString<128> path_storage;
345 SmallVector<wchar_t, 128> path_utf16;
347 if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage),
351 DWORD attributes = ::GetFileAttributesW(path_utf16.begin());
353 if (attributes == INVALID_FILE_ATTRIBUTES) {
354 // See if the file didn't actually exist.
355 error_code ec = make_error_code(windows_error(::GetLastError()));
356 if (ec != windows_error::file_not_found &&
357 ec != windows_error::path_not_found)
362 return error_code::success();
365 bool equivalent(file_status A, file_status B) {
366 assert(status_known(A) && status_known(B));
367 return A.FileIndexHigh == B.FileIndexHigh &&
368 A.FileIndexLow == B.FileIndexLow &&
369 A.FileSizeHigh == B.FileSizeHigh &&
370 A.FileSizeLow == B.FileSizeLow &&
371 A.LastWriteTimeHigh == B.LastWriteTimeHigh &&
372 A.LastWriteTimeLow == B.LastWriteTimeLow &&
373 A.VolumeSerialNumber == B.VolumeSerialNumber;
376 error_code equivalent(const Twine &A, const Twine &B, bool &result) {
377 file_status fsA, fsB;
378 if (error_code ec = status(A, fsA)) return ec;
379 if (error_code ec = status(B, fsB)) return ec;
380 result = equivalent(fsA, fsB);
381 return error_code::success();
384 error_code file_size(const Twine &path, uint64_t &result) {
385 SmallString<128> path_storage;
386 SmallVector<wchar_t, 128> path_utf16;
388 if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage),
392 WIN32_FILE_ATTRIBUTE_DATA FileData;
393 if (!::GetFileAttributesExW(path_utf16.begin(),
394 ::GetFileExInfoStandard,
396 return windows_error(::GetLastError());
399 (uint64_t(FileData.nFileSizeHigh) << (sizeof(FileData.nFileSizeLow) * 8))
400 + FileData.nFileSizeLow;
402 return error_code::success();
405 static bool isReservedName(StringRef path) {
406 // This list of reserved names comes from MSDN, at:
407 // http://msdn.microsoft.com/en-us/library/aa365247%28v=vs.85%29.aspx
408 static const char *sReservedNames[] = { "nul", "con", "prn", "aux",
409 "com1", "com2", "com3", "com4", "com5", "com6",
410 "com7", "com8", "com9", "lpt1", "lpt2", "lpt3",
411 "lpt4", "lpt5", "lpt6", "lpt7", "lpt8", "lpt9" };
413 // First, check to see if this is a device namespace, which always
414 // starts with \\.\, since device namespaces are not legal file paths.
415 if (path.startswith("\\\\.\\"))
418 // Then compare against the list of ancient reserved names
419 for (size_t i = 0; i < sizeof(sReservedNames) / sizeof(const char *); ++i) {
420 if (path.equals_lower(sReservedNames[i]))
424 // The path isn't what we consider reserved.
428 error_code status(const Twine &path, file_status &result) {
429 SmallString<128> path_storage;
430 SmallVector<wchar_t, 128> path_utf16;
432 StringRef path8 = path.toStringRef(path_storage);
433 if (isReservedName(path8)) {
434 result = file_status(file_type::character_file);
435 return error_code::success();
438 if (error_code ec = UTF8ToUTF16(path8, path_utf16))
441 DWORD attr = ::GetFileAttributesW(path_utf16.begin());
442 if (attr == INVALID_FILE_ATTRIBUTES)
443 goto handle_status_error;
445 // Handle reparse points.
446 if (attr & FILE_ATTRIBUTE_REPARSE_POINT) {
448 ::CreateFileW(path_utf16.begin(),
449 0, // Attributes only.
450 FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
453 FILE_FLAG_BACKUP_SEMANTICS,
456 goto handle_status_error;
459 if (attr & FILE_ATTRIBUTE_DIRECTORY)
460 result = file_status(file_type::directory_file);
462 result = file_status(file_type::regular_file);
464 ::CreateFileW(path_utf16.begin(),
465 0, // Attributes only.
466 FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
469 FILE_FLAG_BACKUP_SEMANTICS,
472 goto handle_status_error;
473 BY_HANDLE_FILE_INFORMATION Info;
474 if (!::GetFileInformationByHandle(h, &Info))
475 goto handle_status_error;
476 result.FileIndexHigh = Info.nFileIndexHigh;
477 result.FileIndexLow = Info.nFileIndexLow;
478 result.FileSizeHigh = Info.nFileSizeHigh;
479 result.FileSizeLow = Info.nFileSizeLow;
480 result.LastWriteTimeHigh = Info.ftLastWriteTime.dwHighDateTime;
481 result.LastWriteTimeLow = Info.ftLastWriteTime.dwLowDateTime;
482 result.VolumeSerialNumber = Info.dwVolumeSerialNumber;
485 return error_code::success();
488 error_code ec = windows_error(::GetLastError());
489 if (ec == windows_error::file_not_found ||
490 ec == windows_error::path_not_found)
491 result = file_status(file_type::file_not_found);
492 else if (ec == windows_error::sharing_violation)
493 result = file_status(file_type::type_unknown);
495 result = file_status(file_type::status_error);
499 return error_code::success();
503 // Modifies permissions on a file.
504 error_code permissions(const Twine &path, perms prms) {
505 #if 0 // verify code below before enabling:
506 // If the permissions bits are not trying to modify
507 // "write" permissions, there is nothing to do.
508 if (!(prms & (owner_write|group_write|others_write)))
509 return error_code::success();
511 SmallString<128> path_storage;
512 SmallVector<wchar_t, 128> path_utf16;
514 if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage),
518 DWORD attributes = ::GetFileAttributesW(path_utf16.begin());
520 if (prms & add_perms) {
521 attributes &= ~FILE_ATTRIBUTE_READONLY;
523 else if (prms & remove_perms) {
524 attributes |= FILE_ATTRIBUTE_READONLY;
527 assert(0 && "neither add_perms or remove_perms is set");
530 if ( ! ::SetFileAttributesW(path_utf16.begin(), attributes))
531 return windows_error(::GetLastError());
533 return error_code::success();
537 // FIXME: mode should be used here and default to user r/w only,
538 // it currently comes in as a UNIX mode.
539 error_code unique_file(const Twine &model, int &result_fd,
540 SmallVectorImpl<char> &result_path,
541 bool makeAbsolute, unsigned mode) {
542 // Use result_path as temp storage.
543 result_path.set_size(0);
544 StringRef m = model.toStringRef(result_path);
546 SmallVector<wchar_t, 128> model_utf16;
547 if (error_code ec = UTF8ToUTF16(m, model_utf16)) return ec;
550 // Make model absolute by prepending a temp directory if it's not already.
551 bool absolute = path::is_absolute(m);
554 SmallVector<wchar_t, 64> temp_dir;
555 if (error_code ec = TempDir(temp_dir)) return ec;
556 // Handle c: by removing it.
557 if (model_utf16.size() > 2 && model_utf16[1] == L':') {
558 model_utf16.erase(model_utf16.begin(), model_utf16.begin() + 2);
560 model_utf16.insert(model_utf16.begin(), temp_dir.begin(), temp_dir.end());
564 // Replace '%' with random chars. From here on, DO NOT modify model. It may be
565 // needed if the randomly chosen path already exists.
566 SmallVector<wchar_t, 128> random_path_utf16;
568 // Get a Crypto Provider for CryptGenRandom.
570 if (!::CryptAcquireContextW(&HCPC,
574 CRYPT_VERIFYCONTEXT))
575 return windows_error(::GetLastError());
576 ScopedCryptContext CryptoProvider(HCPC);
579 random_path_utf16.set_size(0);
580 for (SmallVectorImpl<wchar_t>::const_iterator i = model_utf16.begin(),
581 e = model_utf16.end();
585 if (!::CryptGenRandom(CryptoProvider, 1, &val))
586 return windows_error(::GetLastError());
587 random_path_utf16.push_back("0123456789abcdef"[val & 15]);
590 random_path_utf16.push_back(*i);
592 // Make random_path_utf16 null terminated.
593 random_path_utf16.push_back(0);
594 random_path_utf16.pop_back();
596 // Make sure we don't fall into an infinite loop by constantly trying
597 // to create the parent path.
598 bool TriedToCreateParent = false;
600 // Try to create + open the path.
602 HANDLE TempFileHandle = ::CreateFileW(random_path_utf16.begin(),
603 GENERIC_READ | GENERIC_WRITE,
606 // Return ERROR_FILE_EXISTS if the file
609 FILE_ATTRIBUTE_TEMPORARY,
611 if (TempFileHandle == INVALID_HANDLE_VALUE) {
612 // If the file existed, try again, otherwise, error.
613 error_code ec = windows_error(::GetLastError());
614 if (ec == windows_error::file_exists)
615 goto retry_random_path;
616 // Check for non-existing parent directories.
617 if (ec == windows_error::path_not_found && !TriedToCreateParent) {
618 TriedToCreateParent = true;
620 // Create the directories using result_path as temp storage.
621 if (error_code ec = UTF16ToUTF8(random_path_utf16.begin(),
622 random_path_utf16.size(), result_path))
624 StringRef p(result_path.begin(), result_path.size());
625 SmallString<64> dir_to_create;
626 for (path::const_iterator i = path::begin(p),
627 e = --path::end(p); i != e; ++i) {
628 path::append(dir_to_create, *i);
630 if (error_code ec = exists(Twine(dir_to_create), Exists)) return ec;
632 // If c: doesn't exist, bail.
633 if (i->endswith(":"))
636 SmallVector<wchar_t, 64> dir_to_create_utf16;
637 if (error_code ec = UTF8ToUTF16(dir_to_create, dir_to_create_utf16))
640 // Create the directory.
641 if (!::CreateDirectoryW(dir_to_create_utf16.begin(), NULL))
642 return windows_error(::GetLastError());
645 goto retry_create_file;
650 // Set result_path to the utf-8 representation of the path.
651 if (error_code ec = UTF16ToUTF8(random_path_utf16.begin(),
652 random_path_utf16.size(), result_path)) {
653 ::CloseHandle(TempFileHandle);
654 ::DeleteFileW(random_path_utf16.begin());
658 // Convert the Windows API file handle into a C-runtime handle.
659 int fd = ::_open_osfhandle(intptr_t(TempFileHandle), 0);
661 ::CloseHandle(TempFileHandle);
662 ::DeleteFileW(random_path_utf16.begin());
663 // MSDN doesn't say anything about _open_osfhandle setting errno or
664 // GetLastError(), so just return invalid_handle.
665 return windows_error::invalid_handle;
669 return error_code::success();
672 error_code get_magic(const Twine &path, uint32_t len,
673 SmallVectorImpl<char> &result) {
674 SmallString<128> path_storage;
675 SmallVector<wchar_t, 128> path_utf16;
678 // Convert path to UTF-16.
679 if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage),
684 HANDLE file = ::CreateFileW(c_str(path_utf16),
689 FILE_ATTRIBUTE_READONLY,
691 if (file == INVALID_HANDLE_VALUE)
692 return windows_error(::GetLastError());
698 DWORD bytes_read = 0;
699 BOOL read_success = ::ReadFile(file, result.data(), len, &bytes_read, NULL);
700 error_code ec = windows_error(::GetLastError());
702 if (!read_success || (bytes_read != len)) {
703 // Set result size to the number of bytes read if it's valid.
704 if (bytes_read <= len)
705 result.set_size(bytes_read);
706 // ERROR_HANDLE_EOF is mapped to errc::value_too_large.
710 result.set_size(len);
711 return error_code::success();
714 error_code mapped_file_region::init(int FD, bool CloseFD, uint64_t Offset) {
716 // Make sure that the requested size fits within SIZE_T.
717 if (Size > std::numeric_limits<SIZE_T>::max()) {
720 _close(FileDescriptor);
722 ::CloseHandle(FileHandle);
723 return make_error_code(errc::invalid_argument);
728 case readonly: flprotect = PAGE_READONLY; break;
729 case readwrite: flprotect = PAGE_READWRITE; break;
730 case priv: flprotect = PAGE_WRITECOPY; break;
731 default: llvm_unreachable("invalid mapping mode");
734 FileMappingHandle = ::CreateFileMapping(FileHandle,
740 if (FileMappingHandle == NULL) {
741 error_code ec = windows_error(GetLastError());
744 _close(FileDescriptor);
746 ::CloseHandle(FileHandle);
750 DWORD dwDesiredAccess;
752 case readonly: dwDesiredAccess = FILE_MAP_READ; break;
753 case readwrite: dwDesiredAccess = FILE_MAP_WRITE; break;
754 case priv: dwDesiredAccess = FILE_MAP_COPY; break;
755 default: llvm_unreachable("invalid mapping mode");
757 Mapping = ::MapViewOfFile(FileMappingHandle,
762 if (Mapping == NULL) {
763 error_code ec = windows_error(GetLastError());
764 ::CloseHandle(FileMappingHandle);
767 _close(FileDescriptor);
769 ::CloseHandle(FileHandle);
774 MEMORY_BASIC_INFORMATION mbi;
775 SIZE_T Result = VirtualQuery(Mapping, &mbi, sizeof(mbi));
777 error_code ec = windows_error(GetLastError());
778 ::UnmapViewOfFile(Mapping);
779 ::CloseHandle(FileMappingHandle);
782 _close(FileDescriptor);
784 ::CloseHandle(FileHandle);
787 Size = mbi.RegionSize;
790 // Close all the handles except for the view. It will keep the other handles
792 ::CloseHandle(FileMappingHandle);
795 _close(FileDescriptor); // Also closes FileHandle.
797 ::CloseHandle(FileHandle);
798 return error_code::success();
801 mapped_file_region::mapped_file_region(const Twine &path,
810 , FileHandle(INVALID_HANDLE_VALUE)
811 , FileMappingHandle() {
812 SmallString<128> path_storage;
813 SmallVector<wchar_t, 128> path_utf16;
815 // Convert path to UTF-16.
816 if ((ec = UTF8ToUTF16(path.toStringRef(path_storage), path_utf16)))
819 // Get file handle for creating a file mapping.
820 FileHandle = ::CreateFileW(c_str(path_utf16),
821 Mode == readonly ? GENERIC_READ
822 : GENERIC_READ | GENERIC_WRITE,
823 Mode == readonly ? FILE_SHARE_READ
826 Mode == readonly ? OPEN_EXISTING
828 Mode == readonly ? FILE_ATTRIBUTE_READONLY
829 : FILE_ATTRIBUTE_NORMAL,
831 if (FileHandle == INVALID_HANDLE_VALUE) {
832 ec = windows_error(::GetLastError());
837 ec = init(FileDescriptor, true, offset);
839 Mapping = FileMappingHandle = 0;
840 FileHandle = INVALID_HANDLE_VALUE;
845 mapped_file_region::mapped_file_region(int fd,
855 , FileHandle(INVALID_HANDLE_VALUE)
856 , FileMappingHandle() {
857 FileHandle = reinterpret_cast<HANDLE>(_get_osfhandle(fd));
858 if (FileHandle == INVALID_HANDLE_VALUE) {
860 _close(FileDescriptor);
862 ec = make_error_code(errc::bad_file_descriptor);
866 ec = init(FileDescriptor, closefd, offset);
868 Mapping = FileMappingHandle = 0;
869 FileHandle = INVALID_HANDLE_VALUE;
874 mapped_file_region::~mapped_file_region() {
876 ::UnmapViewOfFile(Mapping);
879 #if LLVM_HAS_RVALUE_REFERENCES
880 mapped_file_region::mapped_file_region(mapped_file_region &&other)
883 , Mapping(other.Mapping)
884 , FileDescriptor(other.FileDescriptor)
885 , FileHandle(other.FileHandle)
886 , FileMappingHandle(other.FileMappingHandle) {
887 other.Mapping = other.FileMappingHandle = 0;
888 other.FileHandle = INVALID_HANDLE_VALUE;
889 other.FileDescriptor = 0;
893 mapped_file_region::mapmode mapped_file_region::flags() const {
894 assert(Mapping && "Mapping failed but used anyway!");
898 uint64_t mapped_file_region::size() const {
899 assert(Mapping && "Mapping failed but used anyway!");
903 char *mapped_file_region::data() const {
904 assert(Mode != readonly && "Cannot get non const data for readonly mapping!");
905 assert(Mapping && "Mapping failed but used anyway!");
906 return reinterpret_cast<char*>(Mapping);
909 const char *mapped_file_region::const_data() const {
910 assert(Mapping && "Mapping failed but used anyway!");
911 return reinterpret_cast<const char*>(Mapping);
914 int mapped_file_region::alignment() {
916 ::GetSystemInfo(&SysInfo);
917 return SysInfo.dwAllocationGranularity;
920 error_code detail::directory_iterator_construct(detail::DirIterState &it,
922 SmallVector<wchar_t, 128> path_utf16;
924 if (error_code ec = UTF8ToUTF16(path,
928 // Convert path to the format that Windows is happy with.
929 if (path_utf16.size() > 0 &&
930 !is_separator(path_utf16[path.size() - 1]) &&
931 path_utf16[path.size() - 1] != L':') {
932 path_utf16.push_back(L'\\');
933 path_utf16.push_back(L'*');
935 path_utf16.push_back(L'*');
938 // Get the first directory entry.
939 WIN32_FIND_DATAW FirstFind;
940 ScopedFindHandle FindHandle(::FindFirstFileW(c_str(path_utf16), &FirstFind));
942 return windows_error(::GetLastError());
944 size_t FilenameLen = ::wcslen(FirstFind.cFileName);
945 while ((FilenameLen == 1 && FirstFind.cFileName[0] == L'.') ||
946 (FilenameLen == 2 && FirstFind.cFileName[0] == L'.' &&
947 FirstFind.cFileName[1] == L'.'))
948 if (!::FindNextFileW(FindHandle, &FirstFind)) {
949 error_code ec = windows_error(::GetLastError());
951 if (ec == windows_error::no_more_files)
952 return detail::directory_iterator_destruct(it);
955 FilenameLen = ::wcslen(FirstFind.cFileName);
957 // Construct the current directory entry.
958 SmallString<128> directory_entry_name_utf8;
959 if (error_code ec = UTF16ToUTF8(FirstFind.cFileName,
960 ::wcslen(FirstFind.cFileName),
961 directory_entry_name_utf8))
964 it.IterationHandle = intptr_t(FindHandle.take());
965 SmallString<128> directory_entry_path(path);
966 path::append(directory_entry_path, directory_entry_name_utf8.str());
967 it.CurrentEntry = directory_entry(directory_entry_path.str());
969 return error_code::success();
972 error_code detail::directory_iterator_destruct(detail::DirIterState &it) {
973 if (it.IterationHandle != 0)
974 // Closes the handle if it's valid.
975 ScopedFindHandle close(HANDLE(it.IterationHandle));
976 it.IterationHandle = 0;
977 it.CurrentEntry = directory_entry();
978 return error_code::success();
981 error_code detail::directory_iterator_increment(detail::DirIterState &it) {
982 WIN32_FIND_DATAW FindData;
983 if (!::FindNextFileW(HANDLE(it.IterationHandle), &FindData)) {
984 error_code ec = windows_error(::GetLastError());
986 if (ec == windows_error::no_more_files)
987 return detail::directory_iterator_destruct(it);
991 size_t FilenameLen = ::wcslen(FindData.cFileName);
992 if ((FilenameLen == 1 && FindData.cFileName[0] == L'.') ||
993 (FilenameLen == 2 && FindData.cFileName[0] == L'.' &&
994 FindData.cFileName[1] == L'.'))
995 return directory_iterator_increment(it);
997 SmallString<128> directory_entry_path_utf8;
998 if (error_code ec = UTF16ToUTF8(FindData.cFileName,
999 ::wcslen(FindData.cFileName),
1000 directory_entry_path_utf8))
1003 it.CurrentEntry.replace_filename(Twine(directory_entry_path_utf8));
1004 return error_code::success();
1007 error_code map_file_pages(const Twine &path, off_t file_offset, size_t size,
1008 bool map_writable, void *&result) {
1009 assert(0 && "NOT IMPLEMENTED");
1010 return windows_error::invalid_function;
1013 error_code unmap_file_pages(void *base, size_t size) {
1014 assert(0 && "NOT IMPLEMENTED");
1015 return windows_error::invalid_function;
1020 } // end namespace fs
1021 } // end namespace sys
1022 } // end namespace llvm