From d57f998fd482291034f69877e5a145b72ecefdd9 Mon Sep 17 00:00:00 2001 From: "T. R. Bernstein" Date: Thu, 12 Mar 2026 00:32:14 +0100 Subject: [PATCH] Use C integer types Instead of using hardcoded fixed width integers, use C integer types. As the actual integer size of C depends on the implementation, even if it is often 32bit, we use the C integer types to guard against cases, where that is not the case. --- Sources/Inotify/Inotify.swift | 12 ++++---- Sources/Inotify/InotifyEventMask.swift | 40 +++++++++++++------------- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/Sources/Inotify/Inotify.swift b/Sources/Inotify/Inotify.swift index 2a3c5b6..8614829 100644 --- a/Sources/Inotify/Inotify.swift +++ b/Sources/Inotify/Inotify.swift @@ -2,8 +2,8 @@ import Dispatch import CInotify public actor Inotify { - private let fd: Int32 - private var watches: [Int32: String] = [:] + private let fd: CInt + private var watches: [CInt: String] = [:] private var eventReader: any DispatchSourceRead private var eventStream: AsyncStream public var events: AsyncCompactMapSequence, InotifyEvent> { @@ -11,7 +11,7 @@ public actor Inotify { } public init() throws { - self.fd = inotify_init1(Int32(IN_NONBLOCK | IN_CLOEXEC)) + self.fd = inotify_init1(CInt(IN_NONBLOCK | IN_CLOEXEC)) guard self.fd >= 0 else { throw InotifyError.initFailed(errno: cinotify_get_errno()) } @@ -19,7 +19,7 @@ public actor Inotify { } @discardableResult - public func addWatch(path: String, mask: InotifyEventMask) throws -> Int32 { + public func addWatch(path: String, mask: InotifyEventMask) throws -> CInt { let wd = inotify_add_watch(self.fd, path, mask.rawValue) guard wd >= 0 else { throw InotifyError.addWatchFailed(path: path, errno: cinotify_get_errno()) @@ -28,7 +28,7 @@ public actor Inotify { return wd } - public func removeWatch(_ wd: Int32) throws { + public func removeWatch(_ wd: CInt) throws { guard inotify_rm_watch(self.fd, wd) == 0 else { throw InotifyError.removeWatchFailed(watchDescriptor: wd, errno: cinotify_get_errno()) } @@ -44,7 +44,7 @@ public actor Inotify { return InotifyEvent.init(from: rawEvent, inDirectory: path) } - private static func createEventReader(forFileDescriptor fd: Int32) -> (any DispatchSourceRead, AsyncStream) { + private static func createEventReader(forFileDescriptor fd: CInt) -> (any DispatchSourceRead, AsyncStream) { let (stream, continuation) = AsyncStream.makeStream( of: RawInotifyEvent.self, bufferingPolicy: .bufferingNewest(512) diff --git a/Sources/Inotify/InotifyEventMask.swift b/Sources/Inotify/InotifyEventMask.swift index 6cbd439..5c15818 100644 --- a/Sources/Inotify/InotifyEventMask.swift +++ b/Sources/Inotify/InotifyEventMask.swift @@ -1,7 +1,7 @@ import CInotify public struct InotifyEventMask: OptionSet, Sendable, Hashable { - public let rawValue: UInt32 + public let rawValue: CUnsignedInt public init(rawValue: UInt32) { self.rawValue = rawValue @@ -9,18 +9,18 @@ public struct InotifyEventMask: OptionSet, Sendable, Hashable { // MARK: - Watchable Events - public static let access = InotifyEventMask(rawValue: UInt32(IN_ACCESS)) - public static let attrib = InotifyEventMask(rawValue: UInt32(IN_ATTRIB)) - public static let closeWrite = InotifyEventMask(rawValue: UInt32(IN_CLOSE_WRITE)) - public static let closeNoWrite = InotifyEventMask(rawValue: UInt32(IN_CLOSE_NOWRITE)) - public static let create = InotifyEventMask(rawValue: UInt32(IN_CREATE)) - public static let delete = InotifyEventMask(rawValue: UInt32(IN_DELETE)) - public static let deleteSelf = InotifyEventMask(rawValue: UInt32(IN_DELETE_SELF)) - public static let modify = InotifyEventMask(rawValue: UInt32(IN_MODIFY)) - public static let moveSelf = InotifyEventMask(rawValue: UInt32(IN_MOVE_SELF)) - public static let movedFrom = InotifyEventMask(rawValue: UInt32(IN_MOVED_FROM)) - public static let movedTo = InotifyEventMask(rawValue: UInt32(IN_MOVED_TO)) - public static let open = InotifyEventMask(rawValue: UInt32(IN_OPEN)) + public static let access = InotifyEventMask(rawValue: CUnsignedInt(IN_ACCESS)) + public static let attrib = InotifyEventMask(rawValue: CUnsignedInt(IN_ATTRIB)) + public static let closeWrite = InotifyEventMask(rawValue: CUnsignedInt(IN_CLOSE_WRITE)) + public static let closeNoWrite = InotifyEventMask(rawValue: CUnsignedInt(IN_CLOSE_NOWRITE)) + public static let create = InotifyEventMask(rawValue: CUnsignedInt(IN_CREATE)) + public static let delete = InotifyEventMask(rawValue: CUnsignedInt(IN_DELETE)) + public static let deleteSelf = InotifyEventMask(rawValue: CUnsignedInt(IN_DELETE_SELF)) + public static let modify = InotifyEventMask(rawValue: CUnsignedInt(IN_MODIFY)) + public static let moveSelf = InotifyEventMask(rawValue: CUnsignedInt(IN_MOVE_SELF)) + public static let movedFrom = InotifyEventMask(rawValue: CUnsignedInt(IN_MOVED_FROM)) + public static let movedTo = InotifyEventMask(rawValue: CUnsignedInt(IN_MOVED_TO)) + public static let open = InotifyEventMask(rawValue: CUnsignedInt(IN_OPEN)) // MARK: - Combinations @@ -34,14 +34,14 @@ public struct InotifyEventMask: OptionSet, Sendable, Hashable { // MARK: - Watch Flags - public static let dontFollow = InotifyEventMask(rawValue: UInt32(IN_DONT_FOLLOW)) - public static let onlyDir = InotifyEventMask(rawValue: UInt32(IN_ONLYDIR)) - public static let oneShot = InotifyEventMask(rawValue: UInt32(IN_ONESHOT)) + public static let dontFollow = InotifyEventMask(rawValue: CUnsignedInt(IN_DONT_FOLLOW)) + public static let onlyDir = InotifyEventMask(rawValue: CUnsignedInt(IN_ONLYDIR)) + public static let oneShot = InotifyEventMask(rawValue: CUnsignedInt(IN_ONESHOT)) // MARK: - Kernel-Only Flags - public static let isDir = InotifyEventMask(rawValue: UInt32(IN_ISDIR)) - public static let ignored = InotifyEventMask(rawValue: UInt32(IN_IGNORED)) - public static let queueOverflow = InotifyEventMask(rawValue: UInt32(IN_Q_OVERFLOW)) - public static let unmount = InotifyEventMask(rawValue: UInt32(IN_UNMOUNT)) + public static let isDir = InotifyEventMask(rawValue: CUnsignedInt(IN_ISDIR)) + public static let ignored = InotifyEventMask(rawValue: CUnsignedInt(IN_IGNORED)) + public static let queueOverflow = InotifyEventMask(rawValue: CUnsignedInt(IN_Q_OVERFLOW)) + public static let unmount = InotifyEventMask(rawValue: CUnsignedInt(IN_UNMOUNT)) }