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.toNullTerminatedStringRef(from_storage);
166 StringRef t = to.toNullTerminatedStringRef(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 exists(const Twine &path, bool &result) {
185 SmallString<128> path_storage;
186 SmallVector<wchar_t, 128> path_utf16;
188 if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage),
192 DWORD attributes = ::GetFileAttributesW(path_utf16.begin());
194 if (attributes == INVALID_FILE_ATTRIBUTES) {
195 // See if the file didn't actually exist.
196 error_code ec = make_error_code(windows_error(::GetLastError()));
197 if (ec != error_code(windows_error::file_not_found) &&
198 ec != error_code(windows_error::path_not_found))
203 return make_error_code(errc::success);
206 error_code unique_file(const Twine &model, int &result_fd,
207 SmallVectorImpl<char> &result_path) {
208 // Use result_path as temp storage.
209 result_path.set_size(0);
210 StringRef m = model.toStringRef(result_path);
212 SmallVector<wchar_t, 128> model_utf16;
213 if (error_code ec = UTF8ToUTF16(m, model_utf16)) return ec;
215 // Make model absolute by prepending a temp directory if it's not already.
217 if (error_code ec = path::is_absolute(m, absolute)) return ec;
220 SmallVector<wchar_t, 64> temp_dir;
221 if (error_code ec = TempDir(temp_dir)) return ec;
222 // Handle c: by removing it.
223 if (model_utf16.size() > 2 && model_utf16[1] == L':') {
224 model_utf16.erase(model_utf16.begin(), model_utf16.begin() + 2);
226 model_utf16.insert(model_utf16.begin(), temp_dir.begin(), temp_dir.end());
229 // Replace '%' with random chars. From here on, DO NOT modify model. It may be
230 // needed if the randomly chosen path already exists.
231 SmallVector<wchar_t, 128> random_path_utf16;
233 // Get a Crypto Provider for CryptGenRandom.
234 AutoCryptoProvider CryptoProvider;
235 BOOL success = ::CryptAcquireContextW(&CryptoProvider.CryptoProvider,
241 return make_error_code(windows_error(::GetLastError()));
244 random_path_utf16.set_size(0);
245 for (SmallVectorImpl<wchar_t>::const_iterator i = model_utf16.begin(),
246 e = model_utf16.end();
250 if (!::CryptGenRandom(CryptoProvider, 1, &val))
251 return make_error_code(windows_error(::GetLastError()));
252 random_path_utf16.push_back("0123456789abcdef"[val & 15]);
255 random_path_utf16.push_back(*i);
257 // Make random_path_utf16 null terminated.
258 random_path_utf16.push_back(0);
259 random_path_utf16.pop_back();
261 // Try to create + open the path.
263 HANDLE TempFileHandle = ::CreateFileW(random_path_utf16.begin(),
264 GENERIC_READ | GENERIC_WRITE,
267 // Return ERROR_FILE_EXISTS if the file
270 FILE_ATTRIBUTE_TEMPORARY,
272 if (TempFileHandle == INVALID_HANDLE_VALUE) {
273 // If the file existed, try again, otherwise, error.
274 error_code ec = make_error_code(windows_error(::GetLastError()));
275 if (ec == error_code(windows_error::file_exists))
276 goto retry_random_path;
277 // Check for non-existing parent directories.
278 if (ec == error_code(windows_error::path_not_found)) {
279 // Create the directories using result_path as temp storage.
280 if (error_code ec = UTF16ToUTF8(random_path_utf16.begin(),
281 random_path_utf16.size(), result_path))
283 StringRef p(result_path.begin(), result_path.size());
284 SmallString<64> dir_to_create;
285 for (path::const_iterator i = path::begin(p),
286 e = --path::end(p); i != e; ++i) {
287 if (error_code ec = path::append(dir_to_create, *i)) return ec;
289 if (error_code ec = exists(Twine(dir_to_create), Exists)) return ec;
291 // If c: doesn't exist, bail.
292 if (i->endswith(":"))
295 SmallVector<wchar_t, 64> dir_to_create_utf16;
296 if (error_code ec = UTF8ToUTF16(dir_to_create, dir_to_create_utf16))
299 // Create the directory.
300 if (!::CreateDirectoryW(dir_to_create_utf16.begin(), NULL))
301 return make_error_code(windows_error(::GetLastError()));
304 goto retry_create_file;
309 // Set result_path to the utf-8 representation of the path.
310 if (error_code ec = UTF16ToUTF8(random_path_utf16.begin(),
311 random_path_utf16.size(), result_path)) {
312 ::CloseHandle(TempFileHandle);
313 ::DeleteFileW(random_path_utf16.begin());
317 // Convert the Windows API file handle into a C-runtime handle.
318 int fd = ::_open_osfhandle(intptr_t(TempFileHandle), 0);
320 ::CloseHandle(TempFileHandle);
321 ::DeleteFileW(random_path_utf16.begin());
322 // MSDN doesn't say anything about _open_osfhandle setting errno or
323 // GetLastError(), so just return invalid_handle.
324 return make_error_code(windows_error::invalid_handle);
328 return make_error_code(errc::success);
330 } // end namespace fs
331 } // end namespace sys
332 } // end namespace llvm