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 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);
        }