Skip to content

Commit c22707f

Browse files
committed
fixed some exception messages in VFS
1 parent 12611ed commit c22707f

File tree

1 file changed

+39
-51
lines changed

1 file changed

+39
-51
lines changed

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

Lines changed: 39 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -629,9 +629,7 @@ public void checkAccess(Path p, Set<? extends AccessMode> modes, LinkOption... l
629629
}
630630
} else {
631631
if (modes.contains(AccessMode.WRITE)) {
632-
String msg = String.format("read-only filesystem: '%s'", path);
633-
finer("VFS.checkAccess %s", msg);
634-
throw new SecurityException(msg);
632+
throw securityException("VFS.checkAccess", String.format("read-only filesystem, write access not supported '%s'", path));
635633
}
636634
if (getEntry(path) == null) {
637635
String msg = String.format("no such file or directory: '%s'", path);
@@ -656,9 +654,7 @@ public void createDirectory(Path d, FileAttribute<?>... attrs) throws IOExceptio
656654
finest("VFS.createDirectory delegated '%s' %s", dir, passed ? "passed" : "did not pass");
657655
}
658656
} else {
659-
String msg = String.format("read-only filesystem: '%s'", dir);
660-
finer("VFS.createDirectory %s", msg);
661-
throw new SecurityException(msg);
657+
throw securityException("VFS.createDirectory", String.format("read-only filesystem, create directory not supported '%s'", dir));
662658
}
663659
}
664660

@@ -675,9 +671,7 @@ public void delete(Path p) throws IOException {
675671
finest("VFS.delete delegated '%s' %s", path, passed ? "passed" : "did not pass");
676672
}
677673
} else {
678-
String msg = String.format("read-only filesystem: '%s'", path);
679-
finer("VFS.delete %s", msg);
680-
throw new SecurityException(msg);
674+
throw securityException("VFS.delete", String.format("read-only filesystem, delete not supported: '%s'", path));
681675
}
682676
}
683677

@@ -772,9 +766,7 @@ public void close() throws IOException {
772766
}
773767
};
774768
} else {
775-
String msg = String.format("read-only filesystem: '%s'", path);
776-
finer("VFS.newByteChannel '%s'", msg);
777-
throw new SecurityException(msg);
769+
throw securityException("VFS.newByteChannel", String.format("read-only filesystem, can create byte channel only for READ: '%s'", path));
778770
}
779771
}
780772

@@ -958,9 +950,7 @@ public void copy(Path s, Path t, CopyOption... options) throws IOException {
958950
Path source = toAbsolutePathInternal(s);
959951
Path target = toAbsolutePathInternal(t);
960952
if (pathIsInVfs(target)) {
961-
String msg = String.format("read-only filesystem, can't copy %s -> %s", source, target);
962-
finer("VFS.move %s", msg);
963-
throw new SecurityException(msg);
953+
throw securityException("VFS.move", String.format("read-only filesystem, can't copy '%s' to '%s'", source, target));
964954
} else {
965955
if (allowHostIO == READ_WRITE && pathIsInVfs(source)) {
966956
FileSystem.super.copy(source, target, options);
@@ -979,9 +969,7 @@ public void move(Path s, Path t, CopyOption... options) throws IOException {
979969
if (!pathIsInVfs(source) && !pathIsInVfs(target)) {
980970
delegate.move(source, target, options);
981971
} else {
982-
String msg = String.format("read-only filesystem, can't move %s -> %s", source, target);
983-
finer("VFS.move %s", msg);
984-
throw new SecurityException(msg);
972+
throw securityException("VFS.move", String.format("read-only filesystem, can't move '%s' to '%s'", source, target));
985973
}
986974
}
987975

@@ -1005,9 +993,7 @@ public void createSymbolicLink(Path l, Path t, FileAttribute<?>... attrs) throws
1005993
if (!pathIsInVfs(link) && !pathIsInVfs(target)) {
1006994
delegate.createSymbolicLink(link, target, attrs);
1007995
} else {
1008-
String msg = String.format("read-only filesystem, can't create link %s -> %s", link, target);
1009-
finer("VFS.createSymbolicLink %s", msg);
1010-
throw new SecurityException(msg);
996+
throw securityException("VFS.createSymbolicLink", String.format("read-only filesystem, can't create symbolic link from '%s' to '%s'", link, target));
1011997
}
1012998
}
1013999

@@ -1020,9 +1006,7 @@ public void createLink(Path l, Path e) throws IOException {
10201006
if (!pathIsInVfs(link) && !pathIsInVfs(existing)) {
10211007
delegate.createLink(link, existing);
10221008
} else {
1023-
String msg = String.format("read-only filesystem, can't create link %s -> %s", link, existing);
1024-
finer("VFS.createLink %s", msg);
1025-
throw new SecurityException(msg);
1009+
throw securityException("VFS.createLink", String.format("read-only filesystem, can't create link '%s' to '%s'", link, existing));
10261010
}
10271011
}
10281012

@@ -1033,9 +1017,7 @@ public Path readSymbolicLink(Path l) throws IOException {
10331017
if (!pathIsInVfs(link)) {
10341018
return delegate.readSymbolicLink(link);
10351019
} else {
1036-
String msg = String.format("read-only filesystem, can't read symbolic link %s", link);
1037-
finer("VFS.readSymbolicLink %s", msg);
1038-
throw new SecurityException(msg);
1020+
throw securityException("VFS.readSymbolicLink", String.format("reading symbolic links in VirtualFileSystem not supported %s", link));
10391021
}
10401022
}
10411023

@@ -1046,9 +1028,7 @@ public void setAttribute(Path p, String attribute, Object value, LinkOption... o
10461028
if (!pathIsInVfs(path)) {
10471029
delegate.setAttribute(path, attribute, value, options);
10481030
} else {
1049-
String msg = "read-only filesystem";
1050-
finer("VFS.setAttribute %s", msg);
1051-
throw new SecurityException(msg);
1031+
throw securityException("VFS.setAttribute", String.format("read-only filesystem, can't set attribute '%s' for '%s", attribute, p));
10521032
}
10531033
}
10541034

@@ -1091,6 +1071,14 @@ private static void finest(String msgFormat, Object... args) {
10911071
}
10921072
}
10931073

1074+
private static SecurityException securityException(String from, String msg) {
1075+
finer("%s %s", from, msg);
1076+
throw new SecurityException(msg);
1077+
}
1078+
1079+
/**
1080+
* copy and paste from c.o.t.polyglot.FileSystems.DeniedIOFileSystem
1081+
*/
10941082
private static class DeniedIOFileSystem implements FileSystem {
10951083

10961084
@Override
@@ -1104,53 +1092,53 @@ public Path parsePath(final String path) {
11041092
}
11051093

11061094
@Override
1107-
public void checkAccess(Path path, Set<? extends AccessMode> modes, LinkOption... linkOptions) throws IOException {
1108-
throw new SecurityException(String.format("VFS.checkAccess: filesystem without host IO: '%s'", path));
1095+
public void checkAccess(Path path, Set<? extends AccessMode> modes, LinkOption... linkOptions) {
1096+
throw securityException("VFS.checkAccess", String.format("filesystem without host IO: '%s'", path));
11091097
}
11101098

11111099
@Override
1112-
public void createDirectory(Path dir, FileAttribute<?>... attrs) throws IOException {
1113-
throw new SecurityException(String.format("VFS.createDirectory: filesystem without host IO: '%s'", dir));
1100+
public void createDirectory(Path dir, FileAttribute<?>... attrs) {
1101+
throw securityException("VFS.createDirectory", String.format("filesystem without host IO: '%s'", dir));
11141102
}
11151103

11161104
@Override
1117-
public void delete(Path path) throws IOException {
1118-
throw new SecurityException(String.format("VFS.delete: filesystem without host IO: '%s'", path));
1105+
public void delete(Path path) {
1106+
throw securityException("VFS.delete", String.format("filesystem without host IO: '%s'", path));
11191107
}
11201108

11211109
@Override
1122-
public void copy(Path source, Path target, CopyOption... options) throws IOException {
1123-
throw new SecurityException(String.format("VFS.copy: filesystem without host IO: '%s', '%s'", source, target));
1110+
public void copy(Path source, Path target, CopyOption... options) {
1111+
throw securityException("VFS.copy", String.format("filesystem without host IO: '%s', '%s'", source, target));
11241112
}
11251113

11261114
@Override
1127-
public void move(Path source, Path target, CopyOption... options) throws IOException {
1128-
throw new SecurityException(String.format("VFS.move: filesystem without host IO: '%s', '%s'", source, target));
1115+
public void move(Path source, Path target, CopyOption... options) {
1116+
throw securityException("VFS.move", String.format("filesystem without host IO: '%s', '%s'", source, target));
11291117
}
11301118

11311119
@Override
11321120
public SeekableByteChannel newByteChannel(Path inPath, Set<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException {
1133-
throw new SecurityException(String.format("VFS.newByteChannel: filesystem without host IO: '%s'", inPath));
1121+
throw securityException("VFS.newByteChannel", String.format("Filesystem without host IO: '%s'", inPath));
11341122
}
11351123

11361124
@Override
11371125
public DirectoryStream<Path> newDirectoryStream(Path dir, DirectoryStream.Filter<? super Path> filter) throws IOException {
1138-
throw new SecurityException(String.format("VFS.newDirectoryStream: filesystem without host IO: '%s'", dir));
1126+
throw securityException("VFS.newDirectoryStream", String.format("filesystem without host IO: '%s'", dir));
11391127
}
11401128

11411129
@Override
11421130
public Map<String, Object> readAttributes(Path path, String attributes, LinkOption... options) {
1143-
throw new SecurityException(String.format("VFS.readAttributes: filesystem without host IO: '%s'", path));
1131+
throw securityException("VFS.readAttributes", String.format("filesystem without host IO: '%s'", path));
11441132
}
11451133

11461134
@Override
11471135
public void setAttribute(Path path, String attribute, Object value, LinkOption... options) {
1148-
throw new SecurityException(String.format("VFS.setAttribute: filesystem without host IO: '%s'", path));
1136+
throw securityException("VFS.setAttribute", String.format("filesystem without host IO: '%s'", path));
11491137
}
11501138

11511139
@Override
11521140
public Path toAbsolutePath(Path path) {
1153-
throw new SecurityException(String.format("VFS.toAbsolutePath: filesystem without host IO: '%s'", path));
1141+
throw securityException("VFS.toAbsolutePath", String.format("filesystem without host IO: '%s'", path));
11541142
}
11551143

11561144
@Override
@@ -1159,32 +1147,32 @@ public void setCurrentWorkingDirectory(Path currentWorkingDirectory) {
11591147

11601148
@Override
11611149
public Path toRealPath(Path path, LinkOption... linkOptions) {
1162-
throw new SecurityException(String.format("VFS.toRealPath: filesystem without host IO: '%s'", path));
1150+
throw securityException("VFS.toRealPath", String.format("filesystem without host IO: '%s'", path));
11631151
}
11641152

11651153
@Override
11661154
public Path getTempDirectory() {
1167-
throw new SecurityException(String.format("VFS.getTempDirectory: filesystem without host IO"));
1155+
throw securityException("VFS.getTempDirectory", String.format("filesystem without host IO"));
11681156
}
11691157

11701158
@Override
11711159
public void createLink(Path link, Path existing) {
1172-
throw new SecurityException(String.format("VFS.createLink: filesystem without host IO: '%s'", link));
1160+
throw securityException("VFS.createLink", String.format("filesystem without host IO: '%s'", link));
11731161
}
11741162

11751163
@Override
11761164
public void createSymbolicLink(Path link, Path target, FileAttribute<?>... attrs) {
1177-
throw new SecurityException(String.format("VFS.createSymbolicLink: filesystem without host IO: '%s', '%s'", link, target));
1165+
throw securityException("VFS.createSymbolicLink", String.format("filesystem without host IO: '%s', '%s'", link, target));
11781166
}
11791167

11801168
@Override
11811169
public Path readSymbolicLink(Path link) {
1182-
throw new SecurityException(String.format("VFS.readSymbolicLink: filesystem without host IO: '%s'", link));
1170+
throw securityException("VFS.readSymbolicLink", String.format("filesystem without host IO: '%s'", link));
11831171
}
11841172

11851173
@Override
11861174
public boolean isSameFile(Path path1, Path path2, LinkOption... options) {
1187-
throw new SecurityException(String.format("VFS.isSameFile: filesystem without host IO: '%s', '%s'", path1, path2));
1175+
throw securityException("VFS.isSameFile", String.format("filesystem without host IO: '%s', '%s'", path1, path2));
11881176
}
11891177
}
11901178

0 commit comments

Comments
 (0)