Deliver an event enum with the queue overflow as its own case
The stream's element is now the enum InotifyEvent, and the struct that describes a change to a watched item is FileSystemEvent. A queue overflow was an event with descriptor -1 and an empty path that every consumer had to know about; as a case, the compiler makes them handle it. The enum is also where failed watches of a growing tree will be reported, since no call site can catch them.
This commit is contained in:
@@ -1,37 +1,9 @@
|
||||
import SystemPackage
|
||||
|
||||
/// A filesystem event delivered by an ``Inotify`` instance.
|
||||
///
|
||||
/// When the kernel's event queue overflows, it drops events and reports a
|
||||
/// single event whose ``mask`` contains ``InotifyEventMask/queueOverflow``.
|
||||
/// Such an event belongs to no watch: its ``watchDescriptor`` is `-1` and
|
||||
/// its ``path`` is empty. Consumers that must not miss changes should
|
||||
/// rescan the watched trees when they receive one.
|
||||
public struct InotifyEvent: Sendable, Hashable, CustomStringConvertible {
|
||||
public let watchDescriptor: Int32
|
||||
public let mask: InotifyEventMask
|
||||
public let cookie: UInt32
|
||||
public let path: FilePath
|
||||
/// Whether the event was produced by the library for an item that already
|
||||
/// existed when its directory became watched, rather than by the kernel.
|
||||
public let synthesized: Bool
|
||||
|
||||
public var description: String {
|
||||
var parts = ["InotifyEvent(wd: \(watchDescriptor), mask: \(mask), path: \"\(path)\""]
|
||||
if cookie != 0 { parts.append("cookie: \(cookie)") }
|
||||
return parts.joined(separator: ", ") + ")"
|
||||
}
|
||||
}
|
||||
|
||||
extension InotifyEvent {
|
||||
public init(from rawEvent: RawInotifyEvent, inDirectory path: String) {
|
||||
let dirPath = FilePath(path)
|
||||
self.init(
|
||||
watchDescriptor: rawEvent.watchDescriptor,
|
||||
mask: rawEvent.mask,
|
||||
cookie: rawEvent.cookie,
|
||||
path: dirPath.appending(rawEvent.name),
|
||||
synthesized: rawEvent.synthesized
|
||||
)
|
||||
}
|
||||
/// What an ``Inotify`` instance delivers: a change to a watched item, or a
|
||||
/// condition that affects which changes it can deliver.
|
||||
public enum InotifyEvent: Sendable, Hashable {
|
||||
/// A change to a watched file or directory.
|
||||
case fileSystem(FileSystemEvent)
|
||||
/// The kernel's event queue was full, so it dropped events. Consumers
|
||||
/// that must not miss changes should rescan the watched trees.
|
||||
case queueOverflow
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user