Class: Inotify
- Inherits:
-
Object
- Object
- Inotify
- Includes:
- InotifyCtypes, ObjectSpace
- Defined in:
- lib/inotify.rb
Overview
High-level class for using inotify in ruby.
puts "Watching #{ARGV[0]}"
inotify = Inotify.new
inotify.add_watch(ARGV[0], InotifyEvents::IN_CREATE|InotifyEvents::IN_MODIFY)
inotify.wait_for_event() { |path, mask, name|
puts "#{path}/#{name}"
if inotify.event?(mask, InotifyEvents::IN_CREATE)
puts " * created"
end
if inotify.event?(mask, InotifyEvents::IN_MODIFY)
puts " * modified"
end
}
inotify.rm_watch(ARGV[0])
Instance Method Summary collapse
-
#add_watch(path, event) ⇒ Object
Add a path to inotify watch (events found in InotifyEvents).
-
#close ⇒ Object
Close the inotify file descriptor.
-
#event?(mask, event_num) ⇒ Boolean
Check if an event is set in an event mask.
-
#initialize ⇒ Inotify
constructor
A new instance of Inotify.
-
#recursive_add_watch(path, event) ⇒ Object
Recursively add a path to inotify watch.
-
#rm_all_watches ⇒ Object
Close all watch descriptors.
-
#rm_watch(path) ⇒ Object
Remove a path from inotify watch.
-
#wait_for_event {|, , | ... } ⇒ Object
Wait for an inotify event to happen, yields the path, mask, and file name.
Methods included from InotifyCtypes
#inotify_add_watch, #inotify_close, #inotify_event, #inotify_init, #inotify_read, #inotify_rm_watch
Constructor Details
#initialize ⇒ Inotify
Returns a new instance of Inotify.
175 176 177 178 179 180 |
# File 'lib/inotify.rb', line 175 def initialize @fd = inotify_init @wds = {} define_finalizer(self, proc { close }) end |
Instance Method Details
#add_watch(path, event) ⇒ Object
Add a path to inotify watch (events found in InotifyEvents).
183 184 185 |
# File 'lib/inotify.rb', line 183 def add_watch(path, event) @wds[inotify_add_watch(@fd, path, event)] = path end |
#close ⇒ Object
Close the inotify file descriptor.
220 221 222 |
# File 'lib/inotify.rb', line 220 def close inotify_close(@fd) end |
#event?(mask, event_num) ⇒ Boolean
Check if an event is set in an event mask.
215 216 217 |
# File 'lib/inotify.rb', line 215 def event?(mask, event_num) return (mask & event_num) != 0 end |
#recursive_add_watch(path, event) ⇒ Object
Recursively add a path to inotify watch.
201 202 203 204 205 206 |
# File 'lib/inotify.rb', line 201 def recursive_add_watch(path, event) add_watch(path, event) Dir.glob("#{path}/**/*/").each { |path| add_watch(path, event) } end |
#rm_all_watches ⇒ Object
Close all watch descriptors.
196 197 198 |
# File 'lib/inotify.rb', line 196 def rm_all_watches() @wds.values.each { |v| rm_watch(v) } end |
#rm_watch(path) ⇒ Object
Remove a path from inotify watch.
188 189 190 191 192 193 |
# File 'lib/inotify.rb', line 188 def rm_watch(path) wd = @wds.key(path) if wd inotify_rm_watch(@fd, wd) end end |
#wait_for_event {|, , | ... } ⇒ Object
Wait for an inotify event to happen, yields the path, mask, and file name.
209 210 211 212 |
# File 'lib/inotify.rb', line 209 def wait_for_event() event = inotify_read(@fd) yield @wds[event["wd"]], event["mask"], event["name"] end |