-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathUnionFileSystem.java
More file actions
397 lines (351 loc) · 14.3 KB
/
UnionFileSystem.java
File metadata and controls
397 lines (351 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
* Copyright (c) Forge Development LLC
* SPDX-License-Identifier: LGPL-2.1-only
*/
package cpw.mods.niofs.union;
import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.nio.channels.FileChannel;
import java.nio.channels.SeekableByteChannel;
import java.nio.file.AccessMode;
import java.nio.file.DirectoryStream;
import java.nio.file.FileStore;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.StandardOpenOption;
import java.nio.file.WatchService;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.UserPrincipalLookupService;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.BiPredicate;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.StreamSupport;
import net.minecraftforge.unsafe.UnsafeHacks;
public class UnionFileSystem extends FileSystem {
private static final MethodHandle ZIPFS_EXISTS;
private static final MethodHandle ZIPFS_CH;
private static final MethodHandle FCI_UNINTERUPTIBLE;
static final String SEP_STRING = "/";
static {
try {
var hackfield = MethodHandles.Lookup.class.getDeclaredField("IMPL_LOOKUP");
UnsafeHacks.setAccessible(hackfield);
MethodHandles.Lookup hack = (MethodHandles.Lookup) hackfield.get(null);
var clz = Class.forName("jdk.nio.zipfs.ZipPath");
ZIPFS_EXISTS = hack.findSpecial(clz, "exists", MethodType.methodType(boolean.class), clz);
clz = Class.forName("jdk.nio.zipfs.ZipFileSystem");
ZIPFS_CH = hack.findGetter(clz, "ch", SeekableByteChannel.class);
clz = Class.forName("sun.nio.ch.FileChannelImpl");
FCI_UNINTERUPTIBLE = hack.findSpecial(clz, "setUninterruptible", MethodType.methodType(void.class), clz);
} catch (NoSuchFieldException | IllegalAccessException | ClassNotFoundException | NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
public InputStream buildInputStream(final UnionPath path) {
try {
var bytes = Files.readAllBytes(path);
return new ByteArrayInputStream(bytes);
} catch (IOException ioe)
{
throw new UncheckedIOException(ioe);
}
}
private static class NoSuchFileException extends java.nio.file.NoSuchFileException {
private static final long serialVersionUID = -80990020803201376L;
public NoSuchFileException(final String file) {
super(file);
}
@Override
public synchronized Throwable fillInStackTrace() {
return this;
}
}
private static class UncheckedIOException extends java.io.UncheckedIOException {
private static final long serialVersionUID = -742496979164359087L;
public UncheckedIOException(final IOException cause) {
super(cause);
}
@Override
public synchronized Throwable fillInStackTrace() {
return this;
}
}
private final UnionPath root = new UnionPath(this, "/");
private final UnionFileSystemProvider provider;
private final String key;
private final List<Path> basepaths;
private final BiPredicate<String, String> pathFilter;
private final Map<Path,EmbeddedFileSystemMetadata> embeddedFileSystems;
public Path getPrimaryPath() {
return basepaths.get(basepaths.size()-1);
}
public BiPredicate<String, String> getFilesystemFilter() {
return pathFilter;
}
String getKey() {
return this.key;
}
private record EmbeddedFileSystemMetadata(Path path, FileSystem fs, SeekableByteChannel fsCh) {}
public UnionFileSystem(final UnionFileSystemProvider provider, final BiPredicate<String, String> pathFilter, final String key, final Path... basepaths) {
this.pathFilter = pathFilter;
this.provider = provider;
this.key = key;
this.basepaths = IntStream.range(0, basepaths.length)
.mapToObj(i->basepaths[basepaths.length - i - 1])
.filter(Files::exists)
.toList(); // we flip the list so later elements are first in search order.
this.embeddedFileSystems = this.basepaths.stream().filter(path -> !Files.isDirectory(path))
.map(UnionFileSystem::openFileSystem)
.flatMap(Optional::stream)
.collect(Collectors.toMap(EmbeddedFileSystemMetadata::path, Function.identity()));
}
private static Optional<EmbeddedFileSystemMetadata> openFileSystem(final Path path) {
try {
var zfs = FileSystems.newFileSystem(path);
SeekableByteChannel fci = (SeekableByteChannel) ZIPFS_CH.invoke(zfs);
if (fci instanceof FileChannel) { // we only make file channels uninterruptible because byte channels (JIJ) already are
FCI_UNINTERUPTIBLE.invoke(fci);
}
return Optional.of(new EmbeddedFileSystemMetadata(path, zfs, fci));
} catch (IOException e) {
throw new UncheckedIOException(e);
} catch (Throwable t) {
throw new IllegalStateException(t);
}
}
@Override
public UnionFileSystemProvider provider() {
return provider;
}
@Override
public void close() {
provider().removeFileSystem(this);
}
@Override
public boolean isOpen() {
return true;
}
@Override
public boolean isReadOnly() {
return true;
}
@Override
public String getSeparator() {
return SEP_STRING;
}
@Override
public Iterable<Path> getRootDirectories() {
return Collections.singletonList(root);
}
public Path getRoot() {
return root;
}
@Override
public Iterable<FileStore> getFileStores() {
return Collections::emptyIterator;
}
@Override
public Set<String> supportedFileAttributeViews() {
return Set.of("basic");
}
@Override
public Path getPath(final String first, final String... more) {
if (more.length > 0) {
var args = new String[more.length + 1];
args[0] = first;
System.arraycopy(more, 0, args, 1, more.length);
return new UnionPath(this, args);
}
return new UnionPath(this, first);
}
private Path fastPath(final String... parts) {
return new UnionPath(this, false, parts);
}
@Override
public PathMatcher getPathMatcher(final String syntaxAndPattern) {
throw new UnsupportedOperationException();
}
@Override
public UserPrincipalLookupService getUserPrincipalLookupService() {
throw new UnsupportedOperationException();
}
@Override
public WatchService newWatchService() {
throw new UnsupportedOperationException();
}
private Optional<BasicFileAttributes> getFileAttributes(final Path path) {
try {
if (path.getFileSystem() == FileSystems.getDefault() && !path.toFile().exists()) {
return Optional.empty();
} else if (path.getFileSystem().provider().getScheme().equals("jar") && !zipFsExists(this, path)) {
return Optional.empty();
} else {
return Optional.of(path.getFileSystem().provider().readAttributes(path, BasicFileAttributes.class));
}
} catch (IOException e) {
return Optional.empty();
}
}
private static boolean zipFsExists(UnionFileSystem ufs, Path path) {
try {
if (Optional.ofNullable(ufs.embeddedFileSystems.get(path.getFileSystem())).filter(efs->!efs.fsCh.isOpen()).isPresent()) throw new IllegalStateException("The zip file has closed!");
return (boolean) ZIPFS_EXISTS.invoke(path);
} catch (Throwable t) {
throw new IllegalStateException(t);
}
}
private Optional<Path> findFirstFiltered(final UnionPath path) {
for (Path p : this.basepaths) {
Path realPath = toRealPath(p, path);
if (testFilter(realPath, p)) {
if (realPath.getFileSystem() == FileSystems.getDefault()) {
if (realPath.toFile().exists()) {
return Optional.of(realPath);
}
} else if (realPath.getFileSystem().provider().getScheme().equals("jar")) {
if (zipFsExists(this, realPath)) {
return Optional.of(realPath);
}
} else if (Files.exists(realPath)) {
return Optional.of(realPath);
}
}
}
return Optional.empty();
}
@SuppressWarnings("unchecked")
public <A extends BasicFileAttributes> A readAttributes(final UnionPath path, final Class<A> type, final LinkOption... options) throws IOException {
if (type == BasicFileAttributes.class) {
// We need to run the test on the actual path,
for (Path base : this.basepaths) {
// We need to know the full path for the filter
Path realPath = toRealPath(base, path);
Optional<BasicFileAttributes> fileAttributes = this.getFileAttributes(realPath);
if (fileAttributes.isPresent() && testFilter(realPath, base)) {
return (A) fileAttributes.get();
}
}
throw new NoSuchFileException(path.toString());
} else {
throw new UnsupportedOperationException();
}
}
public void checkAccess(final UnionPath p, final AccessMode... modes) throws IOException {
try {
findFirstFiltered(p).ifPresentOrElse(path-> {
try {
if (modes.length == 0 && path.getFileSystem() == FileSystems.getDefault()) {
if (!path.toFile().exists()) {
throw new UncheckedIOException(new NoSuchFileException(p.toString()));
}
} else {
path.getFileSystem().provider().checkAccess(path, modes);
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}, ()->{
throw new UncheckedIOException(new NoSuchFileException(p.toString()));
});
} catch (UncheckedIOException e) {
throw e.getCause();
}
}
private Path toRealPath(final Path basePath, final UnionPath path) {
var embeddedpath = path.isAbsolute() ? this.root.relativize(path) : path;
var resolvepath = embeddedpath.normalize().toString();
var efsm = embeddedFileSystems.get(basePath);
if (efsm != null) {
return efsm.fs().getPath(resolvepath);
} else {
return basePath.resolve(resolvepath);
}
}
public SeekableByteChannel newReadByteChannel(final UnionPath path) throws IOException {
try {
return findFirstFiltered(path)
.map(UnionFileSystem::byteChannel)
.orElseThrow(FileNotFoundException::new);
} catch (UncheckedIOException ioe) {
throw ioe.getCause();
}
}
private static SeekableByteChannel byteChannel(final Path path) {
try {
return Files.newByteChannel(path, StandardOpenOption.READ);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public DirectoryStream<Path> newDirStream(final UnionPath path, final DirectoryStream.Filter<? super Path> filter) throws IOException {
final var allpaths = new LinkedHashSet<Path>();
for (final var bp : basepaths) {
final var dir = toRealPath(bp, path);
if (dir.getFileSystem() == FileSystems.getDefault() && !dir.toFile().exists()) {
continue;
} else if (dir.getFileSystem().provider().getScheme().equals("jar") && !zipFsExists(this, dir)) {
continue;
} else if (Files.notExists(dir)) {
continue;
}
final var isSimple = embeddedFileSystems.containsKey(bp);
try (final var ds = Files.newDirectoryStream(dir, filter)) {
StreamSupport.stream(ds.spliterator(), false)
.filter(p->testFilter(p, bp))
.map(other -> StreamSupport.stream(Spliterators.spliteratorUnknownSize((isSimple ? other : bp.relativize(other)).iterator(), Spliterator.ORDERED), false)
.map(Path::getFileName).map(Path::toString).toArray(String[]::new))
.map(this::fastPath)
.forEachOrdered(allpaths::add);
}
}
return new DirectoryStream<>() {
@Override
public Iterator<Path> iterator() {
return allpaths.iterator();
}
@Override
public void close() throws IOException {
// noop
}
};
}
/*
* Standardize paths:
* Path separators converted to /
* Directories end with /
* Remove leading / for absolute paths
*/
private boolean testFilter(final Path path, final Path basePath) {
if (pathFilter == null) return true;
var sPath = path.toString();
if (path.getFileSystem() == basePath.getFileSystem()) // Directories, zips will be different file systems.
sPath = basePath.relativize(path).toString().replace('\\', '/');
if (Files.isDirectory(path))
sPath += '/';
if (sPath.length() > 1 && sPath.startsWith("/"))
sPath = sPath.substring(1);
String sBasePath = basePath.toString().replace('\\', '/');
if (sBasePath.length() > 1 && sBasePath.startsWith("/"))
sBasePath = sBasePath.substring(1);
return pathFilter.test(sPath, sBasePath);
}
}