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