Search in sources :

Example 1 with FileSystem

use of org.graalvm.polyglot.io.FileSystem in project graal by oracle.

the class FileSystemProviderTest method newFullIOFileSystem.

static FileSystem newFullIOFileSystem() {
    try {
        final Class<?> clz = Class.forName("com.oracle.truffle.api.vm.FileSystems");
        final Method m = clz.getDeclaredMethod("getDefaultFileSystem");
        m.setAccessible(true);
        return (FileSystem) m.invoke(null);
    } catch (ReflectiveOperationException e) {
        throw new RuntimeException(e);
    }
}
Also used : FileSystem(org.graalvm.polyglot.io.FileSystem) Method(java.lang.reflect.Method)

Example 2 with FileSystem

use of org.graalvm.polyglot.io.FileSystem in project graal by oracle.

the class FileSystemProviderTest method newFullIOFileSystem.

static FileSystem newFullIOFileSystem(final Path workDir) {
    try {
        final Class<?> clz = Class.forName("com.oracle.truffle.api.vm.FileSystems");
        final Method m = clz.getDeclaredMethod("newFullIOFileSystem", Path.class);
        m.setAccessible(true);
        return (FileSystem) m.invoke(null, workDir);
    } catch (ReflectiveOperationException e) {
        throw new RuntimeException(e);
    }
}
Also used : FileSystem(org.graalvm.polyglot.io.FileSystem) Method(java.lang.reflect.Method)

Example 3 with FileSystem

use of org.graalvm.polyglot.io.FileSystem in project graal by oracle.

the class VirtualizedFileSystemTest method createParameters.

@Parameterized.Parameters(name = "{0}")
public static Collection<Configuration> createParameters() throws IOException {
    final List<Configuration> result = new ArrayList<>();
    final FileSystem fullIO = FileSystemProviderTest.newFullIOFileSystem();
    final Path cwd = Paths.get("").toAbsolutePath();
    // Full IO
    Path accessibleDir = createContent(Files.createTempDirectory(VirtualizedFileSystemTest.class.getSimpleName()), fullIO);
    Context ctx = Context.newBuilder(LANGAUGE_ID).allowIO(true).build();
    result.add(new Configuration("Full IO", ctx, accessibleDir, cwd, fullIO, false, true, true, true));
    // No IO
    ctx = Context.newBuilder(LANGAUGE_ID).allowIO(false).build();
    Path privateDir = createContent(Files.createTempDirectory(VirtualizedFileSystemTest.class.getSimpleName()), fullIO);
    result.add(new Configuration("No IO", ctx, privateDir, cwd, fullIO, false, false, false, true));
    // Checked IO
    accessibleDir = createContent(Files.createTempDirectory(VirtualizedFileSystemTest.class.getSimpleName()), fullIO);
    Path readOnlyDir = createContent(Files.createTempDirectory(VirtualizedFileSystemTest.class.getSimpleName()), fullIO);
    privateDir = createContent(Files.createTempDirectory(VirtualizedFileSystemTest.class.getSimpleName()), fullIO);
    FileSystem fileSystem = new RestrictedFileSystem(FileSystemProviderTest.newFullIOFileSystem(accessibleDir), new AccessPredicate(Arrays.asList(accessibleDir, readOnlyDir)), new AccessPredicate(Collections.singleton(accessibleDir)));
    ctx = Context.newBuilder(LANGAUGE_ID).allowIO(true).fileSystem(fileSystem).build();
    result.add(new Configuration("Conditional IO - read/write part", ctx, accessibleDir, fullIO, false, true, true, true));
    fileSystem = new RestrictedFileSystem(FileSystemProviderTest.newFullIOFileSystem(readOnlyDir), new AccessPredicate(Arrays.asList(accessibleDir, readOnlyDir)), new AccessPredicate(Collections.singleton(accessibleDir)));
    ctx = Context.newBuilder(LANGAUGE_ID).allowIO(true).fileSystem(fileSystem).build();
    result.add(new Configuration("Conditional IO - read only part", ctx, readOnlyDir, fullIO, false, true, false, true));
    fileSystem = new RestrictedFileSystem(FileSystemProviderTest.newFullIOFileSystem(privateDir), new AccessPredicate(Arrays.asList(accessibleDir, readOnlyDir)), new AccessPredicate(Collections.singleton(accessibleDir)));
    ctx = Context.newBuilder(LANGAUGE_ID).allowIO(true).fileSystem(fileSystem).build();
    result.add(new Configuration("Conditional IO - private part", ctx, privateDir, fullIO, false, false, false, true));
    // Memory
    fileSystem = new MemoryFileSystem();
    Path memDir = mkdirs(fileSystem.parsePath(URI.create("file:///work")), fileSystem);
    ((MemoryFileSystem) fileSystem).setUserDir(memDir);
    createContent(memDir, fileSystem);
    ctx = Context.newBuilder(LANGAUGE_ID).allowIO(true).fileSystem(fileSystem).build();
    result.add(new Configuration("Memory FileSystem", ctx, memDir, fileSystem, false, true, true, true));
    return result;
}
Also used : Path(java.nio.file.Path) Context(org.graalvm.polyglot.Context) FileSystem(org.graalvm.polyglot.io.FileSystem) ArrayList(java.util.ArrayList)

Example 4 with FileSystem

use of org.graalvm.polyglot.io.FileSystem in project graal by oracle.

the class PolyglotEngineImpl method createContext.

@Override
@SuppressWarnings({ "all" })
public synchronized Context createContext(OutputStream out, OutputStream err, InputStream in, boolean allowHostAccess, boolean allowCreateThread, boolean allowHostIO, Predicate<String> classFilter, Map<String, String> options, Map<String, String[]> arguments, String[] onlyLanguages, FileSystem fileSystem) {
    checkState();
    if (boundEngine && preInitializedContext == null && !contexts.isEmpty()) {
        throw new IllegalArgumentException("Automatically created engines cannot be used to create more than one context. " + "Use Engine.newBuilder().build() to construct a new engine and pass it using Context.newBuilder().engine(engine).build().");
    }
    Set<String> allowedLanguages;
    if (onlyLanguages.length == 0) {
        allowedLanguages = getLanguages().keySet();
    } else {
        allowedLanguages = new HashSet<>(Arrays.asList(onlyLanguages));
    }
    final FileSystem fs;
    if (allowHostIO) {
        fs = fileSystem != null ? fileSystem : FileSystems.getDefaultFileSystem();
    } else {
        fs = FileSystems.newNoIOFileSystem();
    }
    PolyglotContextImpl contextImpl = preInitializedContext;
    preInitializedContext = null;
    if (contextImpl != null) {
        boolean patchResult;
        try {
            patchResult = contextImpl.patch(out, err, in, allowHostAccess, allowCreateThread, classFilter, options, arguments, allowedLanguages, fs);
        } catch (RuntimeException re) {
            contextImpl.closeImpl(false, false);
            PolyglotContextImpl.disposeStaticContext(null);
            throw re;
        }
        if (!patchResult) {
            contextImpl.closeImpl(false, false);
            contextImpl = null;
            PolyglotContextImpl.disposeStaticContext(contextImpl);
        }
    }
    if (contextImpl == null) {
        contextImpl = new PolyglotContextImpl(this, out, err, in, allowHostAccess, allowCreateThread, classFilter, options, arguments, allowedLanguages, fs);
        addContext(contextImpl);
    }
    Context api = impl.getAPIAccess().newContext(contextImpl);
    contextImpl.api = api;
    return api;
}
Also used : EventContext(com.oracle.truffle.api.instrumentation.EventContext) Context(org.graalvm.polyglot.Context) FileSystem(org.graalvm.polyglot.io.FileSystem)

Aggregations

FileSystem (org.graalvm.polyglot.io.FileSystem)4 Method (java.lang.reflect.Method)2 Context (org.graalvm.polyglot.Context)2 EventContext (com.oracle.truffle.api.instrumentation.EventContext)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1