Add data structure for watch descriptor management
For watching whole trees - a change which is upcoming - the watch descriptor IDs will have to be managed in multiple lists. The InotifyWatchManager encapsulates the managment logic within a nice API.
This commit is contained in:
@@ -3,7 +3,7 @@ import CInotify
|
|||||||
|
|
||||||
public actor Inotify {
|
public actor Inotify {
|
||||||
private let fd: CInt
|
private let fd: CInt
|
||||||
private var watches: [CInt: String] = [:]
|
private var watches = InotifyWatchManager()
|
||||||
private var eventReader: any DispatchSourceRead
|
private var eventReader: any DispatchSourceRead
|
||||||
private var eventStream: AsyncStream<RawInotifyEvent>
|
private var eventStream: AsyncStream<RawInotifyEvent>
|
||||||
public var events: AsyncCompactMapSequence<AsyncStream<RawInotifyEvent>, InotifyEvent> {
|
public var events: AsyncCompactMapSequence<AsyncStream<RawInotifyEvent>, InotifyEvent> {
|
||||||
@@ -24,7 +24,7 @@ public actor Inotify {
|
|||||||
guard wd >= 0 else {
|
guard wd >= 0 else {
|
||||||
throw InotifyError.addWatchFailed(path: path, errno: cinotify_get_errno())
|
throw InotifyError.addWatchFailed(path: path, errno: cinotify_get_errno())
|
||||||
}
|
}
|
||||||
watches[wd] = path
|
watches.add(path, withId: wd)
|
||||||
return wd
|
return wd
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,7 +43,7 @@ public actor Inotify {
|
|||||||
guard inotify_rm_watch(self.fd, wd) == 0 else {
|
guard inotify_rm_watch(self.fd, wd) == 0 else {
|
||||||
throw InotifyError.removeWatchFailed(watchDescriptor: wd, errno: cinotify_get_errno())
|
throw InotifyError.removeWatchFailed(watchDescriptor: wd, errno: cinotify_get_errno())
|
||||||
}
|
}
|
||||||
watches.removeValue(forKey: wd)
|
watches.remove(forId: wd)
|
||||||
}
|
}
|
||||||
|
|
||||||
deinit {
|
deinit {
|
||||||
@@ -51,7 +51,7 @@ public actor Inotify {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private func transform(_ rawEvent: RawInotifyEvent) -> InotifyEvent? {
|
private func transform(_ rawEvent: RawInotifyEvent) -> InotifyEvent? {
|
||||||
guard let path = self.watches[rawEvent.watchDescriptor] else { return nil }
|
guard let path = self.watches.path(forId: rawEvent.watchDescriptor) else { return nil }
|
||||||
return InotifyEvent.init(from: rawEvent, inDirectory: path)
|
return InotifyEvent.init(from: rawEvent, inDirectory: path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
18
Sources/Inotify/InotifyWatchManager.swift
Normal file
18
Sources/Inotify/InotifyWatchManager.swift
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
struct InotifyWatchManager {
|
||||||
|
private var watchPaths: [CInt: String] = [:]
|
||||||
|
private var activeWatches: Set<CInt> = []
|
||||||
|
|
||||||
|
mutating func add(_ path: String, withId watchDescriptor: CInt) {
|
||||||
|
self.watchPaths[watchDescriptor] = path
|
||||||
|
self.activeWatches.insert(watchDescriptor)
|
||||||
|
}
|
||||||
|
|
||||||
|
mutating func remove(forId watchDescriptor: CInt) {
|
||||||
|
self.watchPaths.removeValue(forKey: watchDescriptor)
|
||||||
|
self.activeWatches.remove(watchDescriptor)
|
||||||
|
}
|
||||||
|
|
||||||
|
func path(forId watchDescriptor: CInt) -> String? {
|
||||||
|
return self.watchPaths[watchDescriptor]
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user