From 3215d2eb5ade2babc4fc40f65c6dd8b0605e4e6c Mon Sep 17 00:00:00 2001 From: "T. R. Bernstein" Date: Sat, 19 Sep 2026 00:45:22 +0200 Subject: [PATCH] Describe the errno an error stores, not the current one The description looked up the message of whatever errno was set when the description was built, so an error printed later read "Success (errno 28)". --- Sources/CInotify/cinotify.h | 9 +++------ Sources/Inotify/InotifyError.swift | 6 ++---- Tests/InotifyIntegrationTests/InotifyErrorTests.swift | 11 +++++++++++ 3 files changed, 16 insertions(+), 10 deletions(-) create mode 100644 Tests/InotifyIntegrationTests/InotifyErrorTests.swift diff --git a/Sources/CInotify/cinotify.h b/Sources/CInotify/cinotify.h index 3a8b10e..8e217de 100644 --- a/Sources/CInotify/cinotify.h +++ b/Sources/CInotify/cinotify.h @@ -5,6 +5,7 @@ #include #include #include +#include static inline int cinotify_deinit(int fd) { return close(fd); @@ -14,12 +15,8 @@ static inline int cinotify_get_errno(void) { return errno; } -static inline char* get_error_message() { - int error_number = errno; - errno = 0; - char* error_message = strerror(error_number); - if (errno > 0) return NULL; - return error_message; +static inline char* cinotify_error_message(int error_number) { + return strerror(error_number); } #endif diff --git a/Sources/Inotify/InotifyError.swift b/Sources/Inotify/InotifyError.swift index 390a43b..db810c5 100644 --- a/Sources/Inotify/InotifyError.swift +++ b/Sources/Inotify/InotifyError.swift @@ -17,9 +17,7 @@ public enum InotifyError: Error, Sendable, CustomStringConvertible { } private func readableErrno(_ code: Int32) -> String { - if let cStr = get_error_message() { - return String(cString: cStr) + " (errno \(code))" - } - return "errno \(code)" + guard let message = cinotify_error_message(code) else { return "errno \(code)" } + return String(cString: message) + " (errno \(code))" } } diff --git a/Tests/InotifyIntegrationTests/InotifyErrorTests.swift b/Tests/InotifyIntegrationTests/InotifyErrorTests.swift new file mode 100644 index 0000000..510374d --- /dev/null +++ b/Tests/InotifyIntegrationTests/InotifyErrorTests.swift @@ -0,0 +1,11 @@ +import Testing +@testable import Inotify + +@Suite("Error Description") +struct InotifyErrorTests { + @Test func describesTheStoredErrnoAndNotTheCurrentOne() { + let error = InotifyError.addWatchFailed(path: "/watched", errno: 28) + + #expect(error.description == "inotify_add_watch failed for '/watched': No space left on device (errno 28)") + } +}