Skip to content

Commit

Permalink
Merge linux amr64 and x64 (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
illarionov authored Aug 25, 2024
1 parent 28fd2db commit 8de9c0f
Show file tree
Hide file tree
Showing 31 changed files with 573 additions and 644 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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

import arrow.core.Either
import arrow.core.left
import arrow.core.right
import kotlinx.cinterop.alloc
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.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(
dirfd: Int,
path: String,
statFlags: Int,
): Either<Int, StructStat> = memScoped {
val statBuf: stat = alloc()
val exitCode = fstatat(
dirfd,
path,
statBuf.ptr,
statFlags,
)
return if (exitCode == 0) {
statBuf.toStructStat().right()
} else {
errno.left()
}
}

internal fun stat.toStructStat(): StructStat = StructStat(
deviceId = st_dev,
inode = st_ino,
mode = FileModeType.fromLinuxModeType(st_mode),
links = st_nlink.toULong(),
usedId = st_uid.toULong(),
groupId = st_gid.toULong(),
specialFileDeviceId = st_rdev,
size = st_size.toULong(),
blockSize = st_blksize.toULong(),
blocks = st_blocks.toULong(),
accessTime = st_atim.toStructTimespec(),
modificationTime = st_mtim.toStructTimespec(),
changeStatusTime = st_ctim.toStructTimespec(),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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

import arrow.core.Either
import arrow.core.left
import arrow.core.right
import kotlinx.cinterop.alloc
import kotlinx.cinterop.memScoped
import kotlinx.cinterop.ptr
import platform.posix.errno
import platform.posix.fstat
import platform.posix.stat
import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.stat.StructStat

internal actual fun platformFstatFd(fd: Int): Either<Int, StructStat> = memScoped {
val statBuf: stat = alloc()
val exitCode = fstat(
fd,
statBuf.ptr,
)
return if (exitCode == 0) {
statBuf.toStructStat().right()
} else {
errno.left()
}
}

This file was deleted.

10 changes: 0 additions & 10 deletions wasi-emscripten-fs/src/linuxMain/kotlin/Dummy.kt

This file was deleted.

77 changes: 71 additions & 6 deletions wasi-emscripten-fs/src/linuxMain/kotlin/LinuxFileSystem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,85 @@ 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

internal expect fun createPlatformFileSystemOperations(
fsState: PosixFileSystemState,
): Map<FileSystemOperation<*, *, *>, PosixOperationHandler<*, *, *>>

public class LinuxFileSystem(
rootLogger: Logger = Logger,
) : FileSystem {
private val fsState = PosixFileSystemState(rootLogger)
private val operations: Map<FileSystemOperation<*, *, *>, PosixOperationHandler<*, *, *>> =
createPlatformFileSystemOperations(fsState)
private val operations: Map<FileSystemOperation<*, *, *>, 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,
)
private val fsAdapter = BaseFileSystemAdapter(operations)

override fun close() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ 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 LinuxX64CheckAccess : PosixOperationHandler<CheckAccess, CheckAccessError, Unit> {
internal object LinuxCheckAccess : PosixOperationHandler<CheckAccess, CheckAccessError, Unit> {
override fun invoke(input: CheckAccess): Either<CheckAccessError, Unit> {
val resultCode = syscall(
SYS_faccessat2.toLong(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ 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 LinuxX64Chmod : PosixOperationHandler<Chmod, ChmodError, Unit> {
internal object LinuxChmod : PosixOperationHandler<Chmod, ChmodError, Unit> {
override fun invoke(input: Chmod): Either<ChmodError, Unit> {
val resultCode = fchmodat(
input.baseDirectory.toDirFd(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ 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 LinuxX64Chown : PosixOperationHandler<Chown, ChownError, Unit> {
internal object LinuxChown : PosixOperationHandler<Chown, ChownError, Unit> {
override fun invoke(input: Chown): Either<ChownError, Unit> {
val resultCode = fchownat(
input.baseDirectory.toDirFd(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.posix.base.PosixOperat
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 LinuxX64Mkdir : PosixOperationHandler<Mkdir, MkdirError, Unit> {
internal object LinuxMkdir : PosixOperationHandler<Mkdir, MkdirError, Unit> {
override fun invoke(input: Mkdir): Either<MkdirError, Unit> {
val resultCode = mkdirat(
input.baseDirectory.toDirFd(),
Expand Down
Loading

0 comments on commit 8de9c0f

Please sign in to comment.