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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
|
/*
* Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.internal.jrtfs;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.NonWritableChannelException;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.SeekableByteChannel;
import java.nio.file.ClosedFileSystemException;
import java.nio.file.CopyOption;
import java.nio.file.DirectoryStream;
import java.nio.file.FileStore;
import java.nio.file.FileSystem;
import java.nio.file.FileSystemException;
import java.nio.file.InvalidPathException;
import java.nio.file.LinkOption;
import java.nio.file.NoSuchFileException;
import java.nio.file.NotDirectoryException;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.ReadOnlyFileSystemException;
import java.nio.file.StandardOpenOption;
import java.nio.file.WatchService;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.FileTime;
import java.nio.file.attribute.UserPrincipalLookupService;
import java.nio.file.spi.FileSystemProvider;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.regex.Pattern;
import jdk.internal.jimage.ImageReader.Node;
import static java.util.stream.Collectors.toList;
/**
* jrt file system implementation built on System jimage files.
*
* @implNote This class needs to maintain JDK 8 source compatibility.
*
* It is used internally in the JDK to implement jimage/jrtfs access,
* but also compiled and delivered as part of the jrtfs.jar to support access
* to the jimage file provided by the shipped JDK by tools running on JDK 8.
*/
class JrtFileSystem extends FileSystem {
private final JrtFileSystemProvider provider;
private final JrtPath rootPath = new JrtPath(this, "/");
private volatile boolean isOpen;
private volatile boolean isClosable;
private SystemImage image;
JrtFileSystem(JrtFileSystemProvider provider, Map<String, ?> env)
throws IOException
{
this.provider = provider;
this.image = SystemImage.open(); // open image file
this.isOpen = true;
this.isClosable = env != null;
}
// FileSystem method implementations
@Override
public boolean isOpen() {
return isOpen;
}
@Override
public void close() throws IOException {
if (!isClosable)
throw new UnsupportedOperationException();
cleanup();
}
@Override
public FileSystemProvider provider() {
return provider;
}
@Override
public Iterable<Path> getRootDirectories() {
return Collections.singleton(getRootPath());
}
@Override
public JrtPath getPath(String first, String... more) {
if (more.length == 0) {
return new JrtPath(this, first);
}
StringBuilder sb = new StringBuilder();
sb.append(first);
for (String path : more) {
if (!path.isEmpty()) {
if (sb.length() > 0) {
sb.append('/');
}
sb.append(path);
}
}
return new JrtPath(this, sb.toString());
}
@Override
public final boolean isReadOnly() {
return true;
}
@Override
public final UserPrincipalLookupService getUserPrincipalLookupService() {
throw new UnsupportedOperationException();
}
@Override
public final WatchService newWatchService() {
throw new UnsupportedOperationException();
}
@Override
public final Iterable<FileStore> getFileStores() {
return Collections.singleton(getFileStore(getRootPath()));
}
private static final Set<String> supportedFileAttributeViews
= Collections.unmodifiableSet(
new HashSet<String>(Arrays.asList("basic", "jrt")));
@Override
public final Set<String> supportedFileAttributeViews() {
return supportedFileAttributeViews;
}
@Override
public final String toString() {
return "jrt:/";
}
@Override
public final String getSeparator() {
return "/";
}
@Override
public PathMatcher getPathMatcher(String syntaxAndInput) {
int pos = syntaxAndInput.indexOf(':');
if (pos <= 0 || pos == syntaxAndInput.length()) {
throw new IllegalArgumentException("pos is " + pos);
}
String syntax = syntaxAndInput.substring(0, pos);
String input = syntaxAndInput.substring(pos + 1);
String expr;
if (syntax.equalsIgnoreCase("glob")) {
expr = JrtUtils.toRegexPattern(input);
} else if (syntax.equalsIgnoreCase("regex")) {
expr = input;
} else {
throw new UnsupportedOperationException("Syntax '" + syntax
+ "' not recognized");
}
// return matcher
final Pattern pattern = Pattern.compile(expr);
return (Path path) -> pattern.matcher(path.toString()).matches();
}
JrtPath resolveLink(JrtPath path) throws IOException {
Node node = checkNode(path);
if (node.isLink()) {
node = node.resolveLink();
return new JrtPath(this, node.getName()); // TBD, normalized?
}
return path;
}
JrtFileAttributes getFileAttributes(JrtPath path, LinkOption... options)
throws IOException {
Node node = checkNode(path);
if (node.isLink() && followLinks(options)) {
return new JrtFileAttributes(node.resolveLink(true));
}
return new JrtFileAttributes(node);
}
/**
* returns the list of child paths of the given directory "path"
*
* @param path name of the directory whose content is listed
* @return iterator for child paths of the given directory path
*/
Iterator<Path> iteratorOf(JrtPath path, DirectoryStream.Filter<? super Path> filter)
throws IOException {
Node node = checkNode(path).resolveLink(true);
if (!node.isDirectory()) {
throw new NotDirectoryException(path.getName());
}
if (filter == null) {
return node.getChildren()
.stream()
.map(child -> (Path)(path.resolve(new JrtPath(this, child.getNameString()).getFileName())))
.iterator();
}
return node.getChildren()
.stream()
.map(child -> (Path)(path.resolve(new JrtPath(this, child.getNameString()).getFileName())))
.filter(p -> { try { return filter.accept(p);
} catch (IOException x) {}
return false;
})
.iterator();
}
// returns the content of the file resource specified by the path
byte[] getFileContent(JrtPath path) throws IOException {
Node node = checkNode(path);
if (node.isDirectory()) {
throw new FileSystemException(path + " is a directory");
}
//assert node.isResource() : "resource node expected here";
return image.getResource(node);
}
/////////////// Implementation details below this point //////////
// static utility methods
static ReadOnlyFileSystemException readOnly() {
return new ReadOnlyFileSystemException();
}
// do the supplied options imply that we have to chase symlinks?
static boolean followLinks(LinkOption... options) {
if (options != null) {
for (LinkOption lo : options) {
Objects.requireNonNull(lo);
if (lo == LinkOption.NOFOLLOW_LINKS) {
return false;
} else {
throw new AssertionError("should not reach here");
}
}
}
return true;
}
// check that the options passed are supported by (read-only) jrt file system
static void checkOptions(Set<? extends OpenOption> options) {
// check for options of null type and option is an intance of StandardOpenOption
for (OpenOption option : options) {
Objects.requireNonNull(option);
if (!(option instanceof StandardOpenOption)) {
throw new IllegalArgumentException(
"option class: " + option.getClass());
}
}
if (options.contains(StandardOpenOption.WRITE) ||
options.contains(StandardOpenOption.APPEND)) {
throw readOnly();
}
}
// clean up this file system - called from finalize and close
synchronized void cleanup() throws IOException {
if (isOpen) {
isOpen = false;
image.close();
image = null;
}
}
// These methods throw read only file system exception
final void setTimes(JrtPath jrtPath, FileTime mtime, FileTime atime, FileTime ctime)
throws IOException {
throw readOnly();
}
// These methods throw read only file system exception
final void createDirectory(JrtPath jrtPath, FileAttribute<?>... attrs) throws IOException {
throw readOnly();
}
final void deleteFile(JrtPath jrtPath, boolean failIfNotExists)
throws IOException {
throw readOnly();
}
final OutputStream newOutputStream(JrtPath jrtPath, OpenOption... options)
throws IOException {
throw readOnly();
}
final void copyFile(boolean deletesrc, JrtPath srcPath, JrtPath dstPath, CopyOption... options)
throws IOException {
throw readOnly();
}
final FileChannel newFileChannel(JrtPath path,
Set<? extends OpenOption> options,
FileAttribute<?>... attrs)
throws IOException {
throw new UnsupportedOperationException("newFileChannel");
}
final InputStream newInputStream(JrtPath path) throws IOException {
return new ByteArrayInputStream(getFileContent(path));
}
final SeekableByteChannel newByteChannel(JrtPath path,
Set<? extends OpenOption> options,
FileAttribute<?>... attrs)
throws IOException {
checkOptions(options);
byte[] buf = getFileContent(path);
final ReadableByteChannel rbc
= Channels.newChannel(new ByteArrayInputStream(buf));
final long size = buf.length;
return new SeekableByteChannel() {
long read = 0;
@Override
public boolean isOpen() {
return rbc.isOpen();
}
@Override
public long position() throws IOException {
return read;
}
@Override
public SeekableByteChannel position(long pos)
throws IOException {
throw new UnsupportedOperationException();
}
@Override
public int read(ByteBuffer dst) throws IOException {
int n = rbc.read(dst);
if (n > 0) {
read += n;
}
return n;
}
@Override
public SeekableByteChannel truncate(long size)
throws IOException {
throw new NonWritableChannelException();
}
@Override
public int write(ByteBuffer src) throws IOException {
throw new NonWritableChannelException();
}
@Override
public long size() throws IOException {
return size;
}
@Override
public void close() throws IOException {
rbc.close();
}
};
}
final JrtFileStore getFileStore(JrtPath path) {
return new JrtFileStore(path);
}
final void ensureOpen() throws IOException {
if (!isOpen()) {
throw new ClosedFileSystemException();
}
}
final JrtPath getRootPath() {
return rootPath;
}
boolean isSameFile(JrtPath path1, JrtPath path2) throws IOException {
return checkNode(path1) == checkNode(path2);
}
boolean isLink(JrtPath path) throws IOException {
return checkNode(path).isLink();
}
boolean exists(JrtPath path) throws IOException {
try {
checkNode(path);
} catch (NoSuchFileException exp) {
return false;
}
return true;
}
boolean isDirectory(JrtPath path, boolean resolveLinks)
throws IOException {
Node node = checkNode(path);
return resolveLinks && node.isLink()
? node.resolveLink(true).isDirectory()
: node.isDirectory();
}
JrtPath toRealPath(JrtPath path, LinkOption... options)
throws IOException {
Node node = checkNode(path);
if (followLinks(options) && node.isLink()) {
node = node.resolveLink();
}
// image node holds the real/absolute path name
return new JrtPath(this, node.getName(), true);
}
private Node lookup(String path) {
try {
return image.findNode(path);
} catch (RuntimeException | IOException ex) {
throw new InvalidPathException(path, ex.toString());
}
}
private Node lookupSymbolic(String path) {
int i = 1;
while (i < path.length()) {
i = path.indexOf('/', i);
if (i == -1) {
break;
}
String prefix = path.substring(0, i);
Node node = lookup(prefix);
if (node == null) {
break;
}
if (node.isLink()) {
Node link = node.resolveLink(true);
// resolved symbolic path concatenated to the rest of the path
String resPath = link.getName() + path.substring(i);
node = lookup(resPath);
return node != null ? node : lookupSymbolic(resPath);
}
i++;
}
return null;
}
Node checkNode(JrtPath path) throws IOException {
ensureOpen();
String p = path.getResolvedPath();
Node node = lookup(p);
if (node == null) {
node = lookupSymbolic(p);
if (node == null) {
throw new NoSuchFileException(p);
}
}
return node;
}
}
|