下面列出了java.nio.file.Path#getFileSystem ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public <V extends FileAttributeView> V getFileAttributeView(Path path,
Class<V> type, LinkOption... options) {
BundleFileSystem fs = (BundleFileSystem) path.getFileSystem();
if (path.toAbsolutePath().equals(fs.getRootDirectory())) {
// Bug in ZipFS, it will fall over as there is no entry for /
//
// Instead we'll just give a view of the source (e.g. the zipfile
// itself).
// Modifying its times is a bit futile since they are likely to be
// overriden when closing, but this avoids a NullPointerException
// in Files.setTimes().
return Files.getFileAttributeView(fs.getSource(), type, options);
}
return origProvider(path).getFileAttributeView(fs.unwrap(path), type,
options);
}
@Test
public void shouldNotCrashOnBrokenSymlink() throws IOException {
ProjectWorkspace workspace =
TestDataHelper.createProjectWorkspaceForScenario(this, "sdk-discovery-symlink", temp);
workspace.setUp();
Path root = workspace.getPath("");
FileSystem fileSystem = root.getFileSystem();
Path sdksDir = root.resolve("Platforms/MacOSX.platform/Developer/SDKs");
Files.createDirectories(sdksDir);
CreateSymlinksForTests.createSymLink(
sdksDir.resolve("MacOSX.sdk"), fileSystem.getPath("does_not_exist"));
ImmutableMap<String, AppleToolchain> toolchains =
ImmutableMap.of("com.apple.dt.toolchain.XcodeDefault", getDefaultToolchain(root));
ImmutableMap<AppleSdk, AppleSdkPaths> actual =
AppleSdkDiscovery.discoverAppleSdkPaths(
Optional.of(root),
ImmutableList.of(root),
toolchains,
FakeBuckConfig.builder().build().getView(AppleConfig.class));
assertThat(actual.size(), is(0));
}
public static void main(String[] args) throws Exception {
FileSystem fs = FileSystems.getDefault();
if (fs.getClass().getModule() == Object.class.getModule())
throw new RuntimeException("FileSystemProvider not overridden");
// exercise the file system
Path dir = Files.createTempDirectory("tmp");
if (dir.getFileSystem() != fs)
throw new RuntimeException("'dir' not in default file system");
System.out.println("created: " + dir);
Path foo = Files.createFile(dir.resolve("foo"));
if (foo.getFileSystem() != fs)
throw new RuntimeException("'foo' not in default file system");
System.out.println("created: " + foo);
// exercise interop with java.io.File
File file = foo.toFile();
Path path = file.toPath();
if (path.getFileSystem() != fs)
throw new RuntimeException("'path' not in default file system");
if (!path.equals(foo))
throw new RuntimeException(path + " not equal to " + foo);
}
@Override
public DirectoryStream<Path> newDirectoryStream(Path dir,
final Filter<? super Path> filter) throws IOException {
final BundleFileSystem fs = (BundleFileSystem) dir.getFileSystem();
final DirectoryStream<Path> stream = origProvider(dir)
.newDirectoryStream(fs.unwrap(dir), new Filter<Path>() {
@Override
public boolean accept(Path entry) throws IOException {
return filter.accept(fs.wrap(entry));
}
});
return new DirectoryStream<Path>() {
@Override
public void close() throws IOException {
stream.close();
}
@Override
public Iterator<Path> iterator() {
return fs.wrapIterator(stream.iterator());
}
};
}
@Override
public void setAttribute(Path path, String attribute, Object value,
LinkOption... options) throws IOException {
BundleFileSystem fs = (BundleFileSystem) path.getFileSystem();
origProvider(path).setAttribute(fs.unwrap(path), attribute, value,
options);
}
@Override
public AsynchronousFileChannel newAsynchronousFileChannel(
Path path,
Set<? extends OpenOption> options,
@NullableDecl ExecutorService executor,
FileAttribute<?>... attrs)
throws IOException {
// call newFileChannel and cast so that FileChannel support is checked there
JimfsFileChannel channel = (JimfsFileChannel) newFileChannel(path, options, attrs);
if (executor == null) {
JimfsFileSystem fileSystem = (JimfsFileSystem) path.getFileSystem();
executor = fileSystem.getDefaultThreadPool();
}
return channel.asAsynchronousFileChannel(executor);
}
FaultyFileSystem(Path root) throws IOException {
if (root == null) {
root = Files.createTempDirectory("faultyFS");
removeRootAfterClose = true;
} else {
if (! Files.isDirectory(root)) {
throw new IllegalArgumentException("must be a directory.");
}
removeRootAfterClose = false;
}
this.root = root;
delegate = root.getFileSystem();
isOpen = true;
}
FaultyFileSystem(Path root) throws IOException {
if (root == null) {
root = Files.createTempDirectory("faultyFS");
removeRootAfterClose = true;
} else {
if (! Files.isDirectory(root)) {
throw new IllegalArgumentException("must be a directory.");
}
removeRootAfterClose = false;
}
this.root = root;
delegate = root.getFileSystem();
isOpen = true;
}
private boolean contains(Collection<Path> searchPath, Path file) throws IOException {
if (searchPath == null) {
return false;
}
Path enclosingJar = null;
if (file.getFileSystem().provider() == fsInfo.getJarFSProvider()) {
URI uri = file.toUri();
if (uri.getScheme().equals("jar")) {
String ssp = uri.getSchemeSpecificPart();
int sep = ssp.lastIndexOf("!");
if (ssp.startsWith("file:") && sep > 0) {
enclosingJar = Paths.get(URI.create(ssp.substring(0, sep)));
}
}
}
Path nf = normalize(file);
for (Path p : searchPath) {
Path np = normalize(p);
if (np.getFileSystem() == nf.getFileSystem()
&& Files.isDirectory(np)
&& nf.startsWith(np)) {
return true;
}
if (enclosingJar != null
&& Files.isSameFile(enclosingJar, np)) {
return true;
}
}
return false;
}
FaultyFileSystem(Path root) throws IOException {
if (root == null) {
root = Files.createTempDirectory("faultyFS");
removeRootAfterClose = true;
} else {
if (! Files.isDirectory(root)) {
throw new IllegalArgumentException("must be a directory.");
}
removeRootAfterClose = false;
}
this.root = root;
delegate = root.getFileSystem();
isOpen = true;
}
FaultyFileSystem(Path root) throws IOException {
if (root == null) {
root = Files.createTempDirectory("faultyFS");
removeRootAfterClose = true;
} else {
if (! Files.isDirectory(root)) {
throw new IllegalArgumentException("must be a directory.");
}
removeRootAfterClose = false;
}
this.root = root;
delegate = root.getFileSystem();
isOpen = true;
}
public FileWatcher(LuckPermsPlugin plugin, Path basePath) throws IOException {
super(basePath.getFileSystem(), true);
this.watchedLocations = Collections.synchronizedMap(new HashMap<>());
this.basePath = basePath;
super.registerRecursively(basePath);
plugin.getBootstrap().getScheduler().executeAsync(super::runEventProcessingLoop);
}
@Override
public FileChannel newFileChannel(Path path,
Set<? extends OpenOption> options, FileAttribute<?>... attrs)
throws IOException {
final BundleFileSystem fs = (BundleFileSystem) path.getFileSystem();
FileChannel fc = origProvider(path).newFileChannel(fs.unwrap(path),
options, attrs);
return new BundleFileChannel(fc, path, options, attrs);
}
private static Path resolve(Path base, String relativePath) {
FileSystem fs = base.getFileSystem();
Path rp = fs.getPath(relativePath.replace("/", fs.getSeparator()));
return base.resolve(rp);
}
@Override
public boolean isSameFile(Path path, Path path2) throws IOException {
BundleFileSystem fs = (BundleFileSystem) path.getFileSystem();
return origProvider(path).isSameFile(fs.unwrap(path), fs.unwrap(path2));
}
private MCRPath checkFileSystem(Path path) {
if (!(path.getFileSystem() instanceof MCRIFSFileSystem)) {
throw new IllegalArgumentException(path + " is not from " + MCRIFSFileSystem.class.getSimpleName());
}
return MCRPath.toMCRPath(path);
}
private static Path resolve(Path base, String relativePath) {
FileSystem fs = base.getFileSystem();
Path rp = fs.getPath(relativePath.replace("/", fs.getSeparator()));
return base.resolve(rp);
}
private static Path resolve(Path base, String relativePath) {
FileSystem fs = base.getFileSystem();
Path rp = fs.getPath(relativePath.replace("/", fs.getSeparator()));
return base.resolve(rp);
}
@Override
public boolean isHidden(Path path) throws IOException {
BundleFileSystem fs = (BundleFileSystem) path.getFileSystem();
return origProvider(path).isHidden(fs.unwrap(path));
}
public FileSystemStorage(Path rootDirectory) {
this.rootDirectory = rootDirectory;
this.pathNormalizer = new PathNormalizer(rootDirectory.getFileSystem());
}