PathAllowList .java

net.minecraft.world.level.validation.PathAllowList (class): members: getForFileSystem, COMMENT_PREFIX, compiledPaths, ConfigEntry, readPlain, EntryType.

net.minecraft.world.level.validationclass112 linescommon3.8 KB

Implements

API Surface (6)

PathMatcher getForFileSystem(final FileSystem fileSystem)
boolean matches(final Path path)
PathAllowList readPlain(final BufferedReader reader)
record ConfigEntry(PathAllowList.EntryType type, String pattern)
PathMatcher compile(final FileSystem fileSystem)
Optional<PathAllowList.ConfigEntry> parse(final String definition)

Fields & Constants (4)

LOGGER
COMMENT_PREFIX
entries
compiledPaths

String literals (8)

"Failed to compile file pattern list"
"Unterminated type in line '"
"glob"
"regex"
"prefix"
"Unsupported definition type in line '"
"glob:"
"regex:"

Code Graph: Dependencies (1)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

PathAllowList.java112 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.validation;
2
3
import com.google.common.annotations.VisibleForTesting;
4
import com.mojang.logging.LogUtils;
5
import java.io.BufferedReader;
6
import java.nio.file.FileSystem;
7
import java.nio.file.Path;
8
import java.nio.file.PathMatcher;
9
import java.util.List;
10
import java.util.Map;
11
import java.util.Optional;
12
import java.util.concurrent.ConcurrentHashMap;
13
import org.slf4j.Logger;
14
15
public class PathAllowList implements PathMatcher {
16
	private static final Logger LOGGER = LogUtils.getLogger();
17
	private static final String COMMENT_PREFIX = "#";
18
	private final List<PathAllowList.ConfigEntry> entries;
19
	private final Map<String, PathMatcher> compiledPaths = new ConcurrentHashMap<>();
20
21
	public PathAllowList(final List<PathAllowList.ConfigEntry> entries) {
22
		this.entries = entries;
23
	}
24
25
	public PathMatcher getForFileSystem(final FileSystem fileSystem) {
26
		return this.compiledPaths.computeIfAbsent(fileSystem.provider().getScheme(), scheme -> {
27
			List<PathMatcher> compiledMatchers;
28
			try {
29
				compiledMatchers = this.entries.stream().map(e -> e.compile(fileSystem)).toList();
30
			} catch (Exception e) {
31
				LOGGER.error("Failed to compile file pattern list", e);
32
				return path -> false;
33
			}
34
			return switch (compiledMatchers.size()) {
35
				case 0 -> path -> false;
36
				case 1 -> (PathMatcher)compiledMatchers.get(0);
37
				default -> path -> {
38
					for (PathMatcher matcher : compiledMatchers) {
39
						if (matcher.matches(path)) {
40
							return true;
41
						}
42
					}
43
44
					return false;
45
				};
46
			};
47
		});
48
	}
49
50
	@Override
51
	public boolean matches(final Path path) {
52
		return this.getForFileSystem(path.getFileSystem()).matches(path);
53
	}
54
55
	public static PathAllowList readPlain(final BufferedReader reader) {
56
		return new PathAllowList(reader.lines().flatMap(line -> PathAllowList.ConfigEntry.parse(line).stream()).toList());
57
	}
58
59
	public record ConfigEntry(PathAllowList.EntryType type, String pattern) {
60
		public PathMatcher compile(final FileSystem fileSystem) {
61
			return this.type().compile(fileSystem, this.pattern);
62
		}
63
64
		public static Optional<PathAllowList.ConfigEntry> parse(final String definition) {
65
			if (definition.isBlank() || definition.startsWith("#")) {
66
				return Optional.empty();
67
			}
68
69
			if (!definition.startsWith("[")) {
70
				return Optional.of(new PathAllowList.ConfigEntry(PathAllowList.EntryType.PREFIX, definition));
71
			}
72
73
			int split = definition.indexOf(93, 1);
74
			if (split == -1) {
75
				throw new IllegalArgumentException("Unterminated type in line '" + definition + "'");
76
			}
77
78
			String type = definition.substring(1, split);
79
			String contents = definition.substring(split + 1);
80
81
			return switch (type) {
82
				case "glob", "regex" -> Optional.of(new PathAllowList.ConfigEntry(PathAllowList.EntryType.FILESYSTEM, type + ":" + contents));
83
				case "prefix" -> Optional.of(new PathAllowList.ConfigEntry(PathAllowList.EntryType.PREFIX, contents));
84
				default -> throw new IllegalArgumentException("Unsupported definition type in line '" + definition + "'");
85
			};
86
		}
87
88
		@VisibleForTesting
89
		static PathAllowList.ConfigEntry glob(final String pattern) {
90
			return new PathAllowList.ConfigEntry(PathAllowList.EntryType.FILESYSTEM, "glob:" + pattern);
91
		}
92
93
		@VisibleForTesting
94
		static PathAllowList.ConfigEntry regex(final String pattern) {
95
			return new PathAllowList.ConfigEntry(PathAllowList.EntryType.FILESYSTEM, "regex:" + pattern);
96
		}
97
98
		@VisibleForTesting
99
		static PathAllowList.ConfigEntry prefix(final String pattern) {
100
			return new PathAllowList.ConfigEntry(PathAllowList.EntryType.PREFIX, pattern);
101
		}
102
	}
103
104
	@FunctionalInterface
105
	public interface EntryType {
106
		PathAllowList.EntryType FILESYSTEM = FileSystem::getPathMatcher;
107
		PathAllowList.EntryType PREFIX = (fileSystem, pattern) -> path -> path.toString().startsWith(pattern);
108
109
		PathMatcher compile(FileSystem fileSystem, String pattern);
110
	}
111
}
112

Decompiled Minecraft 26.3 source of PathAllowList (net/minecraft/world/level/validation/PathAllowList.java) with Mojang official mappings, stored verbatim on the site.