From 40404f0479ae284cedd1816630d9eab28f33c69b Mon Sep 17 00:00:00 2001 From: Filipp Zhinkin Date: Wed, 18 Dec 2024 18:33:10 -0500 Subject: [PATCH] Check error codes returned by POSIX-ish APIs and throw IOE --- .../src/files/FileSystemAndroid.kt | 11 +++++++++-- core/native/src/files/PathsNative.kt | 18 +++++++++++++----- .../src/files/FileSystemNativeNonAndroid.kt | 11 +++++++++-- core/wasmWasi/src/files/FileSystemWasm.kt | 15 ++++++++++++--- 4 files changed, 43 insertions(+), 12 deletions(-) diff --git a/core/androidNative/src/files/FileSystemAndroid.kt b/core/androidNative/src/files/FileSystemAndroid.kt index 65ba74177..24fbba52e 100644 --- a/core/androidNative/src/files/FileSystemAndroid.kt +++ b/core/androidNative/src/files/FileSystemAndroid.kt @@ -35,7 +35,11 @@ internal actual class OpaqueDirEntry(private val dir: CPointer ) : RawSource { @@ -85,7 +91,7 @@ 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 } } @@ -93,7 +99,9 @@ internal class FileSource( override fun close() { if (closed) return closed = true - fclose(file) + if (fclose(file) != 0) { + throwIOExceptionForErrno("fclose") + } } } @@ -130,13 +138,13 @@ 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") } } @@ -144,7 +152,7 @@ internal class FileSink( if (closed) return closed = true if (fclose(file) != 0) { - throw IOException(errno.toString()) + throwIOExceptionForErrno("fclose") } } } diff --git a/core/nativeNonAndroid/src/files/FileSystemNativeNonAndroid.kt b/core/nativeNonAndroid/src/files/FileSystemNativeNonAndroid.kt index f36286345..a75172231 100644 --- a/core/nativeNonAndroid/src/files/FileSystemNativeNonAndroid.kt +++ b/core/nativeNonAndroid/src/files/FileSystemNativeNonAndroid.kt @@ -23,7 +23,11 @@ internal actual class OpaqueDirEntry(private val dir: CPointer) : 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)") + } } } @@ -31,5 +35,8 @@ internal actual class OpaqueDirEntry(private val dir: CPointer) : AutoClose 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)") } diff --git a/core/wasmWasi/src/files/FileSystemWasm.kt b/core/wasmWasi/src/files/FileSystemWasm.kt index 19810c8b1..252fbe30b 100644 --- a/core/wasmWasi/src/files/FileSystemWasm.kt +++ b/core/wasmWasi/src/files/FileSystemWasm.kt @@ -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}") + } } } } @@ -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}") + } } } } @@ -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}") + } } } }