1 //===- llvm/Support/Win32/PathV2.cpp - Windows Path Impl --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Windows specific implementation of the PathV2 API.
12 //===----------------------------------------------------------------------===//
14 //===----------------------------------------------------------------------===//
15 //=== WARNING: Implementation here must contain only generic Windows code that
16 //=== is guaranteed to work on *all* Windows variants.
17 //===----------------------------------------------------------------------===//
26 error_code UTF8ToUTF16(const StringRef &utf8,
27 SmallVectorImpl<wchar_t> &utf16) {
28 int len = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
29 utf8.begin(), utf8.size(),
33 return make_error_code(windows_error(::GetLastError()));
35 utf16.reserve(len + 1);
38 len = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
39 utf8.begin(), utf8.size(),
40 utf16.begin(), utf16.size());
43 return make_error_code(windows_error(::GetLastError()));
45 // Make utf16 null terminated.
49 return make_error_code(errc::success);
52 error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len,
53 SmallVectorImpl<char> &utf8) {
55 int len = ::WideCharToMultiByte(CP_UTF8, NULL,
61 return make_error_code(windows_error(::GetLastError()));
66 // Now do the actual conversion.
67 len = ::WideCharToMultiByte(CP_UTF8, NULL,
69 utf8.data(), utf8.size(),
73 return make_error_code(windows_error(::GetLastError()));
75 // Make utf8 null terminated.
79 return make_error_code(errc::success);
82 error_code TempDir(SmallVectorImpl<wchar_t> &result) {
84 DWORD len = ::GetTempPathW(result.capacity(), result.begin());
87 return make_error_code(windows_error(::GetLastError()));
89 if (len > result.capacity()) {
95 return make_error_code(errc::success);
98 struct AutoCryptoProvider {
99 HCRYPTPROV CryptoProvider;
101 ~AutoCryptoProvider() {
102 ::CryptReleaseContext(CryptoProvider, 0);
105 operator HCRYPTPROV() const {return CryptoProvider;}
113 error_code current_path(SmallVectorImpl<char> &result) {
114 SmallVector<wchar_t, 128> cur_path;
115 cur_path.reserve(128);
117 DWORD len = ::GetCurrentDirectoryW(cur_path.capacity(), cur_path.data());
119 // A zero return value indicates a failure other than insufficient space.
121 return make_error_code(windows_error(::GetLastError()));
123 // If there's insufficient space, the len returned is larger than the len
125 if (len > cur_path.capacity()) {
126 cur_path.reserve(len);
130 cur_path.set_size(len);
131 // cur_path now holds the current directory in utf-16. Convert to utf-8.
133 // Find out how much space we need. Sadly, this function doesn't return the
134 // size needed unless you tell it the result size is 0, which means you
135 // _always_ have to call it twice.
136 len = ::WideCharToMultiByte(CP_UTF8, NULL,
137 cur_path.data(), cur_path.size(),
142 return make_error_code(windows_error(::GetLastError()));
145 result.set_size(len);
146 // Now do the actual conversion.
147 len = ::WideCharToMultiByte(CP_UTF8, NULL,
148 cur_path.data(), cur_path.size(),
149 result.data(), result.size(),
152 return make_error_code(windows_error(::GetLastError()));
154 return make_error_code(errc::success);
157 } // end namespace path
161 error_code copy_file(const Twine &from, const Twine &to, copy_option copt) {
163 SmallString<128> from_storage;
164 SmallString<128> to_storage;
165 StringRef f = from.toStringRef(from_storage);
166 StringRef t = to.toStringRef(to_storage);
168 // Convert to utf-16.
169 SmallVector<wchar_t, 128> wide_from;
170 SmallVector<wchar_t, 128> wide_to;
171 if (error_code ec = UTF8ToUTF16(f, wide_from)) return ec;
172 if (error_code ec = UTF8ToUTF16(t, wide_to)) return ec;
175 BOOL res = ::CopyFileW(wide_from.begin(), wide_to.begin(),
176 copt != copy_option::overwrite_if_exists);
179 return make_error_code(windows_error(::GetLastError()));
181 return make_error_code(errc::success);
184 error_code create_directory(const Twine &path, bool &existed) {
185 SmallString<128> path_storage;
186 SmallVector<wchar_t, 128> path_utf16;
188 if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage),
192 if (!::CreateDirectoryW(path_utf16.begin(), NULL)) {
193 error_code ec = make_error_code(windows_error(::GetLastError()));
194 if (ec == make_error_code(windows_error::already_exists))
201 return make_error_code(errc::success);
204 error_code create_hard_link(const Twine &to, const Twine &from) {
206 SmallString<128> from_storage;
207 SmallString<128> to_storage;
208 StringRef f = from.toStringRef(from_storage);
209 StringRef t = to.toStringRef(to_storage);
211 // Convert to utf-16.
212 SmallVector<wchar_t, 128> wide_from;
213 SmallVector<wchar_t, 128> wide_to;
214 if (error_code ec = UTF8ToUTF16(f, wide_from)) return ec;
215 if (error_code ec = UTF8ToUTF16(t, wide_to)) return ec;
217 if (!::CreateHardLinkW(wide_from.begin(), wide_to.begin(), NULL))
218 return make_error_code(windows_error(::GetLastError()));
220 return make_error_code(errc::success);
223 error_code exists(const Twine &path, bool &result) {
224 SmallString<128> path_storage;
225 SmallVector<wchar_t, 128> path_utf16;
227 if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage),
231 DWORD attributes = ::GetFileAttributesW(path_utf16.begin());
233 if (attributes == INVALID_FILE_ATTRIBUTES) {
234 // See if the file didn't actually exist.
235 error_code ec = make_error_code(windows_error(::GetLastError()));
236 if (ec != error_code(windows_error::file_not_found) &&
237 ec != error_code(windows_error::path_not_found))
242 return make_error_code(errc::success);
245 error_code unique_file(const Twine &model, int &result_fd,
246 SmallVectorImpl<char> &result_path) {
247 // Use result_path as temp storage.
248 result_path.set_size(0);
249 StringRef m = model.toStringRef(result_path);
251 SmallVector<wchar_t, 128> model_utf16;
252 if (error_code ec = UTF8ToUTF16(m, model_utf16)) return ec;
254 // Make model absolute by prepending a temp directory if it's not already.
256 if (error_code ec = path::is_absolute(m, absolute)) return ec;
259 SmallVector<wchar_t, 64> temp_dir;
260 if (error_code ec = TempDir(temp_dir)) return ec;
261 // Handle c: by removing it.
262 if (model_utf16.size() > 2 && model_utf16[1] == L':') {
263 model_utf16.erase(model_utf16.begin(), model_utf16.begin() + 2);
265 model_utf16.insert(model_utf16.begin(), temp_dir.begin(), temp_dir.end());
268 // Replace '%' with random chars. From here on, DO NOT modify model. It may be
269 // needed if the randomly chosen path already exists.
270 SmallVector<wchar_t, 128> random_path_utf16;
272 // Get a Crypto Provider for CryptGenRandom.
273 AutoCryptoProvider CryptoProvider;
274 BOOL success = ::CryptAcquireContextW(&CryptoProvider.CryptoProvider,
280 return make_error_code(windows_error(::GetLastError()));
283 random_path_utf16.set_size(0);
284 for (SmallVectorImpl<wchar_t>::const_iterator i = model_utf16.begin(),
285 e = model_utf16.end();
289 if (!::CryptGenRandom(CryptoProvider, 1, &val))
290 return make_error_code(windows_error(::GetLastError()));
291 random_path_utf16.push_back("0123456789abcdef"[val & 15]);
294 random_path_utf16.push_back(*i);
296 // Make random_path_utf16 null terminated.
297 random_path_utf16.push_back(0);
298 random_path_utf16.pop_back();
300 // Try to create + open the path.
302 HANDLE TempFileHandle = ::CreateFileW(random_path_utf16.begin(),
303 GENERIC_READ | GENERIC_WRITE,
306 // Return ERROR_FILE_EXISTS if the file
309 FILE_ATTRIBUTE_TEMPORARY,
311 if (TempFileHandle == INVALID_HANDLE_VALUE) {
312 // If the file existed, try again, otherwise, error.
313 error_code ec = make_error_code(windows_error(::GetLastError()));
314 if (ec == error_code(windows_error::file_exists))
315 goto retry_random_path;
316 // Check for non-existing parent directories.
317 if (ec == error_code(windows_error::path_not_found)) {
318 // Create the directories using result_path as temp storage.
319 if (error_code ec = UTF16ToUTF8(random_path_utf16.begin(),
320 random_path_utf16.size(), result_path))
322 StringRef p(result_path.begin(), result_path.size());
323 SmallString<64> dir_to_create;
324 for (path::const_iterator i = path::begin(p),
325 e = --path::end(p); i != e; ++i) {
326 if (error_code ec = path::append(dir_to_create, *i)) return ec;
328 if (error_code ec = exists(Twine(dir_to_create), Exists)) return ec;
330 // If c: doesn't exist, bail.
331 if (i->endswith(":"))
334 SmallVector<wchar_t, 64> dir_to_create_utf16;
335 if (error_code ec = UTF8ToUTF16(dir_to_create, dir_to_create_utf16))
338 // Create the directory.
339 if (!::CreateDirectoryW(dir_to_create_utf16.begin(), NULL))
340 return make_error_code(windows_error(::GetLastError()));
343 goto retry_create_file;
348 // Set result_path to the utf-8 representation of the path.
349 if (error_code ec = UTF16ToUTF8(random_path_utf16.begin(),
350 random_path_utf16.size(), result_path)) {
351 ::CloseHandle(TempFileHandle);
352 ::DeleteFileW(random_path_utf16.begin());
356 // Convert the Windows API file handle into a C-runtime handle.
357 int fd = ::_open_osfhandle(intptr_t(TempFileHandle), 0);
359 ::CloseHandle(TempFileHandle);
360 ::DeleteFileW(random_path_utf16.begin());
361 // MSDN doesn't say anything about _open_osfhandle setting errno or
362 // GetLastError(), so just return invalid_handle.
363 return make_error_code(windows_error::invalid_handle);
367 return make_error_code(errc::success);
369 } // end namespace fs
370 } // end namespace sys
371 } // end namespace llvm