-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(graalvm): experimental prototype for hybrid vfs.
Signed-off-by: Dario Valdespino <dvaldespino00@gmail.com>
- Loading branch information
Showing
2 changed files
with
112 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
packages/graalvm/src/main/kotlin/elide/runtime/plugins/vfs/internal/HybridVfs.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package elide.runtime.plugins.vfs.internal | ||
|
||
import org.graalvm.polyglot.io.FileSystem | ||
import java.net.URI | ||
import java.nio.channels.SeekableByteChannel | ||
import java.nio.file.* | ||
import java.nio.file.DirectoryStream.Filter | ||
import java.nio.file.attribute.FileAttribute | ||
import kotlin.io.path.pathString | ||
|
||
internal class HybridVfs( | ||
private val host: FileSystem, | ||
private val inMemory: FileSystem, | ||
) : FileSystem { | ||
private inline fun <R> withHostFallback(op: (FileSystem) -> R): R { | ||
return runCatching { op(inMemory) }.getOrElse { op(host) } | ||
} | ||
|
||
private fun Path.compatibleWith(fileSystem: FileSystem): Path { | ||
return fileSystem.parsePath(pathString) | ||
} | ||
|
||
override fun parsePath(uri: URI?): Path = host.parsePath(uri) | ||
|
||
override fun parsePath(path: String?): Path = host.parsePath(path) | ||
|
||
override fun checkAccess( | ||
path: Path?, | ||
modes: MutableSet<out AccessMode>?, | ||
vararg linkOptions: LinkOption? | ||
) = withHostFallback { | ||
it.checkAccess(path?.compatibleWith(it), modes, *linkOptions) | ||
} | ||
|
||
override fun createDirectory(dir: Path?, vararg attrs: FileAttribute<*>?) { | ||
return withHostFallback { it.createDirectory(dir?.compatibleWith(it), *attrs) } | ||
} | ||
|
||
override fun delete(path: Path?) { | ||
withHostFallback { it.delete(path?.compatibleWith(it)) } | ||
} | ||
|
||
override fun newByteChannel( | ||
path: Path?, | ||
options: MutableSet<out OpenOption>?, | ||
vararg attrs: FileAttribute<*>? | ||
): SeekableByteChannel = withHostFallback { | ||
it.newByteChannel(path?.compatibleWith(it), options, *attrs) | ||
} | ||
|
||
override fun newDirectoryStream(dir: Path?, filter: Filter<in Path>?): DirectoryStream<Path> = withHostFallback { | ||
it.newDirectoryStream(dir?.compatibleWith(it), filter) | ||
} | ||
|
||
override fun toAbsolutePath(path: Path?): Path { | ||
return host.toAbsolutePath(path) | ||
} | ||
|
||
override fun toRealPath(path: Path?, vararg linkOptions: LinkOption?): Path { | ||
return host.toRealPath(path) | ||
} | ||
|
||
override fun readAttributes( | ||
path: Path?, | ||
attributes: String?, | ||
vararg options: LinkOption? | ||
): MutableMap<String, Any> = withHostFallback { | ||
it.readAttributes(path?.compatibleWith(it), attributes, *options) | ||
} | ||
} |