Skip to content

Instantly share code, notes, and snippets.

@seanlilmateus
Created October 10, 2011 18:53
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seanlilmateus/1276176 to your computer and use it in GitHub Desktop.
Save seanlilmateus/1276176 to your computer and use it in GitHub Desktop.
Handling Filesystem Events with GCD and Macruby
#!/usr/local/bin/macruby
framework 'Foundation'
include Dispatch
#``O_EVTONLY`` is defined as ``0x8000`` in the OS X header files.
# instead of RDONLY flag I use the O_EVTONLY flag to open() the file, otherwise we'll prevent the
# volume that the file is on from being unmounted.
O_EVTONLY = 0x8000
file = File.open("/path/to/config.plist", O_EVTONLY)
q = Queue.new('org.macruby.gcd_filesystem_events.sources')
type = Source::VNODE
option = Source::VNODE_EXTEND
# hold the changes
@oldFile = file.read
Dispatch::Source.new(type, file, option, q) { |src|
src.suspend!
puts "changed data: #{src.data} #{src.handle.path}" if src.suspended?
src.resume!
showChanges
# to cancel
src.cancel!
}
# show last state and New State
def showChanges
file = File.open("/path/to/config.plist", File::RDONLY).read
warn @oldFile
puts file
puts "_________________________________"
@oldFile = file
end
NSRunLoop.currentRunLoop.runUntilDate(NSDate.distantFuture)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment