Created
June 1, 2023 22:47
-
-
Save storoj/11c7acd7a33fefe1fdf23f496ed129e7 to your computer and use it in GitHub Desktop.
FileMon
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FileMon { | |
static let queue = DispatchQueue(label: "FileMon") | |
let path: String | |
let file: Int32 | |
let source: DispatchSourceFileSystemObject | |
deinit { | |
source.cancel() | |
} | |
init(_ path: String, handler: @escaping () -> Void) { | |
self.path = path | |
file = open(path, O_EVTONLY) | |
source = DispatchSource.makeFileSystemObjectSource(fileDescriptor: file, eventMask: .write, queue: FileMon.queue) | |
source.setEventHandler(handler: handler) | |
source.setCancelHandler { [file] in | |
close(file) | |
} | |
source.resume() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment