public async Task LocalPackageFileCache_GetShaTwiceVerifyMissingFilesAreNotCached() { using (var pathContext = new SimpleTestPathContext()) { var cache = new LocalPackageFileCache(); var pathResolver = new VersionFolderPathResolver(pathContext.PackageSource); var identity = new PackageIdentity("X", NuGetVersion.Parse("1.0.0")); var shaPath = pathResolver.GetNupkgMetadataPath(identity.Id, identity.Version); var exists1 = cache.Sha512Exists(shaPath); await SimpleTestPackageUtility.CreateFolderFeedV3Async( pathContext.PackageSource, identity); var exists2 = cache.Sha512Exists(shaPath); var sha512 = cache.GetOrAddSha512(shaPath); var sha512B = cache.GetOrAddSha512(shaPath); // Verify original value was not found exists1.Should().BeFalse(); // Verify false was not cached exists2.Should().BeTrue(); // Verify both hashes are the exact same instance Assert.Same(sha512.Value, sha512B.Value); } }
private LocalPackageInfo GetPackage(string packageId, NuGetVersion version, string path) { if (!_packageCache.TryGetValue(path, out var package)) { var nupkgMetadataPath = PathResolver.GetNupkgMetadataPath(packageId, version); var hashPath = PathResolver.GetHashPath(packageId, version); var zipPath = PathResolver.GetPackageFilePath(packageId, version); // The nupkg metadata file is written last. If this file does not exist then the package is // incomplete and should not be used. if (_packageFileCache.Sha512Exists(nupkgMetadataPath)) { package = CreateLocalPackageInfo(packageId, version, path, nupkgMetadataPath, zipPath); } // if hash file exists and it's not a fallback folder then we generate nupkg metadata file else if (!_isFallbackFolder && _packageFileCache.Sha512Exists(hashPath)) { LocalFolderUtility.GenerateNupkgMetadataFile(zipPath, path, hashPath, nupkgMetadataPath); package = CreateLocalPackageInfo(packageId, version, path, nupkgMetadataPath, zipPath); } if (package != null) { // Cache the package, if it is valid it will not change // for the life of this restore. // Locking is done at a higher level around the id _packageCache.TryAdd(path, package); } } return(package); }
private List <LocalPackageInfo> GetPackages(string id) { var packages = new List <LocalPackageInfo>(); var packageIdRoot = PathResolver.GetVersionListPath(id); if (!Directory.Exists(packageIdRoot)) { return(packages); } foreach (var fullVersionDir in Directory.EnumerateDirectories(packageIdRoot)) { LocalPackageInfo package; if (!_packageCache.TryGetValue(fullVersionDir, out package)) { var versionPart = fullVersionDir.Substring(packageIdRoot.Length).TrimStart(Path.DirectorySeparatorChar); // Get the version part and parse it NuGetVersion version; if (!NuGetVersion.TryParse(versionPart, out version)) { continue; } var hashPath = PathResolver.GetHashPath(id, version); // The hash file is written last. If this file does not exist then the package is // incomplete and should not be used. if (_packageFileCache.Sha512Exists(hashPath)) { var manifestPath = PathResolver.GetManifestFilePath(id, version); var zipPath = PathResolver.GetPackageFilePath(id, version); var sha512Path = PathResolver.GetHashPath(id, version); var nuspec = _packageFileCache.GetOrAddNuspec(manifestPath, fullVersionDir); var files = _packageFileCache.GetOrAddFiles(fullVersionDir); var sha512 = _packageFileCache.GetOrAddSha512(hashPath); var runtimeGraph = _packageFileCache.GetOrAddRuntimeGraph(fullVersionDir); package = new LocalPackageInfo(id, version, fullVersionDir, manifestPath, zipPath, sha512Path, nuspec, files, sha512, runtimeGraph); // Cache the package, if it is valid it will not change // for the life of this restore. // Locking is done at a higher level around the id _packageCache.TryAdd(fullVersionDir, package); } } // Add the package if it is valid if (package != null) { packages.Add(package); } } return(packages); }
private static bool IsPackageOnDisk(ISet <PackageIdentity> packagesChecked, IEnumerable <VersionFolderPathResolver> pathResolvers, LocalPackageFileCache packageFileCache, PackageIdentity identity) { // Each id/version only needs to be checked once if (packagesChecked.Add(identity)) { // Check each package folder. These need to match the order used for restore. foreach (var resolver in pathResolvers) { // Verify the SHA for each package var hashPath = resolver.GetHashPath(identity.Id, identity.Version); var nupkgMetadataPath = resolver.GetNupkgMetadataPath(identity.Id, identity.Version); if (packageFileCache.Sha512Exists(hashPath) || packageFileCache.Sha512Exists(nupkgMetadataPath)) { return(true); } } return(false); } return(true); }
private static IEnumerable <string> GetPackageFiles(LocalPackageFileCache packageFileCache, string packageId, NuGetVersion version, IEnumerable <VersionFolderPathResolver> resolvers) { foreach (var resolver in resolvers) { // Verify the SHA for each package var hashPath = resolver.GetHashPath(packageId, version); if (packageFileCache.Sha512Exists(hashPath)) { yield return(hashPath); break; } var nupkgMetadataPath = resolver.GetNupkgMetadataPath(packageId, version); if (packageFileCache.Sha512Exists(nupkgMetadataPath)) { yield return(nupkgMetadataPath); break; } } }
private List <LocalPackageInfo> GetPackages(string id) { var packages = new List <LocalPackageInfo>(); var packageIdRoot = PathResolver.GetVersionListPath(id); if (!Directory.Exists(packageIdRoot)) { return(packages); } foreach (var fullVersionDir in Directory.EnumerateDirectories(packageIdRoot)) { LocalPackageInfo package; if (!_packageCache.TryGetValue(fullVersionDir, out package)) { var versionPart = fullVersionDir.Substring(packageIdRoot.Length).TrimStart(Path.DirectorySeparatorChar); // Get the version part and parse it NuGetVersion version; if (!NuGetVersion.TryParse(versionPart, out version)) { continue; } var nupkgMetadataPath = PathResolver.GetNupkgMetadataPath(id, version); var hashPath = PathResolver.GetHashPath(id, version); var zipPath = PathResolver.GetPackageFilePath(id, version); var installPath = PathResolver.GetInstallPath(id, version); // The nupkg metadata file is written last. If this file does not exist then the package is // incomplete and should not be used. if (_packageFileCache.Sha512Exists(nupkgMetadataPath)) { package = CreateLocalPackageInfo(id, version, fullVersionDir, nupkgMetadataPath, zipPath); // Cache the package, if it is valid it will not change // for the life of this restore. // Locking is done at a higher level around the id _packageCache.TryAdd(fullVersionDir, package); } // if hash file exists and it's not a fallback folder then we generate nupkg metadata file else if (!_isFallbackFolder && _packageFileCache.Sha512Exists(hashPath)) { LocalFolderUtility.GenerateNupkgMetadataFile(zipPath, installPath, hashPath, nupkgMetadataPath); package = CreateLocalPackageInfo(id, version, fullVersionDir, nupkgMetadataPath, zipPath); // Cache the package, if it is valid it will not change // for the life of this restore. // Locking is done at a higher level around the id _packageCache.TryAdd(fullVersionDir, package); } } // Add the package if it is valid if (package != null) { packages.Add(package); } } return(packages); }