From 6694074aa6414f7c42a2cd9fe1a4b8d2a120ed11 Mon Sep 17 00:00:00 2001 From: AdmiringWorm Date: Wed, 10 May 2023 11:10:38 +0200 Subject: [PATCH] (#508) Update snapshot service to respect version normalization This commit updates the file snapshot service to honor version normalization with fallbacks to old version methods. This is done as we are moving towards normalizing version numbers in most of the code, which causes a conflict when installing, upgrading or uninstalling packages that sometimes it would use non-normalized version number, and other times it would use normalized version numbers. Now with this change it will prefer to use normalized version number, but will continue to read/update package versions that was installed using non-normalized version numbers. --- .../ChocolateyPackageInformationService.cs | 54 +++++++++++++++++-- 1 file changed, 51 insertions(+), 3 deletions(-) diff --git a/src/chocolatey/infrastructure.app/services/ChocolateyPackageInformationService.cs b/src/chocolatey/infrastructure.app/services/ChocolateyPackageInformationService.cs index ddd97b7909..1fc4fa3c73 100644 --- a/src/chocolatey/infrastructure.app/services/ChocolateyPackageInformationService.cs +++ b/src/chocolatey/infrastructure.app/services/ChocolateyPackageInformationService.cs @@ -74,7 +74,8 @@ public ChocolateyPackageInformation Get(IPackageMetadata package) return packageInformation; } - var pkgStorePath = _fileSystem.CombinePaths(ApplicationParameters.ChocolateyPackageInfoStoreLocation, "{0}.{1}".FormatWith(package.Id, package.Version.ToStringSafe())); + var pkgStorePath = GetStorePath(_fileSystem, package.Id, package.Version); + if (!_fileSystem.DirectoryExists(pkgStorePath)) { return packageInformation; @@ -176,7 +177,8 @@ public void Save(ChocolateyPackageInformation packageInformation) return; } - var pkgStorePath = _fileSystem.CombinePaths(ApplicationParameters.ChocolateyPackageInfoStoreLocation, "{0}.{1}".FormatWith(packageInformation.Package.Id, packageInformation.Package.Version.ToStringSafe())); + var pkgStorePath = GetStorePath(_fileSystem, packageInformation.Package.Id, packageInformation.Package.Version); + _fileSystem.EnsureDirectoryExists(pkgStorePath); if (packageInformation.RegistrySnapshot != null) @@ -250,7 +252,7 @@ public void Save(ChocolateyPackageInformation packageInformation) public void Remove(IPackageMetadata package) { - var pkgStorePath = _fileSystem.CombinePaths(ApplicationParameters.ChocolateyPackageInfoStoreLocation, "{0}.{1}".FormatWith(package.Id, package.Version.ToStringSafe())); + var pkgStorePath = GetStorePath(_fileSystem, package.Id, package.Version); if (_config.RegularOutput) this.Log().Info("Removing Package Information for {0}".FormatWith(pkgStorePath)); _fileSystem.DeleteDirectoryChecked(pkgStorePath, recursive: true); } @@ -268,5 +270,51 @@ public void save_package_information(ChocolateyPackageInformation packageInforma public void remove_package_information(IPackageMetadata package) => Remove(package); #pragma warning restore IDE1006 + + private static string GetStorePath(IFileSystem fileSystem, string id, NuGetVersion version) + { + var preferredStorePath = fileSystem.CombinePaths(ApplicationParameters.ChocolateyPackageInfoStoreLocation, "{0}.{1}".FormatWith(id, version.ToNormalizedStringChecked())); + + if (fileSystem.DirectoryExists(preferredStorePath)) + { + return preferredStorePath; + } + + var pkgStorePath = fileSystem.CombinePaths(ApplicationParameters.ChocolateyPackageInfoStoreLocation, "{0}.{1}".FormatWith(id, version.ToStringSafe())); + + if (fileSystem.DirectoryExists(pkgStorePath)) + { + return pkgStorePath; + } + + // Legacy handling for package version that was installed originally as 4.0.0.0 + + var versionFull = version.IsPrerelease + ? "{0}-{1}".FormatWith(version.Version.ToStringSafe(), version.Release) + : version.Version.ToStringSafe(); + pkgStorePath = fileSystem.CombinePaths(ApplicationParameters.ChocolateyPackageInfoStoreLocation, "{0}.{1}".FormatWith(id, versionFull)); + + if (fileSystem.DirectoryExists(pkgStorePath)) + { + return pkgStorePath; + } + + // Legacy handling for package versions that was installed originally as "4.2" + + if (version.Version.Revision == 0 && version.Version.Build == 0) + { + versionFull = version.IsPrerelease + ? "{0}.{1}-{2}".FormatWith(version.Major, version.Minor, version.Release) + : "{0}.{1}".FormatWith(version.Major, version.Minor); + pkgStorePath = fileSystem.CombinePaths(ApplicationParameters.ChocolateyPackageInfoStoreLocation, "{0}.{1}".FormatWith(id, versionFull)); + + if (fileSystem.DirectoryExists(pkgStorePath)) + { + return pkgStorePath; + } + } + + return preferredStorePath; + } } }