| 1 | package net.minecraft.server.packs.linkfs;
|
| 2 |
|
| 3 | import com.google.common.base.Splitter;
|
| 4 | import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
|
| 5 | import java.nio.file.FileStore;
|
| 6 | import java.nio.file.FileSystem;
|
| 7 | import java.nio.file.Path;
|
| 8 | import java.nio.file.PathMatcher;
|
| 9 | import java.nio.file.WatchService;
|
| 10 | import java.nio.file.attribute.UserPrincipalLookupService;
|
| 11 | import java.nio.file.spi.FileSystemProvider;
|
| 12 | import java.util.HashMap;
|
| 13 | import java.util.List;
|
| 14 | import java.util.Map;
|
| 15 | import java.util.Set;
|
| 16 | import java.util.stream.Collectors;
|
| 17 | import java.util.stream.Stream;
|
| 18 | import org.jspecify.annotations.Nullable;
|
| 19 |
|
| 20 | public class LinkFileSystem extends FileSystem {
|
| 21 | private static final Set<String> VIEWS = Set.of("basic");
|
| 22 | public static final String PATH_SEPARATOR = "/";
|
| 23 | private static final Splitter PATH_SPLITTER = Splitter.on('/');
|
| 24 | private final FileStore store;
|
| 25 | private final FileSystemProvider provider = new LinkFSProvider();
|
| 26 | private final LinkFSPath root;
|
| 27 |
|
| 28 | private LinkFileSystem(final String name, final LinkFileSystem.DirectoryEntry rootEntry) {
|
| 29 | this.store = new LinkFSFileStore(name);
|
| 30 | this.root = buildPath(rootEntry, this, "", null);
|
| 31 | }
|
| 32 |
|
| 33 | private static LinkFSPath buildPath(
|
| 34 | final LinkFileSystem.DirectoryEntry entry, final LinkFileSystem fileSystem, final String selfName, final @Nullable LinkFSPath parent
|
| 35 | ) {
|
| 36 | Object2ObjectOpenHashMap<String, LinkFSPath> children = new Object2ObjectOpenHashMap<>();
|
| 37 | LinkFSPath result = new LinkFSPath(fileSystem, selfName, parent, new PathContents.DirectoryContents(children));
|
| 38 | entry.files.forEach((name, linkTarget) -> children.put(name, new LinkFSPath(fileSystem, name, result, new PathContents.FileContents(linkTarget))));
|
| 39 | entry.children.forEach((name, childEntry) -> children.put(name, buildPath(childEntry, fileSystem, name, result)));
|
| 40 | children.trim();
|
| 41 | return result;
|
| 42 | }
|
| 43 |
|
| 44 | @Override
|
| 45 | public FileSystemProvider provider() {
|
| 46 | return this.provider;
|
| 47 | }
|
| 48 |
|
| 49 | @Override
|
| 50 | public void close() {
|
| 51 | }
|
| 52 |
|
| 53 | @Override
|
| 54 | public boolean isOpen() {
|
| 55 | return true;
|
| 56 | }
|
| 57 |
|
| 58 | @Override
|
| 59 | public boolean isReadOnly() {
|
| 60 | return true;
|
| 61 | }
|
| 62 |
|
| 63 | @Override
|
| 64 | public String getSeparator() {
|
| 65 | return "/";
|
| 66 | }
|
| 67 |
|
| 68 | @Override
|
| 69 | public Iterable<Path> getRootDirectories() {
|
| 70 | return List.of(this.root);
|
| 71 | }
|
| 72 |
|
| 73 | @Override
|
| 74 | public Iterable<FileStore> getFileStores() {
|
| 75 | return List.of(this.store);
|
| 76 | }
|
| 77 |
|
| 78 | @Override
|
| 79 | public Set<String> supportedFileAttributeViews() {
|
| 80 | return VIEWS;
|
| 81 | }
|
| 82 |
|
| 83 | @Override
|
| 84 | public Path getPath(final String first, final String... more) {
|
| 85 | Stream<String> path = Stream.of(first);
|
| 86 | if (more.length > 0) {
|
| 87 | path = Stream.concat(path, Stream.of(more));
|
| 88 | }
|
| 89 |
|
| 90 | String joinedPath = path.collect(Collectors.joining("/"));
|
| 91 | if (joinedPath.equals("/")) {
|
| 92 | return this.root;
|
| 93 | }
|
| 94 |
|
| 95 | if (joinedPath.startsWith("/")) {
|
| 96 | LinkFSPath result = this.root;
|
| 97 |
|
| 98 | for (String segment : PATH_SPLITTER.split(joinedPath.substring(1))) {
|
| 99 | if (segment.isEmpty()) {
|
| 100 | throw new IllegalArgumentException("Empty paths not allowed");
|
| 101 | }
|
| 102 |
|
| 103 | result = result.resolveName(segment);
|
| 104 | }
|
| 105 |
|
| 106 | return result;
|
| 107 | } else {
|
| 108 | LinkFSPath result = null;
|
| 109 |
|
| 110 | for (String segment : PATH_SPLITTER.split(joinedPath)) {
|
| 111 | if (segment.isEmpty()) {
|
| 112 | throw new IllegalArgumentException("Empty paths not allowed");
|
| 113 | }
|
| 114 |
|
| 115 | result = new LinkFSPath(this, segment, result, PathContents.RELATIVE);
|
| 116 | }
|
| 117 |
|
| 118 | if (result == null) {
|
| 119 | throw new IllegalArgumentException("Empty paths not allowed");
|
| 120 | } else {
|
| 121 | return result;
|
| 122 | }
|
| 123 | }
|
| 124 | }
|
| 125 |
|
| 126 | @Override
|
| 127 | public PathMatcher getPathMatcher(final String syntaxAndPattern) {
|
| 128 | throw new UnsupportedOperationException();
|
| 129 | }
|
| 130 |
|
| 131 | @Override
|
| 132 | public UserPrincipalLookupService getUserPrincipalLookupService() {
|
| 133 | throw new UnsupportedOperationException();
|
| 134 | }
|
| 135 |
|
| 136 | @Override
|
| 137 | public WatchService newWatchService() {
|
| 138 | throw new UnsupportedOperationException();
|
| 139 | }
|
| 140 |
|
| 141 | public FileStore store() {
|
| 142 | return this.store;
|
| 143 | }
|
| 144 |
|
| 145 | public LinkFSPath rootPath() {
|
| 146 | return this.root;
|
| 147 | }
|
| 148 |
|
| 149 | public static LinkFileSystem.Builder builder() {
|
| 150 | return new LinkFileSystem.Builder();
|
| 151 | }
|
| 152 |
|
| 153 | public static class Builder {
|
| 154 | private final LinkFileSystem.DirectoryEntry root = new LinkFileSystem.DirectoryEntry();
|
| 155 |
|
| 156 | public LinkFileSystem.Builder put(final List<String> path, final String name, final Path target) {
|
| 157 | LinkFileSystem.DirectoryEntry currentEntry = this.root;
|
| 158 |
|
| 159 | for (String segment : path) {
|
| 160 | currentEntry = currentEntry.children.computeIfAbsent(segment, n -> new LinkFileSystem.DirectoryEntry());
|
| 161 | }
|
| 162 |
|
| 163 | currentEntry.files.put(name, target);
|
| 164 | return this;
|
| 165 | }
|
| 166 |
|
| 167 | public LinkFileSystem.Builder put(final List<String> path, final Path target) {
|
| 168 | if (path.isEmpty()) {
|
| 169 | throw new IllegalArgumentException("Path can't be empty");
|
| 170 | }
|
| 171 |
|
| 172 | int lastIndex = path.size() - 1;
|
| 173 | return this.put(path.subList(0, lastIndex), path.get(lastIndex), target);
|
| 174 | }
|
| 175 |
|
| 176 | public FileSystem build(final String name) {
|
| 177 | return new LinkFileSystem(name, this.root);
|
| 178 | }
|
| 179 | }
|
| 180 |
|
| 181 | private record DirectoryEntry(Map<String, LinkFileSystem.DirectoryEntry> children, Map<String, Path> files) {
|
| 182 | public DirectoryEntry() {
|
| 183 | this(new HashMap<>(), new HashMap<>());
|
| 184 | }
|
| 185 | }
|
| 186 | }
|
| 187 |
|