Skip to content

Instantly share code, notes, and snippets.

@storoj
Created June 1, 2023 22:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save storoj/11c7acd7a33fefe1fdf23f496ed129e7 to your computer and use it in GitHub Desktop.
Save storoj/11c7acd7a33fefe1fdf23f496ed129e7 to your computer and use it in GitHub Desktop.
FileMon
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