| 1 | package net.minecraft.util.filefix.virtualfilesystem;
|
| 2 |
|
| 3 | import java.io.IOException;
|
| 4 | import java.net.URI;
|
| 5 | import java.nio.file.LinkOption;
|
| 6 | import java.nio.file.Path;
|
| 7 | import java.nio.file.WatchKey;
|
| 8 | import java.nio.file.WatchService;
|
| 9 | import java.nio.file.WatchEvent.Kind;
|
| 10 | import java.nio.file.WatchEvent.Modifier;
|
| 11 | import java.util.Objects;
|
| 12 | import net.minecraft.util.filefix.virtualfilesystem.exception.CowFSIllegalArgumentException;
|
| 13 | import net.minecraft.util.filefix.virtualfilesystem.exception.CowFSNoSuchFileException;
|
| 14 | import org.jspecify.annotations.Nullable;
|
| 15 |
|
| 16 | public class CopyOnWriteFSPath implements Path {
|
| 17 | private final Path path;
|
| 18 | private final CopyOnWriteFileSystem fs;
|
| 19 | private final boolean isAbsolute;
|
| 20 |
|
| 21 | private CopyOnWriteFSPath(final Path path, final CopyOnWriteFileSystem fs, final boolean isAbsolute) {
|
| 22 | this.path = path;
|
| 23 | this.fs = fs;
|
| 24 | this.isAbsolute = isAbsolute;
|
| 25 | }
|
| 26 |
|
| 27 | protected static CopyOnWriteFSPath of(final CopyOnWriteFileSystem fs, String first, final String... more) {
|
| 28 | boolean isAbsolute = false;
|
| 29 |
|
| 30 | while (first.startsWith("/")) {
|
| 31 | isAbsolute = true;
|
| 32 | first = first.substring(1);
|
| 33 | }
|
| 34 |
|
| 35 | return new CopyOnWriteFSPath(fs.backingFileSystem().getPath(first, more), fs, isAbsolute);
|
| 36 | }
|
| 37 |
|
| 38 | public CopyOnWriteFileSystem getFileSystem() {
|
| 39 | return this.fs;
|
| 40 | }
|
| 41 |
|
| 42 | @Override
|
| 43 | public boolean isAbsolute() {
|
| 44 | return this.isAbsolute;
|
| 45 | }
|
| 46 |
|
| 47 | @Override
|
| 48 | public @Nullable Path getRoot() {
|
| 49 | return this.isAbsolute() ? this.fs.rootPath() : null;
|
| 50 | }
|
| 51 |
|
| 52 | public @Nullable CopyOnWriteFSPath getFileName() {
|
| 53 | return this.isRoot() ? null : new CopyOnWriteFSPath(this.path.getFileName(), this.fs, false);
|
| 54 | }
|
| 55 |
|
| 56 | public @Nullable CopyOnWriteFSPath getParent() {
|
| 57 | if (this.isRoot()) {
|
| 58 | return null;
|
| 59 | } else {
|
| 60 | Path parent = this.path.getParent();
|
| 61 | if (parent == null) {
|
| 62 | return this.isAbsolute() ? this.fs.rootPath() : null;
|
| 63 | } else {
|
| 64 | return new CopyOnWriteFSPath(parent, this.fs, this.isAbsolute());
|
| 65 | }
|
| 66 | }
|
| 67 | }
|
| 68 |
|
| 69 | @Override
|
| 70 | public int getNameCount() {
|
| 71 | return this.path.toString().isEmpty() && this.isAbsolute ? 0 : this.path.getNameCount();
|
| 72 | }
|
| 73 |
|
| 74 | @Override
|
| 75 | public Path getName(final int index) {
|
| 76 | if (this.isRoot()) {
|
| 77 | throw new IllegalArgumentException();
|
| 78 | } else {
|
| 79 | return new CopyOnWriteFSPath(this.path.getName(index), this.fs, false);
|
| 80 | }
|
| 81 | }
|
| 82 |
|
| 83 | @Override
|
| 84 | public Path subpath(final int beginIndex, final int endIndex) {
|
| 85 | return new CopyOnWriteFSPath(this.path.subpath(beginIndex, endIndex), this.fs, false);
|
| 86 | }
|
| 87 |
|
| 88 | @Override
|
| 89 | public boolean startsWith(final Path other) {
|
| 90 | CopyOnWriteFSPath otherCow = asCow(other);
|
| 91 | return this.isAbsolute() != otherCow.isAbsolute() ? false : this.path.startsWith(otherCow.path);
|
| 92 | }
|
| 93 |
|
| 94 | @Override
|
| 95 | public boolean endsWith(final Path other) {
|
| 96 | CopyOnWriteFSPath otherCow = asCow(other);
|
| 97 | if (otherCow.isAbsolute()) {
|
| 98 | return this.isAbsolute() ? this.equals(otherCow) : false;
|
| 99 | } else {
|
| 100 | return this.path.endsWith(otherCow.path);
|
| 101 | }
|
| 102 | }
|
| 103 |
|
| 104 | public CopyOnWriteFSPath normalize() {
|
| 105 | Path normalize = this.path.normalize();
|
| 106 | return !this.isAbsolute() || !normalize.startsWith(".") && !normalize.startsWith("..")
|
| 107 | ? new CopyOnWriteFSPath(normalize, this.fs, this.isAbsolute())
|
| 108 | : this.fs.rootPath();
|
| 109 | }
|
| 110 |
|
| 111 | public CopyOnWriteFSPath resolve(final Path other) {
|
| 112 | CopyOnWriteFSPath otherCow = asCow(other);
|
| 113 | if (other.isAbsolute()) {
|
| 114 | return otherCow;
|
| 115 | }
|
| 116 |
|
| 117 | Path result = this.path.resolve(otherCow.path);
|
| 118 | return new CopyOnWriteFSPath(result, this.fs, this.isAbsolute());
|
| 119 | }
|
| 120 |
|
| 121 | public CopyOnWriteFSPath resolve(final String other) {
|
| 122 | return this.resolve(this.getFileSystem().getPath(other));
|
| 123 | }
|
| 124 |
|
| 125 | public CopyOnWriteFSPath resolve(final String first, final String... more) {
|
| 126 | CopyOnWriteFSPath result = this.resolve(first);
|
| 127 |
|
| 128 | for (String s : more) {
|
| 129 | result = result.resolve(s);
|
| 130 | }
|
| 131 |
|
| 132 | return result;
|
| 133 | }
|
| 134 |
|
| 135 | public CopyOnWriteFSPath relativize(final Path other) {
|
| 136 | CopyOnWriteFSPath otherCow = asCow(other);
|
| 137 | if (this.isAbsolute() != otherCow.isAbsolute()) {
|
| 138 | throw new IllegalArgumentException("'other' is different type of Path");
|
| 139 | }
|
| 140 |
|
| 141 | Path result = this.path.relativize(otherCow.path);
|
| 142 | return new CopyOnWriteFSPath(result, this.fs, false);
|
| 143 | }
|
| 144 |
|
| 145 | @Override
|
| 146 | public URI toUri() {
|
| 147 | throw new UnsupportedOperationException();
|
| 148 | }
|
| 149 |
|
| 150 | public CopyOnWriteFSPath toAbsolutePath() {
|
| 151 | return this.isAbsolute() ? this : new CopyOnWriteFSPath(this.path, this.fs, true);
|
| 152 | }
|
| 153 |
|
| 154 | public CopyOnWriteFSPath toRealPath(final LinkOption... options) throws CowFSNoSuchFileException {
|
| 155 | return this.fs.provider().getRealPath(this);
|
| 156 | }
|
| 157 |
|
| 158 | @Override
|
| 159 | public WatchKey register(final WatchService watcher, final Kind<?>[] events, final Modifier... modifiers) throws IOException {
|
| 160 | throw new UnsupportedOperationException();
|
| 161 | }
|
| 162 |
|
| 163 | @Override
|
| 164 | public int compareTo(final Path other) {
|
| 165 | CopyOnWriteFSPath otherCow = asCow(other);
|
| 166 | return this.toString().compareTo(otherCow.toString());
|
| 167 | }
|
| 168 |
|
| 169 | @Override
|
| 170 | public String toString() {
|
| 171 | return this.isAbsolute() ? "/" + this.path : this.path.toString();
|
| 172 | }
|
| 173 |
|
| 174 | @Override
|
| 175 | public boolean equals(final Object o) {
|
| 176 | if (o != null && this.getClass() == o.getClass()) {
|
| 177 | CopyOnWriteFSPath paths = (CopyOnWriteFSPath)o;
|
| 178 | return this.isAbsolute == paths.isAbsolute && Objects.equals(this.path, paths.path) && Objects.equals(this.fs, paths.fs);
|
| 179 | } else {
|
| 180 | return false;
|
| 181 | }
|
| 182 | }
|
| 183 |
|
| 184 | @Override
|
| 185 | public int hashCode() {
|
| 186 | return Objects.hash(this.path, this.fs, this.isAbsolute);
|
| 187 | }
|
| 188 |
|
| 189 | protected boolean isRoot() {
|
| 190 | return this.getNameCount() == 0;
|
| 191 | }
|
| 192 |
|
| 193 | protected static CopyOnWriteFSPath asCow(final Path other) {
|
| 194 | if (other instanceof CopyOnWriteFSPath copyOnWriteFSPath) {
|
| 195 | return copyOnWriteFSPath;
|
| 196 | } else {
|
| 197 | throw new CowFSIllegalArgumentException("Other path is of mismatching file system");
|
| 198 | }
|
| 199 | }
|
| 200 | }
|
| 201 |
|