| 1 | package net.minecraft.server.packs;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
|
| 5 | import java.io.IOException;
|
| 6 | import java.io.UncheckedIOException;
|
| 7 | import java.nio.file.DirectoryNotEmptyException;
|
| 8 | import java.nio.file.FileVisitResult;
|
| 9 | import java.nio.file.Files;
|
| 10 | import java.nio.file.NoSuchFileException;
|
| 11 | import java.nio.file.Path;
|
| 12 | import java.nio.file.SimpleFileVisitor;
|
| 13 | import java.nio.file.attribute.BasicFileAttributes;
|
| 14 | import java.nio.file.attribute.FileTime;
|
| 15 | import java.util.ArrayList;
|
| 16 | import java.util.Collections;
|
| 17 | import java.util.Comparator;
|
| 18 | import java.util.HashSet;
|
| 19 | import java.util.List;
|
| 20 | import java.util.Set;
|
| 21 | import org.slf4j.Logger;
|
| 22 |
|
| 23 | public class DownloadCacheCleaner {
|
| 24 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 25 |
|
| 26 | public static void vacuumCacheDir(final Path cacheDir, final int maxFiles) {
|
| 27 | try {
|
| 28 | List<DownloadCacheCleaner.PathAndTime> filesAndDates = listFilesWithModificationTimes(cacheDir);
|
| 29 | int toRemove = filesAndDates.size() - maxFiles;
|
| 30 | if (toRemove <= 0) {
|
| 31 | return;
|
| 32 | }
|
| 33 |
|
| 34 | filesAndDates.sort(DownloadCacheCleaner.PathAndTime.NEWEST_FIRST);
|
| 35 | List<DownloadCacheCleaner.PathAndPriority> filesWithDirOrder = prioritizeFilesInDirs(filesAndDates);
|
| 36 | Collections.reverse(filesWithDirOrder);
|
| 37 | filesWithDirOrder.sort(DownloadCacheCleaner.PathAndPriority.HIGHEST_PRIORITY_FIRST);
|
| 38 | Set<Path> emptyDirectoryCandidates = new HashSet<>();
|
| 39 |
|
| 40 | for (int i = 0; i < toRemove; i++) {
|
| 41 | DownloadCacheCleaner.PathAndPriority entry = filesWithDirOrder.get(i);
|
| 42 | Path pathToRemove = entry.path;
|
| 43 |
|
| 44 | try {
|
| 45 | Files.delete(pathToRemove);
|
| 46 | if (entry.removalPriority == 0) {
|
| 47 | emptyDirectoryCandidates.add(pathToRemove.getParent());
|
| 48 | }
|
| 49 | } catch (IOException e) {
|
| 50 | LOGGER.warn("Failed to delete cache file {}", pathToRemove, e);
|
| 51 | }
|
| 52 | }
|
| 53 |
|
| 54 | emptyDirectoryCandidates.remove(cacheDir);
|
| 55 |
|
| 56 | for (Path dir : emptyDirectoryCandidates) {
|
| 57 | try {
|
| 58 | Files.delete(dir);
|
| 59 | } catch (DirectoryNotEmptyException var10) {
|
| 60 | } catch (IOException e) {
|
| 61 | LOGGER.warn("Failed to delete empty(?) cache directory {}", dir, e);
|
| 62 | }
|
| 63 | }
|
| 64 | } catch (IOException | UncheckedIOException e) {
|
| 65 | LOGGER.error("Failed to vacuum cache dir {}", cacheDir, e);
|
| 66 | }
|
| 67 | }
|
| 68 |
|
| 69 | private static List<DownloadCacheCleaner.PathAndTime> listFilesWithModificationTimes(final Path cacheDir) throws IOException {
|
| 70 | try {
|
| 71 | final List<DownloadCacheCleaner.PathAndTime> unsortedFiles = new ArrayList<>();
|
| 72 | Files.walkFileTree(cacheDir, new SimpleFileVisitor<Path>() {
|
| 73 | public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) {
|
| 74 | if (attrs.isRegularFile() && !file.getParent().equals(cacheDir)) {
|
| 75 | FileTime fileTime = attrs.lastModifiedTime();
|
| 76 | unsortedFiles.add(new DownloadCacheCleaner.PathAndTime(file, fileTime));
|
| 77 | }
|
| 78 |
|
| 79 | return FileVisitResult.CONTINUE;
|
| 80 | }
|
| 81 | });
|
| 82 | return unsortedFiles;
|
| 83 | } catch (NoSuchFileException e) {
|
| 84 | return List.of();
|
| 85 | }
|
| 86 | }
|
| 87 |
|
| 88 | private static List<DownloadCacheCleaner.PathAndPriority> prioritizeFilesInDirs(final List<DownloadCacheCleaner.PathAndTime> filesAndDates) {
|
| 89 | List<DownloadCacheCleaner.PathAndPriority> result = new ArrayList<>();
|
| 90 | Object2IntOpenHashMap<Path> parentCounts = new Object2IntOpenHashMap<>();
|
| 91 |
|
| 92 | for (DownloadCacheCleaner.PathAndTime entry : filesAndDates) {
|
| 93 | int removalPriority = parentCounts.addTo(entry.path.getParent(), 1);
|
| 94 | result.add(new DownloadCacheCleaner.PathAndPriority(entry.path, removalPriority));
|
| 95 | }
|
| 96 |
|
| 97 | return result;
|
| 98 | }
|
| 99 |
|
| 100 | private record PathAndPriority(Path path, int removalPriority) {
|
| 101 | public static final Comparator<DownloadCacheCleaner.PathAndPriority> HIGHEST_PRIORITY_FIRST = Comparator.comparing(
|
| 102 | DownloadCacheCleaner.PathAndPriority::removalPriority
|
| 103 | )
|
| 104 | .reversed();
|
| 105 | }
|
| 106 |
|
| 107 | private record PathAndTime(Path path, FileTime modifiedTime) {
|
| 108 | public static final Comparator<DownloadCacheCleaner.PathAndTime> NEWEST_FIRST = Comparator.comparing(DownloadCacheCleaner.PathAndTime::modifiedTime)
|
| 109 | .reversed();
|
| 110 | }
|
| 111 | }
|
| 112 |
|