2017-01-28 04:49:09 +08:00
|
|
|
#include "pch.h"
|
2016-09-19 11:50:08 +08:00
|
|
|
#include "vcpkg_System.h"
|
2017-03-11 08:54:08 +08:00
|
|
|
#include "vcpkg_Checks.h"
|
2016-09-19 11:50:08 +08:00
|
|
|
|
2017-01-06 04:47:08 +08:00
|
|
|
namespace vcpkg::System
|
2016-09-19 11:50:08 +08:00
|
|
|
{
|
|
|
|
fs::path get_exe_path_of_current_process()
|
|
|
|
{
|
2017-03-11 08:55:59 +08:00
|
|
|
wchar_t buf[_MAX_PATH];
|
2016-09-19 11:50:08 +08:00
|
|
|
int bytes = GetModuleFileNameW(nullptr, buf, _MAX_PATH);
|
|
|
|
if (bytes == 0)
|
|
|
|
std::abort();
|
|
|
|
return fs::path(buf, buf + bytes);
|
|
|
|
}
|
|
|
|
|
2017-03-29 03:33:25 +08:00
|
|
|
int cmd_execute_clean(const cwstring_view cmd_line)
|
2017-03-11 08:55:59 +08:00
|
|
|
{
|
2017-04-01 07:29:04 +08:00
|
|
|
static const std::wstring system_root = get_environmental_variable(L"SystemRoot").value_or_exit(VCPKG_LINE_INFO);
|
2017-03-11 08:55:59 +08:00
|
|
|
static const std::wstring system_32 = system_root + LR"(\system32)";
|
|
|
|
static const std::wstring new_PATH = Strings::wformat(LR"(Path=%s;%s;%s\WindowsPowerShell\v1.0\)", system_32, system_root, system_32);
|
|
|
|
|
|
|
|
std::vector<std::wstring> env_wstrings =
|
|
|
|
{
|
|
|
|
L"ALLUSERSPROFILE",
|
|
|
|
L"APPDATA",
|
|
|
|
L"CommonProgramFiles",
|
|
|
|
L"CommonProgramFiles(x86)",
|
|
|
|
L"CommonProgramW6432",
|
|
|
|
L"COMPUTERNAME",
|
|
|
|
L"ComSpec",
|
|
|
|
L"HOMEDRIVE",
|
|
|
|
L"HOMEPATH",
|
|
|
|
L"LOCALAPPDATA",
|
|
|
|
L"LOGONSERVER",
|
|
|
|
L"NUMBER_OF_PROCESSORS",
|
|
|
|
L"OS",
|
|
|
|
L"PATHEXT",
|
|
|
|
L"PROCESSOR_ARCHITECTURE",
|
|
|
|
L"PROCESSOR_IDENTIFIER",
|
|
|
|
L"PROCESSOR_LEVEL",
|
|
|
|
L"PROCESSOR_REVISION",
|
|
|
|
L"ProgramData",
|
|
|
|
L"ProgramFiles",
|
|
|
|
L"ProgramFiles(x86)",
|
|
|
|
L"ProgramW6432",
|
|
|
|
L"PROMPT",
|
|
|
|
L"PSModulePath",
|
|
|
|
L"PUBLIC",
|
|
|
|
L"SystemDrive",
|
|
|
|
L"SystemRoot",
|
|
|
|
L"TEMP",
|
|
|
|
L"TMP",
|
|
|
|
L"USERDNSDOMAIN",
|
|
|
|
L"USERDOMAIN",
|
|
|
|
L"USERDOMAIN_ROAMINGPROFILE",
|
|
|
|
L"USERNAME",
|
|
|
|
L"USERPROFILE",
|
|
|
|
L"windir",
|
|
|
|
// Enables proxy information to be passed to Curl, the underlying download library in cmake.exe
|
|
|
|
L"HTTP_PROXY",
|
|
|
|
L"HTTPS_PROXY",
|
|
|
|
};
|
|
|
|
|
|
|
|
// Flush stdout before launching external process
|
|
|
|
_flushall();
|
|
|
|
|
|
|
|
std::vector<const wchar_t*> env_cstr;
|
|
|
|
env_cstr.reserve(env_wstrings.size() + 2);
|
|
|
|
|
|
|
|
for (auto&& env_wstring : env_wstrings)
|
|
|
|
{
|
2017-03-29 10:26:07 +08:00
|
|
|
const optional<std::wstring> value = System::get_environmental_variable(env_wstring);
|
2017-03-29 06:05:55 +08:00
|
|
|
auto v = value.get();
|
|
|
|
if (!v || v->empty())
|
2017-03-11 08:55:59 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
env_wstring.push_back(L'=');
|
|
|
|
env_wstring.append(*v);
|
|
|
|
env_cstr.push_back(env_wstring.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
env_cstr.push_back(new_PATH.c_str());
|
|
|
|
env_cstr.push_back(nullptr);
|
|
|
|
|
|
|
|
// Basically we are wrapping it in quotes
|
|
|
|
const std::wstring& actual_cmd_line = Strings::wformat(LR"###("%s")###", cmd_line);
|
|
|
|
auto exit_code = _wspawnlpe(_P_WAIT, L"cmd.exe", L"cmd.exe", L"/c", actual_cmd_line.c_str(), nullptr, env_cstr.data());
|
|
|
|
return static_cast<int>(exit_code);
|
|
|
|
}
|
|
|
|
|
2017-03-29 03:33:25 +08:00
|
|
|
int cmd_execute(const cwstring_view cmd_line)
|
2016-09-19 11:50:08 +08:00
|
|
|
{
|
2017-03-04 22:10:59 +08:00
|
|
|
// Flush stdout before launching external process
|
2017-03-11 08:55:59 +08:00
|
|
|
_flushall();
|
2017-02-15 11:09:09 +08:00
|
|
|
|
2016-09-19 11:50:08 +08:00
|
|
|
// Basically we are wrapping it in quotes
|
2016-09-30 10:28:00 +08:00
|
|
|
const std::wstring& actual_cmd_line = Strings::wformat(LR"###("%s")###", cmd_line);
|
2016-09-19 11:50:08 +08:00
|
|
|
int exit_code = _wsystem(actual_cmd_line.c_str());
|
|
|
|
return exit_code;
|
|
|
|
}
|
|
|
|
|
2017-03-29 03:33:25 +08:00
|
|
|
exit_code_and_output cmd_execute_and_capture_output(const cwstring_view cmd_line)
|
2016-09-19 11:50:08 +08:00
|
|
|
{
|
2017-03-04 22:10:59 +08:00
|
|
|
// Flush stdout before launching external process
|
|
|
|
fflush(stdout);
|
2017-02-15 11:09:09 +08:00
|
|
|
|
2017-03-14 07:17:47 +08:00
|
|
|
const std::wstring& actual_cmd_line = Strings::wformat(LR"###("%s 2>&1")###", cmd_line);
|
2016-09-19 11:50:08 +08:00
|
|
|
|
|
|
|
std::string output;
|
|
|
|
char buf[1024];
|
|
|
|
auto pipe = _wpopen(actual_cmd_line.c_str(), L"r");
|
|
|
|
if (pipe == nullptr)
|
|
|
|
{
|
2017-02-15 05:26:21 +08:00
|
|
|
return { 1, output };
|
2016-09-19 11:50:08 +08:00
|
|
|
}
|
|
|
|
while (fgets(buf, 1024, pipe))
|
|
|
|
{
|
|
|
|
output.append(buf);
|
|
|
|
}
|
|
|
|
if (!feof(pipe))
|
|
|
|
{
|
2017-02-15 05:26:21 +08:00
|
|
|
return { 1, output };
|
2016-09-19 11:50:08 +08:00
|
|
|
}
|
|
|
|
auto ec = _pclose(pipe);
|
2017-02-15 05:26:21 +08:00
|
|
|
return { ec, output };
|
2016-09-19 11:50:08 +08:00
|
|
|
}
|
|
|
|
|
2017-03-29 03:33:25 +08:00
|
|
|
std::wstring create_powershell_script_cmd(const fs::path& script_path, const cwstring_view args)
|
2017-03-07 08:16:56 +08:00
|
|
|
{
|
|
|
|
// TODO: switch out ExecutionPolicy Bypass with "Remove Mark Of The Web" code and restore RemoteSigned
|
2017-03-16 07:23:40 +08:00
|
|
|
return Strings::wformat(LR"(powershell -NoProfile -ExecutionPolicy Bypass -Command "& {& '%s' %s}")", script_path.native(), args);
|
2017-03-07 08:16:56 +08:00
|
|
|
}
|
|
|
|
|
2017-03-29 03:33:25 +08:00
|
|
|
void print(const cstring_view message)
|
2016-09-19 11:50:08 +08:00
|
|
|
{
|
2017-02-23 21:33:21 +08:00
|
|
|
fputs(message, stdout);
|
2016-09-19 11:50:08 +08:00
|
|
|
}
|
|
|
|
|
2017-03-29 03:33:25 +08:00
|
|
|
void println(const cstring_view message)
|
2016-09-19 11:50:08 +08:00
|
|
|
{
|
|
|
|
print(message);
|
2017-02-23 21:33:21 +08:00
|
|
|
putchar('\n');
|
2016-09-19 11:50:08 +08:00
|
|
|
}
|
|
|
|
|
2017-03-29 03:33:25 +08:00
|
|
|
void print(const color c, const cstring_view message)
|
2016-09-19 11:50:08 +08:00
|
|
|
{
|
|
|
|
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
|
|
|
|
CONSOLE_SCREEN_BUFFER_INFO consoleScreenBufferInfo{};
|
|
|
|
GetConsoleScreenBufferInfo(hConsole, &consoleScreenBufferInfo);
|
|
|
|
auto original_color = consoleScreenBufferInfo.wAttributes;
|
|
|
|
|
2017-02-16 10:40:39 +08:00
|
|
|
SetConsoleTextAttribute(hConsole, static_cast<WORD>(c) | (original_color & 0xF0));
|
2017-02-23 21:33:21 +08:00
|
|
|
print(message);
|
2016-09-19 11:50:08 +08:00
|
|
|
SetConsoleTextAttribute(hConsole, original_color);
|
|
|
|
}
|
|
|
|
|
2017-03-29 03:33:25 +08:00
|
|
|
void println(const color c, const cstring_view message)
|
2016-09-19 11:50:08 +08:00
|
|
|
{
|
|
|
|
print(c, message);
|
2017-02-23 21:33:21 +08:00
|
|
|
putchar('\n');
|
2016-09-19 11:50:08 +08:00
|
|
|
}
|
|
|
|
|
2017-03-29 03:33:25 +08:00
|
|
|
optional<std::wstring> get_environmental_variable(const cwstring_view varname) noexcept
|
2016-09-19 11:50:08 +08:00
|
|
|
{
|
2017-03-11 08:54:08 +08:00
|
|
|
auto sz = GetEnvironmentVariableW(varname, nullptr, 0);
|
|
|
|
if (sz == 0)
|
2017-03-29 06:05:55 +08:00
|
|
|
return nullopt;
|
2017-03-11 08:54:08 +08:00
|
|
|
|
|
|
|
auto ret = std::make_unique<std::wstring>(sz, L'\0');
|
2017-03-14 08:38:04 +08:00
|
|
|
Checks::check_exit(VCPKG_LINE_INFO, MAXDWORD >= ret->size());
|
2017-03-11 08:54:08 +08:00
|
|
|
auto sz2 = GetEnvironmentVariableW(varname, ret->data(), static_cast<DWORD>(ret->size()));
|
2017-03-14 08:38:04 +08:00
|
|
|
Checks::check_exit(VCPKG_LINE_INFO, sz2 + 1 == sz);
|
2017-03-11 08:54:08 +08:00
|
|
|
ret->pop_back();
|
2017-03-29 06:05:55 +08:00
|
|
|
return *ret.release();
|
2016-09-19 11:50:08 +08:00
|
|
|
}
|
|
|
|
|
2017-03-11 07:43:54 +08:00
|
|
|
static bool is_string_keytype(DWORD hkey_type)
|
|
|
|
{
|
|
|
|
return hkey_type == REG_SZ || hkey_type == REG_MULTI_SZ || hkey_type == REG_EXPAND_SZ;
|
|
|
|
}
|
|
|
|
|
2017-03-29 03:33:25 +08:00
|
|
|
optional<std::wstring> get_registry_string(HKEY base, const cwstring_view subKey, const cwstring_view valuename)
|
2017-03-11 07:43:54 +08:00
|
|
|
{
|
|
|
|
HKEY k = nullptr;
|
|
|
|
LSTATUS ec = RegOpenKeyExW(base, subKey, NULL, KEY_READ, &k);
|
|
|
|
if (ec != ERROR_SUCCESS)
|
2017-03-29 06:05:55 +08:00
|
|
|
return nullopt;
|
2017-03-11 07:43:54 +08:00
|
|
|
|
|
|
|
DWORD dwBufferSize = 0;
|
|
|
|
DWORD dwType = 0;
|
|
|
|
auto rc = RegQueryValueExW(k, valuename, nullptr, &dwType, nullptr, &dwBufferSize);
|
|
|
|
if (rc != ERROR_SUCCESS || !is_string_keytype(dwType) || dwBufferSize == 0 || dwBufferSize % sizeof(wchar_t) != 0)
|
2017-03-29 06:05:55 +08:00
|
|
|
return nullopt;
|
2017-03-11 07:43:54 +08:00
|
|
|
std::wstring ret;
|
|
|
|
ret.resize(dwBufferSize / sizeof(wchar_t));
|
|
|
|
|
|
|
|
rc = RegQueryValueExW(k, valuename, nullptr, &dwType, reinterpret_cast<LPBYTE>(ret.data()), &dwBufferSize);
|
|
|
|
if (rc != ERROR_SUCCESS || !is_string_keytype(dwType) || dwBufferSize != sizeof(wchar_t) * ret.size())
|
2017-03-29 06:05:55 +08:00
|
|
|
return nullopt;
|
2017-03-11 07:43:54 +08:00
|
|
|
|
|
|
|
ret.pop_back(); // remove extra trailing null byte
|
2017-03-29 06:05:55 +08:00
|
|
|
return ret;
|
2017-03-11 07:43:54 +08:00
|
|
|
}
|
2017-01-06 04:47:08 +08:00
|
|
|
}
|