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