Skip to content

Commit

Permalink
Fixing TODO list.
Browse files Browse the repository at this point in the history
  • Loading branch information
jovanstevanovic committed Apr 21, 2021
1 parent 3ead7f9 commit 4849dff
Show file tree
Hide file tree
Showing 7 changed files with 243 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public SeekableByteChannel position(long pos) throws IOException {
if (pos < 0 || pos >= Integer.MAX_VALUE) {
throw new IllegalArgumentException("Illegal position " + pos);
}
this.pos = Math.min((int)pos, last);
this.pos = Math.min((int) pos, last);
return this;
} finally {
endWrite();
Expand Down Expand Up @@ -141,7 +141,7 @@ public void close() throws IOException {
if (closed) {
return;
}
System.out.println("Close method!");

beginWrite();
try {
closed = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,61 @@

import java.io.IOException;
import java.nio.file.ClosedDirectoryStreamException;
import java.nio.file.DirectoryIteratorException;
import java.nio.file.DirectoryStream;
import java.nio.file.NotDirectoryException;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;

public class ResourceDirectoryStream implements DirectoryStream<Path> {

private final ResourceFileSystem fileSystem;
private final DirectoryStream.Filter<? super Path> filter;
private final ResourcePath dir;
private volatile boolean isClosed = false;
private final List<String> data;
private Iterator<Path> directoryIterator;

public ResourceDirectoryStream(ResourcePath path, Filter<? super Path> filter) throws IOException {
this.fileSystem = path.getFileSystem();
public ResourceDirectoryStream(ResourcePath dir, Filter<? super Path> filter) throws IOException {
this.fileSystem = dir.getFileSystem();
this.dir = dir;
this.filter = filter;
this.data = formatDirData(path);
if (!fileSystem.isDirectory(path.getResolvedPath())) {
throw new NotDirectoryException(path.toString());
if (!fileSystem.isDirectory(dir.getResolvedPath())) {
throw new NotDirectoryException(dir.toString());
}
}

// TODO: After recomposing resource storage, get with zero index will be replaced with
// appropriate one.
private List<String> formatDirData(ResourcePath path) {
byte[] data = Resources.get(fileSystem.getString(path.getResolvedPath())).get(0);
return Arrays.asList(fileSystem.getString(data).split("\n"));
}

// TODO: We need to filter data, based on filter parameter.
@Override
public Iterator<Path> iterator() {
if (isClosed) {
throw new ClosedDirectoryStreamException();
}
if (directoryIterator != null) {
throw new IllegalStateException("Iterator has already been returned");
}

try {
directoryIterator = fileSystem.iteratorOf(dir, filter);
} catch (IOException ioException) {
throw new DirectoryIteratorException(ioException);
}

return new Iterator<Path>() {
final Iterator<String> iterator = data.iterator();

@Override
public boolean hasNext() {
if (isClosed) {
return false;
}
return iterator.hasNext();
return directoryIterator.hasNext();
}

@Override
public Path next() {
if (isClosed) {
throw new NoSuchElementException();
}
return new ResourcePath(fileSystem, fileSystem.getBytes(iterator.next()));
return directoryIterator.next();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,26 @@ public boolean supportsFileAttributeView(String name) {

@Override
public <V extends FileStoreAttributeView> V getFileStoreAttributeView(Class<V> type) {
if (type == null)
if (type == null) {
throw new NullPointerException();
}
return null;
}

@Override
public Object getAttribute(String attribute) throws IOException {
if (attribute.equals("totalSpace"))
if (attribute.equals("totalSpace")) {
return getTotalSpace();
if (attribute.equals("usableSpace"))
}
if (attribute.equals("usableSpace")) {
return getUsableSpace();
if (attribute.equals("unallocatedSpace"))
}
if (attribute.equals("unallocatedSpace")) {
return getUnallocatedSpace();
}
throw new UnsupportedOperationException("Attribute isn't supported!");
}

// TODO: Need enhancements.
private static class ResourceFileStoreAttributes {
private final FileStore fileStore;
private final long size;
Expand All @@ -92,11 +95,11 @@ public long totalSpace() {
}

public long usableSpace() throws IOException {
return 0;
return fileStore.getUsableSpace();
}

public long unallocatedSpace() throws IOException {
return 0;
return fileStore.getUnallocatedSpace();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
import java.nio.file.ClosedFileSystemException;
import java.nio.file.CopyOption;
import java.nio.file.DirectoryNotEmptyException;
import java.nio.file.DirectoryStream;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.FileStore;
import java.nio.file.FileSystem;
import java.nio.file.FileSystemException;
import java.nio.file.FileSystemNotFoundException;
import java.nio.file.Files;
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;
Expand All @@ -39,6 +41,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -48,6 +51,7 @@
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.regex.Pattern;

import static com.oracle.svm.core.jdk.ResourceFileSystemUtil.toRegexPattern;
import static java.lang.Boolean.TRUE;
import static java.nio.file.StandardCopyOption.COPY_ATTRIBUTES;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
Expand All @@ -58,7 +62,6 @@
import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
import static java.nio.file.StandardOpenOption.WRITE;

// TODO: Add support for encoding/decoding based on env parameter.
public class ResourceFileSystem extends FileSystem {

private static final String GLOB_SYNTAX = "glob";
Expand All @@ -81,7 +84,7 @@ public class ResourceFileSystem extends FileSystem {

private static final byte[] ROOT_PATH = new byte[]{'/'};
private final IndexNode LOOKUP_KEY = new IndexNode(null, true);
LinkedHashMap<IndexNode, IndexNode> inodes = new LinkedHashMap<>(10);
private final LinkedHashMap<IndexNode, IndexNode> inodes = new LinkedHashMap<>(10);

public ResourceFileSystem(ResourceFileSystemProvider provider, Path resourcePath, Map<String, ?> env) {
this.provider = provider;
Expand All @@ -94,7 +97,7 @@ public ResourceFileSystem(ResourceFileSystemProvider provider, Path resourcePath
readAllEntries();
}

// returns true if there is a name=true/"true" setting in env
// Returns true if there is a name=true/"true" setting in env.
private static boolean isTrue(Map<String, ?> env, String name) {
return "true".equals(env.get(name)) || TRUE.equals(env.get(name));
}
Expand Down Expand Up @@ -217,12 +220,6 @@ public Path getPath(String first, String... more) {
return new ResourcePath(this, getBytes(path));
}

// TODO: Implementation.
public static String toRegexPattern(String globPattern) {
return null;
}

// TODO: Test this method.
@Override
public PathMatcher getPathMatcher(String syntaxAndPattern) {
int pos = syntaxAndPattern.indexOf(':');
Expand Down Expand Up @@ -285,11 +282,10 @@ private void checkOptions(Set<? extends OpenOption> options) {
}
}

// TODO: Need enhancement.
public SeekableByteChannel newByteChannel(byte[] path, Set<? extends OpenOption> options, FileAttribute<?>[] attrs) throws IOException {
checkOptions(options);
if (options.contains(StandardOpenOption.WRITE) || options.contains(StandardOpenOption.APPEND)) {
beginRead(); // only need a read lock, the "update()" will obtain the write lock when the channel is closed
beginRead(); // Only need a read lock, the "update()" will obtain the write lock when the channel is closed.
try {
Entry e = getEntry(path);
if (e != null) {
Expand Down Expand Up @@ -672,24 +668,6 @@ private void buildNodeTree() {
}
}

// Temporary, for debug purpose.
void printTree() {
System.out.println(">>> Tree...");
IndexNode node = inodes.get(LOOKUP_KEY.as(ROOT_PATH));
node = node.child;
ArrayList<IndexNode> queue = new ArrayList<>();
queue.add(node);
while (!queue.isEmpty()) {
node = queue.remove(0);
while (node != null) {
System.out.println(getString(node.name) + " " + node.isDir);
queue.add(node.child);
node = node.sibling;
}
System.out.println("------");
}
}

private InputStream getInputStream(Entry e) throws IOException {
InputStream eis = null;
if (e.type == Entry.NEW) {
Expand Down Expand Up @@ -830,7 +808,6 @@ public FileChannel newFileChannel(byte[] path, Set<? extends OpenOption> options
final Path tmpFile = isFCH ? e.file : getTempPathForEntry(path);
final FileChannel fch = tmpFile.getFileSystem().provider().newFileChannel(tmpFile, options, attrs);
final Entry target = isFCH ? e : new Entry(path, tmpFile, Entry.FILE_CH);
// TODO: Is there a better way to hook into the FileChannel's close method?
return new FileChannel() {

@Override
Expand Down Expand Up @@ -935,6 +912,32 @@ protected void implCloseChannel() throws IOException {
}
}

Iterator<Path> iteratorOf(ResourcePath dir, DirectoryStream.Filter<? super Path> filter) throws IOException {
beginWrite();
try {
ensureOpen();
byte[] path = dir.getResolvedPath();
IndexNode inode = getInode(path);
if (inode == null) {
throw new NotDirectoryException(getString(path));
}
List<Path> list = new ArrayList<>();
IndexNode child = inode.child;
while (child != null) {
byte[] childName = child.name;
ResourcePath childPath = new ResourcePath(this, childName, true);
Path childFileName = childPath.getFileName();
Path resourcePath = dir.resolve(childFileName);
if (filter == null || filter.accept(resourcePath))
list.add(resourcePath);
child = child.sibling;
}
return list.iterator();
} finally {
endWrite();
}
}

private static class IndexNode {

private static final ThreadLocal<IndexNode> cachedKey = new ThreadLocal<>();
Expand Down Expand Up @@ -1177,7 +1180,7 @@ private class EntryOutputStream extends FilterOutputStream {
private int written;
private boolean isClosed;

EntryOutputStream(Entry e, OutputStream os) throws IOException {
EntryOutputStream(Entry e, OutputStream os) {
super(os);
this.e = Objects.requireNonNull(e, "Entry is null!");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,31 +65,34 @@ public String getScheme() {
return "resource";
}

// TODO: Need further testing.
@Override
public FileSystem newFileSystem(URI uri, Map<String, ?> env) {
public FileSystem newFileSystem(URI uri, Map<String, ?> env) throws IOException {
Path path = uriToPath(uri);
synchronized (filesystems) {
if (filesystems.containsKey(path)) {
Path realPath = path.toRealPath();
if (filesystems.containsKey(realPath)) {
throw new FileSystemAlreadyExistsException();
}
ResourceFileSystem resourceFileSystem = new ResourceFileSystem(this, path, env);
filesystems.put(path, resourceFileSystem);
filesystems.put(realPath, resourceFileSystem);
return resourceFileSystem;
}
}

// TODO: Implementation.
@Override
public FileSystem newFileSystem(Path path, Map<String, ?> env) throws IOException {
return super.newFileSystem(path, env);
public FileSystem newFileSystem(Path path, Map<String, ?> env) {
return new ResourceFileSystem(this, path, env);
}

// TODO: Need further testing.
@Override
public FileSystem getFileSystem(URI uri) {
synchronized (filesystems) {
ResourceFileSystem resourceFileSystem = filesystems.get(uriToPath(uri));
ResourceFileSystem resourceFileSystem;
try {
resourceFileSystem = filesystems.get(uriToPath(uri).toRealPath());
} catch (IOException e) {
throw new FileSystemNotFoundException();
}
if (resourceFileSystem == null) {
throw new FileSystemNotFoundException();
}
Expand All @@ -102,7 +105,8 @@ public Path getPath(URI uri) {
String spec = uri.getSchemeSpecificPart();
int sep = spec.indexOf("!/");
if (sep == -1) {
throw new IllegalArgumentException("URI: " + uri + " does not contain path info ex. resource:foo.jar!/bar.txt");
throw new IllegalArgumentException("URI: " + uri +
" does not contain path info ex. resource:file:/foo.jar!/bar.txt");
}
return getFileSystem(uri).getPath(spec.substring(sep + 1));
}
Expand Down Expand Up @@ -149,10 +153,9 @@ public boolean isSameFile(Path path, Path path2) {

@Override
public boolean isHidden(Path path) {
return false;
return toResourcePath(path).isHidden();
}

// TODO: Implement FileStore.
@Override
public FileStore getFileStore(Path path) throws IOException {
return toResourcePath(path).getFileStore();
Expand Down
Loading

0 comments on commit 4849dff

Please sign in to comment.