-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add implementation of utimensat, rmdir, opendirat (#33)
- Loading branch information
1 parent
c12c93d
commit 7f3c4d0
Showing
18 changed files
with
551 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
.../kotlin/ru/pixnews/wasm/sqlite/open/helper/graalvm/host/emscripten/func/SyscallMkdirat.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* 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.graalvm.host.emscripten.func | ||
|
||
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary | ||
import com.oracle.truffle.api.frame.VirtualFrame | ||
import org.graalvm.wasm.WasmContext | ||
import org.graalvm.wasm.WasmInstance | ||
import org.graalvm.wasm.WasmLanguage | ||
import org.graalvm.wasm.WasmModule | ||
import org.graalvm.wasm.memory.WasmMemory | ||
import ru.pixnews.wasm.sqlite.open.helper.common.api.Logger | ||
import ru.pixnews.wasm.sqlite.open.helper.common.api.WasmPtr | ||
import ru.pixnews.wasm.sqlite.open.helper.graalvm.SqliteEmbedderHost | ||
import ru.pixnews.wasm.sqlite.open.helper.graalvm.ext.getArgAsInt | ||
import ru.pixnews.wasm.sqlite.open.helper.graalvm.ext.getArgAsUint | ||
import ru.pixnews.wasm.sqlite.open.helper.graalvm.ext.getArgAsWasmPtr | ||
import ru.pixnews.wasm.sqlite.open.helper.graalvm.host.BaseWasmNode | ||
import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.SysException | ||
import ru.pixnews.wasm.sqlite.open.helper.host.include.DirFd | ||
import ru.pixnews.wasm.sqlite.open.helper.host.include.FileMode | ||
|
||
internal class SyscallMkdirat( | ||
language: WasmLanguage, | ||
module: WasmModule, | ||
private val host: SqliteEmbedderHost, | ||
functionName: String = "__syscall_mkdirat", | ||
) : BaseWasmNode(language, module, functionName) { | ||
private val logger: Logger = host.rootLogger.withTag(SyscallFtruncate64::class.qualifiedName!!) | ||
override fun executeWithContext(frame: VirtualFrame, context: WasmContext, instance: WasmInstance): Any { | ||
val args: Array<Any> = frame.arguments | ||
return syscallMkdirat( | ||
memory(frame), | ||
args.getArgAsInt(0), | ||
args.getArgAsWasmPtr(1), | ||
args.getArgAsUint(2), | ||
) | ||
} | ||
|
||
@Suppress("MemberNameEqualsClassName") | ||
@TruffleBoundary | ||
private fun syscallMkdirat( | ||
memory: WasmMemory, | ||
rawDirFd: Int, | ||
pathnamePtr: WasmPtr<Byte>, | ||
rawMode: UInt, | ||
): Int { | ||
val fs = host.fileSystem | ||
val dirFd = DirFd(rawDirFd) | ||
val mode = FileMode(rawMode) | ||
val path = memory.readString(pathnamePtr.addr, null) | ||
try { | ||
fs.mkdirAt(dirFd, path, mode) | ||
return 0 | ||
} catch (e: SysException) { | ||
logger.v(e) { "__syscall_mkdirat($dirFd, $path, $mode) error: ${e.errNo}" } | ||
return -e.errNo.code | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...in/kotlin/ru/pixnews/wasm/sqlite/open/helper/graalvm/host/emscripten/func/SyscallRmdir.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* 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.graalvm.host.emscripten.func | ||
|
||
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary | ||
import com.oracle.truffle.api.frame.VirtualFrame | ||
import org.graalvm.wasm.WasmContext | ||
import org.graalvm.wasm.WasmInstance | ||
import org.graalvm.wasm.WasmLanguage | ||
import org.graalvm.wasm.WasmModule | ||
import org.graalvm.wasm.memory.WasmMemory | ||
import ru.pixnews.wasm.sqlite.open.helper.common.api.WasmPtr | ||
import ru.pixnews.wasm.sqlite.open.helper.graalvm.SqliteEmbedderHost | ||
import ru.pixnews.wasm.sqlite.open.helper.graalvm.ext.getArgAsWasmPtr | ||
import ru.pixnews.wasm.sqlite.open.helper.graalvm.host.BaseWasmNode | ||
import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.SysException | ||
|
||
internal class SyscallRmdir( | ||
language: WasmLanguage, | ||
module: WasmModule, | ||
private val host: SqliteEmbedderHost, | ||
functionName: String = "__syscall_rmdir", | ||
) : BaseWasmNode(language, module, functionName) { | ||
override fun executeWithContext(frame: VirtualFrame, context: WasmContext, instance: WasmInstance): Any { | ||
val args: Array<Any> = frame.arguments | ||
return syscallRmdirat( | ||
memory(frame), | ||
args.getArgAsWasmPtr(0), | ||
) | ||
} | ||
|
||
@TruffleBoundary | ||
private fun syscallRmdirat( | ||
memory: WasmMemory, | ||
pathnamePtr: WasmPtr<Byte>, | ||
): Int { | ||
val fs = host.fileSystem | ||
val path = memory.readString(pathnamePtr.addr, null) | ||
try { | ||
fs.rmdir(path) | ||
return 0 | ||
} catch (e: SysException) { | ||
return -e.errNo.code | ||
} | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
...otlin/ru/pixnews/wasm/sqlite/open/helper/graalvm/host/emscripten/func/SyscallUtimensat.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* 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.graalvm.host.emscripten.func | ||
|
||
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary | ||
import com.oracle.truffle.api.frame.VirtualFrame | ||
import org.graalvm.wasm.WasmContext | ||
import org.graalvm.wasm.WasmInstance | ||
import org.graalvm.wasm.WasmLanguage | ||
import org.graalvm.wasm.WasmModule | ||
import org.graalvm.wasm.memory.WasmMemory | ||
import ru.pixnews.wasm.sqlite.open.helper.common.api.WasmPtr | ||
import ru.pixnews.wasm.sqlite.open.helper.common.api.isSqlite3Null | ||
import ru.pixnews.wasm.sqlite.open.helper.graalvm.SqliteEmbedderHost | ||
import ru.pixnews.wasm.sqlite.open.helper.graalvm.ext.getArgAsInt | ||
import ru.pixnews.wasm.sqlite.open.helper.graalvm.ext.getArgAsUint | ||
import ru.pixnews.wasm.sqlite.open.helper.graalvm.ext.getArgAsWasmPtr | ||
import ru.pixnews.wasm.sqlite.open.helper.graalvm.host.BaseWasmNode | ||
import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.SysException | ||
import ru.pixnews.wasm.sqlite.open.helper.host.include.DirFd | ||
import ru.pixnews.wasm.sqlite.open.helper.host.include.Fcntl | ||
import ru.pixnews.wasm.sqlite.open.helper.host.include.sys.SysStat.UTIME_NOW | ||
import ru.pixnews.wasm.sqlite.open.helper.host.include.sys.SysStat.UTIME_OMIT | ||
import kotlin.LazyThreadSafetyMode.NONE | ||
import kotlin.time.Duration | ||
import kotlin.time.Duration.Companion.nanoseconds | ||
import kotlin.time.Duration.Companion.seconds | ||
|
||
internal class SyscallUtimensat( | ||
language: WasmLanguage, | ||
module: WasmModule, | ||
private val host: SqliteEmbedderHost, | ||
functionName: String = "__syscall_utimensat", | ||
) : BaseWasmNode(language, module, functionName) { | ||
override fun executeWithContext(frame: VirtualFrame, context: WasmContext, instance: WasmInstance): Any { | ||
val args: Array<Any> = frame.arguments | ||
@Suppress("MagicNumber") | ||
return syscallUtimensat( | ||
memory(frame), | ||
args.getArgAsInt(0), | ||
args.getArgAsWasmPtr(1), | ||
args.getArgAsWasmPtr(2), | ||
args.getArgAsUint(3), | ||
) | ||
} | ||
|
||
@Suppress("MemberNameEqualsClassName") | ||
@TruffleBoundary | ||
private fun syscallUtimensat( | ||
memory: WasmMemory, | ||
rawDirFd: Int, | ||
pathnamePtr: WasmPtr<Byte>, | ||
times: WasmPtr<Byte>, | ||
flags: UInt, | ||
): Int { | ||
val dirFd = DirFd(rawDirFd) | ||
val noFolowSymlinks: Boolean = (flags and Fcntl.AT_SYMLINK_NOFOLLOW) != 0U | ||
val path = memory.readString(pathnamePtr.addr, null) | ||
var atime: Duration? | ||
val mtime: Duration? | ||
@Suppress("MagicNumber") | ||
if (times.isSqlite3Null()) { | ||
atime = host.clock.invoke() | ||
mtime = atime | ||
} else { | ||
val atimeSeconds = memory.load_i64(this, times.addr.toLong()) | ||
val atimeNanoseconds = memory.load_i64(this, times.addr.toLong() + 8) | ||
|
||
val mtimeSeconds = memory.load_i64(this, times.addr.toLong() + 16) | ||
val mtimeNanoseconds = memory.load_i64(this, times.addr.toLong() + 24) | ||
|
||
val now: Duration by lazy(NONE) { host.clock.invoke() } | ||
atime = timesToDuration(atimeSeconds, atimeNanoseconds) { now } | ||
mtime = timesToDuration(mtimeSeconds, mtimeNanoseconds) { now } | ||
} | ||
try { | ||
host.fileSystem.setTimesAt(dirFd, path, atime, mtime, noFolowSymlinks) | ||
return 0 | ||
} catch (e: SysException) { | ||
return -e.errNo.code | ||
} | ||
} | ||
|
||
private fun timesToDuration( | ||
seconds: Long, | ||
nanoseconds: Long, | ||
now: () -> Duration, | ||
): Duration? = when (nanoseconds) { | ||
UTIME_NOW.toLong() -> now() | ||
UTIME_OMIT.toLong() -> null | ||
else -> seconds.seconds + nanoseconds.nanoseconds | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.