diff options
Diffstat (limited to 'src/jsMain/kotlin/io')
-rw-r--r-- | src/jsMain/kotlin/io/Path.kt | 75 | ||||
-rw-r--r-- | src/jsMain/kotlin/io/files.kt | 90 |
2 files changed, 0 insertions, 165 deletions
diff --git a/src/jsMain/kotlin/io/Path.kt b/src/jsMain/kotlin/io/Path.kt deleted file mode 100644 index 8f77203..0000000 --- a/src/jsMain/kotlin/io/Path.kt +++ /dev/null @@ -1,75 +0,0 @@ -package io - -sealed interface Path { - val parts: List<String> - fun toAbsolutePath(relativeTo: Absolute): Absolute { - return relativeTo.resolve(this) - } - - fun resolve(path: Path): Path - - companion object { - val root = Absolute(listOf()) - - fun ofShell(string: String, userHome: Absolute): Path = - ofShell(string.split("/"), userHome) - - fun ofShell(vararg parts: String, userHome: Absolute): Path = - ofShell(parts.toList(), userHome) - - fun of(vararg parts: String): Path = - of(parts.toList()) - - fun of(string: String): Path = - of(string.split("/")) - - fun ofShell(parts: List<String>, userHome: Absolute): Path { - if (parts.firstOrNull() == "~") - return userHome.resolve(Relative(parts.subList(1, parts.size).filter { it.isNotEmpty() })) - return of(parts) - } - - fun of(parts: List<String>): Path { - if (parts.isEmpty()) - return root - if (parts[0] == "") // Starts with a / - return Absolute(parts.subList(1, parts.size).filter { it.isNotEmpty() }) - return Relative(parts.filter { it.isNotEmpty() }) - } - } - - data class Relative internal constructor(override val parts: List<String>) : Path { - override fun resolve(path: Path): Path { - if (path is Absolute) return path - return Relative(this.parts + path.parts) - } - } - - data class Absolute internal constructor(override val parts: List<String>) : Path { - override fun resolve(path: Path): Absolute { - if (path is Absolute) return path - return Absolute(this.parts + path.parts) - } - - fun relativize(path: Path): Relative = when (path) { - is Relative -> path - is Absolute -> { - var commonPrefix = true - val partList = mutableListOf<String>() - var returns = 0 - for ((idx, part) in path.parts.withIndex()) { - if (idx < this.parts.size) { - if (this.parts[idx] == part && commonPrefix) { - continue - } else { - commonPrefix = false - returns++ - } - } - partList.add(part) - } - Relative(List(returns) { "" } + partList) - } - } - } -} diff --git a/src/jsMain/kotlin/io/files.kt b/src/jsMain/kotlin/io/files.kt deleted file mode 100644 index d37035d..0000000 --- a/src/jsMain/kotlin/io/files.kt +++ /dev/null @@ -1,90 +0,0 @@ -package io - -import User - -class IOHandler { - val mounts = mutableListOf<Mount>() - fun mount(absolutePath: Path.Absolute, fileSystem: FileSystem) { - if (mounts.any { it.mountPoint == absolutePath }) - return // TODO sensible error message handling - mounts += Mount(absolutePath, fileSystem) - } - - fun unmount(mountPoint: Path.Absolute) { - mounts.removeAll { it.mountPoint == mountPoint } - } - - fun <T> findMountFor( - workingDirectory: Path.Absolute, - path: Path, - operation: FileSystem.(relativePath: Path) -> T - ): T { - val absolutPath = path.toAbsolutePath(workingDirectory) - val mount = mounts.filter { - it.mountPoint.parts.zip(absolutPath.parts).all { (a, b) -> a == b } - }.maxByOrNull { it.mountPoint.parts.size } ?: throw IllegalStateException("No mount present") - return mount.fileSystem.operation( - Path.Absolute( - absolutPath.parts.subList( - mount.mountPoint.parts.size, - absolutPath.parts.size - ) - ) // TODO: unangenehm - ) - } - - fun findINode(absolutePath: Path.Absolute): INode { - val mount = mounts.filter { - it.mountPoint.parts.zip(absolutePath.parts).all { (a, b) -> a == b } - }.maxByOrNull { it.mountPoint.parts.size } ?: throw IllegalStateException("No mount present") - val iNode = mount.fileSystem.getINode(absolutePath.relativize(mount.mountPoint)) - return when (iNode) { - is INodeResult.File -> iNode.op - is INodeResult.ResolveAgain -> findINode(absolutePath.resolve(iNode.relativeToOriginal)) - } - } - - fun read(workingDirectory: Path.Absolute, path: Path): ReadResult = - findMountFor(workingDirectory, path) { read(it) } - - fun write(workingDirectory: Path.Absolute, path: Path, data: ByteArray): Unit = - findMountFor(workingDirectory, path) { write(it, data) } - - fun stat(workingDirectory: Path.Absolute, path: Path): Unit = - findMountFor(workingDirectory, path) { stat(it) } -} - -interface INode { - val fs: FileSystem -} - -sealed interface INodeResult { - class File(val op: INode) : INodeResult - class ResolveAgain(val relativeToOriginal: Path): INodeResult -} - -interface FileSystem { - fun getINode(relativePath: Path.Relative): INodeResult - fun read(relativePath: Path): ReadResult - fun write(path: Path, data: ByteArray): Unit // Write result - fun stat(path: Path): Unit // TODO io.Stat result -} - -sealed class ReadResult { - class Success(val text: String) : ReadResult() - object NotFound : ReadResult() - object NoAccess : ReadResult() -} - -data class Mount( - val mountPoint: Path.Absolute, - val fileSystem: FileSystem -) - -data class Stat( - val exists: Boolean, - var owner: User, - val created: Long, - val edited: Long, - val size: Long -) |