Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/httpclient/FilePublisher/SecureZipFSProvider.java
41153 views
1
/*
2
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
import java.io.IOException;
25
import java.io.InputStream;
26
import java.net.URI;
27
import java.nio.channels.FileChannel;
28
import java.nio.channels.SeekableByteChannel;
29
import java.nio.file.AccessMode;
30
import java.nio.file.CopyOption;
31
import java.nio.file.DirectoryStream;
32
import java.nio.file.FileStore;
33
import java.nio.file.FileSystem;
34
import java.nio.file.LinkOption;
35
import java.nio.file.OpenOption;
36
import java.nio.file.Path;
37
import java.nio.file.PathMatcher;
38
import java.nio.file.ProviderMismatchException;
39
import java.nio.file.WatchEvent;
40
import java.nio.file.WatchKey;
41
import java.nio.file.WatchService;
42
import java.nio.file.attribute.BasicFileAttributes;
43
import java.nio.file.attribute.FileAttribute;
44
import java.nio.file.attribute.FileAttributeView;
45
import java.nio.file.attribute.UserPrincipalLookupService;
46
import java.nio.file.spi.FileSystemProvider;
47
import java.util.Iterator;
48
import java.util.Map;
49
import java.util.Set;
50
import java.util.concurrent.ConcurrentHashMap;
51
52
public class SecureZipFSProvider extends FileSystemProvider {
53
private final ConcurrentHashMap<FileSystem, SecureZipFS> map =
54
new ConcurrentHashMap<>();
55
private final FileSystemProvider defaultProvider;
56
57
public SecureZipFSProvider(FileSystemProvider provider) {
58
defaultProvider = provider;
59
}
60
61
@Override
62
public String getScheme() {
63
return "jar";
64
}
65
66
public FileSystem newFileSystem(FileSystem fs) {
67
return map.computeIfAbsent(fs, (sfs) ->
68
new SecureZipFS(this, fs));
69
}
70
71
@Override
72
public FileSystem newFileSystem(URI uri, Map<String, ?> env)
73
throws IOException {
74
FileSystem fs = defaultProvider.newFileSystem(uri, env);
75
return map.computeIfAbsent(fs, (sfs) ->
76
new SecureZipFS(this, fs)
77
);
78
}
79
80
@Override
81
public FileSystem getFileSystem(URI uri) {
82
return map.get(defaultProvider.getFileSystem(uri));
83
}
84
85
@Override
86
public Path getPath(URI uri) {
87
Path p = defaultProvider.getPath(uri);
88
return map.get(defaultProvider.getFileSystem(uri)).wrap(p);
89
}
90
91
@Override
92
public InputStream newInputStream(Path path, OpenOption... options)
93
throws IOException {
94
Path p = toTestPath(path).unwrap();
95
96
// Added permission checks before opening the file
97
SecurityManager sm = System.getSecurityManager();
98
if (sm != null) {
99
sm.checkPermission(new RuntimePermission("customPermission"));
100
sm.checkRead(p.toString());
101
}
102
return defaultProvider.newInputStream(p, options);
103
}
104
105
@Override
106
public SeekableByteChannel newByteChannel(Path path,
107
Set<? extends OpenOption> options,
108
FileAttribute<?>... attrs)
109
throws IOException {
110
Path p = toTestPath(path).unwrap();
111
return defaultProvider.newByteChannel(p, options, attrs);
112
}
113
114
@Override
115
public FileChannel newFileChannel(Path path,
116
Set<? extends OpenOption> options,
117
FileAttribute<?>... attrs)
118
throws IOException {
119
Path p = toTestPath(path).unwrap();
120
return defaultProvider.newFileChannel(p, options, attrs);
121
}
122
123
124
@Override
125
public DirectoryStream<Path> newDirectoryStream(Path dir,
126
DirectoryStream.Filter<? super Path> filter) {
127
throw new RuntimeException("not implemented");
128
}
129
130
@Override
131
public void createDirectory(Path dir, FileAttribute<?>... attrs)
132
throws IOException {
133
Path p = toTestPath(dir).unwrap();
134
defaultProvider.createDirectory(p, attrs);
135
}
136
137
@Override
138
public void delete(Path path) throws IOException {
139
Path p = toTestPath(path).unwrap();
140
defaultProvider.delete(p);
141
}
142
143
@Override
144
public void copy(Path source, Path target, CopyOption... options)
145
throws IOException {
146
Path sp = toTestPath(source).unwrap();
147
Path tp = toTestPath(target).unwrap();
148
defaultProvider.copy(sp, tp, options);
149
}
150
151
@Override
152
public void move(Path source, Path target, CopyOption... options)
153
throws IOException {
154
Path sp = toTestPath(source).unwrap();
155
Path tp = toTestPath(target).unwrap();
156
defaultProvider.move(sp, tp, options);
157
}
158
159
@Override
160
public boolean isSameFile(Path path, Path path2)
161
throws IOException {
162
Path p = toTestPath(path).unwrap();
163
Path p2 = toTestPath(path2).unwrap();
164
return defaultProvider.isSameFile(p, p2);
165
}
166
167
@Override
168
public boolean isHidden(Path path) throws IOException {
169
Path p = toTestPath(path).unwrap();
170
return defaultProvider.isHidden(p);
171
}
172
173
@Override
174
public FileStore getFileStore(Path path) throws IOException {
175
Path p = toTestPath(path).unwrap();
176
return defaultProvider.getFileStore(p);
177
}
178
179
@Override
180
public void checkAccess(Path path, AccessMode... modes) throws IOException {
181
Path p = toTestPath(path).unwrap();
182
defaultProvider.checkAccess(p, modes);
183
}
184
185
@Override
186
public <V extends FileAttributeView> V getFileAttributeView(Path path,
187
Class<V> type,
188
LinkOption... options) {
189
Path p = toTestPath(path).unwrap();
190
return defaultProvider.getFileAttributeView(p, type, options);
191
}
192
193
@Override
194
public <A extends BasicFileAttributes> A readAttributes(Path path,
195
Class<A> type,
196
LinkOption... options)
197
throws IOException {
198
Path p = toTestPath(path).unwrap();
199
return defaultProvider.readAttributes(p, type, options);
200
}
201
202
@Override
203
public Map<String, Object> readAttributes(Path path,
204
String attributes,
205
LinkOption... options)
206
throws IOException {
207
Path p = toTestPath(path).unwrap();
208
return defaultProvider.readAttributes(p, attributes, options);
209
}
210
211
@Override
212
public void setAttribute(Path path, String attribute,
213
Object value, LinkOption... options)
214
throws IOException {
215
Path p = toTestPath(path).unwrap();
216
defaultProvider.setAttribute(p, attribute, options);
217
}
218
219
// Checks that the given file is a TestPath
220
static TestPath toTestPath(Path obj) {
221
if (obj == null)
222
throw new NullPointerException();
223
if (!(obj instanceof TestPath))
224
throw new ProviderMismatchException();
225
return (TestPath) obj;
226
}
227
228
static class SecureZipFS extends FileSystem {
229
private final SecureZipFSProvider provider;
230
private final FileSystem delegate;
231
232
public SecureZipFS(SecureZipFSProvider provider, FileSystem delegate) {
233
this.provider = provider;
234
this.delegate = delegate;
235
}
236
237
Path wrap(Path path) {
238
return (path != null) ? new TestPath(this, path) : null;
239
}
240
241
Path unwrap(Path wrapper) {
242
if (wrapper == null)
243
throw new NullPointerException();
244
if (!(wrapper instanceof TestPath))
245
throw new ProviderMismatchException();
246
return ((TestPath) wrapper).unwrap();
247
}
248
249
@Override
250
public FileSystemProvider provider() {
251
return provider;
252
}
253
254
@Override
255
public void close() throws IOException {
256
delegate.close();
257
}
258
259
@Override
260
public boolean isOpen() {
261
return delegate.isOpen();
262
}
263
264
@Override
265
public boolean isReadOnly() {
266
return delegate.isReadOnly();
267
}
268
269
@Override
270
public String getSeparator() {
271
return delegate.getSeparator();
272
}
273
274
@Override
275
public Iterable<Path> getRootDirectories() {
276
return delegate.getRootDirectories();
277
}
278
279
@Override
280
public Iterable<FileStore> getFileStores() {
281
return delegate.getFileStores();
282
}
283
284
@Override
285
public Set<String> supportedFileAttributeViews() {
286
return delegate.supportedFileAttributeViews();
287
}
288
289
@Override
290
public Path getPath(String first, String... more) {
291
return wrap(delegate.getPath(first, more));
292
}
293
294
@Override
295
public PathMatcher getPathMatcher(String syntaxAndPattern) {
296
return delegate.getPathMatcher(syntaxAndPattern);
297
}
298
299
@Override
300
public UserPrincipalLookupService getUserPrincipalLookupService() {
301
return delegate.getUserPrincipalLookupService();
302
}
303
304
@Override
305
public WatchService newWatchService() throws IOException {
306
return delegate.newWatchService();
307
}
308
}
309
310
static class TestPath implements Path {
311
private final SecureZipFS fs;
312
private final Path delegate;
313
314
TestPath(SecureZipFS fs, Path delegate) {
315
this.fs = fs;
316
this.delegate = delegate;
317
}
318
319
Path unwrap() {
320
return delegate;
321
}
322
323
@Override
324
public SecureZipFS getFileSystem() {
325
return fs;
326
}
327
328
@Override
329
public boolean isAbsolute() {
330
return delegate.isAbsolute();
331
}
332
333
@Override
334
public Path getRoot() {
335
return fs.wrap(delegate.getRoot());
336
}
337
338
@Override
339
public Path getFileName() {
340
return fs.wrap(delegate.getFileName());
341
}
342
343
@Override
344
public Path getParent() {
345
return fs.wrap(delegate.getParent());
346
}
347
348
@Override
349
public int getNameCount() {
350
return delegate.getNameCount();
351
}
352
353
@Override
354
public Path getName(int index) {
355
return fs.wrap(delegate.getName(index));
356
}
357
358
@Override
359
public Path subpath(int beginIndex, int endIndex) {
360
return fs.wrap(delegate.subpath(beginIndex, endIndex));
361
}
362
363
@Override
364
public boolean startsWith(Path other) {
365
return delegate.startsWith(other);
366
}
367
368
@Override
369
public boolean endsWith(Path other) {
370
return delegate.endsWith(other);
371
}
372
373
@Override
374
public Path normalize() {
375
return fs.wrap(delegate.normalize());
376
}
377
378
@Override
379
public Path resolve(Path other) {
380
return fs.wrap(delegate.resolve(fs.wrap(other)));
381
}
382
383
@Override
384
public Path relativize(Path other) {
385
return fs.wrap(delegate.relativize(fs.wrap(other)));
386
}
387
388
@Override
389
public URI toUri() {
390
String ssp = delegate.toUri().getSchemeSpecificPart();
391
return URI.create(fs.provider().getScheme() + ":" + ssp);
392
}
393
394
@Override
395
public Path toAbsolutePath() {
396
return fs.wrap(delegate.toAbsolutePath());
397
}
398
399
@Override
400
public Path toRealPath(LinkOption... options) throws IOException {
401
return fs.wrap(delegate.toRealPath(options));
402
}
403
404
@Override
405
public WatchKey register(WatchService watcher,
406
WatchEvent.Kind<?>[] events,
407
WatchEvent.Modifier... modifiers)
408
throws IOException {
409
return delegate.register(watcher, events, modifiers);
410
}
411
412
@Override
413
public Iterator<Path> iterator() {
414
final Iterator<Path> itr = delegate.iterator();
415
return new Iterator<>() {
416
@Override
417
public boolean hasNext() {
418
return itr.hasNext();
419
}
420
421
@Override
422
public Path next() {
423
return fs.wrap(itr.next());
424
}
425
426
@Override
427
public void remove() {
428
itr.remove();
429
}
430
};
431
}
432
433
@Override
434
public int compareTo(Path other) {
435
return delegate.compareTo(fs.unwrap(other));
436
}
437
438
@Override
439
public int hashCode() {
440
return delegate.hashCode();
441
}
442
443
@Override
444
public boolean equals(Object other) {
445
return other instanceof TestPath && delegate.equals(fs.unwrap((TestPath) other));
446
}
447
448
@Override
449
public String toString() {
450
return delegate.toString();
451
}
452
}
453
}
454
455