diff options
Diffstat (limited to 'src/main/kotlin/WebOS.kt')
-rw-r--r-- | src/main/kotlin/WebOS.kt | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/main/kotlin/WebOS.kt b/src/main/kotlin/WebOS.kt index 2ff0b62..d1b0d81 100644 --- a/src/main/kotlin/WebOS.kt +++ b/src/main/kotlin/WebOS.kt @@ -58,7 +58,62 @@ class Console(val os: WebOS, val renderElement: Element?) { class WebOS { private val _consoles = mutableListOf<Console>() val consoles get() = _consoles.toList() + val fileSystem = FileSystem() fun registerConsole(element: Element) { _consoles.add(Console(this, element)) } } + +interface User { + val name: String + val homeDirectory: Path +} + +data class Path private constructor(val parts: List<String>) { + companion object { + fun of(text: String) = + Path(text.split("/")) + fun of(vararg parts: String): Path { + if(parts.any { it.contains("/") }) + throw IllegalArgumentException("Path parts cannot contain forward slashes") + return Path(parts.toList()) + } + } +} + +class FileSystem { + val mounts = mutableMapOf<Path, Mount>() + fun mount(path: Path, mount:Mount) { + mounts[path] = mount + } + fun <T>findMountFor(path: Path, operation: Mount.(relativePath: Path) -> T) :T{ + val (mountPath, mount) = mounts.filterKeys { + // path.startsWith(it) + true + }.maxByOrNull { it.key.parts.size} ?: throw IllegalStateException("No mount present") + return mount.operation(mountPath.relativize(path)) + } + + fun read(path: Path): ReadResult = findMountFor(path) { read(it) } + fun write(path: Path, data: ByteArray): Unit = TODO() + fun stat(path: Path): Unit = TODO() +} + +sealed class ReadResult { + class Success(val text: String) : ReadResult() + object NotFound + object NoAccess +} +interface Mount { + fun read(relativePath: Path): ReadResult = TODO() + fun write(path: Path, data: ByteArray): Unit = TODO() + fun stat(path: Path): Unit = TODO() +} + +data class Stat( + val exists: Boolean, + var owner: User, + val created: Long, + val edited: Long, + val size: Long +) |