Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/jdk/internal/jrtfs/JrtFileSystemProvider.java
41159 views
1
/*
2
* Copyright (c) 2016, 2021, 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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
package jdk.internal.jrtfs;
26
27
import java.io.*;
28
import java.net.MalformedURLException;
29
import java.net.URL;
30
import java.net.URLClassLoader;
31
import java.nio.channels.*;
32
import java.nio.file.*;
33
import java.nio.file.DirectoryStream.Filter;
34
import java.nio.file.attribute.*;
35
import java.nio.file.spi.FileSystemProvider;
36
import java.net.URI;
37
import java.security.AccessController;
38
import java.security.PrivilegedAction;
39
import java.util.HashMap;
40
import java.util.Map;
41
import java.util.Objects;
42
import java.util.Set;
43
import java.util.concurrent.ExecutorService;
44
45
/**
46
* File system provider for jrt file systems. Conditionally creates jrt fs on
47
* .jimage file or exploded modules directory of underlying JDK.
48
*
49
* @implNote This class needs to maintain JDK 8 source compatibility.
50
*
51
* It is used internally in the JDK to implement jimage/jrtfs access,
52
* but also compiled and delivered as part of the jrtfs.jar to support access
53
* to the jimage file provided by the shipped JDK by tools running on JDK 8.
54
*/
55
public final class JrtFileSystemProvider extends FileSystemProvider {
56
57
private volatile FileSystem theFileSystem;
58
59
public JrtFileSystemProvider() {
60
}
61
62
@Override
63
public String getScheme() {
64
return "jrt";
65
}
66
67
/**
68
* Need RuntimePermission "accessSystemModules" to create or get jrt:/
69
*/
70
private void checkPermission() {
71
@SuppressWarnings("removal")
72
SecurityManager sm = System.getSecurityManager();
73
if (sm != null) {
74
RuntimePermission perm = new RuntimePermission("accessSystemModules");
75
sm.checkPermission(perm);
76
}
77
}
78
79
private void checkUri(URI uri) {
80
if (!uri.getScheme().equalsIgnoreCase(getScheme())) {
81
throw new IllegalArgumentException("URI does not match this provider");
82
}
83
if (uri.getAuthority() != null) {
84
throw new IllegalArgumentException("Authority component present");
85
}
86
if (uri.getPath() == null) {
87
throw new IllegalArgumentException("Path component is undefined");
88
}
89
if (!uri.getPath().equals("/")) {
90
throw new IllegalArgumentException("Path component should be '/'");
91
}
92
if (uri.getQuery() != null) {
93
throw new IllegalArgumentException("Query component present");
94
}
95
if (uri.getFragment() != null) {
96
throw new IllegalArgumentException("Fragment component present");
97
}
98
}
99
100
@Override
101
public FileSystem newFileSystem(URI uri, Map<String, ?> env)
102
throws IOException {
103
Objects.requireNonNull(env);
104
checkPermission();
105
checkUri(uri);
106
if (env.containsKey("java.home")) {
107
return newFileSystem((String)env.get("java.home"), uri, env);
108
} else {
109
return new JrtFileSystem(this, env);
110
}
111
}
112
113
private static final String JRT_FS_JAR = "jrt-fs.jar";
114
private FileSystem newFileSystem(String targetHome, URI uri, Map<String, ?> env)
115
throws IOException {
116
Objects.requireNonNull(targetHome);
117
Path jrtfs = FileSystems.getDefault().getPath(targetHome, "lib", JRT_FS_JAR);
118
if (Files.notExists(jrtfs)) {
119
throw new IOException(jrtfs.toString() + " not exist");
120
}
121
Map<String,?> newEnv = new HashMap<>(env);
122
newEnv.remove("java.home");
123
ClassLoader cl = newJrtFsLoader(jrtfs);
124
try {
125
Class<?> c = Class.forName(JrtFileSystemProvider.class.getName(), false, cl);
126
@SuppressWarnings("deprecation")
127
Object tmp = c.newInstance();
128
return ((FileSystemProvider)tmp).newFileSystem(uri, newEnv);
129
} catch (ClassNotFoundException |
130
IllegalAccessException |
131
InstantiationException e) {
132
throw new IOException(e);
133
}
134
}
135
136
private static class JrtFsLoader extends URLClassLoader {
137
JrtFsLoader(URL[] urls) {
138
super(urls);
139
}
140
@Override
141
protected Class<?> loadClass(String cn, boolean resolve)
142
throws ClassNotFoundException
143
{
144
Class<?> c = findLoadedClass(cn);
145
if (c == null) {
146
URL u = findResource(cn.replace('.', '/') + ".class");
147
if (u != null) {
148
c = findClass(cn);
149
} else {
150
return super.loadClass(cn, resolve);
151
}
152
}
153
if (resolve)
154
resolveClass(c);
155
return c;
156
}
157
}
158
159
@SuppressWarnings("removal")
160
private static URLClassLoader newJrtFsLoader(Path jrtfs) {
161
final URL url;
162
try {
163
url = jrtfs.toUri().toURL();
164
} catch (MalformedURLException mue) {
165
throw new IllegalArgumentException(mue);
166
}
167
168
final URL[] urls = new URL[] { url };
169
return AccessController.doPrivileged(
170
new PrivilegedAction<URLClassLoader>() {
171
@Override
172
public URLClassLoader run() {
173
return new JrtFsLoader(urls);
174
}
175
}
176
);
177
}
178
179
@Override
180
public Path getPath(URI uri) {
181
checkPermission();
182
if (!uri.getScheme().equalsIgnoreCase(getScheme())) {
183
throw new IllegalArgumentException("URI does not match this provider");
184
}
185
if (uri.getAuthority() != null) {
186
throw new IllegalArgumentException("Authority component present");
187
}
188
if (uri.getQuery() != null) {
189
throw new IllegalArgumentException("Query component present");
190
}
191
if (uri.getFragment() != null) {
192
throw new IllegalArgumentException("Fragment component present");
193
}
194
String path = uri.getPath();
195
if (path == null || path.charAt(0) != '/' || path.contains("..")) {
196
throw new IllegalArgumentException("Invalid path component");
197
}
198
199
return getTheFileSystem().getPath("/modules" + path);
200
}
201
202
private FileSystem getTheFileSystem() {
203
checkPermission();
204
FileSystem fs = this.theFileSystem;
205
if (fs == null) {
206
synchronized (this) {
207
fs = this.theFileSystem;
208
if (fs == null) {
209
try {
210
this.theFileSystem = fs = new JrtFileSystem(this, null);
211
} catch (IOException ioe) {
212
throw new InternalError(ioe);
213
}
214
}
215
}
216
}
217
return fs;
218
}
219
220
@Override
221
public FileSystem getFileSystem(URI uri) {
222
checkPermission();
223
checkUri(uri);
224
return getTheFileSystem();
225
}
226
227
// Checks that the given file is a JrtPath
228
static final JrtPath toJrtPath(Path path) {
229
Objects.requireNonNull(path, "path");
230
if (!(path instanceof JrtPath)) {
231
throw new ProviderMismatchException();
232
}
233
return (JrtPath) path;
234
}
235
236
@Override
237
public void checkAccess(Path path, AccessMode... modes) throws IOException {
238
toJrtPath(path).checkAccess(modes);
239
}
240
241
@Override
242
public Path readSymbolicLink(Path link) throws IOException {
243
return toJrtPath(link).readSymbolicLink();
244
}
245
246
@Override
247
public void copy(Path src, Path target, CopyOption... options)
248
throws IOException {
249
toJrtPath(src).copy(toJrtPath(target), options);
250
}
251
252
@Override
253
public void createDirectory(Path path, FileAttribute<?>... attrs)
254
throws IOException {
255
toJrtPath(path).createDirectory(attrs);
256
}
257
258
@Override
259
public final void delete(Path path) throws IOException {
260
toJrtPath(path).delete();
261
}
262
263
@Override
264
@SuppressWarnings("unchecked")
265
public <V extends FileAttributeView> V
266
getFileAttributeView(Path path, Class<V> type, LinkOption... options) {
267
return JrtFileAttributeView.get(toJrtPath(path), type, options);
268
}
269
270
@Override
271
public FileStore getFileStore(Path path) throws IOException {
272
return toJrtPath(path).getFileStore();
273
}
274
275
@Override
276
public boolean isHidden(Path path) {
277
return toJrtPath(path).isHidden();
278
}
279
280
@Override
281
public boolean isSameFile(Path path, Path other) throws IOException {
282
return toJrtPath(path).isSameFile(other);
283
}
284
285
@Override
286
public void move(Path src, Path target, CopyOption... options)
287
throws IOException {
288
toJrtPath(src).move(toJrtPath(target), options);
289
}
290
291
@Override
292
public AsynchronousFileChannel newAsynchronousFileChannel(Path path,
293
Set<? extends OpenOption> options,
294
ExecutorService exec,
295
FileAttribute<?>... attrs)
296
throws IOException {
297
throw new UnsupportedOperationException();
298
}
299
300
@Override
301
public SeekableByteChannel newByteChannel(Path path,
302
Set<? extends OpenOption> options,
303
FileAttribute<?>... attrs)
304
throws IOException {
305
return toJrtPath(path).newByteChannel(options, attrs);
306
}
307
308
@Override
309
public DirectoryStream<Path> newDirectoryStream(
310
Path path, Filter<? super Path> filter) throws IOException {
311
return toJrtPath(path).newDirectoryStream(filter);
312
}
313
314
@Override
315
public FileChannel newFileChannel(Path path,
316
Set<? extends OpenOption> options,
317
FileAttribute<?>... attrs)
318
throws IOException {
319
return toJrtPath(path).newFileChannel(options, attrs);
320
}
321
322
@Override
323
public InputStream newInputStream(Path path, OpenOption... options)
324
throws IOException {
325
return toJrtPath(path).newInputStream(options);
326
}
327
328
@Override
329
public OutputStream newOutputStream(Path path, OpenOption... options)
330
throws IOException {
331
return toJrtPath(path).newOutputStream(options);
332
}
333
334
@Override
335
@SuppressWarnings("unchecked") // Cast to A
336
public <A extends BasicFileAttributes> A
337
readAttributes(Path path, Class<A> type, LinkOption... options)
338
throws IOException {
339
if (type == BasicFileAttributes.class || type == JrtFileAttributes.class) {
340
return (A) toJrtPath(path).getAttributes(options);
341
}
342
return null;
343
}
344
345
@Override
346
public Map<String, Object>
347
readAttributes(Path path, String attribute, LinkOption... options)
348
throws IOException {
349
return toJrtPath(path).readAttributes(attribute, options);
350
}
351
352
@Override
353
public void setAttribute(Path path, String attribute,
354
Object value, LinkOption... options)
355
throws IOException {
356
toJrtPath(path).setAttribute(attribute, value, options);
357
}
358
}
359
360