Patterns such as `.*` or `@*` are matched against an item's own name with `fnmatch`, in the same places as excluded names: resolving a tree, extending a watch to a directory that appears later, and delivering events. Dependents that prune large trees can now skip whole families of directories without listing each name.
44 lines
1.1 KiB
Swift
44 lines
1.1 KiB
Swift
import Inotify
|
|
|
|
enum RecursivKind {
|
|
case nonrecursive
|
|
case recursive
|
|
case withAutomaticSubtreeWatching
|
|
}
|
|
|
|
func getEventsForTrigger(
|
|
in dir: String,
|
|
mask: InotifyEventMask,
|
|
recursive: RecursivKind = .nonrecursive,
|
|
exclude: [String] = [],
|
|
excludePatterns: [String] = [],
|
|
trigger: @escaping (String) async throws -> Void,
|
|
) async throws -> [InotifyEvent] {
|
|
let watcher = try Inotify()
|
|
await watcher.exclude(names: exclude)
|
|
await watcher.exclude(patterns: excludePatterns)
|
|
switch recursive {
|
|
case .nonrecursive:
|
|
try await watcher.addWatch(path: dir, mask: mask)
|
|
case .recursive:
|
|
try await watcher.addRecursiveWatch(forDirectory: dir, mask: mask)
|
|
case .withAutomaticSubtreeWatching:
|
|
try await watcher.addWatchWithAutomaticSubtreeWatching(forDirectory: dir, mask: mask)
|
|
}
|
|
|
|
let eventTask = Task {
|
|
var events: [InotifyEvent] = []
|
|
for await event in await watcher.events {
|
|
events.append(event)
|
|
}
|
|
return events
|
|
}
|
|
|
|
try await Task.sleep(for: .milliseconds(100))
|
|
try await trigger(dir)
|
|
try await Task.sleep(for: .milliseconds(500))
|
|
|
|
eventTask.cancel()
|
|
return await eventTask.value
|
|
}
|