Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions core/androidNative/src/files/FileSystemAndroid.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,20 @@ internal actual class OpaqueDirEntry(private val dir: CPointer<cnames.structs.DI
}

actual override fun close() {
closedir(dir)
if (closedir(dir) != 0) {
val err = errno
val strerr = strerror(err)?.toKString() ?: "unknown error"
throw IOException("closedir failed with errno $err ($strerr)")
}
}
}

@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"}")

val err = errno
val strerr = strerror(err)?.toKString() ?: "unknown error"
throw IOException("Can't open directory $path: $err ($strerr)")
}
18 changes: 13 additions & 5 deletions core/native/src/files/PathsNative.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ internal expect fun isAbsoluteImpl(path: String): Boolean

public actual fun Path(path: String): Path = Path(path, null)

private fun throwIOExceptionForErrno(operation: String): Nothing {
val err = errno
val strerr = strerror(err)?.toKString() ?: "unknown error"
throw IOException("$operation failed with errno $err ($strerr)")
}

internal class FileSource(
private val file: CPointer<FILE>
) : RawSource {
Expand All @@ -85,15 +91,17 @@ internal class FileSource(
return when {
bytesRead == byteCount -> bytesRead
feof(file) != 0 -> if (bytesRead == 0L) -1L else bytesRead
ferror(file) != 0 -> throw IOException(errno.toString())
ferror(file) != 0 -> throwIOExceptionForErrno("write")
else -> bytesRead
}
}

override fun close() {
if (closed) return
closed = true
fclose(file)
if (fclose(file) != 0) {
throwIOExceptionForErrno("fclose")
}
}
}

Expand Down Expand Up @@ -130,21 +138,21 @@ internal class FileSink(
variantFwrite(pinned.addressOf(0), byteCount.toUInt(), file).toLong()
}
if (bytesWritten < byteCount) {
throw IOException(errno.toString())
throwIOExceptionForErrno("file write")
}
}

override fun flush() {
if (fflush(file) != 0) {
throw IOException(errno.toString())
throwIOExceptionForErrno("fflush")
}
}

override fun close() {
if (closed) return
closed = true
if (fclose(file) != 0) {
throw IOException(errno.toString())
throwIOExceptionForErrno("fclose")
}
}
}
11 changes: 9 additions & 2 deletions core/nativeNonAndroid/src/files/FileSystemNativeNonAndroid.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,20 @@ internal actual class OpaqueDirEntry(private val dir: CPointer<DIR>) : AutoClose
}

actual override fun close() {
closedir(dir)
if (closedir(dir) != 0) {
val err = errno
val strerr = strerror(err)?.toKString() ?: "unknown error"
throw IOException("closedir failed with errno $err ($strerr)")
}
}
}

@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"}")

val err = errno
val strerr = strerror(err)?.toKString() ?: "unknown error"
throw IOException("Can't open directory $path: $err ($strerr)")
}
15 changes: 12 additions & 3 deletions core/wasmWasi/src/files/FileSystemWasm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,10 @@ internal object WasiFileSystem : SystemFileSystemImpl() {
}
return children
} finally {
fd_close(dir_fd)
val res = Errno(fd_close(dir_fd))
if (res != Errno.success) {
throw IOException("fd_close failed for directory '$directory': ${res.description}")
}
}
}
}
Expand Down Expand Up @@ -488,7 +491,10 @@ private class FileSink(private val fd: Fd) : RawSink {
override fun close() {
if (!closed) {
closed = true
fd_close(fd)
val res = Errno(fd_close(fd))
if (res != Errno.success) {
throw IOException("fd_close failed: ${res.description}")
}
}
}
}
Expand Down Expand Up @@ -536,7 +542,10 @@ private class FileSource(private val fd: Fd) : RawSource {
override fun close() {
if (!closed) {
closed = true
fd_close(fd)
val res = Errno(fd_close(fd))
if (res != Errno.success) {
throw IOException("fd_close failed: ${res.description}")
}
}
}
}
Expand Down