LinkFSPath .java

net.minecraft.server.packs.linkfs.LinkFSPath (class): members: getBasicAttributeView, getDirectoryContents, getBasicAttributes, PATH_COMPARATOR, toFile.

net.minecraft.server.packs.linkfsclass372 linesserver9.9 KB

Implements

API Surface (26)

LinkFileSystem getFileSystem()
boolean isAbsolute()
File toFile()
LinkFSPath getFileName()
int getNameCount()
LinkFSPath getName(final int index)
LinkFSPath subpath(final int beginIndex, final int endIndex)
boolean startsWith(final Path other)
boolean endsWith(final Path other)
LinkFSPath normalize()
LinkFSPath resolve(final Path other)
LinkFSPath relativize(final Path other)
URI toUri()
LinkFSPath toAbsolutePath()
LinkFSPath toRealPath(final LinkOption... options)
WatchKey register(final WatchService watcher, final Kind<?>[] events, final Modifier... modifiers)
int compareTo(final Path other)
boolean equals(final Object other)
int hashCode()
String toString()
boolean exists()
BasicFileAttributeView getBasicAttributeView()
String name()
BasicFileAttributes readAttributes()
void setTimes(final FileTime lastModifiedTime, final FileTime lastAccessTime, final FileTime createTime)
BasicFileAttributes getBasicAttributes()

Fields & Constants (7)

PATH_COMPARATOR
name
fileSystem
parent
pathToRoot
pathString
pathContents

String literals (8)

"Path "
" does not represent file"
"Invalid index: "
"All content types should be already handled"
"absolute mismatch"
"x-mc-link"
"Failed to create URI"
"basic"

Code Graph: Dependencies (4)

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

Full Source

LinkFSPath.java372 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.server.packs.linkfs;
2
3
import com.google.common.base.Joiner;
4
import com.google.common.collect.ImmutableList;
5
import com.google.common.collect.ImmutableList.Builder;
6
import java.io.File;
7
import java.io.IOException;
8
import java.net.URI;
9
import java.net.URISyntaxException;
10
import java.nio.file.LinkOption;
11
import java.nio.file.NoSuchFileException;
12
import java.nio.file.Path;
13
import java.nio.file.ProviderMismatchException;
14
import java.nio.file.ReadOnlyFileSystemException;
15
import java.nio.file.WatchKey;
16
import java.nio.file.WatchService;
17
import java.nio.file.WatchEvent.Kind;
18
import java.nio.file.WatchEvent.Modifier;
19
import java.nio.file.attribute.BasicFileAttributeView;
20
import java.nio.file.attribute.BasicFileAttributes;
21
import java.nio.file.attribute.FileTime;
22
import java.util.Comparator;
23
import java.util.List;
24
import java.util.Objects;
25
import net.minecraft.util.DummyFileAttributes;
26
import org.jspecify.annotations.Nullable;
27
28
class LinkFSPath implements Path {
29
	private static final Comparator<LinkFSPath> PATH_COMPARATOR = Comparator.comparing(LinkFSPath::pathToString);
30
	private final String name;
31
	private final LinkFileSystem fileSystem;
32
	private final @Nullable LinkFSPath parent;
33
	private @Nullable List<String> pathToRoot;
34
	private @Nullable String pathString;
35
	private final PathContents pathContents;
36
37
	public LinkFSPath(final LinkFileSystem fileSystem, final String name, final @Nullable LinkFSPath parent, final PathContents pathContents) {
38
		this.fileSystem = fileSystem;
39
		this.name = name;
40
		this.parent = parent;
41
		this.pathContents = pathContents;
42
	}
43
44
	private LinkFSPath createRelativePath(final @Nullable LinkFSPath parent, final String name) {
45
		return new LinkFSPath(this.fileSystem, name, parent, PathContents.RELATIVE);
46
	}
47
48
	public LinkFileSystem getFileSystem() {
49
		return this.fileSystem;
50
	}
51
52
	@Override
53
	public boolean isAbsolute() {
54
		return this.pathContents != PathContents.RELATIVE;
55
	}
56
57
	@Override
58
	public File toFile() {
59
		if (this.pathContents instanceof PathContents.FileContents file) {
60
			return file.contents().toFile();
61
		} else {
62
			throw new UnsupportedOperationException("Path " + this.pathToString() + " does not represent file");
63
		}
64
	}
65
66
	public @Nullable LinkFSPath getRoot() {
67
		return this.isAbsolute() ? this.fileSystem.rootPath() : null;
68
	}
69
70
	public LinkFSPath getFileName() {
71
		return this.createRelativePath(null, this.name);
72
	}
73
74
	public @Nullable LinkFSPath getParent() {
75
		return this.parent;
76
	}
77
78
	@Override
79
	public int getNameCount() {
80
		return this.pathToRoot().size();
81
	}
82
83
	private List<String> pathToRoot() {
84
		if (this.name.isEmpty()) {
85
			return List.of();
86
		}
87
88
		if (this.pathToRoot == null) {
89
			Builder<String> result = ImmutableList.builder();
90
			if (this.parent != null) {
91
				result.addAll(this.parent.pathToRoot());
92
			}
93
94
			result.add(this.name);
95
			this.pathToRoot = result.build();
96
		}
97
98
		return this.pathToRoot;
99
	}
100
101
	public LinkFSPath getName(final int index) {
102
		List<String> names = this.pathToRoot();
103
		if (index >= 0 && index < names.size()) {
104
			return this.createRelativePath(null, names.get(index));
105
		} else {
106
			throw new IllegalArgumentException("Invalid index: " + index);
107
		}
108
	}
109
110
	public LinkFSPath subpath(final int beginIndex, final int endIndex) {
111
		List<String> names = this.pathToRoot();
112
		if (beginIndex >= 0 && endIndex <= names.size() && beginIndex < endIndex) {
113
			LinkFSPath current = null;
114
115
			for (int i = beginIndex; i < endIndex; i++) {
116
				current = this.createRelativePath(current, names.get(i));
117
			}
118
119
			return current;
120
		} else {
121
			throw new IllegalArgumentException();
122
		}
123
	}
124
125
	@Override
126
	public boolean startsWith(final Path other) {
127
		if (other.isAbsolute() != this.isAbsolute()) {
128
			return false;
129
		}
130
131
		if (other instanceof LinkFSPath otherLink) {
132
			if (otherLink.fileSystem != this.fileSystem) {
133
				return false;
134
			}
135
136
			List<String> thisNames = this.pathToRoot();
137
			List<String> otherNames = otherLink.pathToRoot();
138
			int otherSize = otherNames.size();
139
			if (otherSize > thisNames.size()) {
140
				return false;
141
			}
142
143
			for (int i = 0; i < otherSize; i++) {
144
				if (!otherNames.get(i).equals(thisNames.get(i))) {
145
					return false;
146
				}
147
			}
148
149
			return true;
150
		} else {
151
			return false;
152
		}
153
	}
154
155
	@Override
156
	public boolean endsWith(final Path other) {
157
		if (other.isAbsolute() && !this.isAbsolute()) {
158
			return false;
159
		}
160
161
		if (other instanceof LinkFSPath otherLink) {
162
			if (otherLink.fileSystem != this.fileSystem) {
163
				return false;
164
			}
165
166
			List<String> thisNames = this.pathToRoot();
167
			List<String> otherNames = otherLink.pathToRoot();
168
			int otherSize = otherNames.size();
169
			int delta = thisNames.size() - otherSize;
170
			if (delta < 0) {
171
				return false;
172
			}
173
174
			for (int i = otherSize - 1; i >= 0; i--) {
175
				if (!otherNames.get(i).equals(thisNames.get(delta + i))) {
176
					return false;
177
				}
178
			}
179
180
			return true;
181
		} else {
182
			return false;
183
		}
184
	}
185
186
	public LinkFSPath normalize() {
187
		return this;
188
	}
189
190
	public LinkFSPath resolve(final Path other) {
191
		LinkFSPath otherLink = this.toLinkPath(other);
192
		return other.isAbsolute() ? otherLink : this.resolve(otherLink.pathToRoot());
193
	}
194
195
	private LinkFSPath resolve(final List<String> names) {
196
		LinkFSPath current = this;
197
198
		for (String name : names) {
199
			current = current.resolveName(name);
200
		}
201
202
		return current;
203
	}
204
205
	LinkFSPath resolveName(final String name) {
206
		if (isRelativeOrMissing(this.pathContents)) {
207
			return new LinkFSPath(this.fileSystem, name, this, this.pathContents);
208
		} else if (this.pathContents instanceof PathContents.DirectoryContents directory) {
209
			LinkFSPath child = directory.children().get(name);
210
			return child != null ? child : new LinkFSPath(this.fileSystem, name, this, PathContents.MISSING);
211
		} else if (this.pathContents instanceof PathContents.FileContents) {
212
			return new LinkFSPath(this.fileSystem, name, this, PathContents.MISSING);
213
		} else {
214
			throw new AssertionError("All content types should be already handled");
215
		}
216
	}
217
218
	private static boolean isRelativeOrMissing(final PathContents contents) {
219
		return contents == PathContents.MISSING || contents == PathContents.RELATIVE;
220
	}
221
222
	public LinkFSPath relativize(final Path other) {
223
		LinkFSPath otherLink = this.toLinkPath(other);
224
		if (this.isAbsolute() != otherLink.isAbsolute()) {
225
			throw new IllegalArgumentException("absolute mismatch");
226
		}
227
228
		List<String> thisNames = this.pathToRoot();
229
		List<String> otherNames = otherLink.pathToRoot();
230
		if (thisNames.size() >= otherNames.size()) {
231
			throw new IllegalArgumentException();
232
		}
233
234
		for (int i = 0; i < thisNames.size(); i++) {
235
			if (!thisNames.get(i).equals(otherNames.get(i))) {
236
				throw new IllegalArgumentException();
237
			}
238
		}
239
240
		return otherLink.subpath(thisNames.size(), otherNames.size());
241
	}
242
243
	@Override
244
	public URI toUri() {
245
		try {
246
			return new URI("x-mc-link", this.fileSystem.store().name(), this.pathToString(), null);
247
		} catch (URISyntaxException e) {
248
			throw new AssertionError("Failed to create URI", e);
249
		}
250
	}
251
252
	public LinkFSPath toAbsolutePath() {
253
		return this.isAbsolute() ? this : this.fileSystem.rootPath().resolve(this);
254
	}
255
256
	public LinkFSPath toRealPath(final LinkOption... options) {
257
		return this.toAbsolutePath();
258
	}
259
260
	@Override
261
	public WatchKey register(final WatchService watcher, final Kind<?>[] events, final Modifier... modifiers) {
262
		throw new UnsupportedOperationException();
263
	}
264
265
	@Override
266
	public int compareTo(final Path other) {
267
		LinkFSPath otherPath = this.toLinkPath(other);
268
		return PATH_COMPARATOR.compare(this, otherPath);
269
	}
270
271
	@Override
272
	public boolean equals(final Object other) {
273
		if (other == this) {
274
			return true;
275
		}
276
277
		if (other instanceof LinkFSPath that) {
278
			if (this.fileSystem != that.fileSystem) {
279
				return false;
280
			} else {
281
				boolean hasRealContents = this.hasRealContents();
282
				if (hasRealContents != that.hasRealContents()) {
283
					return false;
284
				} else {
285
					return hasRealContents ? this.pathContents == that.pathContents : Objects.equals(this.parent, that.parent) && Objects.equals(this.name, that.name);
286
				}
287
			}
288
		} else {
289
			return false;
290
		}
291
	}
292
293
	private boolean hasRealContents() {
294
		return !isRelativeOrMissing(this.pathContents);
295
	}
296
297
	@Override
298
	public int hashCode() {
299
		return this.hasRealContents() ? this.pathContents.hashCode() : this.name.hashCode();
300
	}
301
302
	@Override
303
	public String toString() {
304
		return this.pathToString();
305
	}
306
307
	private String pathToString() {
308
		if (this.pathString == null) {
309
			StringBuilder builder = new StringBuilder();
310
			if (this.isAbsolute()) {
311
				builder.append("/");
312
			}
313
314
			Joiner.on("/").appendTo(builder, this.pathToRoot());
315
			this.pathString = builder.toString();
316
		}
317
318
		return this.pathString;
319
	}
320
321
	private LinkFSPath toLinkPath(final @Nullable Path path) {
322
		if (path == null) {
323
			throw new NullPointerException();
324
		} else if (path instanceof LinkFSPath p && p.fileSystem == this.fileSystem) {
325
			return p;
326
		} else {
327
			throw new ProviderMismatchException();
328
		}
329
	}
330
331
	public boolean exists() {
332
		return this.hasRealContents();
333
	}
334
335
	public @Nullable Path getTargetPath() {
336
		return this.pathContents instanceof PathContents.FileContents file ? file.contents() : null;
337
	}
338
339
	public PathContents.@Nullable DirectoryContents getDirectoryContents() {
340
		return this.pathContents instanceof PathContents.DirectoryContents dir ? dir : null;
341
	}
342
343
	public BasicFileAttributeView getBasicAttributeView() {
344
		return new BasicFileAttributeView() {
345
			@Override
346
			public String name() {
347
				return "basic";
348
			}
349
350
			@Override
351
			public BasicFileAttributes readAttributes() throws IOException {
352
				return LinkFSPath.this.getBasicAttributes();
353
			}
354
355
			@Override
356
			public void setTimes(final FileTime lastModifiedTime, final FileTime lastAccessTime, final FileTime createTime) {
357
				throw new ReadOnlyFileSystemException();
358
			}
359
		};
360
	}
361
362
	public BasicFileAttributes getBasicAttributes() throws IOException {
363
		if (this.pathContents instanceof PathContents.DirectoryContents) {
364
			return DummyFileAttributes.DIRECTORY;
365
		} else if (this.pathContents instanceof PathContents.FileContents) {
366
			return DummyFileAttributes.FILE;
367
		} else {
368
			throw new NoSuchFileException(this.pathToString());
369
		}
370
	}
371
}
372

Decompiled Minecraft 26.3 source of LinkFSPath (net/minecraft/server/packs/linkfs/LinkFSPath.java) with Mojang official mappings, stored verbatim on the site.