blob: e7aa93b6e4e7edcb2ac220d1277b8d0dfb72e415 [file] [log] [blame]
Michael J. Spencerdffde992010-11-29 22:28:51 +00001//===- llvm/Support/Win32/PathV2.cpp - 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 PathV2 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 "Windows.h"
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +000020#include <WinCrypt.h>
Michael J. Spencer3920d3b2010-12-03 17:54:07 +000021#include <fcntl.h>
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +000022#include <io.h>
Michael J. Spencer3920d3b2010-12-03 17:54:07 +000023#include <sys/stat.h>
24#include <sys/types.h>
Michael J. Spencerdffde992010-11-29 22:28:51 +000025
Michael J. Spenceraf45fc02010-12-03 18:03:28 +000026// MinGW doesn't define this.
Michael J. Spencerb39e33f2010-12-03 18:04:11 +000027#ifndef _ERRNO_T_DEFINED
28#define _ERRNO_T_DEFINED
29typedef int errno_t;
Michael J. Spenceraf45fc02010-12-03 18:03:28 +000030#endif
31
Michael J. Spencerbee0c382010-12-01 19:32:01 +000032using namespace llvm;
33
34namespace {
Michael J. Spencer998b4702010-12-03 17:53:23 +000035 typedef BOOLEAN (WINAPI *PtrCreateSymbolicLinkW)(
36 /*__in*/ LPCWSTR lpSymlinkFileName,
37 /*__in*/ LPCWSTR lpTargetFileName,
38 /*__in*/ DWORD dwFlags);
39
40 PtrCreateSymbolicLinkW create_symbolic_link_api = PtrCreateSymbolicLinkW(
Michael J. Spencer9b391c52010-12-03 07:41:25 +000041 ::GetProcAddress(::GetModuleHandleA("kernel32.dll"),
42 "CreateSymbolicLinkW"));
43
Michael J. Spencerbee0c382010-12-01 19:32:01 +000044 error_code UTF8ToUTF16(const StringRef &utf8,
45 SmallVectorImpl<wchar_t> &utf16) {
46 int len = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
47 utf8.begin(), utf8.size(),
48 utf16.begin(), 0);
49
50 if (len == 0)
51 return make_error_code(windows_error(::GetLastError()));
52
53 utf16.reserve(len + 1);
54 utf16.set_size(len);
55
56 len = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
57 utf8.begin(), utf8.size(),
58 utf16.begin(), utf16.size());
59
60 if (len == 0)
61 return make_error_code(windows_error(::GetLastError()));
62
63 // Make utf16 null terminated.
64 utf16.push_back(0);
65 utf16.pop_back();
66
67 return make_error_code(errc::success);
68 }
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +000069
70 error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len,
71 SmallVectorImpl<char> &utf8) {
72 // Get length.
73 int len = ::WideCharToMultiByte(CP_UTF8, NULL,
74 utf16, utf16_len,
75 utf8.begin(), 0,
76 NULL, NULL);
77
78 if (len == 0)
79 return make_error_code(windows_error(::GetLastError()));
80
81 utf8.reserve(len);
82 utf8.set_size(len);
83
84 // Now do the actual conversion.
85 len = ::WideCharToMultiByte(CP_UTF8, NULL,
86 utf16, utf16_len,
87 utf8.data(), utf8.size(),
88 NULL, NULL);
89
90 if (len == 0)
91 return make_error_code(windows_error(::GetLastError()));
92
93 // Make utf8 null terminated.
94 utf8.push_back(0);
95 utf8.pop_back();
96
97 return make_error_code(errc::success);
98 }
99
100 error_code TempDir(SmallVectorImpl<wchar_t> &result) {
101 retry_temp_dir:
102 DWORD len = ::GetTempPathW(result.capacity(), result.begin());
103
104 if (len == 0)
105 return make_error_code(windows_error(::GetLastError()));
106
107 if (len > result.capacity()) {
108 result.reserve(len);
109 goto retry_temp_dir;
110 }
111
112 result.set_size(len);
113 return make_error_code(errc::success);
114 }
115
116 struct AutoCryptoProvider {
117 HCRYPTPROV CryptoProvider;
118
119 ~AutoCryptoProvider() {
120 ::CryptReleaseContext(CryptoProvider, 0);
121 }
122
123 operator HCRYPTPROV() const {return CryptoProvider;}
124 };
Michael J. Spencerbee0c382010-12-01 19:32:01 +0000125}
126
Michael J. Spencerdffde992010-11-29 22:28:51 +0000127namespace llvm {
128namespace sys {
129namespace path {
130
131error_code current_path(SmallVectorImpl<char> &result) {
132 SmallVector<wchar_t, 128> cur_path;
133 cur_path.reserve(128);
134retry_cur_dir:
135 DWORD len = ::GetCurrentDirectoryW(cur_path.capacity(), cur_path.data());
136
137 // A zero return value indicates a failure other than insufficient space.
138 if (len == 0)
139 return make_error_code(windows_error(::GetLastError()));
140
141 // If there's insufficient space, the len returned is larger than the len
142 // given.
143 if (len > cur_path.capacity()) {
144 cur_path.reserve(len);
145 goto retry_cur_dir;
146 }
147
148 cur_path.set_size(len);
149 // cur_path now holds the current directory in utf-16. Convert to utf-8.
150
151 // Find out how much space we need. Sadly, this function doesn't return the
152 // size needed unless you tell it the result size is 0, which means you
153 // _always_ have to call it twice.
154 len = ::WideCharToMultiByte(CP_UTF8, NULL,
155 cur_path.data(), cur_path.size(),
156 result.data(), 0,
157 NULL, NULL);
158
159 if (len == 0)
160 return make_error_code(windows_error(::GetLastError()));
161
162 result.reserve(len);
163 result.set_size(len);
164 // Now do the actual conversion.
165 len = ::WideCharToMultiByte(CP_UTF8, NULL,
166 cur_path.data(), cur_path.size(),
167 result.data(), result.size(),
168 NULL, NULL);
169 if (len == 0)
170 return make_error_code(windows_error(::GetLastError()));
171
172 return make_error_code(errc::success);
173}
174
175} // end namespace path
Michael J. Spencerbee0c382010-12-01 19:32:01 +0000176
177namespace fs {
178
179error_code copy_file(const Twine &from, const Twine &to, copy_option copt) {
180 // Get arguments.
181 SmallString<128> from_storage;
182 SmallString<128> to_storage;
Michael J. Spencer2dbdeee2010-12-03 01:21:38 +0000183 StringRef f = from.toStringRef(from_storage);
184 StringRef t = to.toStringRef(to_storage);
Michael J. Spencerbee0c382010-12-01 19:32:01 +0000185
186 // Convert to utf-16.
187 SmallVector<wchar_t, 128> wide_from;
188 SmallVector<wchar_t, 128> wide_to;
189 if (error_code ec = UTF8ToUTF16(f, wide_from)) return ec;
190 if (error_code ec = UTF8ToUTF16(t, wide_to)) return ec;
191
192 // Copy the file.
193 BOOL res = ::CopyFileW(wide_from.begin(), wide_to.begin(),
194 copt != copy_option::overwrite_if_exists);
195
196 if (res == 0)
197 return make_error_code(windows_error(::GetLastError()));
198
199 return make_error_code(errc::success);
200}
201
Michael J. Spencerb83769f2010-12-03 05:42:11 +0000202error_code create_directory(const Twine &path, bool &existed) {
203 SmallString<128> path_storage;
204 SmallVector<wchar_t, 128> path_utf16;
205
206 if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage),
207 path_utf16))
208 return ec;
209
210 if (!::CreateDirectoryW(path_utf16.begin(), NULL)) {
211 error_code ec = make_error_code(windows_error(::GetLastError()));
212 if (ec == make_error_code(windows_error::already_exists))
213 existed = true;
214 else
215 return ec;
216 } else
217 existed = false;
218
219 return make_error_code(errc::success);
220}
221
Michael J. Spencerd7b305f2010-12-03 05:58:41 +0000222error_code create_hard_link(const Twine &to, const Twine &from) {
223 // Get arguments.
224 SmallString<128> from_storage;
225 SmallString<128> to_storage;
226 StringRef f = from.toStringRef(from_storage);
227 StringRef t = to.toStringRef(to_storage);
228
229 // Convert to utf-16.
230 SmallVector<wchar_t, 128> wide_from;
231 SmallVector<wchar_t, 128> wide_to;
232 if (error_code ec = UTF8ToUTF16(f, wide_from)) return ec;
233 if (error_code ec = UTF8ToUTF16(t, wide_to)) return ec;
234
235 if (!::CreateHardLinkW(wide_from.begin(), wide_to.begin(), NULL))
236 return make_error_code(windows_error(::GetLastError()));
237
238 return make_error_code(errc::success);
239}
240
Michael J. Spencer9b391c52010-12-03 07:41:25 +0000241error_code create_symlink(const Twine &to, const Twine &from) {
242 // Only do it if the function is available at runtime.
243 if (!create_symbolic_link_api)
244 return make_error_code(errc::function_not_supported);
245
246 // Get arguments.
247 SmallString<128> from_storage;
248 SmallString<128> to_storage;
249 StringRef f = from.toStringRef(from_storage);
250 StringRef t = to.toStringRef(to_storage);
251
252 // Convert to utf-16.
253 SmallVector<wchar_t, 128> wide_from;
254 SmallVector<wchar_t, 128> wide_to;
255 if (error_code ec = UTF8ToUTF16(f, wide_from)) return ec;
256 if (error_code ec = UTF8ToUTF16(t, wide_to)) return ec;
257
258 if (!create_symbolic_link_api(wide_from.begin(), wide_to.begin(), NULL))
259 return make_error_code(windows_error(::GetLastError()));
260
261 return make_error_code(errc::success);
262}
263
Michael J. Spencer106aa732010-12-03 17:53:43 +0000264error_code remove(const Twine &path, bool &existed) {
265 SmallString<128> path_storage;
266 SmallVector<wchar_t, 128> path_utf16;
267
268 if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage),
269 path_utf16))
270 return ec;
271
272 if (!::DeleteFileW(path_utf16.begin())) {
273 error_code ec = make_error_code(windows_error(::GetLastError()));
274 if (ec != make_error_code(windows_error::file_not_found))
275 return ec;
276 existed = false;
277 } else
278 existed = true;
279
280 return make_error_code(errc::success);
281}
282
Michael J. Spencera50b98c2010-12-03 17:53:55 +0000283error_code rename(const Twine &from, const Twine &to) {
284 // Get arguments.
285 SmallString<128> from_storage;
286 SmallString<128> to_storage;
287 StringRef f = from.toStringRef(from_storage);
288 StringRef t = to.toStringRef(to_storage);
289
290 // Convert to utf-16.
291 SmallVector<wchar_t, 128> wide_from;
292 SmallVector<wchar_t, 128> wide_to;
293 if (error_code ec = UTF8ToUTF16(f, wide_from)) return ec;
294 if (error_code ec = UTF8ToUTF16(t, wide_to)) return ec;
295
296 if (!::MoveFileW(wide_from.begin(), wide_to.begin()))
297 return make_error_code(windows_error(::GetLastError()));
298
299 return make_error_code(errc::success);
300}
301
Michael J. Spencer3920d3b2010-12-03 17:54:07 +0000302error_code resize_file(const Twine &path, uint64_t size) {
303 SmallString<128> path_storage;
304 SmallVector<wchar_t, 128> path_utf16;
305
306 if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage),
307 path_utf16))
308 return ec;
309
310 int fd = ::_wopen(path_utf16.begin(), O_BINARY, S_IREAD | S_IWRITE);
311 if (fd == -1)
312 return error_code(errno, generic_category());
Michael J. Spencerdb04b392010-12-03 18:48:56 +0000313#ifdef HAVE__CHSIZE_S
Michael J. Spencer3920d3b2010-12-03 17:54:07 +0000314 errno_t error = ::_chsize_s(fd, size);
Michael J. Spencerdb04b392010-12-03 18:48:56 +0000315#else
316 errno_t error = ::_chsize(fd, size);
317#endif
Michael J. Spencer3920d3b2010-12-03 17:54:07 +0000318 ::close(fd);
319 return error_code(error, generic_category());
320}
321
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000322error_code exists(const Twine &path, bool &result) {
323 SmallString<128> path_storage;
324 SmallVector<wchar_t, 128> path_utf16;
325
326 if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage),
327 path_utf16))
328 return ec;
329
330 DWORD attributes = ::GetFileAttributesW(path_utf16.begin());
331
332 if (attributes == INVALID_FILE_ATTRIBUTES) {
333 // See if the file didn't actually exist.
334 error_code ec = make_error_code(windows_error(::GetLastError()));
335 if (ec != error_code(windows_error::file_not_found) &&
336 ec != error_code(windows_error::path_not_found))
337 return ec;
338 result = false;
339 } else
340 result = true;
341 return make_error_code(errc::success);
342}
343
Michael J. Spencerb531f452010-12-03 18:49:13 +0000344error_code equivalent(const Twine &A, const Twine &B, bool &result) {
345 // Get arguments.
346 SmallString<128> a_storage;
347 SmallString<128> b_storage;
348 StringRef a = A.toStringRef(a_storage);
349 StringRef b = B.toStringRef(b_storage);
350
351 // Convert to utf-16.
352 SmallVector<wchar_t, 128> wide_a;
353 SmallVector<wchar_t, 128> wide_b;
354 if (error_code ec = UTF8ToUTF16(a, wide_a)) return ec;
355 if (error_code ec = UTF8ToUTF16(b, wide_b)) return ec;
356
357 AutoHandle HandleB(
358 ::CreateFileW(wide_b.begin(),
359 0,
360 FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
361 0,
362 OPEN_EXISTING,
363 FILE_FLAG_BACKUP_SEMANTICS,
364 0));
365
366 AutoHandle HandleA(
367 ::CreateFileW(wide_a.begin(),
368 0,
369 FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
370 0,
371 OPEN_EXISTING,
372 FILE_FLAG_BACKUP_SEMANTICS,
373 0));
374
375 // If both handles are invalid, it's an error.
376 if (HandleA == INVALID_HANDLE_VALUE &&
377 HandleB == INVALID_HANDLE_VALUE)
378 return make_error_code(windows_error(::GetLastError()));
379
380 // If only one is invalid, it's false.
381 if (HandleA == INVALID_HANDLE_VALUE &&
382 HandleB == INVALID_HANDLE_VALUE) {
383 result = false;
384 return make_error_code(errc::success);
385 }
386
387 // Get file information.
388 BY_HANDLE_FILE_INFORMATION InfoA, InfoB;
389 if (!::GetFileInformationByHandle(HandleA, &InfoA))
390 return make_error_code(windows_error(::GetLastError()));
391 if (!::GetFileInformationByHandle(HandleB, &InfoB))
392 return make_error_code(windows_error(::GetLastError()));
393
394 // See if it's all the same.
395 result =
396 InfoA.dwVolumeSerialNumber == InfoB.dwVolumeSerialNumber &&
397 InfoA.nFileIndexHigh == InfoB.nFileIndexHigh &&
398 InfoA.nFileIndexLow == InfoB.nFileIndexLow &&
399 InfoA.nFileSizeHigh == InfoB.nFileSizeHigh &&
400 InfoA.nFileSizeLow == InfoB.nFileSizeLow &&
401 InfoA.ftLastWriteTime.dwLowDateTime ==
402 InfoB.ftLastWriteTime.dwLowDateTime &&
403 InfoA.ftLastWriteTime.dwHighDateTime ==
404 InfoB.ftLastWriteTime.dwHighDateTime;
405
406 return make_error_code(errc::success);
407}
408
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000409error_code unique_file(const Twine &model, int &result_fd,
410 SmallVectorImpl<char> &result_path) {
411 // Use result_path as temp storage.
412 result_path.set_size(0);
413 StringRef m = model.toStringRef(result_path);
414
415 SmallVector<wchar_t, 128> model_utf16;
416 if (error_code ec = UTF8ToUTF16(m, model_utf16)) return ec;
417
418 // Make model absolute by prepending a temp directory if it's not already.
419 bool absolute;
420 if (error_code ec = path::is_absolute(m, absolute)) return ec;
421
422 if (!absolute) {
423 SmallVector<wchar_t, 64> temp_dir;
424 if (error_code ec = TempDir(temp_dir)) return ec;
425 // Handle c: by removing it.
426 if (model_utf16.size() > 2 && model_utf16[1] == L':') {
427 model_utf16.erase(model_utf16.begin(), model_utf16.begin() + 2);
428 }
429 model_utf16.insert(model_utf16.begin(), temp_dir.begin(), temp_dir.end());
430 }
431
432 // Replace '%' with random chars. From here on, DO NOT modify model. It may be
433 // needed if the randomly chosen path already exists.
434 SmallVector<wchar_t, 128> random_path_utf16;
435
436 // Get a Crypto Provider for CryptGenRandom.
437 AutoCryptoProvider CryptoProvider;
438 BOOL success = ::CryptAcquireContextW(&CryptoProvider.CryptoProvider,
439 NULL,
440 NULL,
441 PROV_RSA_FULL,
442 NULL);
443 if (!success)
444 return make_error_code(windows_error(::GetLastError()));
445
446retry_random_path:
447 random_path_utf16.set_size(0);
448 for (SmallVectorImpl<wchar_t>::const_iterator i = model_utf16.begin(),
449 e = model_utf16.end();
450 i != e; ++i) {
451 if (*i == L'%') {
452 BYTE val = 0;
453 if (!::CryptGenRandom(CryptoProvider, 1, &val))
454 return make_error_code(windows_error(::GetLastError()));
455 random_path_utf16.push_back("0123456789abcdef"[val & 15]);
456 }
457 else
458 random_path_utf16.push_back(*i);
459 }
460 // Make random_path_utf16 null terminated.
461 random_path_utf16.push_back(0);
462 random_path_utf16.pop_back();
463
464 // Try to create + open the path.
465retry_create_file:
466 HANDLE TempFileHandle = ::CreateFileW(random_path_utf16.begin(),
467 GENERIC_READ | GENERIC_WRITE,
468 FILE_SHARE_READ,
469 NULL,
470 // Return ERROR_FILE_EXISTS if the file
471 // already exists.
472 CREATE_NEW,
473 FILE_ATTRIBUTE_TEMPORARY,
474 NULL);
475 if (TempFileHandle == INVALID_HANDLE_VALUE) {
476 // If the file existed, try again, otherwise, error.
477 error_code ec = make_error_code(windows_error(::GetLastError()));
478 if (ec == error_code(windows_error::file_exists))
479 goto retry_random_path;
480 // Check for non-existing parent directories.
481 if (ec == error_code(windows_error::path_not_found)) {
482 // Create the directories using result_path as temp storage.
483 if (error_code ec = UTF16ToUTF8(random_path_utf16.begin(),
484 random_path_utf16.size(), result_path))
485 return ec;
486 StringRef p(result_path.begin(), result_path.size());
487 SmallString<64> dir_to_create;
488 for (path::const_iterator i = path::begin(p),
489 e = --path::end(p); i != e; ++i) {
490 if (error_code ec = path::append(dir_to_create, *i)) return ec;
491 bool Exists;
492 if (error_code ec = exists(Twine(dir_to_create), Exists)) return ec;
493 if (!Exists) {
494 // If c: doesn't exist, bail.
495 if (i->endswith(":"))
496 return ec;
497
498 SmallVector<wchar_t, 64> dir_to_create_utf16;
499 if (error_code ec = UTF8ToUTF16(dir_to_create, dir_to_create_utf16))
500 return ec;
501
502 // Create the directory.
503 if (!::CreateDirectoryW(dir_to_create_utf16.begin(), NULL))
504 return make_error_code(windows_error(::GetLastError()));
505 }
506 }
507 goto retry_create_file;
508 }
509 return ec;
510 }
511
512 // Set result_path to the utf-8 representation of the path.
513 if (error_code ec = UTF16ToUTF8(random_path_utf16.begin(),
514 random_path_utf16.size(), result_path)) {
515 ::CloseHandle(TempFileHandle);
516 ::DeleteFileW(random_path_utf16.begin());
517 return ec;
518 }
519
520 // Convert the Windows API file handle into a C-runtime handle.
521 int fd = ::_open_osfhandle(intptr_t(TempFileHandle), 0);
522 if (fd == -1) {
523 ::CloseHandle(TempFileHandle);
524 ::DeleteFileW(random_path_utf16.begin());
525 // MSDN doesn't say anything about _open_osfhandle setting errno or
526 // GetLastError(), so just return invalid_handle.
527 return make_error_code(windows_error::invalid_handle);
528 }
529
530 result_fd = fd;
531 return make_error_code(errc::success);
532}
Michael J. Spencerbee0c382010-12-01 19:32:01 +0000533} // end namespace fs
Michael J. Spencerdffde992010-11-29 22:28:51 +0000534} // end namespace sys
535} // end namespace llvm