| 1 | package net.minecraft.server.packs.linkfs;
|
| 2 |
|
| 3 | import java.io.IOException;
|
| 4 | import java.net.URI;
|
| 5 | import java.nio.channels.SeekableByteChannel;
|
| 6 | import java.nio.file.AccessDeniedException;
|
| 7 | import java.nio.file.AccessMode;
|
| 8 | import java.nio.file.CopyOption;
|
| 9 | import java.nio.file.DirectoryIteratorException;
|
| 10 | import java.nio.file.DirectoryStream;
|
| 11 | import java.nio.file.FileStore;
|
| 12 | import java.nio.file.FileSystem;
|
| 13 | import java.nio.file.Files;
|
| 14 | import java.nio.file.LinkOption;
|
| 15 | import java.nio.file.NoSuchFileException;
|
| 16 | import java.nio.file.NotDirectoryException;
|
| 17 | import java.nio.file.OpenOption;
|
| 18 | import java.nio.file.Path;
|
| 19 | import java.nio.file.ProviderMismatchException;
|
| 20 | import java.nio.file.ReadOnlyFileSystemException;
|
| 21 | import java.nio.file.StandardOpenOption;
|
| 22 | import java.nio.file.DirectoryStream.Filter;
|
| 23 | import java.nio.file.attribute.BasicFileAttributeView;
|
| 24 | import java.nio.file.attribute.BasicFileAttributes;
|
| 25 | import java.nio.file.attribute.FileAttribute;
|
| 26 | import java.nio.file.attribute.FileAttributeView;
|
| 27 | import java.nio.file.spi.FileSystemProvider;
|
| 28 | import java.util.Iterator;
|
| 29 | import java.util.Map;
|
| 30 | import java.util.Set;
|
| 31 | import org.jspecify.annotations.Nullable;
|
| 32 |
|
| 33 | class LinkFSProvider extends FileSystemProvider {
|
| 34 | public static final String SCHEME = "x-mc-link";
|
| 35 |
|
| 36 | @Override
|
| 37 | public String getScheme() {
|
| 38 | return "x-mc-link";
|
| 39 | }
|
| 40 |
|
| 41 | @Override
|
| 42 | public FileSystem newFileSystem(final URI uri, final Map<String, ?> env) {
|
| 43 | throw new UnsupportedOperationException();
|
| 44 | }
|
| 45 |
|
| 46 | @Override
|
| 47 | public FileSystem getFileSystem(final URI uri) {
|
| 48 | throw new UnsupportedOperationException();
|
| 49 | }
|
| 50 |
|
| 51 | @Override
|
| 52 | public Path getPath(final URI uri) {
|
| 53 | throw new UnsupportedOperationException();
|
| 54 | }
|
| 55 |
|
| 56 | @Override
|
| 57 | public SeekableByteChannel newByteChannel(final Path path, final Set<? extends OpenOption> options, final FileAttribute<?>... attrs) throws IOException {
|
| 58 | if (!options.contains(StandardOpenOption.CREATE_NEW)
|
| 59 | && !options.contains(StandardOpenOption.CREATE)
|
| 60 | && !options.contains(StandardOpenOption.APPEND)
|
| 61 | && !options.contains(StandardOpenOption.WRITE)) {
|
| 62 | Path targetPath = toLinkPath(path).toAbsolutePath().getTargetPath();
|
| 63 | if (targetPath == null) {
|
| 64 | throw new NoSuchFileException(path.toString());
|
| 65 | } else {
|
| 66 | return Files.newByteChannel(targetPath, options, attrs);
|
| 67 | }
|
| 68 | } else {
|
| 69 | throw new UnsupportedOperationException();
|
| 70 | }
|
| 71 | }
|
| 72 |
|
| 73 | @Override
|
| 74 | public DirectoryStream<Path> newDirectoryStream(final Path dir, final Filter<? super Path> filter) throws IOException {
|
| 75 | final PathContents.DirectoryContents directoryContents = toLinkPath(dir).toAbsolutePath().getDirectoryContents();
|
| 76 | if (directoryContents == null) {
|
| 77 | throw new NotDirectoryException(dir.toString());
|
| 78 | } else {
|
| 79 | return new DirectoryStream<Path>() {
|
| 80 | @Override
|
| 81 | public Iterator<Path> iterator() {
|
| 82 | return directoryContents.children().values().stream().filter(path -> {
|
| 83 | try {
|
| 84 | return filter.accept(path);
|
| 85 | } catch (IOException e) {
|
| 86 | throw new DirectoryIteratorException(e);
|
| 87 | }
|
| 88 | }).map(path -> (Path)path).iterator();
|
| 89 | }
|
| 90 |
|
| 91 | @Override
|
| 92 | public void close() {
|
| 93 | }
|
| 94 | };
|
| 95 | }
|
| 96 | }
|
| 97 |
|
| 98 | @Override
|
| 99 | public void createDirectory(final Path dir, final FileAttribute<?>... attrs) {
|
| 100 | throw new ReadOnlyFileSystemException();
|
| 101 | }
|
| 102 |
|
| 103 | @Override
|
| 104 | public void delete(final Path path) {
|
| 105 | throw new ReadOnlyFileSystemException();
|
| 106 | }
|
| 107 |
|
| 108 | @Override
|
| 109 | public void copy(final Path source, final Path target, final CopyOption... options) {
|
| 110 | throw new ReadOnlyFileSystemException();
|
| 111 | }
|
| 112 |
|
| 113 | @Override
|
| 114 | public void move(final Path source, final Path target, final CopyOption... options) {
|
| 115 | throw new ReadOnlyFileSystemException();
|
| 116 | }
|
| 117 |
|
| 118 | @Override
|
| 119 | public boolean isSameFile(final Path path, final Path path2) {
|
| 120 | return path instanceof LinkFSPath && path2 instanceof LinkFSPath && path.equals(path2);
|
| 121 | }
|
| 122 |
|
| 123 | @Override
|
| 124 | public boolean isHidden(final Path path) {
|
| 125 | return false;
|
| 126 | }
|
| 127 |
|
| 128 | @Override
|
| 129 | public FileStore getFileStore(final Path path) {
|
| 130 | return toLinkPath(path).getFileSystem().store();
|
| 131 | }
|
| 132 |
|
| 133 | @Override
|
| 134 | public void checkAccess(final Path path, final AccessMode... modes) throws IOException {
|
| 135 | if (modes.length == 0 && !toLinkPath(path).exists()) {
|
| 136 | throw new NoSuchFileException(path.toString());
|
| 137 | }
|
| 138 |
|
| 139 | AccessMode[] var3 = modes;
|
| 140 | int var4 = var3.length;
|
| 141 | int var5 = 0;
|
| 142 |
|
| 143 | while (var5 < var4) {
|
| 144 | AccessMode mode = var3[var5];
|
| 145 | switch (mode) {
|
| 146 | case READ:
|
| 147 | if (!toLinkPath(path).exists()) {
|
| 148 | throw new NoSuchFileException(path.toString());
|
| 149 | }
|
| 150 | default:
|
| 151 | var5++;
|
| 152 | break;
|
| 153 | case EXECUTE:
|
| 154 | case WRITE:
|
| 155 | throw new AccessDeniedException(mode.toString());
|
| 156 | }
|
| 157 | }
|
| 158 | }
|
| 159 |
|
| 160 | @Override
|
| 161 | public <V extends FileAttributeView> @Nullable V getFileAttributeView(final Path path, final Class<V> type, final LinkOption... options) {
|
| 162 | LinkFSPath linkPath = toLinkPath(path);
|
| 163 | return (V)(type == BasicFileAttributeView.class ? linkPath.getBasicAttributeView() : null);
|
| 164 | }
|
| 165 |
|
| 166 | @Override
|
| 167 | public <A extends BasicFileAttributes> A readAttributes(final Path path, final Class<A> type, final LinkOption... options) throws IOException {
|
| 168 | LinkFSPath linkPath = toLinkPath(path).toAbsolutePath();
|
| 169 | if (type == BasicFileAttributes.class) {
|
| 170 | return (A)linkPath.getBasicAttributes();
|
| 171 | } else {
|
| 172 | throw new UnsupportedOperationException("Attributes of type " + type.getName() + " not supported");
|
| 173 | }
|
| 174 | }
|
| 175 |
|
| 176 | @Override
|
| 177 | public Map<String, Object> readAttributes(final Path path, final String attributes, final LinkOption... options) {
|
| 178 | throw new UnsupportedOperationException();
|
| 179 | }
|
| 180 |
|
| 181 | @Override
|
| 182 | public void setAttribute(final Path path, final String attribute, final Object value, final LinkOption... options) {
|
| 183 | throw new ReadOnlyFileSystemException();
|
| 184 | }
|
| 185 |
|
| 186 | private static LinkFSPath toLinkPath(final @Nullable Path path) {
|
| 187 | if (path == null) {
|
| 188 | throw new NullPointerException();
|
| 189 | } else if (path instanceof LinkFSPath p) {
|
| 190 | return p;
|
| 191 | } else {
|
| 192 | throw new ProviderMismatchException();
|
| 193 | }
|
| 194 | }
|
| 195 | }
|
| 196 |
|