Skip to content

Commit ebecb32

Browse files
committed
properly handle real FS files in VFS
1 parent 371a922 commit ebecb32

File tree

1 file changed

+74
-26
lines changed

1 file changed

+74
-26
lines changed

graalpython/org.graalvm.python.embedding/src/org/graalvm/python/embedding/utils/VirtualFileSystem.java

Lines changed: 74 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,12 @@ void extractResources(Path resourcesDirectory) throws IOException {
656656
@Deprecated
657657
public Path parsePath(URI uri) {
658658
if (uri.getScheme().equals("file")) {
659+
Path ret = Paths.get(uri);
660+
if (!pathIsInVfs(ret)) {
661+
if (delegate != null) {
662+
return delegate.parsePath(uri);
663+
}
664+
}
659665
return Paths.get(uri);
660666
} else {
661667
throw new UnsupportedOperationException("Not supported yet.");
@@ -665,51 +671,73 @@ public Path parsePath(URI uri) {
665671
@Override
666672
@Deprecated
667673
public Path parsePath(String path) {
668-
return Paths.get(path);
674+
Path p = Paths.get(path);
675+
if (!pathIsInVfs(p)) {
676+
if (delegate != null) {
677+
return delegate.parsePath(path);
678+
}
679+
}
680+
return p;
669681
}
670682

671683
@Override
672684
@Deprecated
673685
public void checkAccess(Path path, Set<? extends AccessMode> modes, LinkOption... linkOptions) throws IOException {
674-
if (pathIsInVfs(path)) {
686+
if (!pathIsInVfs(path)) {
687+
if (delegate != null) {
688+
delegate.checkAccess(path, modes, linkOptions);
689+
return;
690+
} else {
691+
throw new SecurityException("filesystem without host IO: " + path);
692+
}
693+
} else {
675694
if (modes.contains(AccessMode.WRITE)) {
676695
throw new SecurityException("read-only filesystem");
677696
}
678697
if (getEntry(path) == null) {
679698
throw new NoSuchFileException("no such file or directory");
680699
}
681-
} else if (delegate != null) {
682-
delegate.checkAccess(path, modes, linkOptions);
683-
} else {
684-
throw new SecurityException("read-only filesystem");
685700
}
686701
}
687702

688703
@Override
689704
@Deprecated
690705
public void createDirectory(Path dir, FileAttribute<?>... attrs) throws IOException {
691-
if (delegate == null || pathIsInVfs(dir)) {
692-
throw new SecurityException("read-only filesystem");
706+
if (!pathIsInVfs(dir)) {
707+
if (delegate != null) {
708+
delegate.createDirectory(dir, attrs);
709+
} else {
710+
throw new SecurityException("filesystem without host IO: " + dir);
711+
}
693712
} else {
694-
delegate.createDirectory(dir, attrs);
713+
throw new SecurityException("read-only filesystem");
695714
}
696715
}
697716

698717
@Override
699718
@Deprecated
700719
public void delete(Path path) throws IOException {
701-
if (delegate == null || pathIsInVfs(path)) {
702-
throw new SecurityException("read-only filesystem");
720+
if (!pathIsInVfs(path)) {
721+
if (delegate != null) {
722+
delegate.delete(path);
723+
return;
724+
} else {
725+
throw new SecurityException("filesystem without host IO: " + path);
726+
}
703727
} else {
704-
delegate.delete(path);
728+
throw new SecurityException("read-only filesystem");
705729
}
706730
}
707731

708732
@Override
709733
@Deprecated
710734
public SeekableByteChannel newByteChannel(Path path, Set<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException {
711-
if (delegate != null && !pathIsInVfs(path)) {
712-
return delegate.newByteChannel(path, options, attrs);
735+
if (!pathIsInVfs(path)) {
736+
if (delegate != null) {
737+
return delegate.newByteChannel(path, options, attrs);
738+
} else {
739+
throw new SecurityException("filesystem without host IO: " + path);
740+
}
713741
}
714742

715743
if (options.isEmpty() || (options.size() == 1 && options.contains(StandardOpenOption.READ))) {
@@ -787,8 +815,12 @@ public void close() throws IOException {
787815
@Override
788816
@Deprecated
789817
public DirectoryStream<Path> newDirectoryStream(Path dir, DirectoryStream.Filter<? super Path> filter) throws IOException {
790-
if (delegate != null && !pathIsInVfs(dir)) {
791-
return delegate.newDirectoryStream(dir, filter);
818+
if (!pathIsInVfs(dir)) {
819+
if (delegate != null) {
820+
return delegate.newDirectoryStream(dir, filter);
821+
} else {
822+
throw new SecurityException("filesystem without host IO: " + dir);
823+
}
792824
}
793825
BaseEntry entry = getEntry(dir);
794826
if (entry instanceof FileEntry) {
@@ -813,32 +845,48 @@ public Iterator<Path> iterator() {
813845
@Override
814846
@Deprecated
815847
public Path toAbsolutePath(Path path) {
816-
Path result;
817-
if (shouldExtract(path)) {
848+
boolean pathIsInVFS = pathIsInVfs(path);
849+
if (!pathIsInVFS) {
850+
if (delegate != null) {
851+
return delegate.toAbsolutePath(path);
852+
} else {
853+
throw new SecurityException("filesystem without host IO: " + path);
854+
}
855+
}
856+
Path result = path;
857+
if (pathIsInVFS && shouldExtract(path)) {
818858
result = getExtractedPath(path);
819-
} else {
820-
result = path;
821859
}
822860
return toAbsolutePathInternal(result);
823861
}
824862

825863
@Override
826864
@Deprecated
827865
public Path toRealPath(Path path, LinkOption... linkOptions) throws IOException {
828-
Path result;
829-
if (shouldExtract(path)) {
866+
boolean pathIsInVFS = pathIsInVfs(path);
867+
if (!pathIsInVFS) {
868+
if (delegate != null) {
869+
return delegate.toRealPath(path);
870+
} else {
871+
throw new SecurityException("filesystem without host IO: " + path);
872+
}
873+
}
874+
Path result = path;
875+
if (pathIsInVFS && shouldExtract(path)) {
830876
result = getExtractedPath(path);
831-
} else {
832-
result = path;
833877
}
834878
return result.normalize();
835879
}
836880

837881
@Override
838882
@Deprecated
839883
public Map<String, Object> readAttributes(Path path, String attributes, LinkOption... options) throws IOException {
840-
if (delegate != null && !pathIsInVfs(path)) {
841-
return delegate.readAttributes(path, attributes, options);
884+
if (!pathIsInVfs(path)) {
885+
if (delegate != null) {
886+
return delegate.readAttributes(path, attributes, options);
887+
} else {
888+
throw new SecurityException("filesystem without host IO: " + path);
889+
}
842890
}
843891
BaseEntry entry = getEntry(path);
844892
if (entry == null) {

0 commit comments

Comments
 (0)