Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with unicode characters in symlink target path #4834

Merged
merged 3 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 10 additions & 7 deletions src/AppInstallerCLICore/PortableInstaller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ namespace AppInstaller::CLI::Portable
}
else if (fileType == PortableFileType::Symlink)
{
if (Filesystem::SymlinkExists(filePath) && !Filesystem::VerifySymlink(filePath, entry.SymlinkTarget))
std::filesystem::path symlinkTargetPath{ AppInstaller::Utility::ConvertToUTF16(entry.SymlinkTarget) };
if (Filesystem::SymlinkExists(filePath) && !Filesystem::VerifySymlink(filePath, symlinkTargetPath))
{
AICLI_LOG(CLI, Warning, << "Symlink target does not match ARP Entry. Expected: " << entry.SymlinkTarget << " Actual: " << std::filesystem::read_symlink(filePath));
AICLI_LOG(CLI, Warning, << "Symlink target does not match ARP Entry. Expected: " << symlinkTargetPath << " Actual: " << std::filesystem::read_symlink(filePath));
return false;
}
}
Expand Down Expand Up @@ -121,11 +122,13 @@ namespace AppInstaller::CLI::Portable
}
else if (entry.FileType == PortableFileType::Symlink)
{
std::filesystem::path symlinkTargetPath{ Utility::ConvertToUTF16(entry.SymlinkTarget) };

if (BinariesDependOnPath && !InstallDirectoryAddedToPath)
{
// Scenario indicated by 'ArchiveBinariesDependOnPath' manifest entry.
// Skip symlink creation for portables dependent on binaries that require the install directory to be added to PATH.
std::filesystem::path installDirectory = std::filesystem::path(entry.SymlinkTarget).parent_path();
std::filesystem::path installDirectory = symlinkTargetPath.parent_path();
AddToPathVariable(installDirectory);
AICLI_LOG(Core, Info, << "Install directory added to PATH: " << installDirectory);
CommitToARPEntry(PortableValueName::InstallDirectoryAddedToPath, InstallDirectoryAddedToPath = true);
Expand All @@ -150,15 +153,15 @@ namespace AppInstaller::CLI::Portable
m_stream << Resource::String::OverwritingExistingFileAtMessage(Utility::LocIndView{ filePath.u8string() }) << std::endl;
}

if (Filesystem::CreateSymlink(entry.SymlinkTarget, filePath))
if (Filesystem::CreateSymlink(symlinkTargetPath, filePath))
{
AICLI_LOG(Core, Info, << "Symlink created at: " << filePath);
AICLI_LOG(Core, Info, << "Symlink created at: " << filePath << " with target path: " << symlinkTargetPath);
}
else
{
// If symlink creation fails, resort to adding the package directory to PATH.
AICLI_LOG(Core, Info, << "Failed to create symlink at: " << filePath);
AddToPathVariable(std::filesystem::path(entry.SymlinkTarget).parent_path());
AddToPathVariable(symlinkTargetPath.parent_path());
CommitToARPEntry(PortableValueName::InstallDirectoryAddedToPath, InstallDirectoryAddedToPath = true);
}
}
Expand Down Expand Up @@ -186,7 +189,7 @@ namespace AppInstaller::CLI::Portable
else if (InstallDirectoryAddedToPath)
{
// If symlink doesn't exist, check if install directory was added to PATH directly and remove.
RemoveFromPathVariable(std::filesystem::path(entry.SymlinkTarget).parent_path());
RemoveFromPathVariable(std::filesystem::path(Utility::ConvertToUTF16(entry.SymlinkTarget)).parent_path());
}
}
else if (fileType == PortableFileType::Symlink && Filesystem::SymlinkExists(filePath))
Expand Down
39 changes: 38 additions & 1 deletion src/AppInstallerCLITests/PortableInstaller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,41 @@ TEST_CASE("PortableInstaller_InstallToIndex_ExistingInstallRoot", "[PortableInst
REQUIRE_FALSE(AppInstaller::Filesystem::SymlinkExists(symlinkPath));
REQUIRE_FALSE(AppInstaller::Filesystem::SymlinkExists(symlinkPath2));
REQUIRE_FALSE(std::filesystem::exists(directoryPath));
}
}

TEST_CASE("PortableInstaller_UnicodeSymlinkPath", "[PortableInstaller]")
{
TempDirectory tempDirectory = TestCommon::TempDirectory("TempDirectory", false);

// Modify install location path to include unicode characters.
std::filesystem::path testInstallLocation = tempDirectory.GetPath() / std::filesystem::path{ ConvertToUTF16("романтический") };

std::vector<PortableFileEntry> desiredTestState;

TestCommon::TempFile testPortable("testPortable.txt");
std::ofstream file(testPortable, std::ofstream::out);
file.close();

std::filesystem::path targetPath = testInstallLocation / "testPortable.txt";
std::filesystem::path symlinkPath = tempDirectory.GetPath() / "testSymlink.exe";

desiredTestState.emplace_back(std::move(PortableFileEntry::CreateFileEntry(testPortable.GetPath(), targetPath, {})));
desiredTestState.emplace_back(std::move(PortableFileEntry::CreateSymlinkEntry(symlinkPath, targetPath)));

PortableInstaller portableInstaller = PortableInstaller(ScopeEnum::User, Architecture::X64, "testProductCode");
portableInstaller.TargetInstallLocation = testInstallLocation;
portableInstaller.SetDesiredState(desiredTestState);
REQUIRE(portableInstaller.VerifyExpectedState());

portableInstaller.Install();

PortableInstaller portableInstaller2 = PortableInstaller(ScopeEnum::User, Architecture::X64, "testProductCode");
REQUIRE(portableInstaller2.ARPEntryExists());
REQUIRE(std::filesystem::exists(portableInstaller2.PortableTargetFullPath));
REQUIRE(AppInstaller::Filesystem::SymlinkExists(portableInstaller2.PortableSymlinkFullPath));

portableInstaller2.Uninstall();
REQUIRE_FALSE(std::filesystem::exists(portableInstaller2.PortableTargetFullPath));
REQUIRE_FALSE(AppInstaller::Filesystem::SymlinkExists(portableInstaller2.PortableSymlinkFullPath));
REQUIRE_FALSE(std::filesystem::exists(portableInstaller2.InstallLocation));
}
Loading