private void copy(Path source, Path target, ImmutableSet<CopyOption> options, boolean move) throws IOException { JimfsPath sourcePath = checkPath(source); JimfsPath targetPath = checkPath(target); FileSystemView sourceView = getDefaultView(sourcePath); FileSystemView targetView = getDefaultView(targetPath); sourceView.copy(sourcePath, targetView, targetPath, options, move); }
@Override public void move(Path source, Path target, CopyOption... options) throws IOException { copy(source, target, Options.getMoveOptions(options), true); }
@Override public boolean isHidden(Path path) throws IOException { // TODO(cgdecker): This should probably be configurable, but this seems fine for now /* * If the DOS view is supported, use the Windows isHidden method (check the dos:hidden * attribute). Otherwise, use the Unix isHidden method (just check if the file name starts with * "."). */ JimfsPath checkedPath = checkPath(path); FileSystemView view = getDefaultView(checkedPath); if (getFileStore(path).supportsFileAttributeView("dos")) { return view .readAttributes(checkedPath, DosFileAttributes.class, Options.NOFOLLOW_LINKS) .isHidden(); } return path.getNameCount() > 0 && path.getFileName().toString().startsWith("."); }
@Override public FileChannel newFileChannel( Path path, Set<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException { JimfsPath checkedPath = checkPath(path); if (!checkedPath.getJimfsFileSystem().getFileStore().supportsFeature(FILE_CHANNEL)) { throw new UnsupportedOperationException(); } return newJimfsFileChannel(checkedPath, options, attrs); }
public AbstractSourceInstance(Properties properties) { this.properties = properties; List<FileSystemProvider> list = JimfsFileSystemProvider.installedProviders(); try { URI uri = new URI("jimfs", "vefa", null, null); ImmutableMap<String, ?> env = ImmutableMap.of("config", Configuration.unix()); for (FileSystemProvider provider : list) if (provider instanceof JimfsFileSystemProvider) fileSystem = provider.newFileSystem(uri, env); if (fileSystem == null) fileSystem = new JimfsFileSystemProvider().newFileSystem(uri, env); } catch (Exception e) { throw new RuntimeException("Unable to create VEFA Validator instance", e); } }
@Override public boolean isSameFile(Path path, Path path2) throws IOException { if (path.equals(path2)) { return true; } if (!(path instanceof JimfsPath && path2 instanceof JimfsPath)) { return false; } JimfsPath checkedPath = (JimfsPath) path; JimfsPath checkedPath2 = (JimfsPath) path2; FileSystemView view = getDefaultView(checkedPath); FileSystemView view2 = getDefaultView(checkedPath2); return view.isSameFile(checkedPath, view2, checkedPath2); }
/** * Gets the file system for the given path. */ private static JimfsFileSystem getFileSystem(Path path) { return (JimfsFileSystem) checkPath(path).getFileSystem(); }
/** * Returns the default file system view for the given path. */ private static FileSystemView getDefaultView(JimfsPath path) { return getFileSystem(path).getDefaultView(); }
@Override public AsynchronousFileChannel newAsynchronousFileChannel( Path path, Set<? extends OpenOption> options, @Nullable 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); }
@VisibleForTesting static FileSystem newFileSystem(URI uri, Configuration config) { checkArgument( URI_SCHEME.equals(uri.getScheme()), "uri (%s) must have scheme %s", uri, URI_SCHEME); try { // Create the FileSystem. It uses JimfsFileSystemProvider as its provider, as that is // the provider that actually implements the operations needed for Files methods to work. JimfsFileSystem fileSystem = JimfsFileSystems.newFileSystem(JimfsFileSystemProvider.instance(), uri, config); /* * Now, call FileSystems.newFileSystem, passing it the FileSystem we just created. This * allows the system-loaded SystemJimfsFileSystemProvider instance to cache the FileSystem * so that methods like Paths.get(URI) work. * We do it in this awkward way to avoid issues when the classes in the API (this class * and Configuration, for example) are loaded by a different classloader than the one that * loads SystemJimfsFileSystemProvider using ServiceLoader. See * https://github.com/google/jimfs/issues/18 for gory details. */ try { ImmutableMap<String, ?> env = ImmutableMap.of(FILE_SYSTEM_KEY, fileSystem); FileSystems.newFileSystem(uri, env, SystemJimfsFileSystemProvider.class.getClassLoader()); } catch (ProviderNotFoundException | ServiceConfigurationError ignore) { // See the similar catch block below for why we ignore this. // We log there rather than here so that there's only typically one such message per VM. } return fileSystem; } catch (IOException e) { throw new AssertionError(e); } }
@Override public SeekableByteChannel newByteChannel( Path path, Set<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException { JimfsPath checkedPath = checkPath(path); JimfsFileChannel channel = newJimfsFileChannel(checkedPath, options, attrs); return checkedPath.getJimfsFileSystem().getFileStore().supportsFeature(FILE_CHANNEL) ? channel : new DowngradedSeekableByteChannel(channel); }
private JimfsFileChannel newJimfsFileChannel( JimfsPath path, Set<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException { ImmutableSet<OpenOption> opts = Options.getOptionsForChannel(options); FileSystemView view = getDefaultView(path); RegularFile file = view.getOrCreateRegularFile(path, opts, attrs); return new JimfsFileChannel(file, opts, view.state()); }
@Override public FileSystem newFileSystem(Path path, Map<String, ?> env) throws IOException { JimfsPath checkedPath = checkPath(path); checkNotNull(env); URI pathUri = checkedPath.toUri(); URI jarUri = URI.create("jar:" + pathUri); try { // pass the new jar:jimfs://... URI to be handled by ZipFileSystemProvider return FileSystems.newFileSystem(jarUri, env); } catch (Exception e) { // if any exception occurred, assume the file wasn't a zip file and that we don't support // viewing it as a file system throw new UnsupportedOperationException(e); } }
@Override public FileStore getFileStore(Path path) throws IOException { return getFileSystem(path).getFileStore(); }
@Override public AsynchronousFileChannel newAsynchronousFileChannel( Path path, Set<? extends OpenOption> options, @Nullable 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); }
@VisibleForTesting static FileSystem newFileSystem(URI uri, Configuration config) { checkArgument( URI_SCHEME.equals(uri.getScheme()), "uri (%s) must have scheme %s", uri, URI_SCHEME); try { // Create the FileSystem. It uses JimfsFileSystemProvider as its provider, as that is // the provider that actually implements the operations needed for Files methods to work. JimfsFileSystem fileSystem = JimfsFileSystems.newFileSystem(JimfsFileSystemProvider.instance(), uri, config); // Now, call FileSystems.newFileSystem, passing it the FileSystem we just created. This // allows the system-loaded SystemJimfsFileSystemProvider instance to cache the FileSystem // so that methods like Paths.get(URI) work. // We do it in this awkward way to avoid issues when the classes in the API (this class // and Configuration, for example) are loaded by a different classloader than the one that // loads SystemJimfsFileSystemProvider using ServiceLoader. See // https://github.com/google/jimfs/issues/18 for gory details. ImmutableMap<String, ?> env = ImmutableMap.of(FILE_SYSTEM_KEY, fileSystem); FileSystems.newFileSystem(uri, env, SystemJimfsFileSystemProvider.class.getClassLoader()); return fileSystem; } catch (IOException e) { throw new AssertionError(e); } }
@Override public void createDirectory(Path dir, FileAttribute<?>... attrs) throws IOException { JimfsPath checkedPath = checkPath(dir); FileSystemView view = getDefaultView(checkedPath); view.createDirectory(checkedPath, attrs); }
@Override public boolean isHidden(Path path) throws IOException { // TODO(cgdecker): This should probably be configurable, but this seems fine for now /* * If the DOS view is supported, use the Windows isHidden method (check the dos:hidden * attribute). Otherwise, use the Unix isHidden method (just check if the file name starts with * "."). */ JimfsPath checkedPath = checkPath(path); FileSystemView view = getDefaultView(checkedPath); if (getFileStore(path).supportsFileAttributeView("dos")) { return view .readAttributes(checkedPath, DosFileAttributes.class, Options.NOFOLLOW_LINKS) .isHidden(); } return path.getNameCount() > 0 && path.getFileName().toString().startsWith("."); }
@Override public FileChannel newFileChannel( Path path, Set<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException { JimfsPath checkedPath = checkPath(path); if (!checkedPath.getJimfsFileSystem().getFileStore().supportsFeature(FILE_CHANNEL)) { throw new UnsupportedOperationException(); } return newJimfsFileChannel(checkedPath, options, attrs); }
@Override public boolean isSameFile(Path path, Path path2) throws IOException { if (path.equals(path2)) { return true; } if (!(path instanceof JimfsPath && path2 instanceof JimfsPath)) { return false; } JimfsPath checkedPath = (JimfsPath) path; JimfsPath checkedPath2 = (JimfsPath) path2; FileSystemView view = getDefaultView(checkedPath); FileSystemView view2 = getDefaultView(checkedPath2); return view.isSameFile(checkedPath, view2, checkedPath2); }