| 1 | package net.minecraft.util.eventlog;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import java.io.BufferedReader;
|
| 5 | import java.io.IOException;
|
| 6 | import java.io.InputStreamReader;
|
| 7 | import java.io.OutputStream;
|
| 8 | import java.io.Reader;
|
| 9 | import java.nio.ByteBuffer;
|
| 10 | import java.nio.channels.FileChannel;
|
| 11 | import java.nio.channels.FileLock;
|
| 12 | import java.nio.channels.ReadableByteChannel;
|
| 13 | import java.nio.charset.StandardCharsets;
|
| 14 | import java.nio.file.Files;
|
| 15 | import java.nio.file.Path;
|
| 16 | import java.nio.file.StandardOpenOption;
|
| 17 | import java.time.LocalDate;
|
| 18 | import java.time.format.DateTimeFormatter;
|
| 19 | import java.time.format.DateTimeParseException;
|
| 20 | import java.util.ArrayList;
|
| 21 | import java.util.Iterator;
|
| 22 | import java.util.List;
|
| 23 | import java.util.ListIterator;
|
| 24 | import java.util.Objects;
|
| 25 | import java.util.Set;
|
| 26 | import java.util.stream.Collectors;
|
| 27 | import java.util.stream.Stream;
|
| 28 | import java.util.zip.GZIPInputStream;
|
| 29 | import java.util.zip.GZIPOutputStream;
|
| 30 | import org.jspecify.annotations.Nullable;
|
| 31 | import org.slf4j.Logger;
|
| 32 |
|
| 33 | public class EventLogDirectory {
|
| 34 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 35 | private static final int COMPRESS_BUFFER_SIZE = 4096;
|
| 36 | private static final String COMPRESSED_EXTENSION = ".gz";
|
| 37 | private final Path root;
|
| 38 | private final String extension;
|
| 39 |
|
| 40 | private EventLogDirectory(final Path root, final String extension) {
|
| 41 | this.root = root;
|
| 42 | this.extension = extension;
|
| 43 | }
|
| 44 |
|
| 45 | public static EventLogDirectory open(final Path root, final String extension) throws IOException {
|
| 46 | Files.createDirectories(root);
|
| 47 | return new EventLogDirectory(root, extension);
|
| 48 | }
|
| 49 |
|
| 50 | public EventLogDirectory.FileList listFiles() throws IOException {
|
| 51 | try (Stream<Path> list = Files.list(this.root)) {
|
| 52 | return new EventLogDirectory.FileList(list.filter(x$0 -> Files.isRegularFile(x$0)).map(this::parseFile).filter(Objects::nonNull).toList());
|
| 53 | }
|
| 54 | }
|
| 55 |
|
| 56 | private EventLogDirectory.@Nullable File parseFile(final Path path) {
|
| 57 | String fileName = path.getFileName().toString();
|
| 58 | int extensionIndex = fileName.indexOf(46);
|
| 59 | if (extensionIndex == -1) {
|
| 60 | return null;
|
| 61 | }
|
| 62 |
|
| 63 | EventLogDirectory.FileId id = EventLogDirectory.FileId.parse(fileName.substring(0, extensionIndex));
|
| 64 | if (id != null) {
|
| 65 | String extension = fileName.substring(extensionIndex);
|
| 66 | if (extension.equals(this.extension)) {
|
| 67 | return new EventLogDirectory.RawFile(path, id);
|
| 68 | }
|
| 69 |
|
| 70 | if (extension.equals(this.extension + ".gz")) {
|
| 71 | return new EventLogDirectory.CompressedFile(path, id);
|
| 72 | }
|
| 73 | }
|
| 74 |
|
| 75 | return null;
|
| 76 | }
|
| 77 |
|
| 78 | private static void tryCompress(final Path raw, final Path compressed) throws IOException {
|
| 79 | if (Files.exists(compressed)) {
|
| 80 | throw new IOException("Compressed target file already exists: " + compressed);
|
| 81 | }
|
| 82 |
|
| 83 | try (FileChannel channel = FileChannel.open(raw, StandardOpenOption.WRITE, StandardOpenOption.READ)) {
|
| 84 | FileLock lock = channel.tryLock();
|
| 85 | if (lock == null) {
|
| 86 | throw new IOException("Raw log file is already locked, cannot compress: " + raw);
|
| 87 | }
|
| 88 |
|
| 89 | writeCompressed(channel, compressed);
|
| 90 | channel.truncate(0L);
|
| 91 | }
|
| 92 |
|
| 93 | Files.delete(raw);
|
| 94 | }
|
| 95 |
|
| 96 | private static void writeCompressed(final ReadableByteChannel channel, final Path target) throws IOException {
|
| 97 | try (OutputStream output = new GZIPOutputStream(Files.newOutputStream(target))) {
|
| 98 | byte[] bytes = new byte[4096];
|
| 99 | ByteBuffer buffer = ByteBuffer.wrap(bytes);
|
| 100 |
|
| 101 | while (channel.read(buffer) >= 0) {
|
| 102 | buffer.flip();
|
| 103 | output.write(bytes, 0, buffer.limit());
|
| 104 | buffer.clear();
|
| 105 | }
|
| 106 | }
|
| 107 | }
|
| 108 |
|
| 109 | public EventLogDirectory.RawFile createNewFile(final LocalDate date) throws IOException {
|
| 110 | int index = 1;
|
| 111 | Set<EventLogDirectory.FileId> files = this.listFiles().ids();
|
| 112 |
|
| 113 | EventLogDirectory.FileId id;
|
| 114 | do {
|
| 115 | id = new EventLogDirectory.FileId(date, index++);
|
| 116 | } while (files.contains(id));
|
| 117 |
|
| 118 | EventLogDirectory.RawFile file = new EventLogDirectory.RawFile(this.root.resolve(id.toFileName(this.extension)), id);
|
| 119 | Files.createFile(file.path());
|
| 120 | return file;
|
| 121 | }
|
| 122 |
|
| 123 | public record CompressedFile(Path path, EventLogDirectory.FileId id) implements EventLogDirectory.File {
|
| 124 | @Override
|
| 125 | public @Nullable Reader openReader() throws IOException {
|
| 126 | return !Files.exists(this.path)
|
| 127 | ? null
|
| 128 | : new BufferedReader(new InputStreamReader(new GZIPInputStream(Files.newInputStream(this.path)), StandardCharsets.UTF_8));
|
| 129 | }
|
| 130 |
|
| 131 | @Override
|
| 132 | public EventLogDirectory.CompressedFile compress() {
|
| 133 | return this;
|
| 134 | }
|
| 135 | }
|
| 136 |
|
| 137 | public interface File {
|
| 138 | Path path();
|
| 139 |
|
| 140 | EventLogDirectory.FileId id();
|
| 141 |
|
| 142 | @Nullable Reader openReader() throws IOException;
|
| 143 |
|
| 144 | EventLogDirectory.CompressedFile compress() throws IOException;
|
| 145 | }
|
| 146 |
|
| 147 | public record FileId(LocalDate date, int index) {
|
| 148 | private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.BASIC_ISO_DATE;
|
| 149 |
|
| 150 | public static EventLogDirectory.@Nullable FileId parse(final String name) {
|
| 151 | int separator = name.indexOf("-");
|
| 152 | if (separator == -1) {
|
| 153 | return null;
|
| 154 | }
|
| 155 |
|
| 156 | String date = name.substring(0, separator);
|
| 157 | String index = name.substring(separator + 1);
|
| 158 |
|
| 159 | try {
|
| 160 | return new EventLogDirectory.FileId(LocalDate.parse(date, DATE_FORMATTER), Integer.parseInt(index));
|
| 161 | } catch (NumberFormatException | DateTimeParseException e) {
|
| 162 | return null;
|
| 163 | }
|
| 164 | }
|
| 165 |
|
| 166 | @Override
|
| 167 | public String toString() {
|
| 168 | return DATE_FORMATTER.format(this.date) + "-" + this.index;
|
| 169 | }
|
| 170 |
|
| 171 | public String toFileName(final String extension) {
|
| 172 | return this + extension;
|
| 173 | }
|
| 174 | }
|
| 175 |
|
| 176 | public static class FileList implements Iterable<EventLogDirectory.File> {
|
| 177 | private final List<EventLogDirectory.File> files;
|
| 178 |
|
| 179 | private FileList(final List<EventLogDirectory.File> files) {
|
| 180 | this.files = new ArrayList<>(files);
|
| 181 | }
|
| 182 |
|
| 183 | public EventLogDirectory.FileList prune(final LocalDate date, final int expiryDays) {
|
| 184 | this.files.removeIf(file -> {
|
| 185 | EventLogDirectory.FileId id = file.id();
|
| 186 | LocalDate expiresAt = id.date().plusDays(expiryDays);
|
| 187 | if (!date.isBefore(expiresAt)) {
|
| 188 | try {
|
| 189 | Files.delete(file.path());
|
| 190 | return true;
|
| 191 | } catch (IOException e) {
|
| 192 | EventLogDirectory.LOGGER.warn("Failed to delete expired event log file: {}", file.path(), e);
|
| 193 | }
|
| 194 | }
|
| 195 |
|
| 196 | return false;
|
| 197 | });
|
| 198 | return this;
|
| 199 | }
|
| 200 |
|
| 201 | public EventLogDirectory.FileList compressAll() {
|
| 202 | ListIterator<EventLogDirectory.File> iterator = this.files.listIterator();
|
| 203 |
|
| 204 | while (iterator.hasNext()) {
|
| 205 | EventLogDirectory.File file = iterator.next();
|
| 206 |
|
| 207 | try {
|
| 208 | iterator.set(file.compress());
|
| 209 | } catch (IOException e) {
|
| 210 | EventLogDirectory.LOGGER.warn("Failed to compress event log file: {}", file.path(), e);
|
| 211 | }
|
| 212 | }
|
| 213 |
|
| 214 | return this;
|
| 215 | }
|
| 216 |
|
| 217 | @Override
|
| 218 | public Iterator<EventLogDirectory.File> iterator() {
|
| 219 | return this.files.iterator();
|
| 220 | }
|
| 221 |
|
| 222 | public Stream<EventLogDirectory.File> stream() {
|
| 223 | return this.files.stream();
|
| 224 | }
|
| 225 |
|
| 226 | public Set<EventLogDirectory.FileId> ids() {
|
| 227 | return this.files.stream().map(EventLogDirectory.File::id).collect(Collectors.toSet());
|
| 228 | }
|
| 229 | }
|
| 230 |
|
| 231 | public record RawFile(Path path, EventLogDirectory.FileId id) implements EventLogDirectory.File {
|
| 232 | public FileChannel openChannel() throws IOException {
|
| 233 | return FileChannel.open(this.path, StandardOpenOption.WRITE, StandardOpenOption.READ);
|
| 234 | }
|
| 235 |
|
| 236 | @Override
|
| 237 | public @Nullable Reader openReader() throws IOException {
|
| 238 | return Files.exists(this.path) ? Files.newBufferedReader(this.path) : null;
|
| 239 | }
|
| 240 |
|
| 241 | @Override
|
| 242 | public EventLogDirectory.CompressedFile compress() throws IOException {
|
| 243 | Path compressedPath = this.path.resolveSibling(this.path.getFileName().toString() + ".gz");
|
| 244 | EventLogDirectory.tryCompress(this.path, compressedPath);
|
| 245 | return new EventLogDirectory.CompressedFile(compressedPath, this.id);
|
| 246 | }
|
| 247 | }
|
| 248 | }
|
| 249 |
|