public
Created

Handling Filesystem Events with GCD and Macruby

  • Download Gist
filesystem_events.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#!/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)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.