-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathUnionFileSystemProvider.java
More file actions
280 lines (237 loc) · 10.2 KB
/
UnionFileSystemProvider.java
File metadata and controls
280 lines (237 loc) · 10.2 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
/*
* Copyright (c) Forge Development LLC
* SPDX-License-Identifier: LGPL-2.1-only
*/
package cpw.mods.niofs.union;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.channels.SeekableByteChannel;
import java.nio.file.*;
import java.nio.file.attribute.*;
import java.nio.file.spi.FileSystemProvider;
import java.util.*;
import java.util.function.BiPredicate;
import java.util.stream.Stream;
public class UnionFileSystemProvider extends FileSystemProvider {
private final Map<String, UnionFileSystem> fileSystems = new HashMap<>();
private int index = 0;
@Override
public String getScheme() {
return "union";
}
/**
* Copied from ZipFileSystem, we should just extend ZipFileSystem, but I need to ask cpw
* if there was a reason we are not.
*/
protected Path uriToPath(URI uri) {
String scheme = uri.getScheme();
if ((scheme == null) || !scheme.equalsIgnoreCase(getScheme())) {
throw new IllegalArgumentException("URI scheme is not '" + getScheme() + "'");
}
try {
// only support legacy JAR URL syntax jar:{uri}!/{entry} for now
String spec = uri.getRawSchemeSpecificPart();
int sep = spec.indexOf("!/");
if (sep != -1) {
spec = spec.substring(0, sep);
}
return Paths.get(new URI(spec)).toAbsolutePath();
} catch (URISyntaxException e) {
throw new IllegalArgumentException(e.getMessage(), e);
}
}
/**
* Invoked by FileSystems.newFileSystem, Only returns a value if env contains one of more of:
* "filter": BiPredicate<String, String> - A filter to apply to the opened path
* "additional": List<Path> - Additional paths to join together
* If none specified, throws IllegalArgumentException
* If uri.getScheme() is not "union" throws IllegalArgumentException
* If you wish to create a UnionFileSystem explicitly, invoke newFileSystem(BiPredicate, Path...)
*/
@Override
public FileSystem newFileSystem(final URI uri, final Map<String, ?> env) throws IOException {
@SuppressWarnings("unchecked")
var additional = ((Map<String, List<Path>>)env).getOrDefault("additional", List.<Path>of());
@SuppressWarnings("unchecked")
var filter = ((Map<String, BiPredicate<String, String>>)env).getOrDefault("filter", null);
if (filter == null && additional.isEmpty())
throw new IllegalArgumentException("Missing additional and/or filter");
if (filter == null)
filter = (p, b) -> true;
var path = uriToPath(uri);
var key = makeKey(path);
try {
return newFileSystemInternal(key, filter, Stream.concat(Stream.of(path), additional.stream()).toArray(Path[]::new));
} catch (UncheckedIOException e) {
throw e.getCause();
}
}
/**
* Invoked by FileSystems.newFileSystem, Only returns a value if env contains one of more of:
* "filter": BiPredicate<String, String> - A filter to apply to the opened path
* "additional": List<Path> - Additional paths to join together
* If none specified, throws UnsupportedOperationException instead of IllegalArgumentException
* so that FileSystems.newFileSystem will search for the next provider.
* If you wish to create a UnionFileSystem explicitly, invoke newFileSystem(BiPredicate, Path...)
*/
@Override
public FileSystem newFileSystem(final Path path, final Map<String, ?> env) throws IOException {
@SuppressWarnings("unchecked")
var additional = ((Map<String, List<Path>>)env).getOrDefault("additional", List.<Path>of());
@SuppressWarnings("unchecked")
var filter = ((Map<String, BiPredicate<String, String>>)env).getOrDefault("filter", null);
if (filter == null && additional.isEmpty())
throw new UnsupportedOperationException("Missing additional and/or filter");
var key = makeKey(path);
try {
return newFileSystemInternal(key, filter, Stream.concat(Stream.of(path), additional.stream()).toArray(Path[]::new));
} catch (UncheckedIOException e) {
throw e.getCause();
}
}
public UnionFileSystem newFileSystem(final BiPredicate<String, String> pathfilter, final Path... paths) {
if (paths.length == 0) throw new IllegalArgumentException("Need at least one path");
var key = makeKey(paths[0]);
return newFileSystemInternal(key, pathfilter, paths);
}
private UnionFileSystem newFileSystemInternal(final String key, final BiPredicate<String, String> pathfilter, final Path... paths) {
var normpaths = Arrays.stream(paths)
.map(Path::toAbsolutePath)
.map(Path::normalize)
.toArray(Path[]::new);
synchronized (fileSystems) {
var ufs = new UnionFileSystem(this, pathfilter, key, normpaths);
fileSystems.put(key, ufs);
return ufs;
}
}
private synchronized String makeKey(Path path) {
String key;
if (path instanceof UnionPath p)
key = p.getFileSystem().getKey();
else {
var uri = path.toAbsolutePath().normalize().toUri();
key = uri.getPath();
if (key == null) // Fuck it this doesn't actually mean anything is just nice for debugging
key = uri.toString();
}
key = key.replace('!', '_') + "#" + index++;
if (key.charAt(0) != '/')
key = '/' + key; // URI's require anything that uses schemas to use absolute paths. So make sure our key is absolute.
return key;
}
@Override
public Path getPath(final URI uri) {
var parts = uri.getPath().split("!");
if (parts.length > 1) {
return getFileSystem(uri).getPath(parts[1]);
} else {
return ((UnionFileSystem)getFileSystem(uri)).getRoot();
}
}
@Override
public FileSystem getFileSystem(final URI uri) {
var parts = uri.getPath().split("!");
if (!fileSystems.containsKey(parts[0])) throw new FileSystemNotFoundException();
return fileSystems.get(parts[0]);
}
@Override
public SeekableByteChannel newByteChannel(final Path path, final Set<? extends OpenOption> options, final FileAttribute<?>... attrs) throws IOException {
if (path instanceof UnionPath up) {
if (options.size() > 1) throw new UnsupportedOperationException();
if (!options.isEmpty() && !options.contains(StandardOpenOption.READ)) throw new UnsupportedOperationException();
return up.getFileSystem().newReadByteChannel(up);
}
throw new UnsupportedOperationException();
}
@Override
public DirectoryStream<Path> newDirectoryStream(final Path dir, final DirectoryStream.Filter<? super Path> filter) throws IOException {
if (dir instanceof UnionPath up) {
return up.getFileSystem().newDirStream(up, filter);
}
return null;
}
@Override
public void createDirectory(final Path dir, final FileAttribute<?>... attrs) throws IOException {
throw new UnsupportedOperationException();
}
@Override
public void delete(final Path path) throws IOException {
throw new UnsupportedOperationException();
}
@Override
public void copy(final Path source, final Path target, final CopyOption... options) throws IOException {
throw new UnsupportedOperationException();
}
@Override
public void move(final Path source, final Path target, final CopyOption... options) throws IOException {
throw new UnsupportedOperationException();
}
@Override
public boolean isSameFile(final Path path, final Path path2) throws IOException {
return false;
}
@Override
public boolean isHidden(final Path path) throws IOException {
return false;
}
@Override
public FileStore getFileStore(final Path path) throws IOException {
return null;
}
@Override
public void checkAccess(final Path path, final AccessMode... modes) throws IOException {
if (path instanceof UnionPath p) {
p.getFileSystem().checkAccess(p, modes);
}
}
@SuppressWarnings("unchecked")
@Override
public <V extends FileAttributeView> V getFileAttributeView(final Path path, final Class<V> type, final LinkOption... options) {
if (path instanceof UnionPath && type == BasicFileAttributeView.class) {
return (V) new UnionBasicFileAttributeView(path, options);
}
return null;
}
@Override
public <A extends BasicFileAttributes> A readAttributes(final Path path, final Class<A> type, final LinkOption... options) throws IOException {
if (path instanceof UnionPath p) {
return p.getFileSystem().readAttributes(p, type);
}
throw new UnsupportedOperationException();
}
@Override
public Map<String, Object> readAttributes(final Path path, final String attributes, final LinkOption... options) throws IOException {
throw new UnsupportedOperationException();
}
@Override
public void setAttribute(final Path path, final String attribute, final Object value, final LinkOption... options) throws IOException {
throw new UnsupportedOperationException();
}
void removeFileSystem(UnionFileSystem fs) {
synchronized (fileSystems) {
fileSystems.remove(fs.getKey());
}
}
private final class UnionBasicFileAttributeView implements BasicFileAttributeView {
private final Path path;
private final LinkOption[] options;
public UnionBasicFileAttributeView(Path path, LinkOption[] options) {
this.path = path;
this.options = options;
}
@Override
public String name() {
return "union";
}
@Override
public BasicFileAttributes readAttributes() throws IOException {
return UnionFileSystemProvider.this.readAttributes(path, BasicFileAttributes.class, options);
}
@Override
public void setTimes(FileTime lastModifiedTime, FileTime lastAccessTime, FileTime createTime) {
}
}
}