Skip to content
Draft
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions src/shared/configfile/configfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ bool ConfigKey::ParseImpl(const char* name, const char* value, const wchar_t* fi
const auto parsed = wsl::shared::string::ParseBool(value);
if (!parsed.has_value())
{
EMIT_USER_WARNING(Localization::MessageConfigInvalidBoolean(value, name, filePath, fileLine));
EMIT_USER_WARNING(Localization::MessageConfigInvalidBoolean(
wsl::shared::string::MultiByteToWide(value), wsl::shared::string::MultiByteToWide(name), filePath, fileLine));
return false;
}

Expand All @@ -78,7 +79,8 @@ bool ConfigKey::ParseImpl(const char* name, const char* value, const wchar_t* fi
const long number = strtol(value, &end, 0);
if (*value == '\0' || *end != '\0' || number < INT_MIN || number > INT_MAX)
{
EMIT_USER_WARNING(Localization::MessageConfigInvalidInteger(value, name, filePath, fileLine));
EMIT_USER_WARNING(Localization::MessageConfigInvalidInteger(
wsl::shared::string::MultiByteToWide(value), wsl::shared::string::MultiByteToWide(name), filePath, fileLine));
return false;
}

Expand All @@ -97,7 +99,8 @@ bool ConfigKey::ParseImpl(const char* name, const char* value, const wchar_t* fi
const auto memory = wsl::shared::string::ParseMemorySize(value);
if (!memory.has_value())
{
EMIT_USER_WARNING(wsl::shared::Localization::MessageInvalidNumberString(value, name, filePath, fileLine));
EMIT_USER_WARNING(wsl::shared::Localization::MessageInvalidNumberString(
wsl::shared::string::MultiByteToWide(value), wsl::shared::string::MultiByteToWide(name), filePath, fileLine));
return false;
}

Expand All @@ -121,7 +124,10 @@ bool ConfigKey::ParseImpl(const char* name, const char* value, const wchar_t* fi
}
else
{
THROW_HR_WITH_USER_ERROR(E_INVALIDARG, Localization::MessageConfigMacAddress(value, name, filePath, fileLine));
THROW_HR_WITH_USER_ERROR(
E_INVALIDARG,
Localization::MessageConfigMacAddress(
wsl::shared::string::MultiByteToWide(value), wsl::shared::string::MultiByteToWide(name), filePath, fileLine));
}

return true;
Expand Down Expand Up @@ -188,8 +194,13 @@ void ConfigKey::Parse(const char* name, const char* value, const wchar_t* fileNa
{
if (m_parseResult.has_value())
{
EMIT_USER_WARNING(
Localization::MessageConfigKeyDuplicated(name, fileName, line, m_parseResult->first, fileName, m_parseResult->second));
EMIT_USER_WARNING(Localization::MessageConfigKeyDuplicated(
wsl::shared::string::MultiByteToWide(name),
fileName,
line,
wsl::shared::string::MultiByteToWide(m_parseResult->first),
fileName,
m_parseResult->second));
return;
}

Expand All @@ -213,7 +224,7 @@ static void SetConfig(std::vector<ConfigKey>& keys, const char* keyName, const c
const auto key = std::find_if(keys.begin(), keys.end(), [keyName](const auto& e) { return e.Matches(keyName); });
if (key == keys.end())
{
EMIT_USER_WARNING(Localization::MessageConfigUnknownKey(keyName, filePath, fileLine));
EMIT_USER_WARNING(Localization::MessageConfigUnknownKey(wsl::shared::string::MultiByteToWide(keyName), filePath, fileLine));
return;
}

Expand Down Expand Up @@ -498,7 +509,7 @@ int ParseConfigFile(std::vector<ConfigKey>& keys, FILE* file, int flags, const w
fputs("expected ]\n", stderr);
}

EMIT_USER_WARNING(Localization::MessageConfigExpected("']'", filePath, line));
EMIT_USER_WARNING(Localization::MessageConfigExpected(L"']'", filePath, line));

if (updateConfigFile)
{
Expand Down Expand Up @@ -543,7 +554,7 @@ int ParseConfigFile(std::vector<ConfigKey>& keys, FILE* file, int flags, const w
fputs("expected space or EOL\n", stderr);
}

EMIT_USER_WARNING(Localization::MessageConfigExpected("' ' or '\\n'", filePath, line));
EMIT_USER_WARNING(Localization::MessageConfigExpected(L"' ' or '\\n'", filePath, line));

if (updateConfigFile)
{
Expand Down Expand Up @@ -600,7 +611,7 @@ int ParseConfigFile(std::vector<ConfigKey>& keys, FILE* file, int flags, const w
fputs("expected =\n", stderr);
}

EMIT_USER_WARNING(Localization::MessageConfigExpected("'='", filePath, line));
EMIT_USER_WARNING(Localization::MessageConfigExpected(L"'='", filePath, line));

if (updateConfigFile)
{
Expand Down Expand Up @@ -768,7 +779,7 @@ int ParseConfigFile(std::vector<ConfigKey>& keys, FILE* file, int flags, const w
fprintf(stderr, "expected \"\n");
}

EMIT_USER_WARNING(Localization::MessageConfigExpected("\"", filePath, line));
EMIT_USER_WARNING(Localization::MessageConfigExpected(L"\"", filePath, line));

// This key value will be overwritten, so we can ignore any malformed values.
// However, we can still inform the user of the issue per warning above.
Expand Down
7 changes: 6 additions & 1 deletion src/shared/configfile/configfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,12 @@ class ConfigKey
validValues << e.first;
}

EMIT_USER_WARNING(wsl::shared::Localization::MessageConfigInvalidEnum(value, name, fileName, line, validValues.str().c_str()));
EMIT_USER_WARNING(wsl::shared::Localization::MessageConfigInvalidEnum(
wsl::shared::string::MultiByteToWide(value),
wsl::shared::string::MultiByteToWide(name),
fileName,
line,
wsl::shared::string::MultiByteToWide(validValues.str())));
return {};
}

Expand Down
5 changes: 4 additions & 1 deletion src/shared/inc/JsonUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ T FromJson(const char* Value)
#ifdef WIN32

THROW_HR_WITH_USER_ERROR_MSG(
WSL_E_INVALID_JSON, wsl::shared::Localization::MessageInvalidJson(e.what()), "Invalid JSON: %hs", Value);
WSL_E_INVALID_JSON,
wsl::shared::Localization::MessageInvalidJson(wsl::shared::string::MultiByteToWide(e.what())),
"Invalid JSON: %hs",
Value);

#else
LOG_ERROR("Failed to deserialize json: '{}'. Error: {}", Value, e.what());
Expand Down
78 changes: 6 additions & 72 deletions src/shared/inc/stringshared.h
Original file line number Diff line number Diff line change
Expand Up @@ -927,81 +927,15 @@ struct std::formatter<std::source_location, wchar_t>
template <typename TCtx>
auto format(const std::source_location& location, TCtx& ctx) const
{
return std::format_to(ctx.out(), L"{}[{}:{}]", location.function_name(), location.file_name(), location.line());
return std::format_to(
ctx.out(),
L"{}[{}:{}]",
wsl::shared::string::MultiByteToWide(location.function_name()),
wsl::shared::string::MultiByteToWide(location.file_name()),
location.line());
}
};

// char -> wchar_t formatting is only used by the Windows components. libc++ (used to
// build the Linux components) now provides these as deleted specializations per C++23
// [format.formatter.spec], which would collide, so restrict them to Windows.
#ifdef WIN32

template <>
struct std::formatter<char*, wchar_t>
{
template <typename TCtx>
static constexpr auto parse(TCtx& ctx)
{
return ctx.begin();
}

template <typename TCtx>
auto format(const char* str, TCtx& ctx) const
{
return std::format_to(ctx.out(), "{}", wsl::shared::string::MultiByteToWide(str));
}
};

template <>
struct std::formatter<const char*, wchar_t>
{
template <typename TCtx>
static constexpr auto parse(TCtx& ctx)
{
return ctx.begin();
}

template <typename TCtx>
auto format(const char* str, TCtx& ctx) const
{
return std::format_to(ctx.out(), "{}", wsl::shared::string::MultiByteToWide(str));
}
};

template <std::size_t N>
struct std::formatter<char[N], wchar_t>
{
template <typename TCtx>
static constexpr auto parse(TCtx& ctx)
{
return ctx.begin();
}

template <typename TCtx>
auto format(const char str[N], TCtx& ctx) const
{
return std::format_to(ctx.out(), "{}", wsl::shared::string::MultiByteToWide(str));
}
};

template <class Traits, class Allocator>
struct std::formatter<std::basic_string<char, Traits, Allocator>, wchar_t>
{
template <typename TCtx>
static constexpr auto parse(TCtx& ctx)
{
return ctx.begin();
}

template <typename TCtx>
auto format(const std::basic_string<char, Traits, Allocator>& str, TCtx& ctx) const
{
return std::format_to(ctx.out(), "{}", wsl::shared::string::MultiByteToWide(str));
}
};

#endif // WIN32

template <>
struct std::formatter<std::filesystem::path, wchar_t>
{
Expand Down
3 changes: 2 additions & 1 deletion src/windows/common/WSLCUserSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,8 @@ namespace {
catch (const std::exception& e)
{
warnings.push_back(
{wsl::shared::Localization::WSLCUserSettings_Warning_ParseError(path.wstring(), MultiByteToWide(e.what())), {}});
{wsl::shared::Localization::WSLCUserSettings_Warning_ParseError(path.wstring(), wsl::shared::string::MultiByteToWide(e.what())),
{}});
return std::nullopt;
}
}
Expand Down
17 changes: 13 additions & 4 deletions src/windows/common/WslClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1015,7 +1015,8 @@ int Mount(_In_ std::wstring_view commandLine)
if (mountResult.Result != 0)
{
wsl::windows::common::wslutil::PrintMessage(
Localization::MessageDiskMountFailed(strerror(-mountResult.Result), WSL_UNMOUNT_ARG, disk), stdout);
Localization::MessageDiskMountFailed(wsl::shared::string::MultiByteToWide(strerror(-mountResult.Result)), WSL_UNMOUNT_ARG, disk),
stdout);
return 1;
}
else
Expand Down Expand Up @@ -1227,7 +1228,8 @@ int Unmount(_In_ const std::wstring& arg)

if (value.first != 0)
{
wsl::windows::common::wslutil::PrintMessage(Localization::MessageDetachFailed(strerror(-value.first), WSL_SHUTDOWN_ARG), stdout);
wsl::windows::common::wslutil::PrintMessage(
Localization::MessageDetachFailed(wsl::shared::string::MultiByteToWide(strerror(-value.first)), WSL_SHUTDOWN_ARG), stdout);
return -1;
}

Expand Down Expand Up @@ -1293,13 +1295,20 @@ int Version()
const auto windowsVersion = wsl::windows::common::helpers::GetWindowsVersionString();
wsl::windows::common::wslutil::PrintMessage(
Localization::MessagePackageVersions(
WSL_PACKAGE_VERSION, KERNEL_VERSION, WSLG_VERSION, MSRDC_VERSION, DIRECT3D_VERSION, DXCORE_VERSION, windowsVersion),
STRING_TO_WIDE_STRING(WSL_PACKAGE_VERSION),
STRING_TO_WIDE_STRING(KERNEL_VERSION),
STRING_TO_WIDE_STRING(WSLG_VERSION),
STRING_TO_WIDE_STRING(MSRDC_VERSION),
STRING_TO_WIDE_STRING(DIRECT3D_VERSION),
STRING_TO_WIDE_STRING(DXCORE_VERSION),
wsl::shared::string::MultiByteToWide(windowsVersion)),
stdout);

if constexpr (!wsl::shared::OfficialBuild)
{
// Print additional information if running a debug build.
wsl::windows::common::wslutil::PrintMessage(Localization::MessageBuildInfo(_MSC_VER, COMMIT_HASH, __TIME__ " " __DATE__), stdout);
wsl::windows::common::wslutil::PrintMessage(
Localization::MessageBuildInfo(_MSC_VER, STRING_TO_WIDE_STRING(COMMIT_HASH), STRING_TO_WIDE_STRING(__TIME__ " " __DATE__)), stdout);
}

return 0;
Expand Down
9 changes: 6 additions & 3 deletions src/windows/common/WslCoreConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ void wsl::core::Config::ParseConfigFile(_In_opt_ LPCWSTR ConfigFilePath, _In_opt
int number = 0;
if (FAILED(wil::ResultFromException([&]() { number = std::stoi(portString); })) || (number <= 0 || number > USHRT_MAX))
{
EMIT_USER_WARNING(shared::Localization::MessageConfigInvalidInteger(value, name, fileName, fileLine));
EMIT_USER_WARNING(shared::Localization::MessageConfigInvalidInteger(
shared::string::MultiByteToWide(value), shared::string::MultiByteToWide(name), fileName, fileLine));
}
else
{
Expand All @@ -55,7 +56,8 @@ void wsl::core::Config::ParseConfigFile(_In_opt_ LPCWSTR ConfigFilePath, _In_opt

if (inet_pton(AF_INET, value, &address) != 1)
{
EMIT_USER_WARNING(shared::Localization::MessageConfigInvalidIp(value, name, fileName, fileLine));
EMIT_USER_WARNING(shared::Localization::MessageConfigInvalidIp(
shared::string::MultiByteToWide(value), shared::string::MultiByteToWide(name), fileName, fileLine));
EnableDnsTunneling = false;
}
else
Expand Down Expand Up @@ -447,7 +449,8 @@ void wsl::core::Config::Initialize(_In_opt_ HANDLE UserToken)
if (NetworkingMode == NetworkingMode::Consomme)
{
NetworkingMode = (defaultNetworkingMode == NetworkingMode::Consomme) ? NetworkingMode::None : NetworkingMode::Nat;
EMIT_USER_WARNING(wsl::shared::Localization::MessageConsommeRequiresVirtio(ToString(NetworkingMode)));
EMIT_USER_WARNING(wsl::shared::Localization::MessageConsommeRequiresVirtio(
wsl::shared::string::MultiByteToWide(ToString(NetworkingMode))));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/windows/common/notifications.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ try
</actions>
</toast>)",
Localization::MessagePerformanceTip(Localization::Options::DontImpersonate),
Localization::MessageProblematicDrvFsUsage(binaryName, Localization::Options::DontImpersonate),
Localization::MessageProblematicDrvFsUsage(wsl::shared::string::MultiByteToWide(binaryName), Localization::Options::DontImpersonate),
wslhost::docs_arg,
wslhost::docs_arg_filesystem_url,
Localization::MessageViewDocs(Localization::Options::DontImpersonate),
Expand Down
17 changes: 15 additions & 2 deletions src/windows/common/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,25 @@ BYTE ConvertHexByte(const wchar_t* hex, wchar_t** endPtr)
return static_cast<BYTE>(wcstoul(hex, endPtr, 16));
}

template <typename T>
std::wstring StringViewToWide(std::basic_string_view<T> input)
{
if constexpr (std::is_same_v<T, char>)
{
return wsl::shared::string::MultiByteToWide(std::string{input});
}
else
{
return std::wstring{input};
}
}

template <typename T>
std::vector<BYTE> HexToBytesT(std::basic_string_view<T> input)
{
if (input.length() % 2 != 0)
{
THROW_HR_WITH_USER_ERROR(E_INVALIDARG, wsl::shared::Localization::MessageInvalidHexString(std::basic_string<T>{input}));
THROW_HR_WITH_USER_ERROR(E_INVALIDARG, wsl::shared::Localization::MessageInvalidHexString(StringViewToWide(input)));
}

std::vector<BYTE> result;
Expand All @@ -237,7 +250,7 @@ std::vector<BYTE> HexToBytesT(std::basic_string_view<T> input)
const auto byte = ConvertHexByte(currentHex, &endPtr);
if (endPtr != currentHex + 2)
{
THROW_HR_WITH_USER_ERROR(E_INVALIDARG, wsl::shared::Localization::MessageInvalidHexString(std::basic_string<T>{input}));
THROW_HR_WITH_USER_ERROR(E_INVALIDARG, wsl::shared::Localization::MessageInvalidHexString(StringViewToWide(input)));
}

result.push_back(byte);
Expand Down
10 changes: 5 additions & 5 deletions src/windows/common/wslutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ std::wstring wsl::windows::common::wslutil::GetErrorString(HRESULT result)
kbUrl = std::format(L"[Unexpected build number: {}]", buildNumber);
}

return Localization::MessageOsNotSupported(helpers::GetWindowsVersionString().c_str(), kbUrl.c_str());
return Localization::MessageOsNotSupported(wsl::shared::string::MultiByteToWide(helpers::GetWindowsVersionString()), kbUrl.c_str());

// All the errors below this comment are not supposed to be reachable here (since there's meant to be emitted from the
// service). But if we somehow hit them here, it's better show something useful to the user.
Expand Down Expand Up @@ -1364,15 +1364,15 @@ void wsl::windows::common::wslutil::ParseIpv4Address(const char* Address, in_add
{
if (inet_pton(AF_INET, Address, &Result) != 1)
{
THROW_HR_WITH_USER_ERROR(E_INVALIDARG, wsl::shared::Localization::MessageInvalidIp(Address));
THROW_HR_WITH_USER_ERROR(E_INVALIDARG, wsl::shared::Localization::MessageInvalidIp(wsl::shared::string::MultiByteToWide(Address)));
}
}

void wsl::windows::common::wslutil::ParseIpv6Address(const char* Address, in_addr6& Result)
{
if (inet_pton(AF_INET6, Address, &Result) != 1)
{
THROW_HR_WITH_USER_ERROR(E_INVALIDARG, wsl::shared::Localization::MessageInvalidIp(Address));
THROW_HR_WITH_USER_ERROR(E_INVALIDARG, wsl::shared::Localization::MessageInvalidIp(wsl::shared::string::MultiByteToWide(Address)));
}
}

Expand Down Expand Up @@ -1403,7 +1403,7 @@ std::pair<std::string, std::optional<std::string>> wsl::windows::common::wslutil
std::smatch match;
if (!std::regex_match(Input, match, regex))
{
THROW_HR_WITH_USER_ERROR(E_INVALIDARG, wsl::shared::Localization::MessageWslcInvalidImage(Input.c_str()));
THROW_HR_WITH_USER_ERROR(E_INVALIDARG, wsl::shared::Localization::MessageWslcInvalidImage(wsl::shared::string::MultiByteToWide(Input)));
}

const auto& repo = match[1];
Expand Down Expand Up @@ -1444,7 +1444,7 @@ std::string wsl::windows::common::wslutil::GetCanonicalImageReference(const std:
std::smatch match;
if (!std::regex_match(input, match, regex))
{
THROW_HR_WITH_USER_ERROR(E_INVALIDARG, wsl::shared::Localization::MessageWslcInvalidImage(input.c_str()));
THROW_HR_WITH_USER_ERROR(E_INVALIDARG, wsl::shared::Localization::MessageWslcInvalidImage(wsl::shared::string::MultiByteToWide(input)));
}

auto [domain, path] = NormalizeRepo(match[1].str());
Expand Down
Loading