Skip to content

Commit

Permalink
Promote some APIs to non-delicate
Browse files Browse the repository at this point in the history
  • Loading branch information
05nelsonm committed Feb 12, 2024
1 parent 2111a3d commit bffa3b8
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,28 @@ package io.matthewnelson.kmp.file
import io.matthewnelson.kmp.file.internal.*
import io.matthewnelson.kmp.file.internal.errorCode

@DelicateFileApi
// @Throws(IOException::class)
public fun File.lstat(): Stats = try {
Stats(fs_lstatSync(path))
} catch (t: Throwable) {
throw t.toIOException()
}

@DelicateFileApi
// @Throws(IOException::class)
public fun File.stat(): Stats = try {
Stats(fs_statSync(path))
} catch (t: Throwable) {
throw t.toIOException()
}

@DelicateFileApi
// @Throws(IOException::class)
public fun File.read(): Buffer = try {
Buffer(fs_readFileSync(path))
} catch (t: Throwable) {
throw t.toIOException()
}

@DelicateFileApi
// @Throws(IOException::class)
public fun File.write(data: Buffer) {
try {
fs_writeFileSync(path, data.value)
Expand All @@ -48,7 +48,6 @@ public fun File.write(data: Buffer) {
}
}

@DelicateFileApi
public fun Throwable.toIOException(): IOException {
if (this is IOException) return this

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import io.matthewnelson.kmp.file.internal.fs_Stats
* println(buffer.unwrap())
*
* */
@DelicateFileApi
public value class Buffer internal constructor(
internal val value: buffer_Buffer
) {
Expand Down Expand Up @@ -69,7 +68,6 @@ public value class Buffer internal constructor(
}
}

@DelicateFileApi
public value class Stats internal constructor(
private val value: fs_Stats
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ internal actual val IsWindows: Boolean by lazy {
}
}

@OptIn(DelicateFileApi::class)
@Suppress("NOTHING_TO_INLINE")
internal actual inline fun File.platformReadBytes(): ByteArray = try {
val buffer = read()
Expand All @@ -61,7 +60,6 @@ internal actual inline fun File.platformReadBytes(): ByteArray = try {
throw t.toIOException()
}

@OptIn(DelicateFileApi::class)
@Suppress("NOTHING_TO_INLINE")
internal actual inline fun File.platformReadUtf8(): String = try {
val buffer = read()
Expand All @@ -79,7 +77,6 @@ internal actual inline fun File.platformReadUtf8(): String = try {
throw t.toIOException()
}

@OptIn(DelicateFileApi::class)
@Suppress("NOTHING_TO_INLINE")
internal actual inline fun File.platformWriteBytes(array: ByteArray) {
try {
Expand All @@ -89,7 +86,6 @@ internal actual inline fun File.platformWriteBytes(array: ByteArray) {
}
}

@OptIn(DelicateFileApi::class)
@Suppress("NOTHING_TO_INLINE")
internal actual inline fun File.platformWriteUtf8(text: String) {
try {
Expand Down Expand Up @@ -125,7 +121,6 @@ internal actual fun fs_chmod(path: String, mode: String) {
try {
fs_chmodSync(path, mode)
} catch (t: Throwable) {
@OptIn(DelicateFileApi::class)
throw t.toIOException()
}
}
Expand All @@ -146,7 +141,6 @@ internal actual fun fs_remove(path: String): Boolean {
fs_rmdirSync(path, Options.Remove(force = true, recursive = false))
true
} catch (t: Throwable) {
@OptIn(DelicateFileApi::class)
throw t.toIOException()
}
}
Expand All @@ -165,7 +159,6 @@ internal actual fun fs_realpath(path: String): String {
return try {
fs_realpathSync(path)
} catch (t: Throwable) {
@OptIn(DelicateFileApi::class)
throw t.toIOException()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package io.matthewnelson.kmp.file

import kotlin.test.*

@OptIn(DelicateFileApi::class)
class WrapperUnitTest {

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package io.matthewnelson.kmp.file.internal

import io.matthewnelson.kmp.file.FileNotFoundException
import io.matthewnelson.kmp.file.IOException
import io.matthewnelson.kmp.file.DelicateFileApi
import io.matthewnelson.kmp.file.errnoToIOException
import kotlinx.cinterop.ByteVar
import kotlinx.cinterop.CPointer
Expand All @@ -43,7 +42,7 @@ import platform.posix.rmdir
internal actual fun fs_chmod(path: String, mode: String) { /* no-op */ }

@Throws(IOException::class)
@OptIn(DelicateFileApi::class, ExperimentalForeignApi::class)
@OptIn(ExperimentalForeignApi::class)
internal actual fun fs_remove(path: String): Boolean {
if (remove(path) == 0) return true

Expand All @@ -60,7 +59,7 @@ internal actual fun fs_platform_mkdir(
): Int = mkdir(path)

@Throws(IOException::class)
@OptIn(DelicateFileApi::class, ExperimentalForeignApi::class)
@OptIn(ExperimentalForeignApi::class)
internal actual fun fs_realpath(
path: String,
): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ public inline fun <T: Any?> File.fOpen(
return result
}

/**
* Reads the contents of [FILE] into provided ByteArray.
*
* When return value is:
* - Negative: error, check [errno]
* - 0: no more data to read (break)
* - Positive: [buf] was filled with that much data starting from 0
* */
@DelicateFileApi
@ExperimentalForeignApi
public fun CPointer<FILE>.fRead(
Expand All @@ -73,6 +81,14 @@ public fun CPointer<FILE>.fRead(
fs_platform_fread(this, pinned.addressOf(0), buf.size)
}

/**
* Writes [buf] to [FILE]
*
* When return value is:
* - Negative: error, check [errno]
* - 0: wrote no data (break)
* - Positive: Amount of data from [buf] written.
* */
@DelicateFileApi
@ExperimentalForeignApi
public fun CPointer<FILE>.fWrite(
Expand All @@ -83,7 +99,6 @@ public fun CPointer<FILE>.fWrite(
fs_platform_fwrite(this, pinned.addressOf(offset), len)
}

@DelicateFileApi
@ExperimentalForeignApi
public fun errnoToIOException(errno: Int): IOException {
val message = strerror(errno)?.toKString() ?: "errno: $errno"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import kotlin.test.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue

@OptIn(DelicateFileApi::class)
class ChmodNativeUnitTest: ChmodBaseTest() {

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@
**/
package io.matthewnelson.kmp.file

import kotlinx.cinterop.ExperimentalForeignApi
import platform.posix.F_OK
import platform.posix.X_OK
import platform.posix.access
import platform.posix.errno
import kotlin.experimental.ExperimentalNativeApi

actual val isJvm: Boolean = false
Expand All @@ -36,7 +32,6 @@ actual val isSimulator: Boolean by lazy {
}

@Throws(IOException::class, IndexOutOfBoundsException::class)
@OptIn(DelicateFileApi::class, ExperimentalForeignApi::class)
fun File.checkAccess(vararg access: Int): Boolean {
if (!exists()) throw FileNotFoundException()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public actual class File: Comparable<File> {
* @throws [IOException] if [mode] is inappropriate
* @throws [FileNotFoundException] if the file does not exist
* */
@DelicateFileApi
@Throws(IOException::class)
public fun chmod(mode: String) { fs_chmod(realPath, mode) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import kotlinx.cinterop.*
import platform.posix.*

@Throws(IOException::class)
@OptIn(DelicateFileApi::class, ExperimentalForeignApi::class)
@OptIn(ExperimentalForeignApi::class)
internal actual fun fs_chmod(path: String, mode: String) {
val modeT = try {
Mode(value = mode).toModeT()
Expand All @@ -39,7 +39,7 @@ internal actual fun fs_chmod(path: String, mode: String) {
}

@Throws(IOException::class)
@OptIn(DelicateFileApi::class, ExperimentalForeignApi::class)
@OptIn(ExperimentalForeignApi::class)
internal actual fun fs_remove(path: String): Boolean {
val result = remove(path)
if (result != 0) {
Expand All @@ -50,7 +50,7 @@ internal actual fun fs_remove(path: String): Boolean {
}

@Throws(IOException::class)
@OptIn(DelicateFileApi::class, ExperimentalForeignApi::class)
@OptIn(ExperimentalForeignApi::class)
internal actual fun fs_realpath(path: String): String {
val real = realpath(path, null)
?: throw errnoToIOException(errno)
Expand Down

0 comments on commit bffa3b8

Please sign in to comment.