diff --git a/sqlite-embedder-graalvm/src/jvmMain/kotlin/host/memory/GraalInputStreamWasiMemoryReader.kt b/sqlite-embedder-graalvm/src/jvmMain/kotlin/host/memory/GraalInputStreamWasiMemoryReader.kt index 8954ea0f..873a0231 100644 --- a/sqlite-embedder-graalvm/src/jvmMain/kotlin/host/memory/GraalInputStreamWasiMemoryReader.kt +++ b/sqlite-embedder-graalvm/src/jvmMain/kotlin/host/memory/GraalInputStreamWasiMemoryReader.kt @@ -12,11 +12,11 @@ import arrow.core.left import ru.pixnews.wasm.sqlite.open.helper.common.api.Logger import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.DefaultWasiMemoryReader import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.WasiMemoryReader +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.FileSystem import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.BadFileDescriptor import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.ReadError import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.ext.readCatching import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.model.Fd -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.nio.JvmNioFileSystem import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.nio.op.RunWithChannelFd import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.readwrite.ReadWriteStrategy import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.readwrite.ReadWriteStrategy.CHANGE_POSITION @@ -26,7 +26,7 @@ import java.nio.channels.FileChannel internal class GraalInputStreamWasiMemoryReader( private val memory: GraalvmWasmHostMemoryAdapter, - private val fileSystem: JvmNioFileSystem, + private val fileSystem: FileSystem, logger: Logger, ) : WasiMemoryReader { private val wasmMemory get() = memory.wasmMemory diff --git a/sqlite-embedder-graalvm/src/jvmMain/kotlin/host/module/wasi/function/FdRead.kt b/sqlite-embedder-graalvm/src/jvmMain/kotlin/host/module/wasi/function/FdRead.kt index f50562b6..58912095 100644 --- a/sqlite-embedder-graalvm/src/jvmMain/kotlin/host/module/wasi/function/FdRead.kt +++ b/sqlite-embedder-graalvm/src/jvmMain/kotlin/host/module/wasi/function/FdRead.kt @@ -21,7 +21,6 @@ import ru.pixnews.wasm.sqlite.open.helper.host.EmbedderHost import ru.pixnews.wasm.sqlite.open.helper.host.base.WasmPtr import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.WasiMemoryReader import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.model.Fd -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.nio.JvmNioFileSystem import ru.pixnews.wasm.sqlite.open.helper.host.wasi.preview1.function.FdReadFdPreadFunctionHandle import ru.pixnews.wasm.sqlite.open.helper.host.wasi.preview1.type.Iovec @@ -73,7 +72,7 @@ private class FdRead( val hostMemory = memory.toHostMemory() val wasiMemoryReader: WasiMemoryReader = GraalInputStreamWasiMemoryReader( hostMemory, - handle.host.fileSystem as? JvmNioFileSystem ?: error("JvmFileSystem expected"), + handle.host.fileSystem, this.handle.host.rootLogger, ) return handle.execute(hostMemory, wasiMemoryReader, Fd(fd), pIov, iovCnt, pNum).code diff --git a/sqlite-tests/sqlite-driver-base-tests/src/commonMain/kotlin/tests/room/AbstractBasicRoomTest.kt b/sqlite-tests/sqlite-driver-base-tests/src/commonMain/kotlin/tests/room/AbstractBasicRoomTest.kt index e7148de0..75b71c57 100644 --- a/sqlite-tests/sqlite-driver-base-tests/src/commonMain/kotlin/tests/room/AbstractBasicRoomTest.kt +++ b/sqlite-tests/sqlite-driver-base-tests/src/commonMain/kotlin/tests/room/AbstractBasicRoomTest.kt @@ -13,6 +13,7 @@ import ru.pixnews.wasm.sqlite.driver.test.base.tests.AbstractSqliteDriverTest import ru.pixnews.wasm.sqlite.driver.test.base.tests.TestSqliteDriverFactory import ru.pixnews.wasm.sqlite.driver.test.base.tests.room.UserDatabaseTests.UserDatabaseFactory import kotlin.test.Test +import kotlin.time.Duration.Companion.minutes public abstract class AbstractBasicRoomTest( driverFactory: TestSqliteDriverFactory, @@ -25,7 +26,7 @@ public abstract class AbstractBasicRoomTest( val tests = UserDatabaseTests(driverFactory, databaseFactory, logger, dbLogger) @Test - open fun Test_Room() = runTest { + open fun Test_Room() = runTest(timeout = 3.minutes) { tests.testRoomOnUserDatabase( databaseName = fileInTempDir("test.db"), queryCoroutineContext = coroutineContext, @@ -34,7 +35,7 @@ public abstract class AbstractBasicRoomTest( } @Test - open fun Test_In_Memory_Room() = runTest { + open fun Test_In_Memory_Room() = runTest(timeout = 3.minutes) { tests.testRoomOnUserDatabase( databaseName = null, queryCoroutineContext = this.coroutineContext, diff --git a/wasi-emscripten-fs/api/wasi-emscripten-fs.api b/wasi-emscripten-fs/api/wasi-emscripten-fs.api index eee77d0f..ae111e42 100644 --- a/wasi-emscripten-fs/api/wasi-emscripten-fs.api +++ b/wasi-emscripten-fs/api/wasi-emscripten-fs.api @@ -3,6 +3,37 @@ public abstract interface class ru/pixnews/wasm/sqlite/open/helper/host/filesyst public abstract fun isOperationSupported (Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/op/FileSystemOperation;)Z } +public abstract interface class ru/pixnews/wasm/sqlite/open/helper/host/filesystem/FileSystemEngine { +} + +public abstract interface class ru/pixnews/wasm/sqlite/open/helper/host/filesystem/FileSystemInterceptor { + public abstract fun intercept (Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/FileSystemInterceptor$Chain;)Larrow/core/Either; +} + +public abstract interface class ru/pixnews/wasm/sqlite/open/helper/host/filesystem/FileSystemInterceptor$Chain { + public abstract fun getInput ()Ljava/lang/Object; + public abstract fun getOperation ()Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/op/FileSystemOperation; + public abstract fun proceed (Ljava/lang/Object;)Larrow/core/Either; +} + +public final class ru/pixnews/wasm/sqlite/open/helper/host/filesystem/FileSystemKt { + public static final fun FileSystem (Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/FileSystemEngine;Lkotlin/jvm/functions/Function1;)Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/FileSystem; + public static synthetic fun FileSystem$default (Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/FileSystemEngine;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/FileSystem; +} + +public final class ru/pixnews/wasm/sqlite/open/helper/host/filesystem/dsl/FileSystemConfigBlock { + public fun ()V + public final fun addInterceptor (Lkotlin/jvm/functions/Function1;)V + public final fun addInterceptor (Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/FileSystemInterceptor;)V + public final fun engine (Lkotlin/jvm/functions/Function1;)V +} + +public abstract interface annotation class ru/pixnews/wasm/sqlite/open/helper/host/filesystem/dsl/FileSystemDsl : java/lang/annotation/Annotation { +} + +public abstract interface class ru/pixnews/wasm/sqlite/open/helper/host/filesystem/dsl/FileSystemEngineConfig { +} + public final class ru/pixnews/wasm/sqlite/open/helper/host/filesystem/error/AccessDenied : ru/pixnews/wasm/sqlite/open/helper/host/filesystem/error/CheckAccessError, ru/pixnews/wasm/sqlite/open/helper/host/filesystem/error/ChmodError, ru/pixnews/wasm/sqlite/open/helper/host/filesystem/error/ChownError, ru/pixnews/wasm/sqlite/open/helper/host/filesystem/error/FileSystemOperationError, ru/pixnews/wasm/sqlite/open/helper/host/filesystem/error/GetCurrentWorkingDirectoryError, ru/pixnews/wasm/sqlite/open/helper/host/filesystem/error/MkdirError, ru/pixnews/wasm/sqlite/open/helper/host/filesystem/error/OpenError, ru/pixnews/wasm/sqlite/open/helper/host/filesystem/error/ReadLinkError, ru/pixnews/wasm/sqlite/open/helper/host/filesystem/error/SetTimestampError, ru/pixnews/wasm/sqlite/open/helper/host/filesystem/error/StatError, ru/pixnews/wasm/sqlite/open/helper/host/filesystem/error/TruncateError, ru/pixnews/wasm/sqlite/open/helper/host/filesystem/error/UnlinkError { public fun (Ljava/lang/String;)V public final fun component1 ()Ljava/lang/String; @@ -443,67 +474,69 @@ public abstract interface class ru/pixnews/wasm/sqlite/open/helper/host/filesyst public abstract interface class ru/pixnews/wasm/sqlite/open/helper/host/filesystem/error/WriteError : ru/pixnews/wasm/sqlite/open/helper/host/filesystem/error/FileSystemOperationError { } -public final class ru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator : ru/pixnews/wasm/sqlite/open/helper/host/filesystem/FileSystem { - public fun (Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/FileSystem;Lkotlin/jvm/functions/Function1;Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$LoggingEvents;Ljava/util/Map;)V - public synthetic fun (Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/FileSystem;Lkotlin/jvm/functions/Function1;Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$LoggingEvents;Ljava/util/Map;ILkotlin/jvm/internal/DefaultConstructorMarker;)V - public fun close ()V - public fun execute (Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/op/FileSystemOperation;Ljava/lang/Object;)Larrow/core/Either; - public fun isOperationSupported (Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/op/FileSystemOperation;)Z +public final class ru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor : ru/pixnews/wasm/sqlite/open/helper/host/filesystem/FileSystemInterceptor { + public static final field Companion Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$Companion; + public fun (Lkotlin/jvm/functions/Function1;Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$LoggingEvents;Ljava/util/Map;)V + public synthetic fun (Lkotlin/jvm/functions/Function1;Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$LoggingEvents;Ljava/util/Map;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun intercept (Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/FileSystemInterceptor$Chain;)Larrow/core/Either; } -public final class ru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$LoggingEvents { +public final class ru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$Companion { +} + +public final class ru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$LoggingEvents { public fun ()V - public fun (Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$LoggingEvents$OperationStart;Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$LoggingEvents$OperationEnd;)V - public synthetic fun (Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$LoggingEvents$OperationStart;Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$LoggingEvents$OperationEnd;ILkotlin/jvm/internal/DefaultConstructorMarker;)V - public final fun component1 ()Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$LoggingEvents$OperationStart; - public final fun component2 ()Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$LoggingEvents$OperationEnd; - public final fun copy (Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$LoggingEvents$OperationStart;Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$LoggingEvents$OperationEnd;)Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$LoggingEvents; - public static synthetic fun copy$default (Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$LoggingEvents;Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$LoggingEvents$OperationStart;Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$LoggingEvents$OperationEnd;ILjava/lang/Object;)Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$LoggingEvents; + public fun (Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$LoggingEvents$OperationStart;Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$LoggingEvents$OperationEnd;)V + public synthetic fun (Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$LoggingEvents$OperationStart;Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$LoggingEvents$OperationEnd;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public final fun component1 ()Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$LoggingEvents$OperationStart; + public final fun component2 ()Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$LoggingEvents$OperationEnd; + public final fun copy (Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$LoggingEvents$OperationStart;Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$LoggingEvents$OperationEnd;)Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$LoggingEvents; + public static synthetic fun copy$default (Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$LoggingEvents;Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$LoggingEvents$OperationStart;Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$LoggingEvents$OperationEnd;ILjava/lang/Object;)Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$LoggingEvents; public fun equals (Ljava/lang/Object;)Z - public final fun getEnd ()Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$LoggingEvents$OperationEnd; - public final fun getStart ()Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$LoggingEvents$OperationStart; + public final fun getEnd ()Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$LoggingEvents$OperationEnd; + public final fun getStart ()Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$LoggingEvents$OperationStart; public fun hashCode ()I public fun toString ()Ljava/lang/String; } -public final class ru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$LoggingEvents$OperationEnd { +public final class ru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$LoggingEvents$OperationEnd { public fun ()V - public fun (Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$OperationLoggingLevel;Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$OperationLoggingLevel;Z)V - public synthetic fun (Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$OperationLoggingLevel;Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$OperationLoggingLevel;ZILkotlin/jvm/internal/DefaultConstructorMarker;)V - public final fun component1 ()Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$OperationLoggingLevel; - public final fun component2 ()Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$OperationLoggingLevel; + public fun (Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$OperationLoggingLevel;Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$OperationLoggingLevel;Z)V + public synthetic fun (Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$OperationLoggingLevel;Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$OperationLoggingLevel;ZILkotlin/jvm/internal/DefaultConstructorMarker;)V + public final fun component1 ()Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$OperationLoggingLevel; + public final fun component2 ()Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$OperationLoggingLevel; public final fun component3 ()Z - public final fun copy (Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$OperationLoggingLevel;Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$OperationLoggingLevel;Z)Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$LoggingEvents$OperationEnd; - public static synthetic fun copy$default (Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$LoggingEvents$OperationEnd;Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$OperationLoggingLevel;Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$OperationLoggingLevel;ZILjava/lang/Object;)Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$LoggingEvents$OperationEnd; + public final fun copy (Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$OperationLoggingLevel;Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$OperationLoggingLevel;Z)Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$LoggingEvents$OperationEnd; + public static synthetic fun copy$default (Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$LoggingEvents$OperationEnd;Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$OperationLoggingLevel;Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$OperationLoggingLevel;ZILjava/lang/Object;)Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$LoggingEvents$OperationEnd; public fun equals (Ljava/lang/Object;)Z - public final fun getInputs ()Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$OperationLoggingLevel; - public final fun getOutputs ()Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$OperationLoggingLevel; + public final fun getInputs ()Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$OperationLoggingLevel; + public final fun getOutputs ()Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$OperationLoggingLevel; public final fun getTrackDuration ()Z public fun hashCode ()I public fun toString ()Ljava/lang/String; } -public final class ru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$LoggingEvents$OperationStart { +public final class ru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$LoggingEvents$OperationStart { public fun ()V - public fun (Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$OperationLoggingLevel;)V - public synthetic fun (Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$OperationLoggingLevel;ILkotlin/jvm/internal/DefaultConstructorMarker;)V - public final fun component1 ()Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$OperationLoggingLevel; - public final fun copy (Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$OperationLoggingLevel;)Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$LoggingEvents$OperationStart; - public static synthetic fun copy$default (Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$LoggingEvents$OperationStart;Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$OperationLoggingLevel;ILjava/lang/Object;)Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$LoggingEvents$OperationStart; + public fun (Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$OperationLoggingLevel;)V + public synthetic fun (Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$OperationLoggingLevel;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public final fun component1 ()Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$OperationLoggingLevel; + public final fun copy (Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$OperationLoggingLevel;)Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$LoggingEvents$OperationStart; + public static synthetic fun copy$default (Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$LoggingEvents$OperationStart;Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$OperationLoggingLevel;ILjava/lang/Object;)Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$LoggingEvents$OperationStart; public fun equals (Ljava/lang/Object;)Z - public final fun getInputs ()Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$OperationLoggingLevel; + public final fun getInputs ()Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$OperationLoggingLevel; public fun hashCode ()I public fun toString ()Ljava/lang/String; } -public final class ru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$OperationLoggingLevel : java/lang/Enum { - public static final field BASIC Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$OperationLoggingLevel; - public static final field NAME Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$OperationLoggingLevel; - public static final field NONE Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$OperationLoggingLevel; - public static final field VERBOSE Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$OperationLoggingLevel; +public final class ru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$OperationLoggingLevel : java/lang/Enum { + public static final field BASIC Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$OperationLoggingLevel; + public static final field NAME Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$OperationLoggingLevel; + public static final field NONE Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$OperationLoggingLevel; + public static final field VERBOSE Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$OperationLoggingLevel; public static fun getEntries ()Lkotlin/enums/EnumEntries; - public static fun valueOf (Ljava/lang/String;)Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$OperationLoggingLevel; - public static fun values ()[Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemDecorator$OperationLoggingLevel; + public static fun valueOf (Ljava/lang/String;)Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$OperationLoggingLevel; + public static fun values ()[Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/logging/LoggingFileSystemInterceptor$OperationLoggingLevel; } public abstract class ru/pixnews/wasm/sqlite/open/helper/host/filesystem/model/BaseDirectory { @@ -694,13 +727,13 @@ public final class ru/pixnews/wasm/sqlite/open/helper/host/filesystem/model/When public final fun fromIdOrNull (I)Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/model/Whence; } -public final class ru/pixnews/wasm/sqlite/open/helper/host/filesystem/nio/JvmNioFileSystem : ru/pixnews/wasm/sqlite/open/helper/host/filesystem/FileSystem { - public fun ()V - public fun (Ljava/nio/file/FileSystem;Lru/pixnews/wasm/sqlite/open/helper/common/api/Logger;)V - public synthetic fun (Ljava/nio/file/FileSystem;Lru/pixnews/wasm/sqlite/open/helper/common/api/Logger;ILkotlin/jvm/internal/DefaultConstructorMarker;)V - public fun close ()V - public fun execute (Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/op/FileSystemOperation;Ljava/lang/Object;)Larrow/core/Either; - public fun isOperationSupported (Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/op/FileSystemOperation;)Z +public final class ru/pixnews/wasm/sqlite/open/helper/host/filesystem/nio/NioFileSystem : ru/pixnews/wasm/sqlite/open/helper/host/filesystem/FileSystemEngine { + public static final field INSTANCE Lru/pixnews/wasm/sqlite/open/helper/host/filesystem/nio/NioFileSystem; +} + +public final class ru/pixnews/wasm/sqlite/open/helper/host/filesystem/nio/NioFileSystemConfig : ru/pixnews/wasm/sqlite/open/helper/host/filesystem/dsl/FileSystemEngineConfig { + public final fun getNioFileSystem ()Ljava/nio/file/FileSystem; + public final fun setNioFileSystem (Ljava/nio/file/FileSystem;)V } public final class ru/pixnews/wasm/sqlite/open/helper/host/filesystem/nio/op/RunWithChannelFd { diff --git a/wasi-emscripten-fs/src/commonMain/kotlin/FileSystem.kt b/wasi-emscripten-fs/src/commonMain/kotlin/FileSystem.kt index 72efdbde..43a69bfa 100644 --- a/wasi-emscripten-fs/src/commonMain/kotlin/FileSystem.kt +++ b/wasi-emscripten-fs/src/commonMain/kotlin/FileSystem.kt @@ -7,9 +7,26 @@ package ru.pixnews.wasm.sqlite.open.helper.host.filesystem import arrow.core.Either +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.dsl.FileSystemCommonConfig +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.dsl.FileSystemConfigBlock +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.dsl.FileSystemEngineConfig import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.FileSystemOperationError import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.FileSystemOperation +public fun FileSystem( + engine: FileSystemEngine, + block: FileSystemConfigBlock.() -> Unit = {}, +): FileSystem { + val config = FileSystemConfigBlock().apply(block) + val commonConfig = object : FileSystemCommonConfig { + override val interceptors: List = config.interceptors + } + return engine.create( + commonConfig = commonConfig, + engineConfig = config.engineConfig, + ) +} + public interface FileSystem : AutoCloseable { public fun execute( operation: FileSystemOperation, diff --git a/wasi-emscripten-fs/src/commonMain/kotlin/FileSystemEngine.kt b/wasi-emscripten-fs/src/commonMain/kotlin/FileSystemEngine.kt new file mode 100644 index 00000000..2cb9eee7 --- /dev/null +++ b/wasi-emscripten-fs/src/commonMain/kotlin/FileSystemEngine.kt @@ -0,0 +1,19 @@ +/* + * Copyright 2024, the wasm-sqlite-open-helper project authors and contributors. Please see the AUTHORS file + * for details. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. + * SPDX-License-Identifier: Apache-2.0 + */ + +package ru.pixnews.wasm.sqlite.open.helper.host.filesystem + +import ru.pixnews.wasm.sqlite.open.helper.common.api.InternalWasmSqliteHelperApi +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.dsl.FileSystemCommonConfig +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.dsl.FileSystemEngineConfig + +public interface FileSystemEngine { + @InternalWasmSqliteHelperApi + public fun create( + commonConfig: FileSystemCommonConfig, + engineConfig: E.() -> Unit, + ): FileSystem +} diff --git a/wasi-emscripten-fs/src/commonMain/kotlin/FileSystemInterceptor.kt b/wasi-emscripten-fs/src/commonMain/kotlin/FileSystemInterceptor.kt new file mode 100644 index 00000000..5ccf122e --- /dev/null +++ b/wasi-emscripten-fs/src/commonMain/kotlin/FileSystemInterceptor.kt @@ -0,0 +1,26 @@ +/* + * Copyright 2024, the wasm-sqlite-open-helper project authors and contributors. Please see the AUTHORS file + * for details. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. + * SPDX-License-Identifier: Apache-2.0 + */ + +@file:Suppress("WRONG_MULTIPLE_MODIFIERS_ORDER") + +package ru.pixnews.wasm.sqlite.open.helper.host.filesystem + +import arrow.core.Either +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.FileSystemOperationError +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.FileSystemOperation + +public interface FileSystemInterceptor { + public fun intercept( + chain: Chain, + ): Either + + public interface Chain { + public val operation: FileSystemOperation + public val input: I + + public fun proceed(input: I): Either + } +} diff --git a/wasi-emscripten-fs/src/commonMain/kotlin/dsl/FileSystemCommonConfig.kt b/wasi-emscripten-fs/src/commonMain/kotlin/dsl/FileSystemCommonConfig.kt new file mode 100644 index 00000000..15729ce1 --- /dev/null +++ b/wasi-emscripten-fs/src/commonMain/kotlin/dsl/FileSystemCommonConfig.kt @@ -0,0 +1,15 @@ +/* + * Copyright 2024, the wasm-sqlite-open-helper project authors and contributors. Please see the AUTHORS file + * for details. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. + * SPDX-License-Identifier: Apache-2.0 + */ + +package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.dsl + +import ru.pixnews.wasm.sqlite.open.helper.common.api.InternalWasmSqliteHelperApi +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.FileSystemInterceptor + +@InternalWasmSqliteHelperApi +public interface FileSystemCommonConfig { + public val interceptors: List +} diff --git a/wasi-emscripten-fs/src/commonMain/kotlin/dsl/FileSystemConfigBlock.kt b/wasi-emscripten-fs/src/commonMain/kotlin/dsl/FileSystemConfigBlock.kt new file mode 100644 index 00000000..7ea5b100 --- /dev/null +++ b/wasi-emscripten-fs/src/commonMain/kotlin/dsl/FileSystemConfigBlock.kt @@ -0,0 +1,46 @@ +/* + * Copyright 2024, the wasm-sqlite-open-helper project authors and contributors. Please see the AUTHORS file + * for details. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. + * SPDX-License-Identifier: Apache-2.0 + */ + +package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.dsl + +import arrow.core.Either +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.FileSystemInterceptor +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.FileSystemInterceptor.Chain +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.FileSystemOperationError + +@FileSystemDsl +public class FileSystemConfigBlock { + private val _interceptors: MutableList = mutableListOf() + internal val interceptors: List get() = _interceptors + + internal var engineConfig: E.() -> Unit = {} + private set + + public fun addInterceptor(interceptor: FileSystemInterceptor) { + _interceptors += interceptor + } + + public inline fun addInterceptor( + crossinline block: (chain: Chain) -> Either, + ): Unit = addInterceptor( + object : FileSystemInterceptor { + override fun intercept( + chain: Chain, + ): Either { + @Suppress("UNCHECKED_CAST") + return block(chain as Chain) as Either + } + }, + ) + + public fun engine(block: E.() -> Unit) { + val oldConfig = engineConfig + engineConfig = { + oldConfig() + block() + } + } +} diff --git a/wasi-emscripten-fs/src/commonMain/kotlin/dsl/FileSystemDsl.kt b/wasi-emscripten-fs/src/commonMain/kotlin/dsl/FileSystemDsl.kt new file mode 100644 index 00000000..daae650b --- /dev/null +++ b/wasi-emscripten-fs/src/commonMain/kotlin/dsl/FileSystemDsl.kt @@ -0,0 +1,11 @@ +/* + * Copyright 2024, the wasm-sqlite-open-helper project authors and contributors. Please see the AUTHORS file + * for details. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. + * SPDX-License-Identifier: Apache-2.0 + */ + +package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.dsl + +@DslMarker +@Target(AnnotationTarget.CLASS, AnnotationTarget.TYPEALIAS, AnnotationTarget.TYPE, AnnotationTarget.FUNCTION) +public annotation class FileSystemDsl diff --git a/wasi-emscripten-fs/src/commonMain/kotlin/dsl/FileSystemEngineConfig.kt b/wasi-emscripten-fs/src/commonMain/kotlin/dsl/FileSystemEngineConfig.kt new file mode 100644 index 00000000..2d1370bd --- /dev/null +++ b/wasi-emscripten-fs/src/commonMain/kotlin/dsl/FileSystemEngineConfig.kt @@ -0,0 +1,10 @@ +/* + * Copyright 2024, the wasm-sqlite-open-helper project authors and contributors. Please see the AUTHORS file + * for details. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. + * SPDX-License-Identifier: Apache-2.0 + */ + +package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.dsl + +@FileSystemDsl +public interface FileSystemEngineConfig diff --git a/wasi-emscripten-fs/src/commonMain/kotlin/internal/delegatefs/DelegateOperationsFileSystem.kt b/wasi-emscripten-fs/src/commonMain/kotlin/internal/delegatefs/DelegateOperationsFileSystem.kt new file mode 100644 index 00000000..c222c35a --- /dev/null +++ b/wasi-emscripten-fs/src/commonMain/kotlin/internal/delegatefs/DelegateOperationsFileSystem.kt @@ -0,0 +1,58 @@ +/* + * Copyright 2024, the wasm-sqlite-open-helper project authors and contributors. Please see the AUTHORS file + * for details. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. + * SPDX-License-Identifier: Apache-2.0 + */ + +package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs + +import arrow.core.Either +import arrow.core.left +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.FileSystem +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.FileSystemInterceptor +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.FileSystemInterceptor.Chain +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.FileSystemOperationError +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.NotImplemented +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.FileSystemOperation + +internal class DelegateOperationsFileSystem( + private val operations: Map, FileSystemOperationHandler<*, *, *>>, + interceptors: List, +) : FileSystem { + private val interceptors: List = buildList { + addAll(interceptors) + add(ExecuteOperationInterceptor(operations)) + } + + override fun execute( + operation: FileSystemOperation, + input: I, + ): Either { + val chain = InterceptorChain( + operation = operation, + input = input, + interceptors = interceptors, + ) + return chain.proceed(input) + } + + override fun isOperationSupported(operation: FileSystemOperation<*, *, *>): Boolean { + return operations.containsKey(operation) + } + + override fun close() = Unit + + private class ExecuteOperationInterceptor( + private val operations: Map, FileSystemOperationHandler<*, *, *>>, + ) : FileSystemInterceptor { + @Suppress("UNCHECKED_CAST") + override fun intercept(chain: Chain): Either { + val handler = operations[chain.operation] as? FileSystemOperationHandler + if (handler == null) { + return NotImplemented.left() as Either + } + + return handler.invoke(chain.input) + } + } +} diff --git a/wasi-emscripten-fs/src/nativeMain/kotlin/posix/base/PosixOperationHandler.kt b/wasi-emscripten-fs/src/commonMain/kotlin/internal/delegatefs/FileSystemOperationHandler.kt similarity index 80% rename from wasi-emscripten-fs/src/nativeMain/kotlin/posix/base/PosixOperationHandler.kt rename to wasi-emscripten-fs/src/commonMain/kotlin/internal/delegatefs/FileSystemOperationHandler.kt index 4d5eefae..ee6ef997 100644 --- a/wasi-emscripten-fs/src/nativeMain/kotlin/posix/base/PosixOperationHandler.kt +++ b/wasi-emscripten-fs/src/commonMain/kotlin/internal/delegatefs/FileSystemOperationHandler.kt @@ -4,12 +4,12 @@ * SPDX-License-Identifier: Apache-2.0 */ -package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.base +package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs import arrow.core.Either import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.FileSystemOperationError -internal fun interface PosixOperationHandler< +internal fun interface FileSystemOperationHandler< in I : Any, out E : FileSystemOperationError, out R : Any, diff --git a/wasi-emscripten-fs/src/commonMain/kotlin/internal/delegatefs/InterceptorChain.kt b/wasi-emscripten-fs/src/commonMain/kotlin/internal/delegatefs/InterceptorChain.kt new file mode 100644 index 00000000..2ef39b63 --- /dev/null +++ b/wasi-emscripten-fs/src/commonMain/kotlin/internal/delegatefs/InterceptorChain.kt @@ -0,0 +1,30 @@ +/* + * Copyright 2024, the wasm-sqlite-open-helper project authors and contributors. Please see the AUTHORS file + * for details. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. + * SPDX-License-Identifier: Apache-2.0 + */ + +package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs + +import arrow.core.Either +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.FileSystemInterceptor +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.FileSystemOperationError +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.FileSystemOperation + +internal class InterceptorChain( + override val operation: FileSystemOperation, + override val input: I, + private val interceptors: List, + private val index: Int = 0, +) : FileSystemInterceptor.Chain { + override fun proceed(input: I): Either { + val interceptor = interceptors.getOrNull(index) ?: error("End of interceptor chain") + val next = InterceptorChain( + interceptors = interceptors, + index = index + 1, + operation = operation, + input = input, + ) + return interceptor.intercept(next) + } +} diff --git a/wasi-emscripten-fs/src/commonMain/kotlin/logging/LoggingFileSystemDecorator.kt b/wasi-emscripten-fs/src/commonMain/kotlin/logging/LoggingFileSystemInterceptor.kt similarity index 81% rename from wasi-emscripten-fs/src/commonMain/kotlin/logging/LoggingFileSystemDecorator.kt rename to wasi-emscripten-fs/src/commonMain/kotlin/logging/LoggingFileSystemInterceptor.kt index bf14b21d..7932941b 100644 --- a/wasi-emscripten-fs/src/commonMain/kotlin/logging/LoggingFileSystemDecorator.kt +++ b/wasi-emscripten-fs/src/commonMain/kotlin/logging/LoggingFileSystemInterceptor.kt @@ -7,56 +7,45 @@ package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.logging import arrow.core.Either -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.FileSystem +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.FileSystemInterceptor +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.FileSystemInterceptor.Chain import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.FileSystemOperationError -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.logging.LoggingFileSystemDecorator.LoggingEvents.OperationEnd -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.logging.LoggingFileSystemDecorator.LoggingEvents.OperationStart -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.logging.LoggingFileSystemDecorator.OperationLoggingLevel.BASIC -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.logging.LoggingFileSystemDecorator.OperationLoggingLevel.NONE -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.logging.LoggingFileSystemDecorator.OperationLoggingLevel.VERBOSE +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.logging.LoggingFileSystemInterceptor.LoggingEvents.OperationEnd +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.logging.LoggingFileSystemInterceptor.LoggingEvents.OperationStart +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.logging.LoggingFileSystemInterceptor.OperationLoggingLevel.BASIC +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.logging.LoggingFileSystemInterceptor.OperationLoggingLevel.NONE +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.logging.LoggingFileSystemInterceptor.OperationLoggingLevel.VERBOSE import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.FileSystemOperation import kotlin.time.Duration import kotlin.time.measureTimedValue -public class LoggingFileSystemDecorator( - private val delegate: FileSystem, +public class LoggingFileSystemInterceptor( private val logger: (message: () -> String) -> Unit, private val logEvents: LoggingEvents = LoggingEvents(), private val operationLevels: Map, LoggingEvents> = emptyMap(), -) : FileSystem { - override fun execute( - operation: FileSystemOperation, - input: I, - ): Either { - val loggingEvents = getLoggingEvents(operation) - logOperationStart(loggingEvents.start, operation, input) +) : FileSystemInterceptor { + override fun intercept(chain: Chain): Either { + val loggingEvents = getLoggingEvents(chain.operation) + logOperationStart(loggingEvents.start, chain.operation, chain.input) val duration: Duration? - val output: Either + val output: Either<*, *> if (loggingEvents.end.trackDuration) { val timedValue = measureTimedValue { - delegate.execute(operation, input) + chain.proceed(chain.input) } output = timedValue.value duration = timedValue.duration } else { - output = delegate.execute(operation, input) + output = chain.proceed(chain.input) duration = null } - logOperationEnd(loggingEvents.end, operation, input, output, duration) + logOperationEnd(loggingEvents.end, chain.operation, chain.input, output, duration) return output } - override fun isOperationSupported(operation: FileSystemOperation<*, *, *>): Boolean { - return delegate.isOperationSupported(operation) - } - - override fun close() { - delegate.close() - } - private fun getLoggingEvents(operation: FileSystemOperation<*, *, *>): LoggingEvents { return operationLevels[operation] ?: logEvents } @@ -109,7 +98,7 @@ public class LoggingFileSystemDecorator( ) } - private companion object { + public companion object { private fun buildOperationStartMessage( level: OperationStart, operation: FileSystemOperation<*, *, *>, @@ -123,7 +112,7 @@ public class LoggingFileSystemDecorator( } } - fun buildOperationEndMessage( + private fun buildOperationEndMessage( level: OperationEnd, operation: FileSystemOperation<*, *, *>, input: Any, diff --git a/wasi-emscripten-fs/src/commonMain/kotlin/op/FileSystemOperation.kt b/wasi-emscripten-fs/src/commonMain/kotlin/op/FileSystemOperation.kt index 7a0c4482..05715142 100644 --- a/wasi-emscripten-fs/src/commonMain/kotlin/op/FileSystemOperation.kt +++ b/wasi-emscripten-fs/src/commonMain/kotlin/op/FileSystemOperation.kt @@ -8,6 +8,6 @@ package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.FileSystemOperationError -public interface FileSystemOperation { +public interface FileSystemOperation { public val tag: String } diff --git a/wasi-emscripten-fs/src/commonTest/kotlin/internal/delegatefs/DelegateOperationsFileSystemTest.kt b/wasi-emscripten-fs/src/commonTest/kotlin/internal/delegatefs/DelegateOperationsFileSystemTest.kt new file mode 100644 index 00000000..012dc726 --- /dev/null +++ b/wasi-emscripten-fs/src/commonTest/kotlin/internal/delegatefs/DelegateOperationsFileSystemTest.kt @@ -0,0 +1,62 @@ +/* + * Copyright 2024, the wasm-sqlite-open-helper project authors and contributors. Please see the AUTHORS file + * for details. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. + * SPDX-License-Identifier: Apache-2.0 + */ + +package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs + +import arrow.core.left +import arrow.core.right +import assertk.assertThat +import assertk.assertions.isEqualTo +import assertk.assertions.isFalse +import assertk.assertions.isTrue +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.GetCurrentWorkingDirectoryError +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.NoEntry +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.ReadLinkError +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.FileSystemOperation +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.cwd.GetCurrentWorkingDirectory +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.opencreate.Open +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.readlink.ReadLink +import kotlin.test.Test + +class DelegateOperationsFileSystemTest { + @Test + fun operationFileSystem_should_invoke_correct_delegates() { + val delegateFs = DelegateOperationsFileSystem(testHandlers, emptyList()) + + val cwd = delegateFs.execute(GetCurrentWorkingDirectory, Unit) + assertThat(cwd.getOrNull()).isEqualTo("/") + + val link = delegateFs.execute(ReadLink, ReadLink("/link")) + assertThat(link.leftOrNull()) + .isEqualTo(NoEntry("Test not entry")) + } + + @Test + fun operationFileSystem_test_isOperationSupported() { + val delegateFs = DelegateOperationsFileSystem( + testHandlers, + emptyList(), + ) + assertThat(delegateFs.isOperationSupported(GetCurrentWorkingDirectory)).isTrue() + assertThat(delegateFs.isOperationSupported(ReadLink)).isTrue() + assertThat(delegateFs.isOperationSupported(Open)).isFalse() + } + + internal companion object { + private val testCwdHandler: FileSystemOperationHandler = + FileSystemOperationHandler { _ -> + "/".right() + } + val testReadlinkHandler: FileSystemOperationHandler = + FileSystemOperationHandler { _ -> + NoEntry("Test not entry").left() + } + val testHandlers: Map, FileSystemOperationHandler<*, *, *>> = mapOf( + GetCurrentWorkingDirectory to testCwdHandler, + ReadLink to testReadlinkHandler, + ) + } +} diff --git a/wasi-emscripten-fs/src/commonTest/kotlin/internal/delegatefs/InterceptorChainTest.kt b/wasi-emscripten-fs/src/commonTest/kotlin/internal/delegatefs/InterceptorChainTest.kt new file mode 100644 index 00000000..a073be5a --- /dev/null +++ b/wasi-emscripten-fs/src/commonTest/kotlin/internal/delegatefs/InterceptorChainTest.kt @@ -0,0 +1,81 @@ +/* + * Copyright 2024, the wasm-sqlite-open-helper project authors and contributors. Please see the AUTHORS file + * for details. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. + * SPDX-License-Identifier: Apache-2.0 + */ + +package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs + +import arrow.core.Either +import arrow.core.right +import assertk.assertThat +import assertk.assertions.containsExactly +import assertk.assertions.isEqualTo +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.FileSystemInterceptor +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.FileSystemInterceptor.Chain +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.FileSystemOperationError +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.FileSystemOperation +import kotlin.test.Test + +class InterceptorChainTest { + @Test + @Suppress("UNCHECKED_CAST") + fun interceptors_should_be_executed_in_correct_order() { + val operations: MutableList = mutableListOf() + + class TestInterceptor(val interceptorNo: Int) : FileSystemInterceptor { + override fun intercept( + chain: Chain, + ): Either { + val inputTestOp = chain.input as TestFsOp + operations.add("Interceptor$interceptorNo: Before. Original input: ${inputTestOp.path}") + val modifiedInput = TestFsOp("inp$interceptorNo-${inputTestOp.path}") + + val result = chain.proceed(modifiedInput as I) + + operations.add("Interceptor$interceptorNo: After. Unmodified result: ${result.getOrNull()}") + return result.map { "$it-outp$interceptorNo" as R } + } + } + + val lastInterceptor = object : FileSystemInterceptor { + override fun intercept( + chain: Chain, + ): Either { + val inputTestOp = chain.input as TestFsOp + operations.add(("Operation. Input: ${inputTestOp.path}")) + return "link".right() as Either + } + } + + val interceptors = listOf( + TestInterceptor(1), + TestInterceptor(2), + TestInterceptor(3), + lastInterceptor, + ) + + val input = TestFsOp("Test") + val chain = InterceptorChain(TestFsOp, input, interceptors) + val result = chain.proceed(input) + + assertThat(operations).containsExactly( + "Interceptor1: Before. Original input: Test", + "Interceptor2: Before. Original input: inp1-Test", + "Interceptor3: Before. Original input: inp2-inp1-Test", + "Operation. Input: inp3-inp2-inp1-Test", + "Interceptor3: After. Unmodified result: link", + "Interceptor2: After. Unmodified result: link-outp3", + "Interceptor1: After. Unmodified result: link-outp3-outp2", + ) + assertThat(result.getOrNull()).isEqualTo("link-outp3-outp2-outp1") + } + + data class TestFsOp( + val path: String, + ) { + companion object : FileSystemOperation { + override val tag: String get() = "testop" + } + } +} diff --git a/wasi-emscripten-fs/src/commonTest/kotlin/logging/LoggingFileSystemDecoratorTest.kt b/wasi-emscripten-fs/src/commonTest/kotlin/logging/LoggingFileSystemDecoratorTest.kt index b028b2cb..3fc7136f 100644 --- a/wasi-emscripten-fs/src/commonTest/kotlin/logging/LoggingFileSystemDecoratorTest.kt +++ b/wasi-emscripten-fs/src/commonTest/kotlin/logging/LoggingFileSystemDecoratorTest.kt @@ -6,16 +6,19 @@ package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.loggin +import arrow.core.Either import arrow.core.right import assertk.assertThat import assertk.assertions.containsExactly -import assertk.assertions.isTrue -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.logging.LoggingFileSystemDecorator -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.logging.LoggingFileSystemDecorator.LoggingEvents -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.logging.LoggingFileSystemDecorator.LoggingEvents.OperationEnd -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.logging.LoggingFileSystemDecorator.LoggingEvents.OperationStart -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.logging.LoggingFileSystemDecorator.OperationLoggingLevel.BASIC +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.FileSystemInterceptor.Chain +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.ReadLinkError +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.logging.LoggingFileSystemInterceptor +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.logging.LoggingFileSystemInterceptor.LoggingEvents +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.logging.LoggingFileSystemInterceptor.LoggingEvents.OperationEnd +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.logging.LoggingFileSystemInterceptor.LoggingEvents.OperationStart +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.logging.LoggingFileSystemInterceptor.OperationLoggingLevel.BASIC import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.model.BaseDirectory.CurrentWorkingDirectory +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.FileSystemOperation import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.readlink.ReadLink import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.test.fixtures.TestFileSystem import kotlin.test.Test @@ -29,8 +32,7 @@ class LoggingFileSystemDecoratorTest { } val loggedMessages: MutableList = mutableListOf() - val loggingDecorator = LoggingFileSystemDecorator( - delegate = delegateFs, + val loggingInterceptor = LoggingFileSystemInterceptor( logger = { loggedMessages += it() }, @@ -43,30 +45,17 @@ class LoggingFileSystemDecoratorTest { ), ), ) - loggingDecorator.execute(ReadLink, ReadLink(path = "/testPath", baseDirectory = CurrentWorkingDirectory)) + val chain = object : Chain { + override val operation: FileSystemOperation = ReadLink.Companion + override val input: ReadLink = ReadLink(path = "/testPath", baseDirectory = CurrentWorkingDirectory) + override fun proceed(input: ReadLink): Either = "/link".right() + } + + loggingInterceptor.intercept(chain) assertThat(loggedMessages).containsExactly( "^readlink(ReadLink(path=/testPath, baseDirectory=CurrentWorkingDirectory))", "readlink(): OK. Inputs: ReadLink(path=/testPath, baseDirectory=CurrentWorkingDirectory). Outputs: /link", ) } - - @Test - fun close_should_close_delegate() { - var closeCalled = false - val delegateFs = object : TestFileSystem() { - override fun close() { - closeCalled = true - } - } - - val loggingDecorator = LoggingFileSystemDecorator( - delegate = delegateFs, - logger = { }, - ) - - loggingDecorator.close() - - assertThat(closeCalled).isTrue() - } } diff --git a/wasi-emscripten-fs/src/jvmMain/kotlin/fd/NioFileDescriptorTable.kt b/wasi-emscripten-fs/src/jvmMain/kotlin/fd/NioFileDescriptorTable.kt index c108a935..9f4d563d 100644 --- a/wasi-emscripten-fs/src/jvmMain/kotlin/fd/NioFileDescriptorTable.kt +++ b/wasi-emscripten-fs/src/jvmMain/kotlin/fd/NioFileDescriptorTable.kt @@ -7,12 +7,11 @@ package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.fd import arrow.core.Either -import ru.pixnews.wasm.sqlite.open.helper.common.api.Logger import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.BadFileDescriptor import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.Nfile import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.FileDescriptorTable import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.model.Fd -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.nio.JvmFileSystemState +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.nio.NioFileSystemState import java.nio.channels.FileChannel import java.nio.file.Path import java.util.concurrent.locks.Lock @@ -20,10 +19,8 @@ import java.util.concurrent.locks.ReentrantLock import kotlin.concurrent.withLock internal class NioFileDescriptorTable( - private val fsState: JvmFileSystemState, - logger: Logger, + private val fsState: NioFileSystemState, ) : AutoCloseable { - private val logger = logger.withTag("NioFileDescriptorTable") private val fds: FileDescriptorTable = FileDescriptorTable() private val lock: Lock = ReentrantLock() @@ -59,7 +56,7 @@ internal class NioFileDescriptorTable( try { chan.channel.close() } catch (@Suppress("TooGenericExceptionCaught", "SwallowedException") ex: Exception) { - logger.i { "close(${chan.path}) failed. Ignore." } + // close(${chan.path}) failed. Ignore. } } } diff --git a/wasi-emscripten-fs/src/jvmMain/kotlin/fd/NioFileHandle.kt b/wasi-emscripten-fs/src/jvmMain/kotlin/fd/NioFileHandle.kt index 8fa9ffa2..a623c571 100644 --- a/wasi-emscripten-fs/src/jvmMain/kotlin/fd/NioFileHandle.kt +++ b/wasi-emscripten-fs/src/jvmMain/kotlin/fd/NioFileHandle.kt @@ -12,7 +12,7 @@ import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.ChannelPositi import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.ChannelPositionError.ClosedChannel import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.model.Fd import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.model.Whence -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.nio.JvmFileSystemState +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.nio.NioFileSystemState import java.io.IOException import java.nio.channels.ClosedChannelException import java.nio.channels.FileChannel @@ -22,7 +22,7 @@ import java.util.concurrent.locks.ReentrantLock import java.nio.file.Path as NioPath internal class NioFileHandle( - val fileSystem: JvmFileSystemState, + val fileSystem: NioFileSystemState, val fd: Fd, val path: NioPath, val channel: FileChannel, diff --git a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioAddAdvisoryLockFd.kt b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioAddAdvisoryLockFd.kt index 7ecdeabe..4207760f 100644 --- a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioAddAdvisoryLockFd.kt +++ b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioAddAdvisoryLockFd.kt @@ -19,6 +19,7 @@ import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.NoLock import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.fd.FileLockKey import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.fd.NioFileHandle import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.fd.resolveWhencePosition +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.FileSystemOperationHandler import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.lock.AddAdvisoryLockFd import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.lock.Advisorylock import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.lock.AdvisorylockLockType @@ -31,8 +32,8 @@ import java.nio.channels.OverlappingFileLockException import kotlin.concurrent.withLock internal class NioAddAdvisoryLockFd( - private val fsState: JvmFileSystemState, -) : NioOperationHandler { + private val fsState: NioFileSystemState, +) : FileSystemOperationHandler { override fun invoke(input: AddAdvisoryLockFd): Either = fsState.fsLock.withLock { val channel = fsState.fileDescriptors.get(input.fd) ?: return BadFileDescriptor("File descriptor ${input.fd} is not opened").left() diff --git a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioCheckAccess.kt b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioCheckAccess.kt index 9efe45ac..1e0ebad2 100644 --- a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioCheckAccess.kt +++ b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioCheckAccess.kt @@ -13,6 +13,7 @@ import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.BadFileDescripto import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.CheckAccessError import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.NoEntry import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.ext.asLinkOptions +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.FileSystemOperationHandler import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.nio.cwd.PathResolver import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.nio.cwd.PathResolver.ResolvePathError import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.checkaccess.CheckAccess @@ -27,7 +28,7 @@ import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.NotDirectory as internal class NioCheckAccess( private val pathResolver: PathResolver, -) : NioOperationHandler { +) : FileSystemOperationHandler { override fun invoke(input: CheckAccess): Either = either { val path = pathResolver.resolve(input.path, input.baseDirectory, input.allowEmptyPath) .mapLeft { it.toCheckAccessError() } diff --git a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioChmod.kt b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioChmod.kt index dc5c7d8e..a15be0c1 100644 --- a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioChmod.kt +++ b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioChmod.kt @@ -15,6 +15,7 @@ import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.InvalidArgument import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.IoError import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.PermissionDenied import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.ext.toPosixFilePermissions +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.FileSystemOperationHandler import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.model.FileMode import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.nio.cwd.PathResolver.ResolvePathError import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.nio.cwd.toCommonError @@ -24,8 +25,8 @@ import java.nio.file.Path import kotlin.io.path.setPosixFilePermissions internal class NioChmod( - private val fsState: JvmFileSystemState, -) : NioOperationHandler { + private val fsState: NioFileSystemState, +) : FileSystemOperationHandler { override fun invoke(input: Chmod): Either { val path: Path = fsState.pathResolver.resolve(input.path, input.baseDirectory, false) .mapLeft(ResolvePathError::toCommonError) diff --git a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioChmodFd.kt b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioChmodFd.kt index 375feb97..d477cf18 100644 --- a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioChmodFd.kt +++ b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioChmodFd.kt @@ -10,13 +10,14 @@ import arrow.core.Either import arrow.core.left import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.BadFileDescriptor import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.ChmodError +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.FileSystemOperationHandler import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.nio.NioChmod.Companion.setPosixFilePermissions import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.chmod.ChmodFd import java.nio.file.Path internal class NioChmodFd( - private val fsState: JvmFileSystemState, -) : NioOperationHandler { + private val fsState: NioFileSystemState, +) : FileSystemOperationHandler { override fun invoke(input: ChmodFd): Either { val path: Path = fsState.fileDescriptors.get(input.fd)?.path ?: return BadFileDescriptor("File descriptor `${input.fd}` is not opened").left() diff --git a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioChown.kt b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioChown.kt index dd0399d6..136582a7 100644 --- a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioChown.kt +++ b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioChown.kt @@ -13,6 +13,7 @@ import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.ChownError import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.InvalidArgument import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.IoError import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.NotSupported +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.FileSystemOperationHandler import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.nio.cwd.PathResolver.ResolvePathError import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.nio.cwd.toCommonError import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.chown.Chown @@ -23,8 +24,8 @@ import java.nio.file.attribute.UserPrincipalNotFoundException import kotlin.io.path.fileAttributesView internal class NioChown( - private val fsState: JvmFileSystemState, -) : NioOperationHandler { + private val fsState: NioFileSystemState, +) : FileSystemOperationHandler { override fun invoke(input: Chown): Either { val path: Path = fsState.pathResolver.resolve(input.path, input.baseDirectory, false) .mapLeft(ResolvePathError::toCommonError) diff --git a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioChownFd.kt b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioChownFd.kt index 5fd20132..f7b21a89 100644 --- a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioChownFd.kt +++ b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioChownFd.kt @@ -10,13 +10,14 @@ import arrow.core.Either import arrow.core.left import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.BadFileDescriptor import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.ChownError +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.FileSystemOperationHandler import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.nio.NioChown.Companion.setPosixUserGroup import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.chown.ChownFd import java.nio.file.Path internal class NioChownFd( - private val fsState: JvmFileSystemState, -) : NioOperationHandler { + private val fsState: NioFileSystemState, +) : FileSystemOperationHandler { override fun invoke(input: ChownFd): Either { val path: Path = fsState.fileDescriptors.get(input.fd)?.path ?: return BadFileDescriptor("File descriptor `${input.fd}` is not opened").left() diff --git a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioCloseFd.kt b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioCloseFd.kt index 7ece769c..13a9de2d 100644 --- a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioCloseFd.kt +++ b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioCloseFd.kt @@ -12,13 +12,14 @@ import arrow.core.left import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.BadFileDescriptor import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.CloseError import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.IoError +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.FileSystemOperationHandler import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.close.CloseFd import java.io.IOException import kotlin.concurrent.withLock internal class NioCloseFd( - private val fsState: JvmFileSystemState, -) : NioOperationHandler { + private val fsState: NioFileSystemState, +) : FileSystemOperationHandler { override fun invoke(input: CloseFd): Either = fsState.fsLock.withLock { val fileChannel = fsState.fileDescriptors.remove(input.fd) .mapLeft { BadFileDescriptor(it.message) } diff --git a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioFileSystem.kt b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioFileSystem.kt new file mode 100644 index 00000000..dcaee451 --- /dev/null +++ b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioFileSystem.kt @@ -0,0 +1,26 @@ +/* + * Copyright 2024, the wasm-sqlite-open-helper project authors and contributors. Please see the AUTHORS file + * for details. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. + * SPDX-License-Identifier: Apache-2.0 + */ + +package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.nio + +import ru.pixnews.wasm.sqlite.open.helper.common.api.InternalWasmSqliteHelperApi +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.FileSystem +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.FileSystemEngine +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.dsl.FileSystemCommonConfig + +public object NioFileSystem : FileSystemEngine { + @InternalWasmSqliteHelperApi + override fun create( + commonConfig: FileSystemCommonConfig, + engineConfig: NioFileSystemConfig.() -> Unit, + ): FileSystem { + val nioConfig = NioFileSystemConfig().apply(engineConfig) + return NioFileSystemImpl( + javaFs = nioConfig.nioFileSystem, + interceptors = commonConfig.interceptors, + ) + } +} diff --git a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioOperationHandler.kt b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioFileSystemConfig.kt similarity index 52% rename from wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioOperationHandler.kt rename to wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioFileSystemConfig.kt index 2dc25682..1ff4ce31 100644 --- a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioOperationHandler.kt +++ b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioFileSystemConfig.kt @@ -6,13 +6,9 @@ package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.nio -import arrow.core.Either -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.FileSystemOperationError +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.dsl.FileSystemEngineConfig +import java.nio.file.FileSystems -internal fun interface NioOperationHandler< - in I : Any, - out E : FileSystemOperationError, - out R : Any, - > { - public operator fun invoke(input: I): Either +public class NioFileSystemConfig internal constructor() : FileSystemEngineConfig { + public var nioFileSystem: java.nio.file.FileSystem = FileSystems.getDefault() } diff --git a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/JvmNioFileSystem.kt b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioFileSystemImpl.kt similarity index 85% rename from wasi-emscripten-fs/src/jvmMain/kotlin/nio/JvmNioFileSystem.kt rename to wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioFileSystemImpl.kt index 89c1442d..7711411a 100644 --- a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/JvmNioFileSystem.kt +++ b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioFileSystemImpl.kt @@ -7,11 +7,11 @@ package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.nio import arrow.core.Either -import arrow.core.left -import ru.pixnews.wasm.sqlite.open.helper.common.api.Logger import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.FileSystem +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.FileSystemInterceptor import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.FileSystemOperationError -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.NotImplemented +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.DelegateOperationsFileSystem +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.FileSystemOperationHandler import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.nio.cwd.CurrentDirectoryProvider import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.nio.cwd.JvmCurrentDirectoryProvider import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.nio.op.RunWithChannelFd @@ -41,13 +41,13 @@ import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.unlink.UnlinkDirect import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.unlink.UnlinkFile import java.nio.file.FileSystems -public class JvmNioFileSystem( +internal class NioFileSystemImpl( javaFs: java.nio.file.FileSystem = FileSystems.getDefault(), - rootLogger: Logger = Logger, + interceptors: List, ) : FileSystem { private val currentDirectoryProvider: CurrentDirectoryProvider = JvmCurrentDirectoryProvider(javaFs) - private val fsState = JvmFileSystemState(rootLogger, javaFs) - private val operations: Map, NioOperationHandler<*, *, *>> = mapOf( + private val fsState = NioFileSystemState(javaFs) + private val operations: Map, FileSystemOperationHandler<*, *, *>> = mapOf( Open to NioOpen(fsState), CloseFd to NioCloseFd(fsState), AddAdvisoryLockFd to NioAddAdvisoryLockFd(fsState), @@ -73,6 +73,7 @@ public class JvmNioFileSystem( WriteFd to NioWriteFd(fsState), RunWithChannelFd to NioRunWithRawChannelFd(fsState), ) + private val fsAdapter = DelegateOperationsFileSystem(operations, interceptors) override fun close() { fsState.close() @@ -80,18 +81,10 @@ public class JvmNioFileSystem( override fun isOperationSupported( operation: FileSystemOperation<*, *, *>, - ): Boolean = operations.containsKey(operation) + ): Boolean = fsAdapter.isOperationSupported(operation) - @Suppress("UNCHECKED_CAST") override fun execute( operation: FileSystemOperation, input: I, - ): Either { - val handler = operations[operation] as NioOperationHandler? - if (handler == null) { - return NotImplemented.left() as Either - } - - return handler.invoke(input) - } + ): Either = fsAdapter.execute(operation, input) } diff --git a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/JvmFileSystemState.kt b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioFileSystemState.kt similarity index 88% rename from wasi-emscripten-fs/src/jvmMain/kotlin/nio/JvmFileSystemState.kt rename to wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioFileSystemState.kt index 0300b5e5..e6230a44 100644 --- a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/JvmFileSystemState.kt +++ b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioFileSystemState.kt @@ -6,7 +6,6 @@ package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.nio -import ru.pixnews.wasm.sqlite.open.helper.common.api.Logger import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.fd.NioFileDescriptorTable import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.nio.cwd.JvmPathResolver import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.nio.cwd.PathResolver @@ -15,12 +14,11 @@ import java.util.concurrent.locks.Lock import java.util.concurrent.locks.ReentrantLock import java.nio.file.FileSystem as NioFileSystem -internal class JvmFileSystemState( - rootLogger: Logger, +internal class NioFileSystemState( val javaFs: NioFileSystem = FileSystems.getDefault(), ) : AutoCloseable { val fsLock: Lock = ReentrantLock() - val fileDescriptors: NioFileDescriptorTable = NioFileDescriptorTable(this, rootLogger) + val fileDescriptors: NioFileDescriptorTable = NioFileDescriptorTable(this) val pathResolver: PathResolver = JvmPathResolver(javaFs, fileDescriptors) override fun close() { diff --git a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioGetCurrentWorkingDirectory.kt b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioGetCurrentWorkingDirectory.kt index 6f18b904..d89729d7 100644 --- a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioGetCurrentWorkingDirectory.kt +++ b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioGetCurrentWorkingDirectory.kt @@ -8,13 +8,14 @@ package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.nio import arrow.core.Either import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.GetCurrentWorkingDirectoryError +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.FileSystemOperationHandler import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.nio.cwd.CurrentDirectoryProvider import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.cwd.GetCurrentWorkingDirectory import java.nio.file.Path internal class NioGetCurrentWorkingDirectory( private val currentDirectoryProvider: CurrentDirectoryProvider, -) : NioOperationHandler { +) : FileSystemOperationHandler { override fun invoke(input: GetCurrentWorkingDirectory): Either { return currentDirectoryProvider.getCurrentWorkingDirectory().map(Path::toString) } diff --git a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioMkdir.kt b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioMkdir.kt index ca456375..86d7ae83 100644 --- a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioMkdir.kt +++ b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioMkdir.kt @@ -15,6 +15,7 @@ import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.MkdirError import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.PermissionDenied import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.ext.asFileAttribute import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.ext.toPosixFilePermissions +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.FileSystemOperationHandler import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.nio.cwd.PathResolver.ResolvePathError import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.nio.cwd.toCommonError import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.mkdir.Mkdir @@ -24,8 +25,8 @@ import java.nio.file.Files import java.nio.file.Path internal class NioMkdir( - private val fsState: JvmFileSystemState, -) : NioOperationHandler { + private val fsState: NioFileSystemState, +) : FileSystemOperationHandler { override fun invoke(input: Mkdir): Either { val path: Path = fsState.pathResolver.resolve(input.path, input.baseDirectory, false) .mapLeft(ResolvePathError::toCommonError) diff --git a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioOpen.kt b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioOpen.kt index 603bf67b..8fe2489f 100644 --- a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioOpen.kt +++ b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioOpen.kt @@ -17,6 +17,7 @@ import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.OpenError import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.PermissionDenied import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.ext.asFileAttribute import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.ext.toPosixFilePermissions +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.FileSystemOperationHandler import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.model.Fd import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.nio.cwd.PathResolver.ResolvePathError import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.nio.cwd.toCommonError @@ -33,8 +34,8 @@ import java.nio.file.StandardOpenOption import kotlin.concurrent.withLock internal class NioOpen( - private val fsState: JvmFileSystemState, -) : NioOperationHandler { + private val fsState: NioFileSystemState, +) : FileSystemOperationHandler { override fun invoke(input: Open): Either = either { val path = fsState.pathResolver.resolve(input.path, input.baseDirectory, false) .mapLeft(ResolvePathError::toCommonError) diff --git a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioReadFd.kt b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioReadFd.kt index 285d5bb1..c13a53f7 100644 --- a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioReadFd.kt +++ b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioReadFd.kt @@ -15,6 +15,7 @@ import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.ext.asByteBuffer import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.ext.readCatching import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.fd.getPosition import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.ChannelPositionError +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.FileSystemOperationHandler import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.Messages.fileDescriptorNotOpenedMessage import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.readwrite.FileSystemByteBuffer import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.readwrite.ReadFd @@ -25,8 +26,8 @@ import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.InvalidArgument import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.IoError as FileSystemOperationIoError internal class NioReadFd( - private val fsState: JvmFileSystemState, -) : NioOperationHandler { + private val fsState: NioFileSystemState, +) : FileSystemOperationHandler { override fun invoke(input: ReadFd): Either = fsState.fsLock.withLock { val channel = fsState.fileDescriptors.get(input.fd) ?: return BadFileDescriptor(fileDescriptorNotOpenedMessage(input.fd)).left() diff --git a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioReadLink.kt b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioReadLink.kt index c6071833..4d1dd9df 100644 --- a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioReadLink.kt +++ b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioReadLink.kt @@ -13,6 +13,7 @@ import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.AccessDenied import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.InvalidArgument import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.IoError import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.ReadLinkError +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.FileSystemOperationHandler import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.nio.cwd.PathResolver.ResolvePathError import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.nio.cwd.toCommonError import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.readlink.ReadLink @@ -21,8 +22,8 @@ import java.nio.file.Files import java.nio.file.NotLinkException internal class NioReadLink( - private val fsState: JvmFileSystemState, -) : NioOperationHandler { + private val fsState: NioFileSystemState, +) : FileSystemOperationHandler { override fun invoke(input: ReadLink): Either { val path: java.nio.file.Path = fsState.pathResolver.resolve(input.path, input.baseDirectory, false) .mapLeft(ResolvePathError::toCommonError) diff --git a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioRemoveAdvisoryLockFd.kt b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioRemoveAdvisoryLockFd.kt index 0c39cb4b..850ea8c2 100644 --- a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioRemoveAdvisoryLockFd.kt +++ b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioRemoveAdvisoryLockFd.kt @@ -16,6 +16,7 @@ import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.IoError import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.fd.NioFileHandle import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.fd.resolveWhencePosition import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.ChannelPositionError +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.FileSystemOperationHandler import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.lock.Advisorylock import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.lock.RemoveAdvisoryLockFd import java.io.IOException @@ -25,8 +26,8 @@ import kotlin.concurrent.withLock import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.InvalidArgument as BaseInvalidArgument internal class NioRemoveAdvisoryLockFd( - private val fsState: JvmFileSystemState, -) : NioOperationHandler { + private val fsState: NioFileSystemState, +) : FileSystemOperationHandler { override fun invoke(input: RemoveAdvisoryLockFd): Either { val channel = fsState.fileDescriptors.get(input.fd) ?: return BadFileDescriptor("File descriptor ${input.fd} is not opened").left() diff --git a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioRunWithRawChannelFd.kt b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioRunWithRawChannelFd.kt index 1d700ef5..ee117e37 100644 --- a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioRunWithRawChannelFd.kt +++ b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioRunWithRawChannelFd.kt @@ -11,13 +11,14 @@ import arrow.core.left import arrow.core.right import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.BadFileDescriptor import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.FileSystemOperationError +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.FileSystemOperationHandler import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.nio.op.RunWithChannelFd import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.Messages.fileDescriptorNotOpenedMessage import kotlin.concurrent.withLock internal class NioRunWithRawChannelFd internal constructor( - private val fsState: JvmFileSystemState, -) : NioOperationHandler, FileSystemOperationError, R> { + private val fsState: NioFileSystemState, +) : FileSystemOperationHandler, FileSystemOperationError, R> { override fun invoke(input: RunWithChannelFd): Either = fsState.fsLock.withLock { val channel = fsState.fileDescriptors.get(input.fd) ?: return input.block(BadFileDescriptor(fileDescriptorNotOpenedMessage(input.fd)).left()) diff --git a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioSeekFd.kt b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioSeekFd.kt index ce670092..ce170b91 100644 --- a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioSeekFd.kt +++ b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioSeekFd.kt @@ -15,11 +15,12 @@ import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.SeekError import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.fd.resolveWhencePosition import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.fd.setPosition import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.ChannelPositionError +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.FileSystemOperationHandler import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.seek.SeekFd internal class NioSeekFd( - private val fsState: JvmFileSystemState, -) : NioOperationHandler { + private val fsState: NioFileSystemState, +) : FileSystemOperationHandler { override fun invoke(input: SeekFd): Either { val channel = fsState.fileDescriptors.get(input.fd) ?: return BadFileDescriptor("File descriptor `${input.fd}` is not opened").left() diff --git a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioSetTimestamp.kt b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioSetTimestamp.kt index 0b2be936..d073bd00 100644 --- a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioSetTimestamp.kt +++ b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioSetTimestamp.kt @@ -12,6 +12,7 @@ import arrow.core.left import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.IoError import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.SetTimestampError import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.ext.asLinkOptions +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.FileSystemOperationHandler import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.nio.cwd.PathResolver.ResolvePathError import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.nio.cwd.toCommonError import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.settimestamp.SetTimestamp @@ -23,8 +24,8 @@ import java.util.concurrent.TimeUnit.NANOSECONDS import kotlin.io.path.fileAttributesView internal class NioSetTimestamp( - private val fsState: JvmFileSystemState, -) : NioOperationHandler { + private val fsState: NioFileSystemState, +) : FileSystemOperationHandler { override fun invoke(input: SetTimestamp): Either { val path: Path = fsState.pathResolver.resolve(input.path, input.baseDirectory, false) .mapLeft(ResolvePathError::toCommonError) diff --git a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioSetTimestampFd.kt b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioSetTimestampFd.kt index e4155345..388fba61 100644 --- a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioSetTimestampFd.kt +++ b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioSetTimestampFd.kt @@ -10,13 +10,14 @@ import arrow.core.Either import arrow.core.left import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.BadFileDescriptor import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.SetTimestampError +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.FileSystemOperationHandler import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.nio.NioSetTimestamp.Companion.setTimestamp import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.settimestamp.SetTimestampFd import java.nio.file.Path internal class NioSetTimestampFd( - private val fsState: JvmFileSystemState, -) : NioOperationHandler { + private val fsState: NioFileSystemState, +) : FileSystemOperationHandler { override fun invoke(input: SetTimestampFd): Either { val path: Path = fsState.fileDescriptors.get(input.fd)?.path ?: return BadFileDescriptor("File descriptor `${input.fd}` is not opened").left() diff --git a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioStat.kt b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioStat.kt index d9e8ce3d..486fd2e1 100644 --- a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioStat.kt +++ b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioStat.kt @@ -16,6 +16,7 @@ import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.IoError import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.NoEntry import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.StatError import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.ext.asLinkOptions +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.FileSystemOperationHandler import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.nio.cwd.PathResolver.ResolvePathError import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.stat.FileModeType import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.stat.Stat @@ -30,8 +31,8 @@ import kotlin.io.path.readAttributes import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.NotDirectory as BaseNotDirectory internal class NioStat( - private val fsState: JvmFileSystemState, -) : NioOperationHandler { + private val fsState: NioFileSystemState, +) : FileSystemOperationHandler { override fun invoke(input: Stat): Either { val path: Path = fsState.pathResolver.resolve(input.path, input.baseDirectory, false) .mapLeft { it.toStatError() } diff --git a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioStatFd.kt b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioStatFd.kt index 326c1700..c7ba55f5 100644 --- a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioStatFd.kt +++ b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioStatFd.kt @@ -10,14 +10,15 @@ import arrow.core.Either import arrow.core.left import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.BadFileDescriptor import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.StatError +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.FileSystemOperationHandler import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.nio.NioStat.Companion.statCatching import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.stat.StatFd import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.stat.StructStat import java.nio.file.Path internal class NioStatFd( - private val fsState: JvmFileSystemState, -) : NioOperationHandler { + private val fsState: NioFileSystemState, +) : FileSystemOperationHandler { override fun invoke(input: StatFd): Either { val path: Path = fsState.fileDescriptors.get(input.fd)?.path ?: return BadFileDescriptor("File descriptor `${input.fd}` is not opened").left() diff --git a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioSync.kt b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioSync.kt index c199785d..01c3d090 100644 --- a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioSync.kt +++ b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioSync.kt @@ -11,14 +11,15 @@ import arrow.core.left import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.BadFileDescriptor import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.IoError import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.SyncError +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.FileSystemOperationHandler import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.Messages.fileDescriptorNotOpenedMessage import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.sync.SyncFd import java.io.IOException import java.nio.channels.ClosedChannelException internal class NioSync( - private val fsState: JvmFileSystemState, -) : NioOperationHandler { + private val fsState: NioFileSystemState, +) : FileSystemOperationHandler { override fun invoke(input: SyncFd): Either { val channel = fsState.fileDescriptors.get(input.fd) ?: return BadFileDescriptor(fileDescriptorNotOpenedMessage(input.fd)).left() diff --git a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioTruncateFd.kt b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioTruncateFd.kt index 5f925f17..79e2f221 100644 --- a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioTruncateFd.kt +++ b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioTruncateFd.kt @@ -12,6 +12,7 @@ import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.BadFileDescripto import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.InvalidArgument import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.IoError import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.TruncateError +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.FileSystemOperationHandler import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.model.Fd import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.Messages.fileDescriptorNotOpenedMessage import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.truncate.TruncateFd @@ -20,8 +21,8 @@ import java.nio.channels.ClosedChannelException import java.nio.channels.NonReadableChannelException internal class NioTruncateFd( - private val fsState: JvmFileSystemState, -) : NioOperationHandler { + private val fsState: NioFileSystemState, +) : FileSystemOperationHandler { override fun invoke(input: TruncateFd): Either { val channel = fsState.fileDescriptors.get(input.fd) ?: return BadFileDescriptor(fileDescriptorNotOpenedMessage(input.fd)).left() diff --git a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioUnlinkDirectory.kt b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioUnlinkDirectory.kt index 268e5ec6..4790363e 100644 --- a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioUnlinkDirectory.kt +++ b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioUnlinkDirectory.kt @@ -11,6 +11,7 @@ import arrow.core.getOrElse import arrow.core.left import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.NotDirectory import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.UnlinkError +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.FileSystemOperationHandler import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.nio.NioUnlinkFile.Companion.toUnlinkError import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.nio.cwd.PathResolver import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.unlink.UnlinkFile @@ -20,7 +21,7 @@ import kotlin.io.path.isDirectory internal class NioUnlinkDirectory( private val pathResolver: PathResolver, -) : NioOperationHandler { +) : FileSystemOperationHandler { @Suppress("ReturnCount") override fun invoke(input: UnlinkFile): Either { val path: Path = pathResolver.resolve(input.path, input.baseDirectory, false) diff --git a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioUnlinkFile.kt b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioUnlinkFile.kt index efaaacfd..ee2d8156 100644 --- a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioUnlinkFile.kt +++ b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioUnlinkFile.kt @@ -15,6 +15,7 @@ import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.IoError import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.NoEntry import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.PathIsDirectory import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.UnlinkError +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.FileSystemOperationHandler import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.nio.cwd.PathResolver import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.nio.cwd.PathResolver.ResolvePathError import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.unlink.UnlinkFile @@ -28,7 +29,7 @@ import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.NotDirectory as internal class NioUnlinkFile( private val pathResolver: PathResolver, -) : NioOperationHandler { +) : FileSystemOperationHandler { @Suppress("ReturnCount") override fun invoke(input: UnlinkFile): Either { val path: Path = pathResolver.resolve(input.path, input.baseDirectory, false) diff --git a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioWriteFd.kt b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioWriteFd.kt index 82e19eae..56d1e8c0 100644 --- a/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioWriteFd.kt +++ b/wasi-emscripten-fs/src/jvmMain/kotlin/nio/NioWriteFd.kt @@ -16,6 +16,7 @@ import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.ext.writeCatching import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.fd.NioFileHandle import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.fd.getPosition import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.ChannelPositionError +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.FileSystemOperationHandler import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.Messages.fileDescriptorNotOpenedMessage import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.readwrite.FileSystemByteBuffer import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.readwrite.ReadWriteStrategy.CHANGE_POSITION @@ -26,8 +27,8 @@ import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.InvalidArgument import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.IoError as BaseIoError internal class NioWriteFd( - private val fsState: JvmFileSystemState, -) : NioOperationHandler { + private val fsState: NioFileSystemState, +) : FileSystemOperationHandler { override fun invoke(input: WriteFd): Either = fsState.fsLock.withLock { val channel = fsState.fileDescriptors.get(input.fd) ?: return BadFileDescriptor(fileDescriptorNotOpenedMessage(input.fd)).left() diff --git a/wasi-emscripten-fs/src/linuxArm64Main/kotlin/posix/LinuxStat.linuxArm64.kt b/wasi-emscripten-fs/src/linuxArm64Main/kotlin/linux/LinuxStat.linuxArm64.kt similarity index 92% rename from wasi-emscripten-fs/src/linuxArm64Main/kotlin/posix/LinuxStat.linuxArm64.kt rename to wasi-emscripten-fs/src/linuxArm64Main/kotlin/linux/LinuxStat.linuxArm64.kt index 3845b342..919aedc3 100644 --- a/wasi-emscripten-fs/src/linuxArm64Main/kotlin/posix/LinuxStat.linuxArm64.kt +++ b/wasi-emscripten-fs/src/linuxArm64Main/kotlin/linux/LinuxStat.linuxArm64.kt @@ -4,7 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix +package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.linux import arrow.core.Either import arrow.core.left @@ -14,9 +14,9 @@ import kotlinx.cinterop.memScoped import kotlinx.cinterop.ptr import platform.posix.errno import platform.posix.stat +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.linux.ext.toStructTimespec import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.stat.FileModeType import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.stat.StructStat -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.ext.toStructTimespec import ru.pixnews.wasm.sqlite.open.helper.host.platform.linux.fstatat internal actual fun platformFstatat( diff --git a/wasi-emscripten-fs/src/linuxArm64Main/kotlin/posix/LinuxStatFd.linuxArm64.kt b/wasi-emscripten-fs/src/linuxArm64Main/kotlin/linux/LinuxStatFd.linuxArm64.kt similarity index 93% rename from wasi-emscripten-fs/src/linuxArm64Main/kotlin/posix/LinuxStatFd.linuxArm64.kt rename to wasi-emscripten-fs/src/linuxArm64Main/kotlin/linux/LinuxStatFd.linuxArm64.kt index df87e5d5..344abf82 100644 --- a/wasi-emscripten-fs/src/linuxArm64Main/kotlin/posix/LinuxStatFd.linuxArm64.kt +++ b/wasi-emscripten-fs/src/linuxArm64Main/kotlin/linux/LinuxStatFd.linuxArm64.kt @@ -4,7 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix +package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.linux import arrow.core.Either import arrow.core.left diff --git a/wasi-emscripten-fs/src/linuxMain/kotlin/LinuxFileSystem.kt b/wasi-emscripten-fs/src/linuxMain/kotlin/LinuxFileSystem.kt index 50a41a49..9a9b566c 100644 --- a/wasi-emscripten-fs/src/linuxMain/kotlin/LinuxFileSystem.kt +++ b/wasi-emscripten-fs/src/linuxMain/kotlin/LinuxFileSystem.kt @@ -6,101 +6,16 @@ package ru.pixnews.wasm.sqlite.open.helper.host.filesystem -import arrow.core.Either -import ru.pixnews.wasm.sqlite.open.helper.common.api.Logger -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.FileSystemOperationError -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.FileSystemOperation -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.checkaccess.CheckAccess -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.chmod.Chmod -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.chmod.ChmodFd -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.chown.Chown -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.chown.ChownFd -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.close.CloseFd -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.cwd.GetCurrentWorkingDirectory -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.lock.AddAdvisoryLockFd -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.lock.RemoveAdvisoryLockFd -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.mkdir.Mkdir -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.opencreate.Open -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.readlink.ReadLink -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.readwrite.ReadFd -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.readwrite.WriteFd -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.seek.SeekFd -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.settimestamp.SetTimestamp -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.settimestamp.SetTimestampFd -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.stat.Stat -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.stat.StatFd -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.sync.SyncFd -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.truncate.TruncateFd -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.unlink.UnlinkDirectory -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.unlink.UnlinkFile -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.LinuxAddAdvisoryLockFd -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.LinuxCheckAccess -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.LinuxChmod -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.LinuxChmodFd -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.LinuxChown -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.LinuxChownFd -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.LinuxGetCurrentWorkingDirectory -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.LinuxMkdir -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.LinuxOpen -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.LinuxReadFd -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.LinuxReadLink -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.LinuxRemoveAdvisoryLockFd -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.LinuxSeekFd -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.LinuxSetTimestamp -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.LinuxSetTimestampFd -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.LinuxStat -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.LinuxStatFd -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.LinuxSync -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.LinuxTruncateFd -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.LinuxUnlinkDirectory -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.LinuxUnlinkFile -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.LinuxWriteFd -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.PosixCloseFd -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.base.BaseFileSystemAdapter -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.base.PosixFileSystemState -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.base.PosixOperationHandler +import ru.pixnews.wasm.sqlite.open.helper.common.api.InternalWasmSqliteHelperApi +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.dsl.FileSystemCommonConfig +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.linux.LinuxFileSystemImpl -public class LinuxFileSystem( - rootLogger: Logger = Logger, -) : FileSystem { - private val fsState = PosixFileSystemState(rootLogger) - private val operations: Map, PosixOperationHandler<*, *, *>> = mapOf( - Open to LinuxOpen(fsState), - CloseFd to PosixCloseFd(fsState), - AddAdvisoryLockFd to LinuxAddAdvisoryLockFd, - RemoveAdvisoryLockFd to LinuxRemoveAdvisoryLockFd, - CheckAccess to LinuxCheckAccess, - Chmod to LinuxChmod, - ChmodFd to LinuxChmodFd, - Chown to LinuxChown, - ChownFd to LinuxChownFd, - GetCurrentWorkingDirectory to LinuxGetCurrentWorkingDirectory, - Mkdir to LinuxMkdir, - ReadFd to LinuxReadFd, - ReadLink to LinuxReadLink, - SeekFd to LinuxSeekFd, - SetTimestamp to LinuxSetTimestamp, - SetTimestampFd to LinuxSetTimestampFd, - Stat to LinuxStat, - StatFd to LinuxStatFd, - SyncFd to LinuxSync, - TruncateFd to LinuxTruncateFd, - UnlinkFile to LinuxUnlinkFile, - UnlinkDirectory to LinuxUnlinkDirectory, - WriteFd to LinuxWriteFd, +public object LinuxFileSystem : FileSystemEngine { + @InternalWasmSqliteHelperApi + override fun create( + commonConfig: FileSystemCommonConfig, + engineConfig: Nothing.() -> Unit, + ): FileSystem = LinuxFileSystemImpl( + interceptors = commonConfig.interceptors, ) - private val fsAdapter = BaseFileSystemAdapter(operations) - - override fun close() { - fsState.close() - } - - override fun execute( - operation: FileSystemOperation, - input: I, - ): Either = fsAdapter.execute(operation, input) - - override fun isOperationSupported(operation: FileSystemOperation<*, *, *>): Boolean { - return fsAdapter.isOperationSupported(operation) - } } diff --git a/wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxAddAdvisoryLockFd.kt b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxAddAdvisoryLockFd.kt similarity index 92% rename from wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxAddAdvisoryLockFd.kt rename to wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxAddAdvisoryLockFd.kt index ba8e38ed..007b5777 100644 --- a/wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxAddAdvisoryLockFd.kt +++ b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxAddAdvisoryLockFd.kt @@ -4,7 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix +package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.linux import arrow.core.Either import arrow.core.left @@ -29,16 +29,16 @@ import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.BadFileDescripto import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.Interrupted import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.InvalidArgument import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.NoLock +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.FileSystemOperationHandler import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.model.Fd import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.lock.AddAdvisoryLockFd import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.lock.Advisorylock import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.lock.AdvisorylockLockType import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.lock.AdvisorylockLockType.READ import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.lock.AdvisorylockLockType.WRITE -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.base.PosixOperationHandler import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.ext.toPosixWhence -internal object LinuxAddAdvisoryLockFd : PosixOperationHandler { +internal object LinuxAddAdvisoryLockFd : FileSystemOperationHandler { override fun invoke(input: AddAdvisoryLockFd): Either = memScoped { val structFlockInstance: flock = alloc { setFromAdvisoryLock(input.flock) diff --git a/wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxCheckAccess.kt b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxCheckAccess.kt similarity index 93% rename from wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxCheckAccess.kt rename to wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxCheckAccess.kt index ec656eb8..c4779d61 100644 --- a/wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxCheckAccess.kt +++ b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxCheckAccess.kt @@ -4,7 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix +package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.linux import arrow.core.Either import arrow.core.left @@ -38,19 +38,19 @@ import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.NotDirectory import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.ReadOnlyFileSystem import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.TextFileBusy import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.TooManySymbolicLinks +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.FileSystemOperationHandler +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.linux.ext.toDirFd import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.checkaccess.CheckAccess import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.checkaccess.FileAccessibilityCheck import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.checkaccess.FileAccessibilityCheck.EXECUTABLE import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.checkaccess.FileAccessibilityCheck.READABLE import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.checkaccess.FileAccessibilityCheck.WRITEABLE -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.base.PosixOperationHandler -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.ext.toDirFd import ru.pixnews.wasm.sqlite.open.helper.host.platform.linux.AT_EACCESS import ru.pixnews.wasm.sqlite.open.helper.host.platform.linux.AT_EMPTY_PATH import ru.pixnews.wasm.sqlite.open.helper.host.platform.linux.AT_SYMLINK_NOFOLLOW import ru.pixnews.wasm.sqlite.open.helper.host.platform.linux.SYS_faccessat2 -internal object LinuxCheckAccess : PosixOperationHandler { +internal object LinuxCheckAccess : FileSystemOperationHandler { override fun invoke(input: CheckAccess): Either { val resultCode = syscall( SYS_faccessat2.toLong(), diff --git a/wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxChmod.kt b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxChmod.kt similarity index 91% rename from wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxChmod.kt rename to wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxChmod.kt index 8d6f6a4c..d5b25884 100644 --- a/wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxChmod.kt +++ b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxChmod.kt @@ -4,7 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix +package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.linux import arrow.core.Either import arrow.core.left @@ -33,13 +33,13 @@ import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.NotDirectory import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.NotSupported import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.ReadOnlyFileSystem import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.TooManySymbolicLinks +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.FileSystemOperationHandler +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.linux.ext.toDirFd import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.chmod.Chmod -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.base.PosixOperationHandler -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.ext.toDirFd import ru.pixnews.wasm.sqlite.open.helper.host.platform.linux.AT_SYMLINK_NOFOLLOW import ru.pixnews.wasm.sqlite.open.helper.host.platform.linux.fchmodat -internal object LinuxChmod : PosixOperationHandler { +internal object LinuxChmod : FileSystemOperationHandler { override fun invoke(input: Chmod): Either { val resultCode = fchmodat( input.baseDirectory.toDirFd(), diff --git a/wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxChmodFd.kt b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxChmodFd.kt similarity index 92% rename from wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxChmodFd.kt rename to wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxChmodFd.kt index e2c33565..2ff5a261 100644 --- a/wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxChmodFd.kt +++ b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxChmodFd.kt @@ -4,7 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix +package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.linux import arrow.core.Either import arrow.core.left @@ -34,10 +34,10 @@ import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.NotDirectory import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.NotSupported import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.ReadOnlyFileSystem import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.TooManySymbolicLinks +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.FileSystemOperationHandler import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.chmod.ChmodFd -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.base.PosixOperationHandler -internal object LinuxChmodFd : PosixOperationHandler { +internal object LinuxChmodFd : FileSystemOperationHandler { override fun invoke(input: ChmodFd): Either { val resultCode = fchmod(input.fd.fd, input.mode.mask) return if (resultCode == 0) { diff --git a/wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxChown.kt b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxChown.kt similarity index 91% rename from wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxChown.kt rename to wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxChown.kt index 89c5e493..4c689c69 100644 --- a/wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxChown.kt +++ b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxChown.kt @@ -4,7 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix +package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.linux import arrow.core.Either import arrow.core.left @@ -31,13 +31,13 @@ import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.NoEntry import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.NotDirectory import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.ReadOnlyFileSystem import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.TooManySymbolicLinks +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.FileSystemOperationHandler +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.linux.ext.toDirFd import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.chown.Chown -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.base.PosixOperationHandler -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.ext.toDirFd import ru.pixnews.wasm.sqlite.open.helper.host.platform.linux.AT_SYMLINK_NOFOLLOW import ru.pixnews.wasm.sqlite.open.helper.host.platform.linux.fchownat -internal object LinuxChown : PosixOperationHandler { +internal object LinuxChown : FileSystemOperationHandler { override fun invoke(input: Chown): Either { val resultCode = fchownat( input.baseDirectory.toDirFd(), diff --git a/wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxChownFd.kt b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxChownFd.kt similarity index 91% rename from wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxChownFd.kt rename to wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxChownFd.kt index a6b8d144..1d5b24ce 100644 --- a/wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxChownFd.kt +++ b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxChownFd.kt @@ -4,7 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix +package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.linux import arrow.core.Either import arrow.core.left @@ -32,10 +32,10 @@ import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.NoEntry import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.NotDirectory import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.ReadOnlyFileSystem import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.TooManySymbolicLinks +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.FileSystemOperationHandler import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.chown.ChownFd -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.base.PosixOperationHandler -internal object LinuxChownFd : PosixOperationHandler { +internal object LinuxChownFd : FileSystemOperationHandler { override fun invoke(input: ChownFd): Either { val resultCode = fchown(input.fd.fd, input.owner.toUInt(), input.group.toUInt()) return if (resultCode == 0) { diff --git a/wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxFileSystemImpl.kt b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxFileSystemImpl.kt new file mode 100644 index 00000000..aa6cc3fa --- /dev/null +++ b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxFileSystemImpl.kt @@ -0,0 +1,85 @@ +/* + * Copyright 2024, the wasm-sqlite-open-helper project authors and contributors. Please see the AUTHORS file + * for details. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. + * SPDX-License-Identifier: Apache-2.0 + */ + +package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.linux + +import arrow.core.Either +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.FileSystem +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.FileSystemInterceptor +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.FileSystemOperationError +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.DelegateOperationsFileSystem +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.FileSystemOperationHandler +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.FileSystemOperation +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.checkaccess.CheckAccess +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.chmod.Chmod +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.chmod.ChmodFd +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.chown.Chown +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.chown.ChownFd +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.close.CloseFd +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.cwd.GetCurrentWorkingDirectory +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.lock.AddAdvisoryLockFd +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.lock.RemoveAdvisoryLockFd +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.mkdir.Mkdir +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.opencreate.Open +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.readlink.ReadLink +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.readwrite.ReadFd +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.readwrite.WriteFd +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.seek.SeekFd +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.settimestamp.SetTimestamp +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.settimestamp.SetTimestampFd +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.stat.Stat +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.stat.StatFd +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.sync.SyncFd +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.truncate.TruncateFd +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.unlink.UnlinkDirectory +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.unlink.UnlinkFile +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.PosixCloseFd +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.base.PosixFileSystemState + +public class LinuxFileSystemImpl( + interceptors: List, +) : FileSystem { + private val fsState = PosixFileSystemState() + private val operations: Map, FileSystemOperationHandler<*, *, *>> = mapOf( + Open to LinuxOpen(fsState), + CloseFd to PosixCloseFd(fsState), + AddAdvisoryLockFd to LinuxAddAdvisoryLockFd, + RemoveAdvisoryLockFd to LinuxRemoveAdvisoryLockFd, + CheckAccess to LinuxCheckAccess, + Chmod to LinuxChmod, + ChmodFd to LinuxChmodFd, + Chown to LinuxChown, + ChownFd to LinuxChownFd, + GetCurrentWorkingDirectory to LinuxGetCurrentWorkingDirectory, + Mkdir to LinuxMkdir, + ReadFd to LinuxReadFd, + ReadLink to LinuxReadLink, + SeekFd to LinuxSeekFd, + SetTimestamp to LinuxSetTimestamp, + SetTimestampFd to LinuxSetTimestampFd, + Stat to LinuxStat, + StatFd to LinuxStatFd, + SyncFd to LinuxSync, + TruncateFd to LinuxTruncateFd, + UnlinkFile to LinuxUnlinkFile, + UnlinkDirectory to LinuxUnlinkDirectory, + WriteFd to LinuxWriteFd, + ) + private val fsAdapter = DelegateOperationsFileSystem(operations, interceptors) + + override fun close() { + fsState.close() + } + + override fun execute( + operation: FileSystemOperation, + input: I, + ): Either = fsAdapter.execute(operation, input) + + override fun isOperationSupported(operation: FileSystemOperation<*, *, *>): Boolean { + return fsAdapter.isOperationSupported(operation) + } +} diff --git a/wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxGetCurrentWorkingDirectory.kt b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxGetCurrentWorkingDirectory.kt similarity index 88% rename from wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxGetCurrentWorkingDirectory.kt rename to wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxGetCurrentWorkingDirectory.kt index 8276f8b9..4efefe12 100644 --- a/wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxGetCurrentWorkingDirectory.kt +++ b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxGetCurrentWorkingDirectory.kt @@ -4,7 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix +package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.linux import arrow.core.Either import arrow.core.left @@ -25,11 +25,11 @@ import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.GetCurrentWorkin import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.InvalidArgument import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.NameTooLong import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.NoEntry +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.FileSystemOperationHandler import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.cwd.GetCurrentWorkingDirectory -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.base.PosixOperationHandler internal object LinuxGetCurrentWorkingDirectory : - PosixOperationHandler { + FileSystemOperationHandler { override fun invoke(input: GetCurrentWorkingDirectory): Either { val byteArray = ByteArray(PATH_MAX) val cwd: CPointer>? = byteArray.usePinned { bytes -> diff --git a/wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxMkdir.kt b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxMkdir.kt similarity index 91% rename from wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxMkdir.kt rename to wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxMkdir.kt index 0e53a670..99b78298 100644 --- a/wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxMkdir.kt +++ b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxMkdir.kt @@ -4,7 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix +package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.linux import arrow.core.Either import arrow.core.left @@ -36,12 +36,12 @@ import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.NoSpace import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.NotDirectory import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.ReadOnlyFileSystem import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.TooManySymbolicLinks +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.FileSystemOperationHandler +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.linux.ext.toDirFd import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.mkdir.Mkdir -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.base.PosixOperationHandler -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.ext.toDirFd import ru.pixnews.wasm.sqlite.open.helper.host.platform.linux.mkdirat -internal object LinuxMkdir : PosixOperationHandler { +internal object LinuxMkdir : FileSystemOperationHandler { override fun invoke(input: Mkdir): Either { val resultCode = mkdirat( input.baseDirectory.toDirFd(), diff --git a/wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxOpen.kt b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxOpen.kt similarity index 95% rename from wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxOpen.kt rename to wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxOpen.kt index 18965812..d661eb73 100644 --- a/wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxOpen.kt +++ b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxOpen.kt @@ -4,7 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix +package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.linux import arrow.core.Either import arrow.core.left @@ -27,14 +27,14 @@ import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.InvalidArgument import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.NotCapable import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.OpenError import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.TooManySymbolicLinks +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.FileSystemOperationHandler +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.linux.ext.toDirFd import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.model.Fd import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.opencreate.Open import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.opencreate.OpenFileFlags import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.opencreate.OpenFileFlags.OpenFileFlag import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.opencreate.OpenFileFlags.OpenFileFlag.O_ACCMODE import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.base.PosixFileSystemState -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.base.PosixOperationHandler -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.ext.toDirFd import ru.pixnews.wasm.sqlite.open.helper.host.platform.linux.RESOLVE_BENEATH import ru.pixnews.wasm.sqlite.open.helper.host.platform.linux.RESOLVE_CACHED import ru.pixnews.wasm.sqlite.open.helper.host.platform.linux.RESOLVE_IN_ROOT @@ -46,7 +46,7 @@ import ru.pixnews.wasm.sqlite.open.helper.host.platform.linux.open_how internal class LinuxOpen( private val state: PosixFileSystemState, -) : PosixOperationHandler { +) : FileSystemOperationHandler { override fun invoke(input: Open): Either { val errorOrFd = memScoped { val openHow: open_how = alloc { diff --git a/wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxReadFd.kt b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxReadFd.kt similarity index 95% rename from wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxReadFd.kt rename to wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxReadFd.kt index 80d83721..f42e416b 100644 --- a/wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxReadFd.kt +++ b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxReadFd.kt @@ -4,7 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix +package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.linux import arrow.core.Either import arrow.core.left @@ -37,14 +37,14 @@ import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.NotSupported import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.Nxio import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.PathIsDirectory import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.ReadError +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.FileSystemOperationHandler import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.model.Fd import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.readwrite.FileSystemByteBuffer import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.readwrite.ReadFd import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.readwrite.ReadWriteStrategy.CHANGE_POSITION import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.readwrite.ReadWriteStrategy.DO_NOT_CHANGE_POSITION -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.base.PosixOperationHandler -internal object LinuxReadFd : PosixOperationHandler { +internal object LinuxReadFd : FileSystemOperationHandler { override fun invoke(input: ReadFd): Either { return when (input.strategy) { CHANGE_POSITION -> callReadWrite(input.fd, input.iovecs) { fd, iovecs, size -> diff --git a/wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxReadLink.kt b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxReadLink.kt similarity index 93% rename from wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxReadLink.kt rename to wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxReadLink.kt index 1aaaf548..20a584a6 100644 --- a/wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxReadLink.kt +++ b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxReadLink.kt @@ -4,7 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix +package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.linux import arrow.core.Either import arrow.core.getOrElse @@ -36,14 +36,14 @@ import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.NoEntry import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.NotDirectory import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.ReadLinkError import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.TooManySymbolicLinks +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.FileSystemOperationHandler +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.linux.ext.toDirFd import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.readlink.ReadLink -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.base.PosixOperationHandler -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.ext.toDirFd import ru.pixnews.wasm.sqlite.open.helper.host.platform.linux.AT_EMPTY_PATH import ru.pixnews.wasm.sqlite.open.helper.host.platform.linux.fstatat import ru.pixnews.wasm.sqlite.open.helper.host.platform.linux.readlinkat -internal object LinuxReadLink : PosixOperationHandler { +internal object LinuxReadLink : FileSystemOperationHandler { private const val PATH_STEP = 1024 private val MAX_PATH_SIZE = maxOf(1024 * 1024, PATH_MAX) diff --git a/wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxRemoveAdvisoryLockFd.kt b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxRemoveAdvisoryLockFd.kt similarity index 76% rename from wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxRemoveAdvisoryLockFd.kt rename to wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxRemoveAdvisoryLockFd.kt index 9cee5e1d..c5fb6788 100644 --- a/wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxRemoveAdvisoryLockFd.kt +++ b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxRemoveAdvisoryLockFd.kt @@ -4,7 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix +package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.linux import arrow.core.Either import arrow.core.left @@ -17,12 +17,12 @@ import platform.posix.errno import platform.posix.fcntl import platform.posix.flock import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.AdvisoryLockError +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.FileSystemOperationHandler +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.linux.LinuxAddAdvisoryLockFd.errnoToAdvisoryLockError +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.linux.LinuxAddAdvisoryLockFd.setFromAdvisoryLock import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.lock.RemoveAdvisoryLockFd -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.LinuxAddAdvisoryLockFd.errnoToAdvisoryLockError -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.LinuxAddAdvisoryLockFd.setFromAdvisoryLock -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.base.PosixOperationHandler -internal object LinuxRemoveAdvisoryLockFd : PosixOperationHandler { +internal object LinuxRemoveAdvisoryLockFd : FileSystemOperationHandler { override fun invoke(input: RemoveAdvisoryLockFd): Either = memScoped { val structFlockInstance: flock = alloc { setFromAdvisoryLock(input.flock) diff --git a/wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxSeekFd.kt b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxSeekFd.kt similarity index 82% rename from wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxSeekFd.kt rename to wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxSeekFd.kt index 484bc960..1d3c7e9f 100644 --- a/wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxSeekFd.kt +++ b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxSeekFd.kt @@ -4,7 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix +package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.linux import arrow.core.Either import arrow.core.left @@ -13,12 +13,12 @@ import platform.posix.errno import platform.posix.lseek import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.Overflow import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.SeekError +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.FileSystemOperationHandler import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.seek.SeekFd -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.base.PosixOperationHandler import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.ext.errnoToSeekError import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.ext.toPosixWhence -internal object LinuxSeekFd : PosixOperationHandler { +internal object LinuxSeekFd : FileSystemOperationHandler { override fun invoke(input: SeekFd): Either { if (input.fileDelta > Int.MAX_VALUE) { return Overflow("input.fileDelta too big. Request: $input").left() diff --git a/wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxSetTimestamp.kt b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxSetTimestamp.kt similarity index 92% rename from wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxSetTimestamp.kt rename to wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxSetTimestamp.kt index 63b8b0ed..7bffffde 100644 --- a/wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxSetTimestamp.kt +++ b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxSetTimestamp.kt @@ -4,7 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix +package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.linux import arrow.core.Either import arrow.core.left @@ -37,14 +37,14 @@ import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.PermissionDenied import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.ReadOnlyFileSystem import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.SetTimestampError import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.TooManySymbolicLinks +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.FileSystemOperationHandler +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.linux.ext.toDirFd import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.settimestamp.SetTimestamp -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.base.PosixOperationHandler -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.ext.toDirFd import ru.pixnews.wasm.sqlite.open.helper.host.platform.linux.AT_SYMLINK_NOFOLLOW import ru.pixnews.wasm.sqlite.open.helper.host.platform.linux.UTIME_OMIT import ru.pixnews.wasm.sqlite.open.helper.host.platform.linux.utimensat -internal object LinuxSetTimestamp : PosixOperationHandler { +internal object LinuxSetTimestamp : FileSystemOperationHandler { override fun invoke(input: SetTimestamp): Either = memScoped { val timespec: CArrayPointer = allocArray(2) timespec[0].set(input.atimeNanoseconds) diff --git a/wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxSetTimestampFd.kt b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxSetTimestampFd.kt similarity index 90% rename from wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxSetTimestampFd.kt rename to wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxSetTimestampFd.kt index 253afe2d..e327d0b0 100644 --- a/wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxSetTimestampFd.kt +++ b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxSetTimestampFd.kt @@ -4,7 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix +package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.linux import arrow.core.Either import arrow.core.left @@ -27,10 +27,10 @@ import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.IoError import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.PermissionDenied import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.ReadOnlyFileSystem import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.SetTimestampError +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.FileSystemOperationHandler import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.settimestamp.SetTimestampFd -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.base.PosixOperationHandler -internal object LinuxSetTimestampFd : PosixOperationHandler { +internal object LinuxSetTimestampFd : FileSystemOperationHandler { override fun invoke(input: SetTimestampFd): Either = memScoped { val timespec: CPointer = allocArray(2) timespec[0].set(input.atimeNanoseconds) diff --git a/wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxStat.kt b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxStat.kt similarity index 89% rename from wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxStat.kt rename to wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxStat.kt index 858e5369..09a782b4 100644 --- a/wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxStat.kt +++ b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxStat.kt @@ -4,7 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix +package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.linux import arrow.core.Either import platform.posix.EACCES @@ -24,10 +24,10 @@ import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.NoEntry import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.NotDirectory import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.StatError import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.TooManySymbolicLinks +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.FileSystemOperationHandler +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.linux.ext.toDirFd import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.stat.Stat import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.stat.StructStat -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.base.PosixOperationHandler -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.ext.toDirFd import ru.pixnews.wasm.sqlite.open.helper.host.platform.linux.AT_SYMLINK_NOFOLLOW internal expect fun platformFstatat( @@ -36,7 +36,7 @@ internal expect fun platformFstatat( statFlags: Int, ): Either -internal object LinuxStat : PosixOperationHandler { +internal object LinuxStat : FileSystemOperationHandler { override fun invoke(input: Stat): Either { return platformFstatat( input.baseDirectory.toDirFd(), diff --git a/wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxStatFd.kt b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxStatFd.kt similarity index 85% rename from wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxStatFd.kt rename to wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxStatFd.kt index a9defd15..40376f65 100644 --- a/wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxStatFd.kt +++ b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxStatFd.kt @@ -4,7 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix +package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.linux import arrow.core.Either import platform.posix.EACCES @@ -15,13 +15,13 @@ import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.BadFileDescripto import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.InvalidArgument import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.IoError import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.StatError +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.FileSystemOperationHandler import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.stat.StatFd import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.stat.StructStat -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.base.PosixOperationHandler internal expect fun platformFstatFd(fd: Int): Either -internal object LinuxStatFd : PosixOperationHandler { +internal object LinuxStatFd : FileSystemOperationHandler { override fun invoke(input: StatFd): Either { return platformFstatFd(input.fd.fd).mapLeft { it.errnoToStatFdError(input) diff --git a/wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxSync.kt b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxSync.kt similarity index 88% rename from wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxSync.kt rename to wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxSync.kt index caf7d2fb..a549f24f 100644 --- a/wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxSync.kt +++ b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxSync.kt @@ -4,7 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix +package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.linux import arrow.core.Either import arrow.core.left @@ -25,10 +25,10 @@ import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.InvalidArgument import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.IoError import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.NoSpace import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.SyncError +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.FileSystemOperationHandler import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.sync.SyncFd -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.base.PosixOperationHandler -internal object LinuxSync : PosixOperationHandler { +internal object LinuxSync : FileSystemOperationHandler { override fun invoke(input: SyncFd): Either { val resultCode = if (input.syncMetadata) { fsync(input.fd.fd) diff --git a/wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxTruncateFd.kt b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxTruncateFd.kt similarity index 92% rename from wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxTruncateFd.kt rename to wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxTruncateFd.kt index 32dc6071..48b8f4bf 100644 --- a/wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxTruncateFd.kt +++ b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxTruncateFd.kt @@ -4,7 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix +package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.linux import arrow.core.Either import arrow.core.left @@ -37,10 +37,10 @@ import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.PathIsDirectory import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.TextFileBusy import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.TooManySymbolicLinks import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.TruncateError +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.FileSystemOperationHandler import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.truncate.TruncateFd -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.base.PosixOperationHandler -internal object LinuxTruncateFd : PosixOperationHandler { +internal object LinuxTruncateFd : FileSystemOperationHandler { override fun invoke(input: TruncateFd): Either { if (input.length > Long.MAX_VALUE.toULong()) { return FileTooBig("Argument length is large than the maximum file size").left() diff --git a/wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxUnlinkDirectory.kt b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxUnlinkDirectory.kt similarity index 91% rename from wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxUnlinkDirectory.kt rename to wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxUnlinkDirectory.kt index 40ed5e32..751e89e9 100644 --- a/wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxUnlinkDirectory.kt +++ b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxUnlinkDirectory.kt @@ -4,7 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix +package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.linux import arrow.core.Either import arrow.core.left @@ -35,13 +35,13 @@ import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.PathIsDirectory import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.PermissionDenied import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.TooManySymbolicLinks import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.UnlinkError +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.FileSystemOperationHandler +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.linux.ext.toDirFd import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.unlink.UnlinkDirectory -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.base.PosixOperationHandler -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.ext.toDirFd import ru.pixnews.wasm.sqlite.open.helper.host.platform.linux.AT_REMOVEDIR import ru.pixnews.wasm.sqlite.open.helper.host.platform.linux.unlinkat -internal object LinuxUnlinkDirectory : PosixOperationHandler { +internal object LinuxUnlinkDirectory : FileSystemOperationHandler { override fun invoke(input: UnlinkDirectory): Either { val resultCode = unlinkat( input.baseDirectory.toDirFd(), diff --git a/wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxUnlinkFile.kt b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxUnlinkFile.kt similarity index 91% rename from wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxUnlinkFile.kt rename to wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxUnlinkFile.kt index 59c0494b..f0ac4c41 100644 --- a/wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxUnlinkFile.kt +++ b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxUnlinkFile.kt @@ -4,7 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix +package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.linux import arrow.core.Either import arrow.core.left @@ -35,12 +35,12 @@ import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.PathIsDirectory import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.PermissionDenied import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.TooManySymbolicLinks import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.UnlinkError +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.FileSystemOperationHandler +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.linux.ext.toDirFd import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.unlink.UnlinkFile -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.base.PosixOperationHandler -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.ext.toDirFd import ru.pixnews.wasm.sqlite.open.helper.host.platform.linux.unlinkat -internal object LinuxUnlinkFile : PosixOperationHandler { +internal object LinuxUnlinkFile : FileSystemOperationHandler { override fun invoke(input: UnlinkFile): Either { val resultCode = unlinkat( input.baseDirectory.toDirFd(), diff --git a/wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxWriteFd.kt b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxWriteFd.kt similarity index 93% rename from wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxWriteFd.kt rename to wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxWriteFd.kt index a2630e0f..ed743866 100644 --- a/wasi-emscripten-fs/src/linuxMain/kotlin/posix/LinuxWriteFd.kt +++ b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/LinuxWriteFd.kt @@ -4,7 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix +package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.linux import arrow.core.Either import arrow.core.left @@ -38,15 +38,15 @@ import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.Nxio import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.PermissionDenied import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.Pipe import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.WriteError +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.FileSystemOperationHandler +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.linux.LinuxReadFd.callReadWrite import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.model.Fd import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.readwrite.FileSystemByteBuffer import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.readwrite.ReadWriteStrategy.CHANGE_POSITION import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.readwrite.ReadWriteStrategy.DO_NOT_CHANGE_POSITION import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.readwrite.WriteFd -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.LinuxReadFd.callReadWrite -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.base.PosixOperationHandler -internal object LinuxWriteFd : PosixOperationHandler { +internal object LinuxWriteFd : FileSystemOperationHandler { override fun invoke(input: WriteFd): Either { return when (input.strategy) { CHANGE_POSITION -> callReadWrite(input.fd, input.cIovecs) { fd: Fd, iovecs: CPointer, size: Int -> diff --git a/wasi-emscripten-fs/src/linuxMain/kotlin/posix/PosixStatExt.kt b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/PosixStatExt.kt similarity index 94% rename from wasi-emscripten-fs/src/linuxMain/kotlin/posix/PosixStatExt.kt rename to wasi-emscripten-fs/src/linuxMain/kotlin/linux/PosixStatExt.kt index 66d83cf9..f41beb0e 100644 --- a/wasi-emscripten-fs/src/linuxMain/kotlin/posix/PosixStatExt.kt +++ b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/PosixStatExt.kt @@ -4,7 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix +package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.linux import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.stat.FileModeType diff --git a/wasi-emscripten-fs/src/linuxMain/kotlin/posix/ext/BaseDirectoryExt.kt b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/ext/BaseDirectoryExt.kt similarity index 93% rename from wasi-emscripten-fs/src/linuxMain/kotlin/posix/ext/BaseDirectoryExt.kt rename to wasi-emscripten-fs/src/linuxMain/kotlin/linux/ext/BaseDirectoryExt.kt index 6f749dcc..850e93e5 100644 --- a/wasi-emscripten-fs/src/linuxMain/kotlin/posix/ext/BaseDirectoryExt.kt +++ b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/ext/BaseDirectoryExt.kt @@ -4,7 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.ext +package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.linux.ext import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.model.BaseDirectory import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.model.BaseDirectory.CurrentWorkingDirectory diff --git a/wasi-emscripten-fs/src/linuxMain/kotlin/posix/ext/StructTimespecExt.kt b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/ext/StructTimespecExt.kt similarity index 89% rename from wasi-emscripten-fs/src/linuxMain/kotlin/posix/ext/StructTimespecExt.kt rename to wasi-emscripten-fs/src/linuxMain/kotlin/linux/ext/StructTimespecExt.kt index ac76c8c6..4afe6085 100644 --- a/wasi-emscripten-fs/src/linuxMain/kotlin/posix/ext/StructTimespecExt.kt +++ b/wasi-emscripten-fs/src/linuxMain/kotlin/linux/ext/StructTimespecExt.kt @@ -4,7 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.ext +package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.linux.ext import platform.posix.timespec import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.stat.StructTimespec diff --git a/wasi-emscripten-fs/src/linuxTest/kotlin/posix/LinuxOpenTest.kt b/wasi-emscripten-fs/src/linuxTest/kotlin/linux/LinuxOpenTest.kt similarity index 93% rename from wasi-emscripten-fs/src/linuxTest/kotlin/posix/LinuxOpenTest.kt rename to wasi-emscripten-fs/src/linuxTest/kotlin/linux/LinuxOpenTest.kt index ed26b7d1..f5e93f9c 100644 --- a/wasi-emscripten-fs/src/linuxTest/kotlin/posix/LinuxOpenTest.kt +++ b/wasi-emscripten-fs/src/linuxTest/kotlin/linux/LinuxOpenTest.kt @@ -4,7 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix +package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.linux import assertk.all import assertk.assertThat @@ -18,7 +18,6 @@ import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.opencreate.Open import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.opencreate.OpenFileFlags import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.base.PosixFileSystemState import ru.pixnews.wasm.sqlite.test.utils.TempFolder -import ru.pixnews.wasm.sqlite.test.utils.TestLogger import ru.pixnews.wasm.sqlite.test.utils.absolutePath import ru.pixnews.wasm.sqlite.test.utils.assertions.FileModeBit import ru.pixnews.wasm.sqlite.test.utils.assertions.fileMode @@ -43,7 +42,7 @@ class LinuxOpenTest { @Test fun create_file_with_absolute_path_should_work() { - val fsState = PosixFileSystemState(TestLogger()) + val fsState = PosixFileSystemState() val linuxOpen = LinuxOpen(fsState) val tempFileAbsolutePath = tempFolder.absolutePath() diff --git a/wasi-emscripten-fs/src/linuxTest/kotlin/posix/LinuxOpenTestToFlagsMask.kt b/wasi-emscripten-fs/src/linuxTest/kotlin/linux/LinuxOpenTestToFlagsMask.kt similarity index 91% rename from wasi-emscripten-fs/src/linuxTest/kotlin/posix/LinuxOpenTestToFlagsMask.kt rename to wasi-emscripten-fs/src/linuxTest/kotlin/linux/LinuxOpenTestToFlagsMask.kt index 565043f2..effb26b2 100644 --- a/wasi-emscripten-fs/src/linuxTest/kotlin/posix/LinuxOpenTestToFlagsMask.kt +++ b/wasi-emscripten-fs/src/linuxTest/kotlin/linux/LinuxOpenTestToFlagsMask.kt @@ -4,13 +4,13 @@ * SPDX-License-Identifier: Apache-2.0 */ -package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix +package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.linux import assertk.assertThat import assertk.assertions.isEqualTo import assertk.tableOf +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.linux.LinuxOpen.Companion.toLinuxMask import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.opencreate.OpenFileFlags -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.LinuxOpen.Companion.toLinuxMask import kotlin.test.Test import platform.posix.O_CREAT as POSIX_O_CREAT import platform.posix.O_RDONLY as POSIX_O_RDONLY diff --git a/wasi-emscripten-fs/src/linuxX64Main/kotlin/posix/LinuxStat.linuxX64.kt b/wasi-emscripten-fs/src/linuxX64Main/kotlin/linux/LinuxStat.linuxX64.kt similarity index 92% rename from wasi-emscripten-fs/src/linuxX64Main/kotlin/posix/LinuxStat.linuxX64.kt rename to wasi-emscripten-fs/src/linuxX64Main/kotlin/linux/LinuxStat.linuxX64.kt index 9baf4885..69e9c7ec 100644 --- a/wasi-emscripten-fs/src/linuxX64Main/kotlin/posix/LinuxStat.linuxX64.kt +++ b/wasi-emscripten-fs/src/linuxX64Main/kotlin/linux/LinuxStat.linuxX64.kt @@ -4,7 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix +package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.linux import arrow.core.Either import arrow.core.left @@ -14,9 +14,9 @@ import kotlinx.cinterop.memScoped import kotlinx.cinterop.ptr import platform.posix.errno import platform.posix.stat +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.linux.ext.toStructTimespec import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.stat.FileModeType import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.stat.StructStat -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.ext.toStructTimespec import ru.pixnews.wasm.sqlite.open.helper.host.platform.linux.fstatat internal actual fun platformFstatat( diff --git a/wasi-emscripten-fs/src/linuxX64Main/kotlin/posix/LinuxStatFd.linuxX64.kt b/wasi-emscripten-fs/src/linuxX64Main/kotlin/linux/LinuxStatFd.linuxX64.kt similarity index 93% rename from wasi-emscripten-fs/src/linuxX64Main/kotlin/posix/LinuxStatFd.linuxX64.kt rename to wasi-emscripten-fs/src/linuxX64Main/kotlin/linux/LinuxStatFd.linuxX64.kt index df87e5d5..344abf82 100644 --- a/wasi-emscripten-fs/src/linuxX64Main/kotlin/posix/LinuxStatFd.linuxX64.kt +++ b/wasi-emscripten-fs/src/linuxX64Main/kotlin/linux/LinuxStatFd.linuxX64.kt @@ -4,7 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix +package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.linux import arrow.core.Either import arrow.core.left diff --git a/wasi-emscripten-fs/src/mingwMain/kotlin/posix/MingwSeekFd.kt b/wasi-emscripten-fs/src/mingwMain/kotlin/posix/MingwSeekFd.kt index f2f25263..b0cfbd9b 100644 --- a/wasi-emscripten-fs/src/mingwMain/kotlin/posix/MingwSeekFd.kt +++ b/wasi-emscripten-fs/src/mingwMain/kotlin/posix/MingwSeekFd.kt @@ -13,12 +13,12 @@ import platform.posix.errno import platform.posix.lseek import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.Overflow import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.SeekError +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.FileSystemOperationHandler import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.seek.SeekFd -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.base.PosixOperationHandler import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.ext.errnoToSeekError import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.ext.toPosixWhence -internal object MingwSeekFd : PosixOperationHandler { +internal object MingwSeekFd : FileSystemOperationHandler { override fun invoke(input: SeekFd): Either { if (input.fileDelta > Int.MAX_VALUE) { return Overflow("input.fileDelta too big. Request: $input").left() diff --git a/wasi-emscripten-fs/src/nativeMain/kotlin/posix/PosixCloseFd.kt b/wasi-emscripten-fs/src/nativeMain/kotlin/posix/PosixCloseFd.kt index 13165d1a..dfefc9be 100644 --- a/wasi-emscripten-fs/src/nativeMain/kotlin/posix/PosixCloseFd.kt +++ b/wasi-emscripten-fs/src/nativeMain/kotlin/posix/PosixCloseFd.kt @@ -20,16 +20,16 @@ import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.CloseError import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.Interrupted import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.IoError import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.NoSpace +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs.FileSystemOperationHandler import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.model.Fd import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.close.CloseFd import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.base.PosixFileSystemState -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.base.PosixOperationHandler internal expect fun Int.platformSpecificErrnoToCloseError(fd: Fd): CloseError internal class PosixCloseFd( private val fsState: PosixFileSystemState, -) : PosixOperationHandler { +) : FileSystemOperationHandler { override fun invoke(input: CloseFd): Either { fsState.remove(input.fd) val retval = close(input.fd.fd) diff --git a/wasi-emscripten-fs/src/nativeMain/kotlin/posix/base/BaseFileSystemAdapter.kt b/wasi-emscripten-fs/src/nativeMain/kotlin/posix/base/BaseFileSystemAdapter.kt deleted file mode 100644 index b6fb8a32..00000000 --- a/wasi-emscripten-fs/src/nativeMain/kotlin/posix/base/BaseFileSystemAdapter.kt +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright 2024, the wasm-sqlite-open-helper project authors and contributors. Please see the AUTHORS file - * for details. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. - * SPDX-License-Identifier: Apache-2.0 - */ - -package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.base - -import arrow.core.Either -import arrow.core.left -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.FileSystem -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.FileSystemOperationError -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.NotImplemented -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.FileSystemOperation - -internal class BaseFileSystemAdapter( - private val operations: Map, PosixOperationHandler<*, *, *>>, -) : FileSystem { - @Suppress("UNCHECKED_CAST") - override fun execute( - operation: FileSystemOperation, - input: I, - ): Either { - val handler = operations[operation] as PosixOperationHandler? - if (handler == null) { - return NotImplemented.left() as Either - } - - return handler.invoke(input) - } - - override fun isOperationSupported(operation: FileSystemOperation<*, *, *>): Boolean { - return operations.containsKey(operation) - } - - override fun close() = Unit -} diff --git a/wasi-emscripten-fs/src/nativeMain/kotlin/posix/base/PosixFileSystemState.kt b/wasi-emscripten-fs/src/nativeMain/kotlin/posix/base/PosixFileSystemState.kt index 256a7698..218701ae 100644 --- a/wasi-emscripten-fs/src/nativeMain/kotlin/posix/base/PosixFileSystemState.kt +++ b/wasi-emscripten-fs/src/nativeMain/kotlin/posix/base/PosixFileSystemState.kt @@ -6,16 +6,12 @@ package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.base -import ru.pixnews.wasm.sqlite.open.helper.common.api.Logger import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.model.Fd import ru.pixnews.wasm.sqlite.open.helper.io.lock.ReentrantLock import ru.pixnews.wasm.sqlite.open.helper.io.lock.reentrantLock import ru.pixnews.wasm.sqlite.open.helper.io.lock.withLock -internal class PosixFileSystemState( - rootLogger: Logger, -) : AutoCloseable { - private val logger: Logger = rootLogger.withTag("PosixFileSystemState") +internal class PosixFileSystemState : AutoCloseable { private val lock: ReentrantLock = reentrantLock() private val openFileDescriptors: MutableSet = mutableSetOf() @@ -39,7 +35,7 @@ internal class PosixFileSystemState( for (fd in fileDescriptors) { val errNo = platform.posix.close(fd.fd) if (errNo != 0) { - logger.i { "close($fd) failed with errno $errNo. Ignore." } + // close($fd) failed with errno $errNo. Ignore. } } } diff --git a/wasi-emscripten-host/src/commonMain/kotlin/ext/FileSystemLogger.kt b/wasi-emscripten-host/src/commonMain/kotlin/ext/FileSystemLogger.kt new file mode 100644 index 00000000..187b7ead --- /dev/null +++ b/wasi-emscripten-host/src/commonMain/kotlin/ext/FileSystemLogger.kt @@ -0,0 +1,42 @@ +/* + * Copyright 2024, the wasm-sqlite-open-helper project authors and contributors. Please see the AUTHORS file + * for details. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. + * SPDX-License-Identifier: Apache-2.0 + */ + +package ru.pixnews.wasm.sqlite.open.helper.host.ext + +import ru.pixnews.wasm.sqlite.open.helper.common.api.Logger +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.FileSystem +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.FileSystemEngine +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.dsl.FileSystemConfigBlock +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.dsl.FileSystemEngineConfig +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.logging.LoggingFileSystemInterceptor +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.logging.LoggingFileSystemInterceptor.LoggingEvents +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.logging.LoggingFileSystemInterceptor.LoggingEvents.OperationEnd +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.logging.LoggingFileSystemInterceptor.OperationLoggingLevel.BASIC + +internal operator fun LoggingFileSystemInterceptor.Companion.invoke( + logger: Logger, +): LoggingFileSystemInterceptor { + return LoggingFileSystemInterceptor( + logger = { logger.v(null, it) }, + logEvents = LoggingEvents( + end = OperationEnd( + inputs = BASIC, + outputs = BASIC, + trackDuration = false, + ), + ), + ) +} + +@Suppress("FunctionName") +internal fun DefaultFileSystem( + engine: FileSystemEngine, + rootLogger: Logger, + block: FileSystemConfigBlock.() -> Unit = {}, +): FileSystem = FileSystem(engine) { + addInterceptor(LoggingFileSystemInterceptor(rootLogger)) + block() +} diff --git a/wasi-emscripten-host/src/jvmMain/kotlin/EmbedderHost.jvm.kt b/wasi-emscripten-host/src/jvmMain/kotlin/EmbedderHost.jvm.kt index b524ba00..cc81d2bb 100644 --- a/wasi-emscripten-host/src/jvmMain/kotlin/EmbedderHost.jvm.kt +++ b/wasi-emscripten-host/src/jvmMain/kotlin/EmbedderHost.jvm.kt @@ -7,7 +7,8 @@ package ru.pixnews.wasm.sqlite.open.helper.host import ru.pixnews.wasm.sqlite.open.helper.host.EmbedderHost.Builder -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.nio.JvmNioFileSystem +import ru.pixnews.wasm.sqlite.open.helper.host.ext.DefaultFileSystem +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.nio.NioFileSystem import ru.pixnews.wasm.sqlite.open.helper.host.jvm.JvmEmbedderHost import ru.pixnews.wasm.sqlite.open.helper.host.jvm.JvmEmbedderHost.JvmClock import ru.pixnews.wasm.sqlite.open.helper.host.jvm.JvmEmbedderHost.JvmCommandArgsProvider @@ -21,7 +22,7 @@ internal actual fun createDefaultEmbedderHost(builder: Builder): EmbedderHost = rootLogger = builder.rootLogger, systemEnvProvider = builder.systemEnvProvider ?: JvmSystemEnvProvider, commandArgsProvider = builder.commandArgsProvider ?: JvmCommandArgsProvider, - fileSystem = builder.fileSystem ?: JvmNioFileSystem(rootLogger = builder.rootLogger), + fileSystem = builder.fileSystem ?: DefaultFileSystem(NioFileSystem, builder.rootLogger.withTag("FSnio")), clock = builder.clock ?: JvmClock, monotonicClock = builder.monotonicClock ?: JvmMonotonicClock, localTimeFormatter = builder.localTimeFormatter ?: JvmLocalTimeFormatter(), diff --git a/wasi-emscripten-host/src/jvmMain/kotlin/jvm/JvmEmbedderHost.kt b/wasi-emscripten-host/src/jvmMain/kotlin/jvm/JvmEmbedderHost.kt index bf4204d8..cd1ce95e 100644 --- a/wasi-emscripten-host/src/jvmMain/kotlin/jvm/JvmEmbedderHost.kt +++ b/wasi-emscripten-host/src/jvmMain/kotlin/jvm/JvmEmbedderHost.kt @@ -15,14 +15,15 @@ import ru.pixnews.wasm.sqlite.open.helper.host.EmbedderHost.LocalTimeFormatter import ru.pixnews.wasm.sqlite.open.helper.host.EmbedderHost.MonotonicClock import ru.pixnews.wasm.sqlite.open.helper.host.EmbedderHost.SystemEnvProvider import ru.pixnews.wasm.sqlite.open.helper.host.EmbedderHost.TimeZoneInfoProvider +import ru.pixnews.wasm.sqlite.open.helper.host.ext.DefaultFileSystem import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.FileSystem -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.nio.JvmNioFileSystem +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.nio.NioFileSystem public class JvmEmbedderHost( public override val rootLogger: Logger = Logger, public override val systemEnvProvider: SystemEnvProvider = JvmSystemEnvProvider, public override val commandArgsProvider: CommandArgsProvider = JvmCommandArgsProvider, - public override val fileSystem: FileSystem = JvmNioFileSystem(rootLogger = rootLogger), + public override val fileSystem: FileSystem = DefaultFileSystem(NioFileSystem, rootLogger.withTag("FSnio")), public override val clock: Clock = JvmClock, public override val monotonicClock: MonotonicClock = JvmMonotonicClock, public override val localTimeFormatter: LocalTimeFormatter = JvmLocalTimeFormatter(), diff --git a/wasi-emscripten-host/src/linuxMain/kotlin/EmbedderHost.linux.kt b/wasi-emscripten-host/src/linuxMain/kotlin/EmbedderHost.linux.kt index 006ed02a..9a903afc 100644 --- a/wasi-emscripten-host/src/linuxMain/kotlin/EmbedderHost.linux.kt +++ b/wasi-emscripten-host/src/linuxMain/kotlin/EmbedderHost.linux.kt @@ -6,14 +6,9 @@ package ru.pixnews.wasm.sqlite.open.helper.host -import ru.pixnews.wasm.sqlite.open.helper.common.api.Logger import ru.pixnews.wasm.sqlite.open.helper.host.EmbedderHost.Builder -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.FileSystem +import ru.pixnews.wasm.sqlite.open.helper.host.ext.DefaultFileSystem import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.LinuxFileSystem -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.logging.LoggingFileSystemDecorator -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.logging.LoggingFileSystemDecorator.LoggingEvents -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.logging.LoggingFileSystemDecorator.LoggingEvents.OperationEnd -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.logging.LoggingFileSystemDecorator.OperationLoggingLevel.BASIC import ru.pixnews.wasm.sqlite.open.helper.host.internal.CommonClock import ru.pixnews.wasm.sqlite.open.helper.host.internal.CommonMonotonicClock import ru.pixnews.wasm.sqlite.open.helper.host.linux.LinuxEmbedderHost @@ -27,27 +22,10 @@ internal actual fun createDefaultEmbedderHost(builder: Builder): EmbedderHost = rootLogger = builder.rootLogger, systemEnvProvider = builder.systemEnvProvider ?: NativeSystemEnvProvider, commandArgsProvider = builder.commandArgsProvider ?: NativeCommandArgsProvider, - fileSystem = builder.fileSystem ?: createLinuxFileSystem(builder.rootLogger), + fileSystem = builder.fileSystem ?: DefaultFileSystem(LinuxFileSystem, builder.rootLogger.withTag("FSlnx")), monotonicClock = builder.monotonicClock ?: CommonMonotonicClock(), clock = builder.clock ?: CommonClock(), localTimeFormatter = builder.localTimeFormatter ?: LinuxLocalTimeFormatter(), timeZoneInfo = builder.timeZoneInfo ?: LinuxTimeZoneInfoProvider(), entropySource = builder.entropySource ?: LinuxEntropySource(), ) - -private fun createLinuxFileSystem(logger: Logger): FileSystem { - val linuxFileSystem = LinuxFileSystem(logger) - val fsLogger = logger.withTag("FSlnx") - val loggingDecorator = LoggingFileSystemDecorator( - delegate = linuxFileSystem, - logger = { fsLogger.v(null, it) }, - logEvents = LoggingEvents( - end = OperationEnd( - inputs = BASIC, - outputs = BASIC, - trackDuration = true, - ), - ), - ) - return loggingDecorator -} diff --git a/wasi-emscripten-host/src/linuxMain/kotlin/linux/LinuxEmbedderHost.kt b/wasi-emscripten-host/src/linuxMain/kotlin/linux/LinuxEmbedderHost.kt index b0ee09fb..06cb0ed3 100644 --- a/wasi-emscripten-host/src/linuxMain/kotlin/linux/LinuxEmbedderHost.kt +++ b/wasi-emscripten-host/src/linuxMain/kotlin/linux/LinuxEmbedderHost.kt @@ -15,8 +15,9 @@ import ru.pixnews.wasm.sqlite.open.helper.host.EmbedderHost.LocalTimeFormatter import ru.pixnews.wasm.sqlite.open.helper.host.EmbedderHost.MonotonicClock import ru.pixnews.wasm.sqlite.open.helper.host.EmbedderHost.SystemEnvProvider import ru.pixnews.wasm.sqlite.open.helper.host.EmbedderHost.TimeZoneInfoProvider +import ru.pixnews.wasm.sqlite.open.helper.host.ext.DefaultFileSystem import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.FileSystem -import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.NotImplementedFileSystem +import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.LinuxFileSystem import ru.pixnews.wasm.sqlite.open.helper.host.internal.CommonClock import ru.pixnews.wasm.sqlite.open.helper.host.internal.CommonMonotonicClock @@ -24,7 +25,7 @@ public class LinuxEmbedderHost( override val rootLogger: Logger, override val systemEnvProvider: SystemEnvProvider = NativeSystemEnvProvider, override val commandArgsProvider: CommandArgsProvider = NativeCommandArgsProvider, - override val fileSystem: FileSystem = NotImplementedFileSystem, + override val fileSystem: FileSystem = DefaultFileSystem(LinuxFileSystem, rootLogger.withTag("FSlnx")), override val monotonicClock: MonotonicClock = CommonMonotonicClock(), override val clock: Clock = CommonClock(), override val localTimeFormatter: LocalTimeFormatter = LinuxLocalTimeFormatter(),