Example #1
0
        public void EnforceSizeLimit(long bytes, Registry registry)
        {
            int  numFiles;
            long curBytes;

            GetSizeInfo(out numFiles, out curBytes);
            if (curBytes > bytes)
            {
                // This object will let us determine whether a module is compatible with any of our instances
                KspVersionCriteria aggregateCriteria = manager?.Instances.Values
                                                       .Where(ksp => ksp.Valid)
                                                       .Select(ksp => ksp.VersionCriteria())
                                                       .Aggregate(
                    manager?.CurrentInstance?.VersionCriteria()
                    ?? new KspVersionCriteria(null),
                    (combinedCrit, nextCrit) => combinedCrit.Union(nextCrit)
                    );

                // This object lets us find the modules associated with a cached file
                Dictionary <string, List <CkanModule> > hashMap = registry.GetDownloadHashIndex();

                // Prune the module lists to only those that are compatible
                foreach (var kvp in hashMap)
                {
                    kvp.Value.RemoveAll(mod => !mod.IsCompatibleKSP(aggregateCriteria));
                }

                // Now get all the files in all the caches...
                List <FileInfo> files = allFiles();
                // ... and sort them by compatibilty and timestamp...
                files.Sort((a, b) => compareFiles(
                               hashMap, aggregateCriteria, a, b
                               ));

                // ... and delete them till we're under the limit
                foreach (FileInfo fi in files)
                {
                    curBytes -= fi.Length;
                    fi.Delete();
                    File.Delete($"{fi.Name}.sha1");
                    File.Delete($"{fi.Name}.sha256");
                    if (curBytes <= bytes)
                    {
                        // Limit met, all done!
                        break;
                    }
                }
                OnCacheChanged();
                sha1Cache.Clear();
                sha256Cache.Clear();
            }
        }