nativeNonAndroidMain.files.FileSystemNativeNonAndroid.kt Maven / Gradle / Ivy
/*
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file.
*/
package kotlinx.io.files
import kotlinx.cinterop.CPointer
import kotlinx.cinterop.ExperimentalForeignApi
import kotlinx.cinterop.get
import kotlinx.cinterop.toKString
import kotlinx.io.IOException
import platform.posix.DIR
import platform.posix.closedir
import platform.posix.errno
import platform.posix.strerror
@OptIn(ExperimentalForeignApi::class)
internal actual class OpaqueDirEntry(private val dir: CPointer) : AutoCloseable {
actual fun readdir(): String? {
val entry = platform.posix.readdir(dir) ?: return null
return entry[0].d_name.toKString()
}
actual override fun close() {
closedir(dir)
}
}
@OptIn(ExperimentalForeignApi::class)
internal actual fun opendir(path: String): OpaqueDirEntry {
val dirent = platform.posix.opendir(path)
if (dirent != null) return OpaqueDirEntry(dirent)
throw IOException("Can't open directory $path: ${strerror(errno)?.toKString() ?: "reason unknown"}")
}