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)".
This commit is contained in:
T. R. Bernstein
2026-09-19 00:45:22 +02:00
parent 21f096aede
commit 3215d2eb5a
3 changed files with 16 additions and 10 deletions
+3 -6
View File
@@ -5,6 +5,7 @@
#include <sys/inotify.h> #include <sys/inotify.h>
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
#include <string.h>
static inline int cinotify_deinit(int fd) { static inline int cinotify_deinit(int fd) {
return close(fd); return close(fd);
@@ -14,12 +15,8 @@ static inline int cinotify_get_errno(void) {
return errno; return errno;
} }
static inline char* get_error_message() { static inline char* cinotify_error_message(int error_number) {
int error_number = errno; return strerror(error_number);
errno = 0;
char* error_message = strerror(error_number);
if (errno > 0) return NULL;
return error_message;
} }
#endif #endif
+2 -4
View File
@@ -17,9 +17,7 @@ public enum InotifyError: Error, Sendable, CustomStringConvertible {
} }
private func readableErrno(_ code: Int32) -> String { private func readableErrno(_ code: Int32) -> String {
if let cStr = get_error_message() { guard let message = cinotify_error_message(code) else { return "errno \(code)" }
return String(cString: cStr) + " (errno \(code))" return String(cString: message) + " (errno \(code))"
}
return "errno \(code)"
} }
} }
@@ -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)")
}
}