Implement watching a path

Each inotify instance produces events for paths in its watch list. Each
item in the watch list is identified by its watch descriptor. Different
paths can be watched for different events.
This commit is contained in:
T. R. Bernstein
2026-03-11 17:58:06 +01:00
parent 098339f9d1
commit 564c409c15
5 changed files with 92 additions and 0 deletions

View File

@@ -2,6 +2,7 @@ import CInotify
public actor Inotify {
private let fd: Int32
private var watches: [Int32: String] = [:]
public init() throws {
self.fd = inotify_init1(Int32(IN_NONBLOCK | IN_CLOEXEC))
@@ -10,6 +11,16 @@ public actor Inotify {
}
}
@discardableResult
public func addWatch(path: String, mask: InotifyEventMask) throws -> Int32 {
let wd = inotify_add_watch(self.fd, path, mask.rawValue)
guard wd >= 0 else {
throw InotifyError.addWatchFailed(path: path, errno: cinotify_get_errno())
}
watches[wd] = path
return wd
}
deinit {
cinotify_deinit(self.fd)
}