From 9352098728c66b54ccc2efd17b1486ad540b6d1a Mon Sep 17 00:00:00 2001 From: jgvanwyk <123784102+jgvanwyk@users.noreply.github.com> Date: Sat, 22 Jul 2023 23:41:52 -0400 Subject: [PATCH] Switched to using Core Foundation types Also switched to using URL(fileURLWithPath:) all the time rather (this wouldn't compile on some macOS versions). --- .../FileSystemEventStream.swift | 64 ++++++++----------- .../StringConvertible.swift | 12 +--- .../SwiftFileSystemEventsTests.swift | 10 +++ 3 files changed, 38 insertions(+), 48 deletions(-) diff --git a/Sources/SwiftFileSystemEvents/FileSystemEventStream.swift b/Sources/SwiftFileSystemEvents/FileSystemEventStream.swift index ed10056..9ee8561 100644 --- a/Sources/SwiftFileSystemEvents/FileSystemEventStream.swift +++ b/Sources/SwiftFileSystemEvents/FileSystemEventStream.swift @@ -42,33 +42,34 @@ public final class FileSystemEventStream { latency: TimeInterval = 0, flags: Flags = [], handler: @escaping (FileSystemEvent) -> Void) { + var flags = flags + flags.insert(.useCFTypes) self.handler = handler - let pathsToWatch: CFArray - if #available(macOS 13.0, *) { - pathsToWatch = directoriesToWatch.map { $0.path(percentEncoded: false) } as CFArray - } else { - pathsToWatch = directoriesToWatch.map { $0.path } as CFArray - } + let pathsToWatch = directoriesToWatch.map { $0.path } as CFArray // We pass an unmanaged pointer to `self` as context info to the stream. // `FileSystemEventStream.callback` uses this to call `handler` with each event. // As the memory for `self` is managed by Swift, we pass `nil` for both `retain` // and `release`. - var context = FSEventStreamContext(version: 0, - info: Unmanaged.passUnretained(self).toOpaque(), - retain: nil, - release: nil, - copyDescription: nil) + var context = FSEventStreamContext( + version: 0, + info: Unmanaged.passUnretained(self).toOpaque(), + retain: nil, + release: nil, + copyDescription: nil + ) // While the return value of `FSEventStreamCreate` is imported in Swift as // `FSEventStreamRef?`, the documentation for `FSEventStreamCreate` asserts that // its return value will always be a valid `FSEventStreamRef`, so we unwrap the // return value here. - self.streamRef = FSEventStreamCreate(kCFAllocatorDefault, - Self.callback, - &context, - pathsToWatch, - sinceWhen.rawValue, - latency, - flags.rawValue)! + self.streamRef = FSEventStreamCreate( + kCFAllocatorDefault, + Self.callback, + &context, + pathsToWatch, + sinceWhen.rawValue, + latency, + flags.rawValue + )! } deinit { @@ -77,12 +78,10 @@ public final class FileSystemEventStream { private static let callback: FSEventStreamCallback = { _, info, numEvents, eventPaths, eventFlags, eventIDs in guard let info = info else { return } - let eventPaths = eventPaths.assumingMemoryBound(to: UnsafeMutablePointer.self) + let eventPaths = Unmanaged.fromOpaque(eventPaths).takeUnretainedValue() as! [CFString] let stream = Unmanaged.fromOpaque(info).takeUnretainedValue() for index in 0..