diff --git a/doc/release-notes/1.31/6694-portable-archive-alias-hardlink.md b/doc/release-notes/1.31/6694-portable-archive-alias-hardlink.md new file mode 100644 index 0000000000..aa32af1c85 --- /dev/null +++ b/doc/release-notes/1.31/6694-portable-archive-alias-hardlink.md @@ -0,0 +1,5 @@ +## Bug Fixes + +### Minor Bug Fixes + +* Fixed installing archive portables whose command alias points to a file in a subdirectory of the archive, which failed with "Hash not found for hardlink target", and an alias that differs from the file name only by case no longer replaces the original file. diff --git a/src/AppInstallerCLICore/PortableInstaller.cpp b/src/AppInstallerCLICore/PortableInstaller.cpp index 0d8b426715..f6faeda070 100644 --- a/src/AppInstallerCLICore/PortableInstaller.cpp +++ b/src/AppInstallerCLICore/PortableInstaller.cpp @@ -592,7 +592,7 @@ namespace AppInstaller::CLI::Portable { // If alias differs from original filename, a hardlink exists in the install directory. // Track it so uninstall removes it even when state is reconstructed from ARP values. - if (!targetFullPath.empty() && targetFullPath.filename() != symlinkFullPath.filename()) + if (!targetFullPath.empty() && !Filesystem::FileNamesEqual(targetFullPath.filename(), symlinkFullPath.filename())) { std::filesystem::path hardlinkPath = InstallLocation / symlinkFullPath.filename(); diff --git a/src/AppInstallerCLICore/Workflows/PortableFlow.cpp b/src/AppInstallerCLICore/Workflows/PortableFlow.cpp index 094f1c1373..eff9b42bf7 100644 --- a/src/AppInstallerCLICore/Workflows/PortableFlow.cpp +++ b/src/AppInstallerCLICore/Workflows/PortableFlow.cpp @@ -205,6 +205,7 @@ namespace AppInstaller::CLI::Workflow // Build a map of installed target path → SHA256 as file entries are created, // so nested installer files that need hardlinks can reuse the hash without re-reading from disk. + // Files inside subdirectories are moved with their directory, so their hash is computed when first needed. std::map fileHashes; for (const auto& entry : std::filesystem::directory_iterator(installerPath)) @@ -252,11 +253,15 @@ namespace AppInstaller::CLI::Workflow // If alias differs from original filename, create hardlink // Hardlink will be placed in the same directory as the original file to avoid pathing issues and same-volume restrictions - if (commandAlias != originalFilename) + // File names are case-insensitive, so an alias that differs only by case would replace the original file + if (!Filesystem::FileNamesEqual(commandAlias, originalFilename)) { std::filesystem::path hardlinkPath = targetPath.parent_path() / commandAlias; - auto it = fileHashes.find(std::filesystem::weakly_canonical(targetPath)); - THROW_HR_IF_MSG(APPINSTALLER_CLI_ERROR_PORTABLE_INSTALL_FAILED, it == fileHashes.end(), "Hash not found for hardlink target: %ls", targetPath.c_str()); + auto [it, inserted] = fileHashes.try_emplace(std::filesystem::weakly_canonical(targetPath)); + if (inserted) + { + it->second = Utility::SHA256::ConvertToString(Utility::SHA256::ComputeHashFromFile(installerPath / relativeFilePath)); + } entries.emplace_back(PortableFileEntry::CreateHardlinkEntry(hardlinkPath, targetPath, it->second)); } entries.emplace_back(PortableFileEntry::CreateSymlinkEntry(symlinkDirectory / commandAlias, targetPath)); @@ -295,7 +300,7 @@ namespace AppInstaller::CLI::Workflow entries.emplace_back(PortableFileEntry::CreateFileEntry(installerPath, targetFullPath, fileSha256)); // If alias differs from original filename, create hardlink - if (commandAlias != originalFilename) + if (!Filesystem::FileNamesEqual(commandAlias, originalFilename)) { std::filesystem::path hardlinkPath = targetInstallDirectory / commandAlias; entries.emplace_back(PortableFileEntry::CreateHardlinkEntry(hardlinkPath, targetFullPath, fileSha256)); diff --git a/src/AppInstallerCLITests/Filesystem.cpp b/src/AppInstallerCLITests/Filesystem.cpp index 30a7db2622..b702e024d4 100644 --- a/src/AppInstallerCLITests/Filesystem.cpp +++ b/src/AppInstallerCLITests/Filesystem.cpp @@ -198,6 +198,14 @@ TEST_CASE("ReplaceCommonPathPrefix", "[filesystem]") REQUIRE(shouldNotReplace.u8string() == "C:\\test1\\test3\\subdir1\\subdir2"); } +TEST_CASE("FileNamesEqual", "[filesystem]") +{ + REQUIRE(FileNamesEqual(L"Tool.exe", L"tool.exe")); + REQUIRE(FileNamesEqual(L"\u00DC.exe", L"\u00FC.exe")); + REQUIRE_FALSE(FileNamesEqual(L"tool.exe", L"tools.exe")); + REQUIRE_FALSE(FileNamesEqual(L"\u00DC.exe", L"U.exe")); +} + TEST_CASE("GetExecutablePathForProcess", "[filesystem]") { std::filesystem::path thisExecutable = GetExecutablePathForProcess(GetCurrentProcess()); diff --git a/src/AppInstallerCLITests/InstallFlow.cpp b/src/AppInstallerCLITests/InstallFlow.cpp index 39d12b7bfb..7df20e9ba8 100644 --- a/src/AppInstallerCLITests/InstallFlow.cpp +++ b/src/AppInstallerCLITests/InstallFlow.cpp @@ -807,6 +807,73 @@ TEST_CASE("PortableInstallFlow_RejectsEscapingPathsAtPointOfUse", "[InstallFlow] REQUIRE_TERMINATED_WITH(context, APPINSTALLER_CLI_ERROR_INVALID_MANIFEST); } +TEST_CASE("PortableInstallFlow_ArchiveCommandAlias", "[InstallFlow][workflow]") +{ + const std::string relativeFilePath = GENERATE("TestPortable.exe", "subdir\\TestPortable.exe"); + const std::string commandAlias = GENERATE("TestAlias", "testPortable"); + CAPTURE(relativeFilePath, commandAlias); + TestHook::SetIsRunningWithNonDefaultFullToken_Override tokenOverride(false); + // Only the files in the install directory are under test, so skip creating the alias symlink. + TestHook::SetCreateSymlinkResult_Override createSymlinkResultOverride(true); + + // Keep the alias paths out of the real links directory, since installing removes existing files there. + TempDirectory links("PortableArchiveCommandAliasLinks", true); + const auto originalLinks = AppInstaller::Runtime::GetPathDetailsFor(AppInstaller::Runtime::PathName::PortableLinksUserLocation); + auto restoreLinks = wil::scope_exit([&]() + { + AppInstaller::Runtime::TestHook_SetPathOverride(AppInstaller::Runtime::PathName::PortableLinksUserLocation, originalLinks); + }); + AppInstaller::Filesystem::PathDetails linksDetails; + linksDetails.Path = links.GetPath(); + linksDetails.Create = false; + AppInstaller::Runtime::TestHook_SetPathOverride(AppInstaller::Runtime::PathName::PortableLinksUserLocation, linksDetails); + + TestCommon::TempDirectory targetDirectory("TestPortableInstallRoot", false); + TestCommon::TempDirectory extractedDirectory("TestPortableExtractedRoot", true); + const std::filesystem::path extractedFile = extractedDirectory.GetPath() / relativeFilePath; + std::filesystem::create_directories(extractedFile.parent_path()); + std::filesystem::copy_file(TestDataFile("AppInstallerTestExeInstaller.exe").GetPath(), extractedFile); + + std::ostringstream installOutput; + TestContext context{ installOutput, std::cin }; + auto previousThreadGlobals = context.SetForCurrentThread(); + + ManifestInstaller installer; + installer.BaseInstallerType = InstallerTypeEnum::Zip; + installer.NestedInstallerFiles = { { relativeFilePath, commandAlias } }; + + const std::string productCode = "PortableArchiveCommandAliasTest"; + AppInstaller::Registry::Portable::PortableARPEntry arpEntry(ScopeEnum::User, Architecture::X64, productCode); + auto cleanup = wil::scope_exit([&]() { arpEntry.Delete(); }); + AppInstaller::CLI::Portable::PortableInstaller portableInstaller{ + ScopeEnum::User, Architecture::X64, productCode, OperationType::Install }; + portableInstaller.TargetInstallLocation = targetDirectory.GetPath(); + + context.Add(installer); + context.Add(extractedDirectory.GetPath()); + context.Add(std::move(portableInstaller)); + + PortableInstallImpl(context); + INFO(installOutput.str()); + + // A failed install has already been rolled back. + REQUIRE_FALSE(context.IsTerminated()); + + // Use CHECK to allow the uninstall to still occur + const std::filesystem::path installedFile = targetDirectory.GetPath() / relativeFilePath; + CHECK(std::filesystem::exists(installedFile)); + + // An alias that differs only by case refers to the original file, so no hardlink is created for it. + if (commandAlias == "TestAlias") + { + CHECK(std::filesystem::exists(installedFile.parent_path() / "TestAlias.exe")); + } + + auto& installedPortable = context.Get(); + installedPortable.PrepareForCleanUp(); + installedPortable.Uninstall(); +} + TEST_CASE("InstallFlow_Portable_SymlinkCreationFail", "[InstallFlow][workflow]") { TestHook::SetIsRunningWithNonDefaultFullToken_Override tokenOverride(false); diff --git a/src/AppInstallerSharedLib/Filesystem.cpp b/src/AppInstallerSharedLib/Filesystem.cpp index b781eb5f21..b79233b483 100644 --- a/src/AppInstallerSharedLib/Filesystem.cpp +++ b/src/AppInstallerSharedLib/Filesystem.cpp @@ -569,6 +569,11 @@ namespace AppInstaller::Filesystem } } + bool FileNamesEqual(const std::filesystem::path& first, const std::filesystem::path& second) + { + return CompareStringOrdinal(first.c_str(), -1, second.c_str(), -1, TRUE) == CSTR_EQUAL; + } + bool SymlinkExists(const std::filesystem::path& symlinkPath) { return std::filesystem::is_symlink(std::filesystem::symlink_status(symlinkPath)); diff --git a/src/AppInstallerSharedLib/Public/winget/Filesystem.h b/src/AppInstallerSharedLib/Public/winget/Filesystem.h index ed736ca888..1a12cade70 100644 --- a/src/AppInstallerSharedLib/Public/winget/Filesystem.h +++ b/src/AppInstallerSharedLib/Public/winget/Filesystem.h @@ -54,6 +54,9 @@ namespace AppInstaller::Filesystem // Appends the .exe extension to the path if not present. void AppendExtension(std::filesystem::path& value, const std::string& extension); + // Determines whether two file names are equal, ignoring case the way the file system does. + bool FileNamesEqual(const std::filesystem::path& first, const std::filesystem::path& second); + // Checks if the path is a symlink and exists. bool SymlinkExists(const std::filesystem::path& symlinkPath); bool CreateSymlink(const std::filesystem::path& path, const std::filesystem::path& target);