//===- llvm/Support/Windows/PathV2.inc - Windows Path Impl ------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements the Windows specific implementation of the PathV2 API.
//
//===----------------------------------------------------------------------===//
//===----------------------------------------------------------------------===//
//=== WARNING: Implementation here must contain only generic Windows code that
//=== is guaranteed to work on *all* Windows variants.
//===----------------------------------------------------------------------===//
#include "Windows.h"
#include <fcntl.h>
#include <io.h>
#include <sys/stat.h>
#include <sys/types.h>
#undef max
// MinGW doesn't define this.
#ifndef _ERRNO_T_DEFINED
#define _ERRNO_T_DEFINED
typedef int errno_t;
#endif
using namespace llvm;
namespace {
typedef BOOLEAN (WINAPI *PtrCreateSymbolicLinkW)(
/*__in*/ LPCWSTR lpSymlinkFileName,
/*__in*/ LPCWSTR lpTargetFileName,
/*__in*/ DWORD dwFlags);
PtrCreateSymbolicLinkW create_symbolic_link_api = PtrCreateSymbolicLinkW(
::GetProcAddress(::GetModuleHandleA("kernel32.dll"),
"CreateSymbolicLinkW"));
error_code UTF8ToUTF16(StringRef utf8, SmallVectorImpl<wchar_t> &utf16) {
int len = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
utf8.begin(), utf8.size(),
utf16.begin(), 0);
if (len == 0)
return windows_error(::GetLastError());
utf16.reserve(len + 1);
utf16.set_size(len);
len = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
utf8.begin(), utf8.size(),
utf16.begin(), utf16.size());
if (len == 0)
return windows_error(::GetLastError());
// Make utf16 null terminated.
utf16.push_back(0);
utf16.pop_back();
return error_code::success();
}
error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len,
SmallVectorImpl<char> &utf8) {
// Get length.
int len = ::WideCharToMultiByte(CP_UTF8, 0,
utf16, utf16_len,
utf8.begin(), 0,
NULL, NULL);
if (len == 0)
return windows_error(::GetLastError());
utf8.reserve(len);
utf8.set_size(len);
// Now do the actual conversion.
len = ::WideCharToMultiByte(CP_UTF8, 0,
utf16, utf16_len,
utf8.data(), utf8.size(),
NULL, NULL);
if (len == 0)
return windows_error(::GetLastError());
// Make utf8 null terminated.
utf8.push_back(0);
utf8.pop_back();
return error_code::success();
}
error_code TempDir(SmallVectorImpl<wchar_t> &result) {
retry_temp_dir:
DWORD len = ::GetTempPathW(result.capacity(), result.begin());
if (len == 0)
return windows_error(::GetLastError());
if (len > result.capacity()) {
result.reserve(len);
goto retry_temp_dir;
}
result.set_size(len);
return error_code::success();
}
bool is_separator(const wchar_t value) {
switch (value) {
case L'\\':
case L'/':
return true;
default:
return false;
}
}
}
namespace llvm {
namespace sys {
namespace fs {
error_code current_path(SmallVectorImpl<char> &result) {
SmallVector<wchar_t, 128> cur_path;
cur_path.reserve(128);
retry_cur_dir:
DWORD len = ::GetCurrentDirectoryW(cur_path.capacity(), cur_path.data());
// A zero return value indicates a failure other than insufficient space.
if (len == 0)
return windows_error(::GetLastError());
// If there's insufficient space, the len returned is larger than the len
// given.
if (len > cur_path.capacity()) {
cur_path.reserve(len);
goto retry_cur_dir;
}
cur_path