Add a version of sys::fs::status that uses fstat.
[oota-llvm.git] / lib / Support / Windows / Path.inc
1 //===- llvm/Support/Windows/Path.inc - Windows Path Impl --------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the Windows specific implementation of the Path API.
11 //
12 //===----------------------------------------------------------------------===//
13
14 //===----------------------------------------------------------------------===//
15 //=== WARNING: Implementation here must contain only generic Windows code that
16 //===          is guaranteed to work on *all* Windows variants.
17 //===----------------------------------------------------------------------===//
18
19 #include "llvm/ADT/STLExtras.h"
20 #include "Windows.h"
21 #include <fcntl.h>
22 #include <io.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25
26 #undef max
27
28 // MinGW doesn't define this.
29 #ifndef _ERRNO_T_DEFINED
30 #define _ERRNO_T_DEFINED
31 typedef int errno_t;
32 #endif
33
34 using namespace llvm;
35
36 namespace {
37   typedef BOOLEAN (WINAPI *PtrCreateSymbolicLinkW)(
38     /*__in*/ LPCWSTR lpSymlinkFileName,
39     /*__in*/ LPCWSTR lpTargetFileName,
40     /*__in*/ DWORD dwFlags);
41
42   PtrCreateSymbolicLinkW create_symbolic_link_api = PtrCreateSymbolicLinkW(
43     ::GetProcAddress(::GetModuleHandleA("kernel32.dll"),
44                      "CreateSymbolicLinkW"));
45
46   error_code UTF8ToUTF16(StringRef utf8, SmallVectorImpl<wchar_t> &utf16) {
47     int len = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
48                                     utf8.begin(), utf8.size(),
49                                     utf16.begin(), 0);
50
51     if (len == 0)
52       return windows_error(::GetLastError());
53
54     utf16.reserve(len + 1);
55     utf16.set_size(len);
56
57     len = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
58                                     utf8.begin(), utf8.size(),
59                                     utf16.begin(), utf16.size());
60
61     if (len == 0)
62       return windows_error(::GetLastError());
63
64     // Make utf16 null terminated.
65     utf16.push_back(0);
66     utf16.pop_back();
67
68     return error_code::success();
69   }
70
71   error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len,
72                                SmallVectorImpl<char> &utf8) {
73     // Get length.
74     int len = ::WideCharToMultiByte(CP_UTF8, 0,
75                                     utf16, utf16_len,
76                                     utf8.begin(), 0,
77                                     NULL, NULL);
78
79     if (len == 0)
80       return windows_error(::GetLastError());
81
82     utf8.reserve(len);
83     utf8.set_size(len);
84
85     // Now do the actual conversion.
86     len = ::WideCharToMultiByte(CP_UTF8, 0,
87                                 utf16, utf16_len,
88                                 utf8.data(), utf8.size(),
89                                 NULL, NULL);
90
91     if (len == 0)
92       return windows_error(::GetLastError());
93
94     // Make utf8 null terminated.
95     utf8.push_back(0);
96     utf8.pop_back();
97
98     return error_code::success();
99   }
100
101   error_code TempDir(SmallVectorImpl<wchar_t> &result) {
102   retry_temp_dir:
103     DWORD len = ::GetTempPathW(result.capacity(), result.begin());
104
105     if (len == 0)
106       return windows_error(::GetLastError());
107
108     if (len > result.capacity()) {
109       result.reserve(len);
110       goto retry_temp_dir;
111     }
112
113     result.set_size(len);
114     return error_code::success();
115   }
116
117   bool is_separator(const wchar_t value) {
118     switch (value) {
119     case L'\\':
120     case L'/':
121       return true;
122     default:
123       return false;
124     }
125   }
126 }
127
128 // FIXME: mode should be used here and default to user r/w only,
129 // it currently comes in as a UNIX mode.
130 static error_code createUniqueEntity(const Twine &model, int &result_fd,
131                                      SmallVectorImpl<char> &result_path,
132                                      bool makeAbsolute, unsigned mode,
133                                      FSEntity Type) {
134   // Use result_path as temp storage.
135   result_path.set_size(0);
136   StringRef m = model.toStringRef(result_path);
137
138   SmallVector<wchar_t, 128> model_utf16;
139   if (error_code ec = UTF8ToUTF16(m, model_utf16)) return ec;
140
141   if (makeAbsolute) {
142     // Make model absolute by prepending a temp directory if it's not already.
143     bool absolute = sys::path::is_absolute(m);
144
145     if (!absolute) {
146       SmallVector<wchar_t, 64> temp_dir;
147       if (error_code ec = TempDir(temp_dir)) return ec;
148       // Handle c: by removing it.
149       if (model_utf16.size() > 2 && model_utf16[1] == L':') {
150         model_utf16.erase(model_utf16.begin(), model_utf16.begin() + 2);
151       }
152       model_utf16.insert(model_utf16.begin(), temp_dir.begin(), temp_dir.end());
153     }
154   }
155
156   // Replace '%' with random chars. From here on, DO NOT modify model. It may be
157   // needed if the randomly chosen path already exists.
158   SmallVector<wchar_t, 128> random_path_utf16;
159
160   // Get a Crypto Provider for CryptGenRandom.
161   HCRYPTPROV HCPC;
162   if (!::CryptAcquireContextW(&HCPC,
163                               NULL,
164                               NULL,
165                               PROV_RSA_FULL,
166                               CRYPT_VERIFYCONTEXT))
167     return windows_error(::GetLastError());
168   ScopedCryptContext CryptoProvider(HCPC);
169
170 retry_random_path:
171   random_path_utf16.set_size(0);
172   for (SmallVectorImpl<wchar_t>::const_iterator i = model_utf16.begin(),
173                                                 e = model_utf16.end();
174                                                 i != e; ++i) {
175     if (*i == L'%') {
176       BYTE val = 0;
177       if (!::CryptGenRandom(CryptoProvider, 1, &val))
178           return windows_error(::GetLastError());
179       random_path_utf16.push_back("0123456789abcdef"[val & 15]);
180     }
181     else
182       random_path_utf16.push_back(*i);
183   }
184   // Make random_path_utf16 null terminated.
185   random_path_utf16.push_back(0);
186   random_path_utf16.pop_back();
187
188   HANDLE TempFileHandle;
189
190   switch (Type) {
191   case FS_File: {
192     // Try to create + open the path.
193     TempFileHandle =
194         ::CreateFileW(random_path_utf16.begin(), GENERIC_READ | GENERIC_WRITE,
195                       FILE_SHARE_READ, NULL,
196                       // Return ERROR_FILE_EXISTS if the file
197                       // already exists.
198                       CREATE_NEW, FILE_ATTRIBUTE_TEMPORARY, NULL);
199     if (TempFileHandle == INVALID_HANDLE_VALUE) {
200       // If the file existed, try again, otherwise, error.
201       error_code ec = windows_error(::GetLastError());
202       if (ec == windows_error::file_exists)
203         goto retry_random_path;
204
205       return ec;
206     }
207
208     // Convert the Windows API file handle into a C-runtime handle.
209     int fd = ::_open_osfhandle(intptr_t(TempFileHandle), 0);
210     if (fd == -1) {
211       ::CloseHandle(TempFileHandle);
212       ::DeleteFileW(random_path_utf16.begin());
213       // MSDN doesn't say anything about _open_osfhandle setting errno or
214       // GetLastError(), so just return invalid_handle.
215       return windows_error::invalid_handle;
216     }
217
218     result_fd = fd;
219     break;
220   }
221
222   case FS_Name: {
223     DWORD attributes = ::GetFileAttributesW(random_path_utf16.begin());
224     if (attributes != INVALID_FILE_ATTRIBUTES)
225       goto retry_random_path;
226     error_code EC = make_error_code(windows_error(::GetLastError()));
227     if (EC != windows_error::file_not_found &&
228         EC != windows_error::path_not_found)
229       return EC;
230     break;
231   }
232
233   case FS_Dir:
234     if (!::CreateDirectoryW(random_path_utf16.begin(), NULL)) {
235       error_code EC = windows_error(::GetLastError());
236       if (EC != windows_error::already_exists)
237         return EC;
238       goto retry_random_path;
239     }
240     break;
241   }
242
243   // Set result_path to the utf-8 representation of the path.
244   if (error_code ec = UTF16ToUTF8(random_path_utf16.begin(),
245                                   random_path_utf16.size(), result_path)) {
246     switch (Type) {
247     case FS_File:
248       ::CloseHandle(TempFileHandle);
249       ::DeleteFileW(random_path_utf16.begin());
250     case FS_Name:
251       break;
252     case FS_Dir:
253       ::RemoveDirectoryW(random_path_utf16.begin());
254       break;
255     }
256     return ec;
257   }
258
259   return error_code::success();
260 }
261
262 namespace llvm {
263 namespace sys  {
264 namespace fs {
265
266 std::string getMainExecutable(const char *argv0, void *MainExecAddr) {
267   char pathname[MAX_PATH];
268   DWORD ret = ::GetModuleFileNameA(NULL, pathname, MAX_PATH);
269   return ret != MAX_PATH ? pathname : "";
270 }
271
272
273 TimeValue file_status::getLastModificationTime() const {
274   ULARGE_INTEGER UI;
275   UI.LowPart = LastWriteTimeLow;
276   UI.HighPart = LastWriteTimeHigh;
277
278   TimeValue Ret;
279   Ret.fromWin32Time(UI.QuadPart);
280   return Ret;
281 }
282
283 error_code current_path(SmallVectorImpl<char> &result) {
284   SmallVector<wchar_t, 128> cur_path;
285   cur_path.reserve(128);
286 retry_cur_dir:
287   DWORD len = ::GetCurrentDirectoryW(cur_path.capacity(), cur_path.data());
288
289   // A zero return value indicates a failure other than insufficient space.
290   if (len == 0)
291     return windows_error(::GetLastError());
292
293   // If there's insufficient space, the len returned is larger than the len
294   // given.
295   if (len > cur_path.capacity()) {
296     cur_path.reserve(len);
297     goto retry_cur_dir;
298   }
299
300   cur_path.set_size(len);
301   // cur_path now holds the current directory in utf-16. Convert to utf-8.
302
303   // Find out how much space we need. Sadly, this function doesn't return the
304   // size needed unless you tell it the result size is 0, which means you
305   // _always_ have to call it twice.
306   len = ::WideCharToMultiByte(CP_UTF8, 0,
307                               cur_path.data(), cur_path.size(),
308                               result.data(), 0,
309                               NULL, NULL);
310
311   if (len == 0)
312     return make_error_code(windows_error(::GetLastError()));
313
314   result.reserve(len);
315   result.set_size(len);
316   // Now do the actual conversion.
317   len = ::WideCharToMultiByte(CP_UTF8, 0,
318                               cur_path.data(), cur_path.size(),
319                               result.data(), result.size(),
320                               NULL, NULL);
321   if (len == 0)
322     return windows_error(::GetLastError());
323
324   return error_code::success();
325 }
326
327 error_code copy_file(const Twine &from, const Twine &to, copy_option copt) {
328   // Get arguments.
329   SmallString<128> from_storage;
330   SmallString<128> to_storage;
331   StringRef f = from.toStringRef(from_storage);
332   StringRef t = to.toStringRef(to_storage);
333
334   // Convert to utf-16.
335   SmallVector<wchar_t, 128> wide_from;
336   SmallVector<wchar_t, 128> wide_to;
337   if (error_code ec = UTF8ToUTF16(f, wide_from)) return ec;
338   if (error_code ec = UTF8ToUTF16(t, wide_to)) return ec;
339
340   // Copy the file.
341   BOOL res = ::CopyFileW(wide_from.begin(), wide_to.begin(),
342                          copt != copy_option::overwrite_if_exists);
343
344   if (res == 0)
345     return windows_error(::GetLastError());
346
347   return error_code::success();
348 }
349
350 error_code create_directory(const Twine &path, bool &existed) {
351   SmallString<128> path_storage;
352   SmallVector<wchar_t, 128> path_utf16;
353
354   if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage),
355                                   path_utf16))
356     return ec;
357
358   if (!::CreateDirectoryW(path_utf16.begin(), NULL)) {
359     error_code ec = windows_error(::GetLastError());
360     if (ec == windows_error::already_exists)
361       existed = true;
362     else
363       return ec;
364   } else
365     existed = false;
366
367   return error_code::success();
368 }
369
370 error_code create_hard_link(const Twine &to, const Twine &from) {
371   // Get arguments.
372   SmallString<128> from_storage;
373   SmallString<128> to_storage;
374   StringRef f = from.toStringRef(from_storage);
375   StringRef t = to.toStringRef(to_storage);
376
377   // Convert to utf-16.
378   SmallVector<wchar_t, 128> wide_from;
379   SmallVector<wchar_t, 128> wide_to;
380   if (error_code ec = UTF8ToUTF16(f, wide_from)) return ec;
381   if (error_code ec = UTF8ToUTF16(t, wide_to)) return ec;
382
383   if (!::CreateHardLinkW(wide_from.begin(), wide_to.begin(), NULL))
384     return windows_error(::GetLastError());
385
386   return error_code::success();
387 }
388
389 error_code create_symlink(const Twine &to, const Twine &from) {
390   // Only do it if the function is available at runtime.
391   if (!create_symbolic_link_api)
392     return make_error_code(errc::function_not_supported);
393
394   // Get arguments.
395   SmallString<128> from_storage;
396   SmallString<128> to_storage;
397   StringRef f = from.toStringRef(from_storage);
398   StringRef t = to.toStringRef(to_storage);
399
400   // Convert to utf-16.
401   SmallVector<wchar_t, 128> wide_from;
402   SmallVector<wchar_t, 128> wide_to;
403   if (error_code ec = UTF8ToUTF16(f, wide_from)) return ec;
404   if (error_code ec = UTF8ToUTF16(t, wide_to)) return ec;
405
406   if (!create_symbolic_link_api(wide_from.begin(), wide_to.begin(), 0))
407     return windows_error(::GetLastError());
408
409   return error_code::success();
410 }
411
412 error_code remove(const Twine &path, bool &existed) {
413   SmallString<128> path_storage;
414   SmallVector<wchar_t, 128> path_utf16;
415
416   file_status st;
417   if (error_code ec = status(path, st))
418     return ec;
419
420   if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage),
421                                   path_utf16))
422     return ec;
423
424   if (st.type() == file_type::directory_file) {
425     if (!::RemoveDirectoryW(c_str(path_utf16))) {
426       error_code ec = windows_error(::GetLastError());
427       if (ec != windows_error::file_not_found)
428         return ec;
429       existed = false;
430     } else
431       existed = true;
432   } else {
433     if (!::DeleteFileW(c_str(path_utf16))) {
434       error_code ec = windows_error(::GetLastError());
435       if (ec != windows_error::file_not_found)
436         return ec;
437       existed = false;
438     } else
439       existed = true;
440   }
441
442   return error_code::success();
443 }
444
445 error_code rename(const Twine &from, const Twine &to) {
446   // Get arguments.
447   SmallString<128> from_storage;
448   SmallString<128> to_storage;
449   StringRef f = from.toStringRef(from_storage);
450   StringRef t = to.toStringRef(to_storage);
451
452   // Convert to utf-16.
453   SmallVector<wchar_t, 128> wide_from;
454   SmallVector<wchar_t, 128> wide_to;
455   if (error_code ec = UTF8ToUTF16(f, wide_from)) return ec;
456   if (error_code ec = UTF8ToUTF16(t, wide_to)) return ec;
457
458   error_code ec = error_code::success();
459   for (int i = 0; i < 2000; i++) {
460     if (::MoveFileExW(wide_from.begin(), wide_to.begin(),
461                       MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING))
462       return error_code::success();
463     ec = windows_error(::GetLastError());
464     if (ec != windows_error::access_denied)
465       break;
466     // Retry MoveFile() at ACCESS_DENIED.
467     // System scanners (eg. indexer) might open the source file when
468     // It is written and closed.
469     ::Sleep(1);
470   }
471
472   return ec;
473 }
474
475 error_code resize_file(const Twine &path, uint64_t size) {
476   SmallString<128> path_storage;
477   SmallVector<wchar_t, 128> path_utf16;
478
479   if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage),
480                                   path_utf16))
481     return ec;
482
483   int fd = ::_wopen(path_utf16.begin(), O_BINARY | _O_RDWR, S_IWRITE);
484   if (fd == -1)
485     return error_code(errno, generic_category());
486 #ifdef HAVE__CHSIZE_S
487   errno_t error = ::_chsize_s(fd, size);
488 #else
489   errno_t error = ::_chsize(fd, size);
490 #endif
491   ::close(fd);
492   return error_code(error, generic_category());
493 }
494
495 error_code exists(const Twine &path, bool &result) {
496   SmallString<128> path_storage;
497   SmallVector<wchar_t, 128> path_utf16;
498
499   if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage),
500                                   path_utf16))
501     return ec;
502
503   DWORD attributes = ::GetFileAttributesW(path_utf16.begin());
504
505   if (attributes == INVALID_FILE_ATTRIBUTES) {
506     // See if the file didn't actually exist.
507     error_code ec = make_error_code(windows_error(::GetLastError()));
508     if (ec != windows_error::file_not_found &&
509         ec != windows_error::path_not_found)
510       return ec;
511     result = false;
512   } else
513     result = true;
514   return error_code::success();
515 }
516
517 bool can_write(const Twine &Path) {
518   // FIXME: take security attributes into account.
519   SmallString<128> PathStorage;
520   SmallVector<wchar_t, 128> PathUtf16;
521
522   if (UTF8ToUTF16(Path.toStringRef(PathStorage), PathUtf16))
523     return false;
524
525   DWORD Attr = ::GetFileAttributesW(PathUtf16.begin());
526   return (Attr != INVALID_FILE_ATTRIBUTES) && !(Attr & FILE_ATTRIBUTE_READONLY);
527 }
528
529 bool can_execute(const Twine &Path) {
530   SmallString<128> PathStorage;
531   SmallVector<wchar_t, 128> PathUtf16;
532
533   if (UTF8ToUTF16(Path.toStringRef(PathStorage), PathUtf16))
534     return false;
535
536   DWORD Attr = ::GetFileAttributesW(PathUtf16.begin());
537   return Attr != INVALID_FILE_ATTRIBUTES;
538 }
539
540 bool equivalent(file_status A, file_status B) {
541   assert(status_known(A) && status_known(B));
542   return A.FileIndexHigh      == B.FileIndexHigh &&
543          A.FileIndexLow       == B.FileIndexLow &&
544          A.FileSizeHigh       == B.FileSizeHigh &&
545          A.FileSizeLow        == B.FileSizeLow &&
546          A.LastWriteTimeHigh  == B.LastWriteTimeHigh &&
547          A.LastWriteTimeLow   == B.LastWriteTimeLow &&
548          A.VolumeSerialNumber == B.VolumeSerialNumber;
549 }
550
551 error_code equivalent(const Twine &A, const Twine &B, bool &result) {
552   file_status fsA, fsB;
553   if (error_code ec = status(A, fsA)) return ec;
554   if (error_code ec = status(B, fsB)) return ec;
555   result = equivalent(fsA, fsB);
556   return error_code::success();
557 }
558
559 error_code getUniqueID(const Twine Path, uint64_t &Result) {
560   file_status Status;
561   if (error_code E = status(Path, Status))
562     return E;
563
564   // The file is uniquely identified by the volume serial number along
565   // with the 64-bit file identifier.
566   Result = (static_cast<uint64_t>(Status.FileIndexHigh) << 32ULL) |
567            static_cast<uint64_t>(Status.FileIndexLow);
568   
569   // Because the serial number is 32-bits, but we've already used up all 64
570   // bits for the file index, XOR the serial number into the high 32 bits of
571   // the resulting value.  We could potentially get collisons from this, but
572   // the likelihood is low.
573   Result ^= (static_cast<uint64_t>(Status.VolumeSerialNumber) << 32ULL);
574
575   return error_code::success();
576 }
577
578 static bool isReservedName(StringRef path) {
579   // This list of reserved names comes from MSDN, at:
580   // http://msdn.microsoft.com/en-us/library/aa365247%28v=vs.85%29.aspx
581   static const char *sReservedNames[] = { "nul", "con", "prn", "aux",
582                               "com1", "com2", "com3", "com4", "com5", "com6",
583                               "com7", "com8", "com9", "lpt1", "lpt2", "lpt3",
584                               "lpt4", "lpt5", "lpt6", "lpt7", "lpt8", "lpt9" };
585
586   // First, check to see if this is a device namespace, which always
587   // starts with \\.\, since device namespaces are not legal file paths.
588   if (path.startswith("\\\\.\\"))
589     return true;
590
591   // Then compare against the list of ancient reserved names
592   for (size_t i = 0; i < array_lengthof(sReservedNames); ++i) {
593     if (path.equals_lower(sReservedNames[i]))
594       return true;
595   }
596
597   // The path isn't what we consider reserved.
598   return false;
599 }
600
601 static error_code getStatus(HANDLE FileHandle, file_status &Result) {
602   if (FileHandle == INVALID_HANDLE_VALUE)
603     goto handle_status_error;
604
605   BY_HANDLE_FILE_INFORMATION Info;
606   if (!::GetFileInformationByHandle(FileHandle, &Info))
607     goto handle_status_error;
608
609   Result = file_status(
610         file_type::regular_file, Info.ftLastWriteTime.dwHighDateTime,
611         Info.ftLastWriteTime.dwLowDateTime, Info.dwVolumeSerialNumber,
612         Info.nFileSizeHigh, Info.nFileSizeLow, Info.nFileIndexHigh,
613         Info.nFileIndexLow);
614   return error_code::success();
615
616 handle_status_error:
617   error_code EC = windows_error(::GetLastError());
618   if (EC == windows_error::file_not_found ||
619       EC == windows_error::path_not_found)
620     Result = file_status(file_type::file_not_found);
621   else if (EC == windows_error::sharing_violation)
622     Result = file_status(file_type::type_unknown);
623   else {
624     Result = file_status(file_type::status_error);
625     return EC;
626   }
627   return error_code::success();
628 }
629
630 error_code status(const Twine &path, file_status &result) {
631   SmallString<128> path_storage;
632   SmallVector<wchar_t, 128> path_utf16;
633
634   StringRef path8 = path.toStringRef(path_storage);
635   if (isReservedName(path8)) {
636     result = file_status(file_type::character_file);
637     return error_code::success();
638   }
639
640   if (error_code ec = UTF8ToUTF16(path8, path_utf16))
641     return ec;
642
643   DWORD attr = ::GetFileAttributesW(path_utf16.begin());
644   if (attr == INVALID_FILE_ATTRIBUTES)
645     return getStatus(INVALID_HANDLE_VALUE, result);
646
647   // Handle reparse points.
648   if (attr & FILE_ATTRIBUTE_REPARSE_POINT) {
649     ScopedFileHandle h(
650       ::CreateFileW(path_utf16.begin(),
651                     0, // Attributes only.
652                     FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
653                     NULL,
654                     OPEN_EXISTING,
655                     FILE_FLAG_BACKUP_SEMANTICS,
656                     0));
657     if (!h)
658       return getStatus(INVALID_HANDLE_VALUE, result);
659   }
660
661   if (attr & FILE_ATTRIBUTE_DIRECTORY)
662     result = file_status(file_type::directory_file);
663   else {
664     ScopedFileHandle h(
665       ::CreateFileW(path_utf16.begin(),
666                     0, // Attributes only.
667                     FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
668                     NULL,
669                     OPEN_EXISTING,
670                     FILE_FLAG_BACKUP_SEMANTICS,
671                     0));
672     if (!h)
673       return getStatus(INVALID_HANDLE_VALUE, result);
674     BY_HANDLE_FILE_INFORMATION Info;
675     if (!::GetFileInformationByHandle(h, &Info))
676       return getStatus(INVALID_HANDLE_VALUE, result);
677
678     return getStatus(h, result);
679   }
680   return error_code::success();
681 }
682
683 error_code status(int FD, file_status &Result) {
684   HANDLE FileHandle = reinterpret_cast<HANDLE>(_get_osfhandle(FD));
685   return getStatus(FileHandle, Result);
686 }
687
688 error_code setLastModificationAndAccessTime(int FD, TimeValue Time) {
689   ULARGE_INTEGER UI;
690   UI.QuadPart = Time.toWin32Time();
691   FILETIME FT;
692   FT.dwLowDateTime = UI.LowPart;
693   FT.dwHighDateTime = UI.HighPart;
694   HANDLE FileHandle = reinterpret_cast<HANDLE>(_get_osfhandle(FD));
695   if (!SetFileTime(FileHandle, NULL, &FT, &FT))
696     return windows_error(::GetLastError());
697   return error_code::success();
698 }
699
700 error_code get_magic(const Twine &path, uint32_t len,
701                      SmallVectorImpl<char> &result) {
702   SmallString<128> path_storage;
703   SmallVector<wchar_t, 128> path_utf16;
704   result.set_size(0);
705
706   // Convert path to UTF-16.
707   if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage),
708                                   path_utf16))
709     return ec;
710
711   // Open file.
712   HANDLE file = ::CreateFileW(c_str(path_utf16),
713                               GENERIC_READ,
714                               FILE_SHARE_READ,
715                               NULL,
716                               OPEN_EXISTING,
717                               FILE_ATTRIBUTE_READONLY,
718                               NULL);
719   if (file == INVALID_HANDLE_VALUE)
720     return windows_error(::GetLastError());
721
722   // Allocate buffer.
723   result.reserve(len);
724
725   // Get magic!
726   DWORD bytes_read = 0;
727   BOOL read_success = ::ReadFile(file, result.data(), len, &bytes_read, NULL);
728   error_code ec = windows_error(::GetLastError());
729   ::CloseHandle(file);
730   if (!read_success || (bytes_read != len)) {
731     // Set result size to the number of bytes read if it's valid.
732     if (bytes_read <= len)
733       result.set_size(bytes_read);
734     // ERROR_HANDLE_EOF is mapped to errc::value_too_large.
735     return ec;
736   }
737
738   result.set_size(len);
739   return error_code::success();
740 }
741
742 error_code mapped_file_region::init(int FD, bool CloseFD, uint64_t Offset) {
743   FileDescriptor = FD;
744   // Make sure that the requested size fits within SIZE_T.
745   if (Size > std::numeric_limits<SIZE_T>::max()) {
746     if (FileDescriptor) {
747       if (CloseFD)
748         _close(FileDescriptor);
749     } else
750       ::CloseHandle(FileHandle);
751     return make_error_code(errc::invalid_argument);
752   }
753
754   DWORD flprotect;
755   switch (Mode) {
756   case readonly:  flprotect = PAGE_READONLY; break;
757   case readwrite: flprotect = PAGE_READWRITE; break;
758   case priv:      flprotect = PAGE_WRITECOPY; break;
759   default: llvm_unreachable("invalid mapping mode");
760   }
761
762   FileMappingHandle = ::CreateFileMapping(FileHandle,
763                                           0,
764                                           flprotect,
765                                           Size >> 32,
766                                           Size & 0xffffffff,
767                                           0);
768   if (FileMappingHandle == NULL) {
769     error_code ec = windows_error(GetLastError());
770     if (FileDescriptor) {
771       if (CloseFD)
772         _close(FileDescriptor);
773     } else
774       ::CloseHandle(FileHandle);
775     return ec;
776   }
777
778   DWORD dwDesiredAccess;
779   switch (Mode) {
780   case readonly:  dwDesiredAccess = FILE_MAP_READ; break;
781   case readwrite: dwDesiredAccess = FILE_MAP_WRITE; break;
782   case priv:      dwDesiredAccess = FILE_MAP_COPY; break;
783   default: llvm_unreachable("invalid mapping mode");
784   }
785   Mapping = ::MapViewOfFile(FileMappingHandle,
786                             dwDesiredAccess,
787                             Offset >> 32,
788                             Offset & 0xffffffff,
789                             Size);
790   if (Mapping == NULL) {
791     error_code ec = windows_error(GetLastError());
792     ::CloseHandle(FileMappingHandle);
793     if (FileDescriptor) {
794       if (CloseFD)
795         _close(FileDescriptor);
796     } else
797       ::CloseHandle(FileHandle);
798     return ec;
799   }
800
801   if (Size == 0) {
802     MEMORY_BASIC_INFORMATION mbi;
803     SIZE_T Result = VirtualQuery(Mapping, &mbi, sizeof(mbi));
804     if (Result == 0) {
805       error_code ec = windows_error(GetLastError());
806       ::UnmapViewOfFile(Mapping);
807       ::CloseHandle(FileMappingHandle);
808       if (FileDescriptor) {
809         if (CloseFD)
810           _close(FileDescriptor);
811       } else
812         ::CloseHandle(FileHandle);
813       return ec;
814     }
815     Size = mbi.RegionSize;
816   }
817
818   // Close all the handles except for the view. It will keep the other handles
819   // alive.
820   ::CloseHandle(FileMappingHandle);
821   if (FileDescriptor) {
822     if (CloseFD)
823       _close(FileDescriptor); // Also closes FileHandle.
824   } else
825     ::CloseHandle(FileHandle);
826   return error_code::success();
827 }
828
829 mapped_file_region::mapped_file_region(const Twine &path,
830                                        mapmode mode,
831                                        uint64_t length,
832                                        uint64_t offset,
833                                        error_code &ec) 
834   : Mode(mode)
835   , Size(length)
836   , Mapping()
837   , FileDescriptor()
838   , FileHandle(INVALID_HANDLE_VALUE)
839   , FileMappingHandle() {
840   SmallString<128> path_storage;
841   SmallVector<wchar_t, 128> path_utf16;
842
843   // Convert path to UTF-16.
844   if ((ec = UTF8ToUTF16(path.toStringRef(path_storage), path_utf16)))
845     return;
846
847   // Get file handle for creating a file mapping.
848   FileHandle = ::CreateFileW(c_str(path_utf16),
849                              Mode == readonly ? GENERIC_READ
850                                               : GENERIC_READ | GENERIC_WRITE,
851                              Mode == readonly ? FILE_SHARE_READ
852                                               : 0,
853                              0,
854                              Mode == readonly ? OPEN_EXISTING
855                                               : OPEN_ALWAYS,
856                              Mode == readonly ? FILE_ATTRIBUTE_READONLY
857                                               : FILE_ATTRIBUTE_NORMAL,
858                              0);
859   if (FileHandle == INVALID_HANDLE_VALUE) {
860     ec = windows_error(::GetLastError());
861     return;
862   }
863
864   FileDescriptor = 0;
865   ec = init(FileDescriptor, true, offset);
866   if (ec) {
867     Mapping = FileMappingHandle = 0;
868     FileHandle = INVALID_HANDLE_VALUE;
869     FileDescriptor = 0;
870   }
871 }
872
873 mapped_file_region::mapped_file_region(int fd,
874                                        bool closefd,
875                                        mapmode mode,
876                                        uint64_t length,
877                                        uint64_t offset,
878                                        error_code &ec)
879   : Mode(mode)
880   , Size(length)
881   , Mapping()
882   , FileDescriptor(fd)
883   , FileHandle(INVALID_HANDLE_VALUE)
884   , FileMappingHandle() {
885   FileHandle = reinterpret_cast<HANDLE>(_get_osfhandle(fd));
886   if (FileHandle == INVALID_HANDLE_VALUE) {
887     if (closefd)
888       _close(FileDescriptor);
889     FileDescriptor = 0;
890     ec = make_error_code(errc::bad_file_descriptor);
891     return;
892   }
893
894   ec = init(FileDescriptor, closefd, offset);
895   if (ec) {
896     Mapping = FileMappingHandle = 0;
897     FileHandle = INVALID_HANDLE_VALUE;
898     FileDescriptor = 0;
899   }
900 }
901
902 mapped_file_region::~mapped_file_region() {
903   if (Mapping)
904     ::UnmapViewOfFile(Mapping);
905 }
906
907 #if LLVM_HAS_RVALUE_REFERENCES
908 mapped_file_region::mapped_file_region(mapped_file_region &&other)
909   : Mode(other.Mode)
910   , Size(other.Size)
911   , Mapping(other.Mapping)
912   , FileDescriptor(other.FileDescriptor)
913   , FileHandle(other.FileHandle)
914   , FileMappingHandle(other.FileMappingHandle) {
915   other.Mapping = other.FileMappingHandle = 0;
916   other.FileHandle = INVALID_HANDLE_VALUE;
917   other.FileDescriptor = 0;
918 }
919 #endif
920
921 mapped_file_region::mapmode mapped_file_region::flags() const {
922   assert(Mapping && "Mapping failed but used anyway!");
923   return Mode;
924 }
925
926 uint64_t mapped_file_region::size() const {
927   assert(Mapping && "Mapping failed but used anyway!");
928   return Size;
929 }
930
931 char *mapped_file_region::data() const {
932   assert(Mode != readonly && "Cannot get non const data for readonly mapping!");
933   assert(Mapping && "Mapping failed but used anyway!");
934   return reinterpret_cast<char*>(Mapping);
935 }
936
937 const char *mapped_file_region::const_data() const {
938   assert(Mapping && "Mapping failed but used anyway!");
939   return reinterpret_cast<const char*>(Mapping);
940 }
941
942 int mapped_file_region::alignment() {
943   SYSTEM_INFO SysInfo;
944   ::GetSystemInfo(&SysInfo);
945   return SysInfo.dwAllocationGranularity;
946 }
947
948 error_code detail::directory_iterator_construct(detail::DirIterState &it,
949                                                 StringRef path){
950   SmallVector<wchar_t, 128> path_utf16;
951
952   if (error_code ec = UTF8ToUTF16(path,
953                                   path_utf16))
954     return ec;
955
956   // Convert path to the format that Windows is happy with.
957   if (path_utf16.size() > 0 &&
958       !is_separator(path_utf16[path.size() - 1]) &&
959       path_utf16[path.size() - 1] != L':') {
960     path_utf16.push_back(L'\\');
961     path_utf16.push_back(L'*');
962   } else {
963     path_utf16.push_back(L'*');
964   }
965
966   //  Get the first directory entry.
967   WIN32_FIND_DATAW FirstFind;
968   ScopedFindHandle FindHandle(::FindFirstFileW(c_str(path_utf16), &FirstFind));
969   if (!FindHandle)
970     return windows_error(::GetLastError());
971
972   size_t FilenameLen = ::wcslen(FirstFind.cFileName);
973   while ((FilenameLen == 1 && FirstFind.cFileName[0] == L'.') ||
974          (FilenameLen == 2 && FirstFind.cFileName[0] == L'.' &&
975                               FirstFind.cFileName[1] == L'.'))
976     if (!::FindNextFileW(FindHandle, &FirstFind)) {
977       error_code ec = windows_error(::GetLastError());
978       // Check for end.
979       if (ec == windows_error::no_more_files)
980         return detail::directory_iterator_destruct(it);
981       return ec;
982     } else
983       FilenameLen = ::wcslen(FirstFind.cFileName);
984
985   // Construct the current directory entry.
986   SmallString<128> directory_entry_name_utf8;
987   if (error_code ec = UTF16ToUTF8(FirstFind.cFileName,
988                                   ::wcslen(FirstFind.cFileName),
989                                   directory_entry_name_utf8))
990     return ec;
991
992   it.IterationHandle = intptr_t(FindHandle.take());
993   SmallString<128> directory_entry_path(path);
994   path::append(directory_entry_path, directory_entry_name_utf8.str());
995   it.CurrentEntry = directory_entry(directory_entry_path.str());
996
997   return error_code::success();
998 }
999
1000 error_code detail::directory_iterator_destruct(detail::DirIterState &it) {
1001   if (it.IterationHandle != 0)
1002     // Closes the handle if it's valid.
1003     ScopedFindHandle close(HANDLE(it.IterationHandle));
1004   it.IterationHandle = 0;
1005   it.CurrentEntry = directory_entry();
1006   return error_code::success();
1007 }
1008
1009 error_code detail::directory_iterator_increment(detail::DirIterState &it) {
1010   WIN32_FIND_DATAW FindData;
1011   if (!::FindNextFileW(HANDLE(it.IterationHandle), &FindData)) {
1012     error_code ec = windows_error(::GetLastError());
1013     // Check for end.
1014     if (ec == windows_error::no_more_files)
1015       return detail::directory_iterator_destruct(it);
1016     return ec;
1017   }
1018
1019   size_t FilenameLen = ::wcslen(FindData.cFileName);
1020   if ((FilenameLen == 1 && FindData.cFileName[0] == L'.') ||
1021       (FilenameLen == 2 && FindData.cFileName[0] == L'.' &&
1022                            FindData.cFileName[1] == L'.'))
1023     return directory_iterator_increment(it);
1024
1025   SmallString<128> directory_entry_path_utf8;
1026   if (error_code ec = UTF16ToUTF8(FindData.cFileName,
1027                                   ::wcslen(FindData.cFileName),
1028                                   directory_entry_path_utf8))
1029     return ec;
1030
1031   it.CurrentEntry.replace_filename(Twine(directory_entry_path_utf8));
1032   return error_code::success();
1033 }
1034
1035 error_code map_file_pages(const Twine &path, off_t file_offset, size_t size,  
1036                                             bool map_writable, void *&result) {
1037   assert(0 && "NOT IMPLEMENTED");
1038   return windows_error::invalid_function;
1039 }
1040
1041 error_code unmap_file_pages(void *base, size_t size) {
1042   assert(0 && "NOT IMPLEMENTED");
1043   return windows_error::invalid_function;
1044 }
1045
1046
1047
1048 } // end namespace fs
1049 } // end namespace sys
1050 } // end namespace llvm