@samitouri / QOSAMI-WSL / commits / 9144c170

Don't try to download a new distribution if the name is already in use (#13046)

Blue committed Jun 4, 2025 at 18:59 UTC 9144c17073a575b015b0c0e4dbe717d5cb4bad52
2 files changed +61 -1
src/windows/common/WslInstall.cpp
+9 -1
@@ -271,6 +271,15 @@ std::pair<std::wstring, GUID> WslInstall::InstallModernDistribution(
271 const std::optional<uint64_t>& vhdSize,
272 const bool fixedVhd)
273 {
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);
283
284 const auto downloadInfo = wsl::shared::Arm64 ? distribution.Arm64Url : distribution.Amd64Url;
285 THROW_HR_IF(E_UNEXPECTED, !downloadInfo.has_value());
@@ -304,7 +313,6 @@ std::pair<std::wstring, GUID> WslInstall::InstallModernDistribution(
313
314 wsl::windows::common::HandleConsoleProgressBar progressBar(file.get(), Localization::MessageImportProgress());
315
307 - wsl::windows::common::SvcComm service;
316 auto [id, installedName] = service.RegisterDistribution(
317 name.has_value() ? name->c_str() : distribution.Name.c_str(),
318 version.value_or(LXSS_WSL_VERSION_DEFAULT),
test/windows/UnitTests.cpp
+52
@@ -5085,6 +5085,58 @@ Error code: Wsl/InstallDistro/WSL_E_DISTRO_NOT_FOUND\r\n",
5085 L"");
5086 }
5087
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 +
5140 // Validate handling of case where no default install distro is configured.
5141 {
5142 auto manifest =