Initialize TempFileHandle.
[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 = INVALID_HANDLE_VALUE;
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 create_directory(const Twine &path, bool &existed) {
328   SmallString<128> path_storage;
329   SmallVector<wchar_t, 128> path_utf16;
330
331   if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage),
332                                   path_utf16))
333     return ec;
334
335   if (!::CreateDirectoryW(path_utf16.begin(), NULL)) {
336     error_code ec = windows_error(::GetLastError());
337     if (ec == windows_error::already_exists)
338       existed = true;
339     else
340       return ec;
341   } else
342     existed = false;
343
344   return error_code::success();
345 }
346
347 error_code create_hard_link(const Twine &to, const Twine &from) {
348   // Get arguments.
349   SmallString<128> from_storage;
350   SmallString<128> to_storage;
351   StringRef f = from.toStringRef(from_storage);
352   StringRef t = to.toStringRef(to_storage);
353
354   // Convert to utf-16.
355   SmallVector<wchar_t, 128> wide_from;
356   SmallVector<wchar_t, 128> wide_to;
357   if (error_code ec = UTF8ToUTF16(f, wide_from)) return ec;
358   if (error_code ec = UTF8ToUTF16(t, wide_to)) return ec;
359
360   if (!::CreateHardLinkW(wide_from.begin(), wide_to.begin(), NULL))
361     return windows_error(::GetLastError());
362
363   return error_code::success();
364 }
365
366 error_code create_symlink(const Twine &to, const Twine &from) {
367   // Only do it if the function is available at runtime.
368   if (!create_symbolic_link_api)
369     return make_error_code(errc::function_not_supported);
370
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 (!create_symbolic_link_api(wide_from.begin(), wide_to.begin(), 0))
384     return windows_error(::GetLastError());
385
386   return error_code::success();
387 }
388
389 error_code remove(const Twine &path, bool &existed) {
390   SmallString<128> path_storage;
391   SmallVector<wchar_t, 128> path_utf16;
392
393   file_status st;
394   if (error_code ec = status(path, st))
395     return ec;
396
397   if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage),
398                                   path_utf16))
399     return ec;
400
401   if (st.type() == file_type::directory_file) {
402     if (!::RemoveDirectoryW(c_str(path_utf16))) {
403       error_code ec = windows_error(::GetLastError());
404       if (ec != windows_error::file_not_found)
405         return ec;
406       existed = false;
407     } else
408       existed = true;
409   } else {
410     if (!::DeleteFileW(c_str(path_utf16))) {
411       error_code ec = windows_error(::GetLastError());
412       if (ec != windows_error::file_not_found)
413         return ec;
414       existed = false;
415     } else
416       existed = true;
417   }
418
419   return error_code::success();
420 }
421
422 error_code rename(const Twine &from, const Twine &to) {
423   // Get arguments.
424   SmallString<128> from_storage;
425   SmallString<128> to_storage;
426   StringRef f = from.toStringRef(from_storage);
427   StringRef t = to.toStringRef(to_storage);
428
429   // Convert to utf-16.
430   SmallVector<wchar_t, 128> wide_from;
431   SmallVector<wchar_t, 128> wide_to;
432   if (error_code ec = UTF8ToUTF16(f, wide_from)) return ec;
433   if (error_code ec = UTF8ToUTF16(t, wide_to)) return ec;
434
435   error_code ec = error_code::success();
436   for (int i = 0; i < 2000; i++) {
437     if (::MoveFileExW(wide_from.begin(), wide_to.begin(),
438                       MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING))
439       return error_code::success();
440     ec = windows_error(::GetLastError());
441     if (ec != windows_error::access_denied)
442       break;
443     // Retry MoveFile() at ACCESS_DENIED.
444     // System scanners (eg. indexer) might open the source file when
445     // It is written and closed.
446     ::Sleep(1);
447   }
448
449   return ec;
450 }
451
452 error_code resize_file(const Twine &path, uint64_t size) {
453   SmallString<128> path_storage;
454   SmallVector<wchar_t, 128> path_utf16;
455
456   if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage),
457                                   path_utf16))
458     return ec;
459
460   int fd = ::_wopen(path_utf16.begin(), O_BINARY | _O_RDWR, S_IWRITE);
461   if (fd == -1)
462     return error_code(errno, generic_category());
463 #ifdef HAVE__CHSIZE_S
464   errno_t error = ::_chsize_s(fd, size);
465 #else
466   errno_t error = ::_chsize(fd, size);
467 #endif
468   ::close(fd);
469   return error_code(error, generic_category());
470 }
471
472 error_code exists(const Twine &path, bool &result) {
473   SmallString<128> path_storage;
474   SmallVector<wchar_t, 128> path_utf16;
475
476   if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage),
477                                   path_utf16))
478     return ec;
479
480   DWORD attributes = ::GetFileAttributesW(path_utf16.begin());
481
482   if (attributes == INVALID_FILE_ATTRIBUTES) {
483     // See if the file didn't actually exist.
484     error_code ec = make_error_code(windows_error(::GetLastError()));
485     if (ec != windows_error::file_not_found &&
486         ec != windows_error::path_not_found)
487       return ec;
488     result = false;
489   } else
490     result = true;
491   return error_code::success();
492 }
493
494 bool can_write(const Twine &Path) {
495   // FIXME: take security attributes into account.
496   SmallString<128> PathStorage;
497   SmallVector<wchar_t, 128> PathUtf16;
498
499   if (UTF8ToUTF16(Path.toStringRef(PathStorage), PathUtf16))
500     return false;
501
502   DWORD Attr = ::GetFileAttributesW(PathUtf16.begin());
503   return (Attr != INVALID_FILE_ATTRIBUTES) && !(Attr & FILE_ATTRIBUTE_READONLY);
504 }
505
506 bool can_execute(const Twine &Path) {
507   SmallString<128> PathStorage;
508   SmallVector<wchar_t, 128> PathUtf16;
509
510   if (UTF8ToUTF16(Path.toStringRef(PathStorage), PathUtf16))
511     return false;
512
513   DWORD Attr = ::GetFileAttributesW(PathUtf16.begin());
514   return Attr != INVALID_FILE_ATTRIBUTES;
515 }
516
517 bool equivalent(file_status A, file_status B) {
518   assert(status_known(A) && status_known(B));
519   return A.FileIndexHigh      == B.FileIndexHigh &&
520          A.FileIndexLow       == B.FileIndexLow &&
521          A.FileSizeHigh       == B.FileSizeHigh &&
522          A.FileSizeLow        == B.FileSizeLow &&
523          A.LastWriteTimeHigh  == B.LastWriteTimeHigh &&
524          A.LastWriteTimeLow   == B.LastWriteTimeLow &&
525          A.VolumeSerialNumber == B.VolumeSerialNumber;
526 }
527
528 error_code equivalent(const Twine &A, const Twine &B, bool &result) {
529   file_status fsA, fsB;
530   if (error_code ec = status(A, fsA)) return ec;
531   if (error_code ec = status(B, fsB)) return ec;
532   result = equivalent(fsA, fsB);
533   return error_code::success();
534 }
535
536 error_code getUniqueID(const Twine Path, uint64_t &Result) {
537   file_status Status;
538   if (error_code E = status(Path, Status))
539     return E;
540
541   // The file is uniquely identified by the volume serial number along
542   // with the 64-bit file identifier.
543   Result = (static_cast<uint64_t>(Status.FileIndexHigh) << 32ULL) |
544            static_cast<uint64_t>(Status.FileIndexLow);
545   
546   // Because the serial number is 32-bits, but we've already used up all 64
547   // bits for the file index, XOR the serial number into the high 32 bits of
548   // the resulting value.  We could potentially get collisons from this, but
549   // the likelihood is low.
550   Result ^= (static_cast<uint64_t>(Status.VolumeSerialNumber) << 32ULL);
551
552   return error_code::success();
553 }
554
555 static bool isReservedName(StringRef path) {
556   // This list of reserved names comes from MSDN, at:
557   // http://msdn.microsoft.com/en-us/library/aa365247%28v=vs.85%29.aspx
558   static const char *sReservedNames[] = { "nul", "con", "prn", "aux",
559                               "com1", "com2", "com3", "com4", "com5", "com6",
560                               "com7", "com8", "com9", "lpt1", "lpt2", "lpt3",
561                               "lpt4", "lpt5", "lpt6", "lpt7", "lpt8", "lpt9" };
562
563   // First, check to see if this is a device namespace, which always
564   // starts with \\.\, since device namespaces are not legal file paths.
565   if (path.startswith("\\\\.\\"))
566     return true;
567
568   // Then compare against the list of ancient reserved names
569   for (size_t i = 0; i < array_lengthof(sReservedNames); ++i) {
570     if (path.equals_lower(sReservedNames[i]))
571       return true;
572   }
573
574   // The path isn't what we consider reserved.
575   return false;
576 }
577
578 static error_code getStatus(HANDLE FileHandle, file_status &Result) {
579   if (FileHandle == INVALID_HANDLE_VALUE)
580     goto handle_status_error;
581
582   switch (::GetFileType(FileHandle)) {
583   default:
584     llvm_unreachable("Don't know anything about this file type");
585   case FILE_TYPE_UNKNOWN: {
586     DWORD Err = ::GetLastError();
587     if (Err != NO_ERROR)
588       return windows_error(Err);
589     Result = file_status(file_type::type_unknown);
590     return error_code::success();
591   }
592   case FILE_TYPE_DISK:
593     break;
594   case FILE_TYPE_CHAR:
595     Result = file_status(file_type::character_file);
596     return error_code::success();
597   case FILE_TYPE_PIPE:
598     Result = file_status(file_type::fifo_file);
599     return error_code::success();
600   }
601
602   BY_HANDLE_FILE_INFORMATION Info;
603   if (!::GetFileInformationByHandle(FileHandle, &Info))
604     goto handle_status_error;
605
606   Result = file_status(
607         file_type::regular_file, Info.ftLastWriteTime.dwHighDateTime,
608         Info.ftLastWriteTime.dwLowDateTime, Info.dwVolumeSerialNumber,
609         Info.nFileSizeHigh, Info.nFileSizeLow, Info.nFileIndexHigh,
610         Info.nFileIndexLow);
611   return error_code::success();
612
613 handle_status_error:
614   error_code EC = windows_error(::GetLastError());
615   if (EC == windows_error::file_not_found ||
616       EC == windows_error::path_not_found)
617     Result = file_status(file_type::file_not_found);
618   else if (EC == windows_error::sharing_violation)
619     Result = file_status(file_type::type_unknown);
620   else {
621     Result = file_status(file_type::status_error);
622     return EC;
623   }
624   return error_code::success();
625 }
626
627 error_code status(const Twine &path, file_status &result) {
628   SmallString<128> path_storage;
629   SmallVector<wchar_t, 128> path_utf16;
630
631   StringRef path8 = path.toStringRef(path_storage);
632   if (isReservedName(path8)) {
633     result = file_status(file_type::character_file);
634     return error_code::success();
635   }
636
637   if (error_code ec = UTF8ToUTF16(path8, path_utf16))
638     return ec;
639
640   DWORD attr = ::GetFileAttributesW(path_utf16.begin());
641   if (attr == INVALID_FILE_ATTRIBUTES)
642     return getStatus(INVALID_HANDLE_VALUE, result);
643
644   // Handle reparse points.
645   if (attr & FILE_ATTRIBUTE_REPARSE_POINT) {
646     ScopedFileHandle h(
647       ::CreateFileW(path_utf16.begin(),
648                     0, // Attributes only.
649                     FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
650                     NULL,
651                     OPEN_EXISTING,
652                     FILE_FLAG_BACKUP_SEMANTICS,
653                     0));
654     if (!h)
655       return getStatus(INVALID_HANDLE_VALUE, result);
656   }
657
658   if (attr & FILE_ATTRIBUTE_DIRECTORY)
659     result = file_status(file_type::directory_file);
660   else {
661     ScopedFileHandle h(
662       ::CreateFileW(path_utf16.begin(),
663                     0, // Attributes only.
664                     FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
665                     NULL,
666                     OPEN_EXISTING,
667                     FILE_FLAG_BACKUP_SEMANTICS,
668                     0));
669     if (!h)
670       return getStatus(INVALID_HANDLE_VALUE, result);
671     BY_HANDLE_FILE_INFORMATION Info;
672     if (!::GetFileInformationByHandle(h, &Info))
673       return getStatus(INVALID_HANDLE_VALUE, result);
674
675     return getStatus(h, result);
676   }
677   return error_code::success();
678 }
679
680 error_code status(int FD, file_status &Result) {
681   HANDLE FileHandle = reinterpret_cast<HANDLE>(_get_osfhandle(FD));
682   return getStatus(FileHandle, Result);
683 }
684
685 error_code setLastModificationAndAccessTime(int FD, TimeValue Time) {
686   ULARGE_INTEGER UI;
687   UI.QuadPart = Time.toWin32Time();
688   FILETIME FT;
689   FT.dwLowDateTime = UI.LowPart;
690   FT.dwHighDateTime = UI.HighPart;
691   HANDLE FileHandle = reinterpret_cast<HANDLE>(_get_osfhandle(FD));
692   if (!SetFileTime(FileHandle, NULL, &FT, &FT))
693     return windows_error(::GetLastError());
694   return error_code::success();
695 }
696
697 error_code get_magic(const Twine &path, uint32_t len,
698                      SmallVectorImpl<char> &result) {
699   SmallString<128> path_storage;
700   SmallVector<wchar_t, 128> path_utf16;
701   result.set_size(0);
702
703   // Convert path to UTF-16.
704   if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage),
705                                   path_utf16))
706     return ec;
707
708   // Open file.
709   HANDLE file = ::CreateFileW(c_str(path_utf16),
710                               GENERIC_READ,
711                               FILE_SHARE_READ,
712                               NULL,
713                               OPEN_EXISTING,
714                               FILE_ATTRIBUTE_READONLY,
715                               NULL);
716   if (file == INVALID_HANDLE_VALUE)
717     return windows_error(::GetLastError());
718
719   // Allocate buffer.
720   result.reserve(len);
721
722   // Get magic!
723   DWORD bytes_read = 0;
724   BOOL read_success = ::ReadFile(file, result.data(), len, &bytes_read, NULL);
725   error_code ec = windows_error(::GetLastError());
726   ::CloseHandle(file);
727   if (!read_success || (bytes_read != len)) {
728     // Set result size to the number of bytes read if it's valid.
729     if (bytes_read <= len)
730       result.set_size(bytes_read);
731     // ERROR_HANDLE_EOF is mapped to errc::value_too_large.
732     return ec;
733   }
734
735   result.set_size(len);
736   return error_code::success();
737 }
738
739 error_code mapped_file_region::init(int FD, bool CloseFD, uint64_t Offset) {
740   FileDescriptor = FD;
741   // Make sure that the requested size fits within SIZE_T.
742   if (Size > std::numeric_limits<SIZE_T>::max()) {
743     if (FileDescriptor) {
744       if (CloseFD)
745         _close(FileDescriptor);
746     } else
747       ::CloseHandle(FileHandle);
748     return make_error_code(errc::invalid_argument);
749   }
750
751   DWORD flprotect;
752   switch (Mode) {
753   case readonly:  flprotect = PAGE_READONLY; break;
754   case readwrite: flprotect = PAGE_READWRITE; break;
755   case priv:      flprotect = PAGE_WRITECOPY; break;
756   }
757
758   FileMappingHandle = ::CreateFileMapping(FileHandle,
759                                           0,
760                                           flprotect,
761                                           Size >> 32,
762                                           Size & 0xffffffff,
763                                           0);
764   if (FileMappingHandle == NULL) {
765     error_code ec = windows_error(GetLastError());
766     if (FileDescriptor) {
767       if (CloseFD)
768         _close(FileDescriptor);
769     } else
770       ::CloseHandle(FileHandle);
771     return ec;
772   }
773
774   DWORD dwDesiredAccess;
775   switch (Mode) {
776   case readonly:  dwDesiredAccess = FILE_MAP_READ; break;
777   case readwrite: dwDesiredAccess = FILE_MAP_WRITE; break;
778   case priv:      dwDesiredAccess = FILE_MAP_COPY; break;
779   }
780   Mapping = ::MapViewOfFile(FileMappingHandle,
781                             dwDesiredAccess,
782                             Offset >> 32,
783                             Offset & 0xffffffff,
784                             Size);
785   if (Mapping == NULL) {
786     error_code ec = windows_error(GetLastError());
787     ::CloseHandle(FileMappingHandle);
788     if (FileDescriptor) {
789       if (CloseFD)
790         _close(FileDescriptor);
791     } else
792       ::CloseHandle(FileHandle);
793     return ec;
794   }
795
796   if (Size == 0) {
797     MEMORY_BASIC_INFORMATION mbi;
798     SIZE_T Result = VirtualQuery(Mapping, &mbi, sizeof(mbi));
799     if (Result == 0) {
800       error_code ec = windows_error(GetLastError());
801       ::UnmapViewOfFile(Mapping);
802       ::CloseHandle(FileMappingHandle);
803       if (FileDescriptor) {
804         if (CloseFD)
805           _close(FileDescriptor);
806       } else
807         ::CloseHandle(FileHandle);
808       return ec;
809     }
810     Size = mbi.RegionSize;
811   }
812
813   // Close all the handles except for the view. It will keep the other handles
814   // alive.
815   ::CloseHandle(FileMappingHandle);
816   if (FileDescriptor) {
817     if (CloseFD)
818       _close(FileDescriptor); // Also closes FileHandle.
819   } else
820     ::CloseHandle(FileHandle);
821   return error_code::success();
822 }
823
824 mapped_file_region::mapped_file_region(const Twine &path,
825                                        mapmode mode,
826                                        uint64_t length,
827                                        uint64_t offset,
828                                        error_code &ec) 
829   : Mode(mode)
830   , Size(length)
831   , Mapping()
832   , FileDescriptor()
833   , FileHandle(INVALID_HANDLE_VALUE)
834   , FileMappingHandle() {
835   SmallString<128> path_storage;
836   SmallVector<wchar_t, 128> path_utf16;
837
838   // Convert path to UTF-16.
839   if ((ec = UTF8ToUTF16(path.toStringRef(path_storage), path_utf16)))
840     return;
841
842   // Get file handle for creating a file mapping.
843   FileHandle = ::CreateFileW(c_str(path_utf16),
844                              Mode == readonly ? GENERIC_READ
845                                               : GENERIC_READ | GENERIC_WRITE,
846                              Mode == readonly ? FILE_SHARE_READ
847                                               : 0,
848                              0,
849                              Mode == readonly ? OPEN_EXISTING
850                                               : OPEN_ALWAYS,
851                              Mode == readonly ? FILE_ATTRIBUTE_READONLY
852                                               : FILE_ATTRIBUTE_NORMAL,
853                              0);
854   if (FileHandle == INVALID_HANDLE_VALUE) {
855     ec = windows_error(::GetLastError());
856     return;
857   }
858
859   FileDescriptor = 0;
860   ec = init(FileDescriptor, true, offset);
861   if (ec) {
862     Mapping = FileMappingHandle = 0;
863     FileHandle = INVALID_HANDLE_VALUE;
864     FileDescriptor = 0;
865   }
866 }
867
868 mapped_file_region::mapped_file_region(int fd,
869                                        bool closefd,
870                                        mapmode mode,
871                                        uint64_t length,
872                                        uint64_t offset,
873                                        error_code &ec)
874   : Mode(mode)
875   , Size(length)
876   , Mapping()
877   , FileDescriptor(fd)
878   , FileHandle(INVALID_HANDLE_VALUE)
879   , FileMappingHandle() {
880   FileHandle = reinterpret_cast<HANDLE>(_get_osfhandle(fd));
881   if (FileHandle == INVALID_HANDLE_VALUE) {
882     if (closefd)
883       _close(FileDescriptor);
884     FileDescriptor = 0;
885     ec = make_error_code(errc::bad_file_descriptor);
886     return;
887   }
888
889   ec = init(FileDescriptor, closefd, offset);
890   if (ec) {
891     Mapping = FileMappingHandle = 0;
892     FileHandle = INVALID_HANDLE_VALUE;
893     FileDescriptor = 0;
894   }
895 }
896
897 mapped_file_region::~mapped_file_region() {
898   if (Mapping)
899     ::UnmapViewOfFile(Mapping);
900 }
901
902 #if LLVM_HAS_RVALUE_REFERENCES
903 mapped_file_region::mapped_file_region(mapped_file_region &&other)
904   : Mode(other.Mode)
905   , Size(other.Size)
906   , Mapping(other.Mapping)
907   , FileDescriptor(other.FileDescriptor)
908   , FileHandle(other.FileHandle)
909   , FileMappingHandle(other.FileMappingHandle) {
910   other.Mapping = other.FileMappingHandle = 0;
911   other.FileHandle = INVALID_HANDLE_VALUE;
912   other.FileDescriptor = 0;
913 }
914 #endif
915
916 mapped_file_region::mapmode mapped_file_region::flags() const {
917   assert(Mapping && "Mapping failed but used anyway!");
918   return Mode;
919 }
920
921 uint64_t mapped_file_region::size() const {
922   assert(Mapping && "Mapping failed but used anyway!");
923   return Size;
924 }
925
926 char *mapped_file_region::data() const {
927   assert(Mode != readonly && "Cannot get non const data for readonly mapping!");
928   assert(Mapping && "Mapping failed but used anyway!");
929   return reinterpret_cast<char*>(Mapping);
930 }
931
932 const char *mapped_file_region::const_data() const {
933   assert(Mapping && "Mapping failed but used anyway!");
934   return reinterpret_cast<const char*>(Mapping);
935 }
936
937 int mapped_file_region::alignment() {
938   SYSTEM_INFO SysInfo;
939   ::GetSystemInfo(&SysInfo);
940   return SysInfo.dwAllocationGranularity;
941 }
942
943 error_code detail::directory_iterator_construct(detail::DirIterState &it,
944                                                 StringRef path){
945   SmallVector<wchar_t, 128> path_utf16;
946
947   if (error_code ec = UTF8ToUTF16(path,
948                                   path_utf16))
949     return ec;
950
951   // Convert path to the format that Windows is happy with.
952   if (path_utf16.size() > 0 &&
953       !is_separator(path_utf16[path.size() - 1]) &&
954       path_utf16[path.size() - 1] != L':') {
955     path_utf16.push_back(L'\\');
956     path_utf16.push_back(L'*');
957   } else {
958     path_utf16.push_back(L'*');
959   }
960
961   //  Get the first directory entry.
962   WIN32_FIND_DATAW FirstFind;
963   ScopedFindHandle FindHandle(::FindFirstFileW(c_str(path_utf16), &FirstFind));
964   if (!FindHandle)
965     return windows_error(::GetLastError());
966
967   size_t FilenameLen = ::wcslen(FirstFind.cFileName);
968   while ((FilenameLen == 1 && FirstFind.cFileName[0] == L'.') ||
969          (FilenameLen == 2 && FirstFind.cFileName[0] == L'.' &&
970                               FirstFind.cFileName[1] == L'.'))
971     if (!::FindNextFileW(FindHandle, &FirstFind)) {
972       error_code ec = windows_error(::GetLastError());
973       // Check for end.
974       if (ec == windows_error::no_more_files)
975         return detail::directory_iterator_destruct(it);
976       return ec;
977     } else
978       FilenameLen = ::wcslen(FirstFind.cFileName);
979
980   // Construct the current directory entry.
981   SmallString<128> directory_entry_name_utf8;
982   if (error_code ec = UTF16ToUTF8(FirstFind.cFileName,
983                                   ::wcslen(FirstFind.cFileName),
984                                   directory_entry_name_utf8))
985     return ec;
986
987   it.IterationHandle = intptr_t(FindHandle.take());
988   SmallString<128> directory_entry_path(path);
989   path::append(directory_entry_path, directory_entry_name_utf8.str());
990   it.CurrentEntry = directory_entry(directory_entry_path.str());
991
992   return error_code::success();
993 }
994
995 error_code detail::directory_iterator_destruct(detail::DirIterState &it) {
996   if (it.IterationHandle != 0)
997     // Closes the handle if it's valid.
998     ScopedFindHandle close(HANDLE(it.IterationHandle));
999   it.IterationHandle = 0;
1000   it.CurrentEntry = directory_entry();
1001   return error_code::success();
1002 }
1003
1004 error_code detail::directory_iterator_increment(detail::DirIterState &it) {
1005   WIN32_FIND_DATAW FindData;
1006   if (!::FindNextFileW(HANDLE(it.IterationHandle), &FindData)) {
1007     error_code ec = windows_error(::GetLastError());
1008     // Check for end.
1009     if (ec == windows_error::no_more_files)
1010       return detail::directory_iterator_destruct(it);
1011     return ec;
1012   }
1013
1014   size_t FilenameLen = ::wcslen(FindData.cFileName);
1015   if ((FilenameLen == 1 && FindData.cFileName[0] == L'.') ||
1016       (FilenameLen == 2 && FindData.cFileName[0] == L'.' &&
1017                            FindData.cFileName[1] == L'.'))
1018     return directory_iterator_increment(it);
1019
1020   SmallString<128> directory_entry_path_utf8;
1021   if (error_code ec = UTF16ToUTF8(FindData.cFileName,
1022                                   ::wcslen(FindData.cFileName),
1023                                   directory_entry_path_utf8))
1024     return ec;
1025
1026   it.CurrentEntry.replace_filename(Twine(directory_entry_path_utf8));
1027   return error_code::success();
1028 }
1029
1030 error_code map_file_pages(const Twine &path, off_t file_offset, size_t size,  
1031                                             bool map_writable, void *&result) {
1032   assert(0 && "NOT IMPLEMENTED");
1033   return windows_error::invalid_function;
1034 }
1035
1036 error_code unmap_file_pages(void *base, size_t size) {
1037   assert(0 && "NOT IMPLEMENTED");
1038   return windows_error::invalid_function;
1039 }
1040
1041 error_code openFileForRead(const Twine &Name, int &ResultFD) {
1042   SmallString<128> PathStorage;
1043   SmallVector<wchar_t, 128> PathUTF16;
1044
1045   if (error_code EC = UTF8ToUTF16(Name.toStringRef(PathStorage),
1046                                   PathUTF16))
1047     return EC;
1048
1049   HANDLE H = ::CreateFileW(PathUTF16.begin(), GENERIC_READ,
1050                            FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1051                            OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1052   if (H == INVALID_HANDLE_VALUE) {
1053     error_code EC = windows_error(::GetLastError());
1054     // Provide a better error message when trying to open directories.
1055     // This only runs if we failed to open the file, so there is probably
1056     // no performances issues.
1057     if (EC != windows_error::access_denied)
1058       return EC;
1059     if (is_directory(Name))
1060       return error_code(errc::is_a_directory, posix_category());
1061     return EC;
1062   }
1063
1064   int FD = ::_open_osfhandle(intptr_t(H), 0);
1065   if (FD == -1) {
1066     ::CloseHandle(H);
1067     return windows_error::invalid_handle;
1068   }
1069
1070   ResultFD = FD;
1071   return error_code::success();
1072 }
1073
1074 error_code openFileForWrite(const Twine &Name, int &ResultFD,
1075                             sys::fs::OpenFlags Flags, unsigned Mode) {
1076   // Verify that we don't have both "append" and "excl".
1077   assert((!(Flags & sys::fs::F_Excl) || !(Flags & sys::fs::F_Append)) &&
1078          "Cannot specify both 'excl' and 'append' file creation flags!");
1079
1080   SmallString<128> PathStorage;
1081   SmallVector<wchar_t, 128> PathUTF16;
1082
1083   if (error_code EC = UTF8ToUTF16(Name.toStringRef(PathStorage),
1084                                   PathUTF16))
1085     return EC;
1086
1087   DWORD CreationDisposition;
1088   if (Flags & F_Excl)
1089     CreationDisposition = CREATE_NEW;
1090   else if (Flags & F_Append) 
1091     CreationDisposition = OPEN_ALWAYS;
1092   else
1093     CreationDisposition = CREATE_ALWAYS;
1094
1095   HANDLE H = ::CreateFileW(PathUTF16.begin(), GENERIC_WRITE,
1096                            FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1097                            CreationDisposition, FILE_ATTRIBUTE_NORMAL, NULL);
1098
1099   if (H == INVALID_HANDLE_VALUE) {
1100     error_code EC = windows_error(::GetLastError());
1101     // Provide a better error message when trying to open directories.
1102     // This only runs if we failed to open the file, so there is probably
1103     // no performances issues.
1104     if (EC != windows_error::access_denied)
1105       return EC;
1106     if (is_directory(Name))
1107       return error_code(errc::is_a_directory, posix_category());
1108     return EC;
1109   }
1110
1111   int OpenFlags = 0;
1112   if (Flags & F_Append)
1113     OpenFlags |= _O_APPEND;
1114
1115   if (!(Flags & F_Binary))
1116     OpenFlags |= _O_TEXT;
1117
1118   int FD = ::_open_osfhandle(intptr_t(H), OpenFlags);
1119   if (FD == -1) {
1120     ::CloseHandle(H);
1121     return windows_error::invalid_handle;
1122   }
1123
1124   ResultFD = FD;
1125   return error_code::success();
1126 }
1127
1128 } // end namespace fs
1129 } // end namespace sys
1130 } // end namespace llvm