@Override public InputStream newInputStream(Path path, OpenOption... options) throws IOException { JimfsPath checkedPath = checkPath(path); ImmutableSet<OpenOption> opts = Options.getOptionsForInputStream(options); FileSystemView view = getDefaultView(checkedPath); RegularFile file = view.getOrCreateRegularFile(checkedPath, opts, NO_ATTRS); return new JimfsInputStream(file, view.state()); }
/** * Deletes the given directory entry from its parent directory. */ private void delete(DirectoryEntry entry, DeleteMode deleteMode, JimfsPath pathForException) throws IOException { Directory parent = entry.directory(); File file = entry.file(); checkDeletable(file, deleteMode, pathForException); parent.unlink(entry.name()); parent.updateModifiedTime(); file.deleted(); }
@Override public JimfsPath toRealPath(LinkOption... options) throws IOException { return getJimfsFileSystem() .getDefaultView() .toRealPath(this, pathService, Options.getLinkOptions(options)); }
private ErrorProneInMemoryFileManager(Optional<Class<?>> clazz) { super(new Context(), /* register= */ false, UTF_8); this.fileSystem = Jimfs.newFileSystem(Configuration.unix()); this.clazz = clazz; }
@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 FileLock tryLock(long position, long size, boolean shared) throws IOException { Util.checkNotNegative(position, "position"); Util.checkNotNegative(size, "size"); channel.checkOpen(); if (shared) { channel.checkReadable(); } else { channel.checkWritable(); } return new JimfsFileChannel.FakeFileLock(this, position, size, shared); }
/** * Reads attributes of the file located by the given path in this view as an object. */ public <A extends BasicFileAttributes> A readAttributes( JimfsPath path, Class<A> type, Set<? super LinkOption> options) throws IOException { File file = lookUpWithLock(path, options) .requireExists(path) .file(); return store.readAttributes(file, type); }
/** * Gets the URI of the given path in this file system. */ public URI toUri(JimfsPath path) { fileStore.state().checkOpen(); return pathService.toUri(uri, path.toAbsolutePath()); }
@Override public File lookup() throws IOException { checkOpen(); // per the spec, must check that the stream is open for each view operation return view .lookUpWithLock(checkedPath, optionsSet) .requireExists(checkedPath) .file(); } },
@Override public <A extends BasicFileAttributes> A readAttributes( Path path, Class<A> type, LinkOption... options) throws IOException { JimfsPath checkedPath = checkPath(path); return getDefaultView(checkedPath) .readAttributes(checkedPath, type, Options.getLinkOptions(options)); }
@Override public void setAttribute(Path path, String attribute, Object value, LinkOption... options) throws IOException { JimfsPath checkedPath = checkPath(path); getDefaultView(checkedPath) .setAttribute(checkedPath, attribute, value, Options.getLinkOptions(options)); } }
/** * Links the given name to the given file in this directory. * * @throws IllegalArgumentException if {@code name} is a reserved name such as "." or if an * entry already exists for the name */ public void link(Name name, File file) { DirectoryEntry entry = new DirectoryEntry(this, checkNotReserved(name, "link"), file); put(entry); file.linked(entry); }
@Override public PathMatcher getPathMatcher(String syntaxAndPattern) { fileStore.state().checkOpen(); return pathService.createPathMatcher(syntaxAndPattern); }
/** * Creates a copy of the given file, copying its attributes as well according to the given * {@code attributeCopyOption}. */ File copyWithoutContent(File file, AttributeCopyOption attributeCopyOption) throws IOException { File copy = factory.copyWithoutContent(file); setInitialAttributes(copy); attributes.copyAttributes(file, copy, attributeCopyOption); return copy; }
@Override public synchronized void close() throws IOException { if (isOpen()) { fileSystemState.unregister(this); file.closed(); // file is set to null here and only here file = null; } }
@Override public void setUp() throws IOException { rootDir = Jimfs.newFileSystem(Configuration.unix()).getPath("/tmp"); Files.createDirectory(rootDir); }
/** * Reads attributes of the file located by the given path in this view as a map. */ public ImmutableMap<String, Object> readAttributes( JimfsPath path, String attributes, Set<? super LinkOption> options) throws IOException { File file = lookUpWithLock(path, options) .requireExists(path) .file(); return store.readAttributes(file, attributes); }
public void testByteSource_size_ofSymlinkToRegularFile() throws IOException { try (FileSystem fs = Jimfs.newFileSystem(Configuration.unix())) { Path file = fs.getPath("file"); Files.write(file, new byte[10]); Path link = fs.getPath("link"); Files.createSymbolicLink(link, file); ByteSource source = MoreFiles.asByteSource(link); assertEquals(10L, (long) source.sizeIfKnown().get()); assertEquals(10L, source.size()); } }