Skip to content

Commit 24e53f0

Browse files
committed
[GR-54914] Add some validation to methods in VirtualFileSystem.Builder
PullRequest: graalpython/3535
2 parents c3e9513 + b4b8c75 commit 24e53f0

File tree

2 files changed

+50
-18
lines changed

2 files changed

+50
-18
lines changed

graalpython/com.oracle.graal.python.test.integration/src/org/graalvm/python/embedding/utils/test/VirtualFileSystemTest.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import java.nio.file.AccessMode;
6161
import java.nio.file.DirectoryStream;
6262
import java.nio.file.Files;
63+
import java.nio.file.InvalidPathException;
6364
import java.nio.file.NoSuchFileException;
6465
import java.nio.file.NotDirectoryException;
6566
import java.nio.file.Path;
@@ -494,15 +495,19 @@ private static void checkExtractedFile(Path extractedFile, String[] expectedCont
494495
}
495496
}
496497

498+
private static void checkException(Class<?> exType, Callable<Object> c) {
499+
checkException(exType, c, null);
500+
}
501+
497502
private static void checkException(Class<?> exType, Callable<Object> c, String msg) {
498503
boolean gotEx = false;
499504
try {
500505
c.call();
501506
} catch (Exception e) {
502-
assert e.getClass() == exType;
507+
assertEquals(e.getClass(), exType);
503508
gotEx = true;
504509
}
505-
assertTrue(msg, gotEx);
510+
assertTrue(msg != null ? msg : "expected " + exType.getName(), gotEx);
506511
}
507512

508513
@Test
@@ -863,6 +868,24 @@ public void externalResourcesBuilderTest() throws IOException {
863868
}
864869
}
865870

871+
@Test
872+
public void vfsMountPointTest() {
873+
if (IS_WINDOWS) {
874+
checkException(IllegalArgumentException.class, () -> VirtualFileSystem.newBuilder().windowsMountPoint("test"));
875+
checkException(IllegalArgumentException.class, () -> VirtualFileSystem.newBuilder().windowsMountPoint("test\\"));
876+
checkException(IllegalArgumentException.class, () -> VirtualFileSystem.newBuilder().windowsMountPoint("\\test\\"));
877+
checkException(IllegalArgumentException.class, () -> VirtualFileSystem.newBuilder().windowsMountPoint("\\test"));
878+
checkException(IllegalArgumentException.class, () -> VirtualFileSystem.newBuilder().windowsMountPoint("X:\\test\\"));
879+
checkException(InvalidPathException.class, () -> VirtualFileSystem.newBuilder().windowsMountPoint("X:\\test|test"));
880+
} else {
881+
checkException(IllegalArgumentException.class, () -> VirtualFileSystem.newBuilder().unixMountPoint("test"));
882+
checkException(IllegalArgumentException.class, () -> VirtualFileSystem.newBuilder().unixMountPoint("test/"));
883+
checkException(IllegalArgumentException.class, () -> VirtualFileSystem.newBuilder().unixMountPoint("/test/"));
884+
checkException(IllegalArgumentException.class, () -> VirtualFileSystem.newBuilder().unixMountPoint("X:\\test"));
885+
checkException(InvalidPathException.class, () -> VirtualFileSystem.newBuilder().unixMountPoint("/test/\0"));
886+
}
887+
}
888+
866889
private static Builder addTestOptions(Builder builder) {
867890
return builder.option("engine.WarnInterpreterOnly", "false");
868891
}

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

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,9 @@ public static final class Builder {
134134
return s.endsWith(".so") || s.endsWith(".dylib") || s.endsWith(".pyd") || s.endsWith(".dll") || s.endsWith(".ttf");
135135
};
136136

137-
private String windowsMountPoint = "X:\\graalpy_vfs";
138-
private String unixMountPoint = "/graalpy_vfs";
137+
private static final String DEFAULT_WINDOWS_MOUNT_POINT = "X:\\graalpy_vfs";
138+
private String DEFAULT_UNIX_MOUNT_POINT = "/graalpy_vfs";
139+
private Path mountPoint;
139140
private Predicate<Path> extractFilter = DEFAULT_EXTRACT_FILTER;
140141
private HostIO allowHostIO = HostIO.READ_WRITE;
141142
private boolean caseInsensitive = isWindows();
@@ -171,7 +172,9 @@ public Builder allowHostIO(HostIO b) {
171172
* will not be accessible.
172173
*/
173174
public Builder windowsMountPoint(String s) {
174-
windowsMountPoint = s;
175+
if (isWindows()) {
176+
mountPoint = getMountPointAsPath(s);
177+
}
175178
return this;
176179
}
177180

@@ -184,7 +187,9 @@ public Builder windowsMountPoint(String s) {
184187
* will not be accessible.
185188
*/
186189
public Builder unixMountPoint(String s) {
187-
unixMountPoint = s;
190+
if (!isWindows()) {
191+
mountPoint = getMountPointAsPath(s);
192+
}
188193
return this;
189194
}
190195

@@ -219,10 +224,21 @@ public Builder extractFilter(Predicate<Path> filter) {
219224
}
220225

221226
public VirtualFileSystem build() {
222-
return new VirtualFileSystem(extractFilter, windowsMountPoint, unixMountPoint, allowHostIO, resourceLoadingClass, caseInsensitive);
227+
if (mountPoint == null) {
228+
mountPoint = isWindows() ? Path.of(DEFAULT_WINDOWS_MOUNT_POINT) : Path.of(DEFAULT_UNIX_MOUNT_POINT);
229+
}
230+
return new VirtualFileSystem(extractFilter, mountPoint, allowHostIO, resourceLoadingClass, caseInsensitive);
223231
}
224232
}
225233

234+
private static Path getMountPointAsPath(String mp) {
235+
Path mountPoint = Path.of(mp);
236+
if (mp.endsWith(PLATFORM_SEPARATOR) || !mountPoint.isAbsolute()) {
237+
throw new IllegalArgumentException(String.format("Virtual filesystem mount point must be set to an absolute path without a trailing separator: '%s'", mp));
238+
}
239+
return mountPoint;
240+
}
241+
226242
public static Builder newBuilder() {
227243
return new Builder();
228244
}
@@ -353,8 +369,7 @@ private void removeExtractDir() {
353369
* causing that no extraction will happen.
354370
*/
355371
VirtualFileSystem(Predicate<Path> extractFilter,
356-
String windowsMountPoint,
357-
String unixMountPoint,
372+
Path mountPoint,
358373
HostIO allowHostIO,
359374
Class<?> resourceLoadingClass,
360375
boolean caseInsensitive) {
@@ -365,15 +380,9 @@ private void removeExtractDir() {
365380
}
366381

367382
this.caseInsensitive = caseInsensitive;
368-
String mp = System.getenv("GRAALPY_VFS_MOUNT_POINT");
369-
if (mp == null) {
370-
mp = isWindows() ? windowsMountPoint : unixMountPoint;
371-
}
372-
this.mountPoint = Path.of(mp);
373-
this.mountPointLowerCase = mp.toLowerCase(Locale.ROOT);
374-
if (mp.endsWith(PLATFORM_SEPARATOR) || !mountPoint.isAbsolute()) {
375-
throw new IllegalArgumentException("GRAALPY_VFS_MOUNT_POINT must be set to an absolute path without a trailing separator");
376-
}
383+
this.mountPoint = mountPoint;
384+
385+
this.mountPointLowerCase = mountPoint.toString().toLowerCase(Locale.ROOT);
377386

378387
fine("VirtualFilesystem %s, allowHostIO: %s, resourceLoadingClass: %s, caseInsensitive: %s, extractOnStartup: %s%s",
379388
mountPoint, allowHostIO.toString(), this.resourceLoadingClass.getName(), caseInsensitive, extractOnStartup, extractFilter != null ? "" : ", extractFilter: null");

0 commit comments

Comments
 (0)