Skip to content

Commit 9144c17

Browse files
authored
Don't try to download a new distribution if the name is already in use (#13046)
1 parent e881324 commit 9144c17

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

src/windows/common/WslInstall.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,15 @@ std::pair<std::wstring, GUID> WslInstall::InstallModernDistribution(
271271
const std::optional<uint64_t>& vhdSize,
272272
const bool fixedVhd)
273273
{
274+
wsl::windows::common::SvcComm service;
275+
276+
// Fail early if the distributions name is already in use.
277+
auto result = wil::ResultFromException([&]() {
278+
service.GetDistributionId(name.has_value() ? name->c_str() : distribution.Name.c_str(), LXSS_GET_DISTRO_ID_LIST_ALL);
279+
});
280+
281+
THROW_HR_IF(HRESULT_FROM_WIN32(ERROR_ALREADY_EXISTS), SUCCEEDED(result));
282+
LOG_HR_IF(result, result != WSL_E_DISTRO_NOT_FOUND);
274283

275284
const auto downloadInfo = wsl::shared::Arm64 ? distribution.Arm64Url : distribution.Amd64Url;
276285
THROW_HR_IF(E_UNEXPECTED, !downloadInfo.has_value());
@@ -304,7 +313,6 @@ std::pair<std::wstring, GUID> WslInstall::InstallModernDistribution(
304313

305314
wsl::windows::common::HandleConsoleProgressBar progressBar(file.get(), Localization::MessageImportProgress());
306315

307-
wsl::windows::common::SvcComm service;
308316
auto [id, installedName] = service.RegisterDistribution(
309317
name.has_value() ? name->c_str() : distribution.Name.c_str(),
310318
version.value_or(LXSS_WSL_VERSION_DEFAULT),

test/windows/UnitTests.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5085,6 +5085,58 @@ Error code: Wsl/InstallDistro/WSL_E_DISTRO_NOT_FOUND\r\n",
50855085
L"");
50865086
}
50875087

5088+
// Validate that a distribution isn't downloaded if its name is already in use.
5089+
{
5090+
auto manifest = std::format(
5091+
R"({{
5092+
"ModernDistributions": {{
5093+
"debian": [
5094+
{{
5095+
"Name": "{}",
5096+
"FriendlyName": "DebianFriendlyName",
5097+
"Amd64Url": {{
5098+
"Url": "file://doesnotexist",
5099+
"Sha256": ""
5100+
}}
5101+
}},
5102+
{{
5103+
"Name": "dummy",
5104+
"FriendlyName": "dummy",
5105+
"Amd64Url": {{
5106+
"Url": "file://doesnotexist",
5107+
"Sha256": ""
5108+
}}
5109+
}}
5110+
]
5111+
}}
5112+
}})",
5113+
LXSS_DISTRO_NAME_TEST);
5114+
5115+
auto restore = SetManifest(manifest);
5116+
5117+
{
5118+
auto [out, err] = LxsstuLaunchWslAndCaptureOutput(std::format(L"--install {}", LXSS_DISTRO_NAME_TEST_L), -1);
5119+
5120+
VERIFY_ARE_EQUAL(
5121+
out,
5122+
L"A distribution with the supplied name already exists. Use --name to chose a different name.\r\n"
5123+
L"Error code: Wsl/InstallDistro/ERROR_ALREADY_EXISTS\r\n");
5124+
5125+
VERIFY_ARE_EQUAL(err, L"");
5126+
}
5127+
5128+
{
5129+
auto [out, err] = LxsstuLaunchWslAndCaptureOutput(std::format(L"--install dummy --name {}", LXSS_DISTRO_NAME_TEST_L), -1);
5130+
5131+
VERIFY_ARE_EQUAL(
5132+
out,
5133+
L"A distribution with the supplied name already exists. Use --name to chose a different name.\r\n"
5134+
L"Error code: Wsl/InstallDistro/ERROR_ALREADY_EXISTS\r\n");
5135+
5136+
VERIFY_ARE_EQUAL(err, L"");
5137+
}
5138+
}
5139+
50885140
// Validate handling of case where no default install distro is configured.
50895141
{
50905142
auto manifest =

0 commit comments

Comments
 (0)