guard / rb-inotify

A thorough inotify wrapper for Ruby using FFI.
MIT License
312 stars 64 forks source link

Different events generated while monitoring file and directory (with file) #31

Closed mkasztelnik closed 10 years ago

mkasztelnik commented 10 years ago

This is a issue discovered and discussed in this thread: https://github.com/guard/listen/issues/155

I'm using kubuntu 13.10 and SublimeText3 (build 3056 but the same issue occurs in previous builds). It seems like rb-inotify generates different events when listening file and directory. Following code:

require 'rb-inotify'
notifier = INotify::Notifier.new
notifier.watch('/home/marek/git/rails/air/spec/models/appliance_spec.rb', :attrib) do |event|
  puts "#{event.flags} is now in #{event.absolute_name}!"
end

notifier.run

generates:

[:attrib] is now in /home/marek/git/rails/air/spec/models/appliance_spec.rb!
[:ignored] is now in /home/marek/git/rails/air/spec/models/appliance_spec.rb!

while saving file using Sublimetext3. But following code:

require 'rb-inotify'
EVENTS = [:recursive, :attrib, :create, :delete, :move, :close_write]
notifier = INotify::Notifier.new
notifier.watch('/home/marek/git/rails/air', *EVENTS) do |event|
  puts "#{event.flags} is now in #{event.absolute_name}!"
end

notifier.run

generates:

[:create] is now in /home/marek/git/rails/air/spec/models/.subleb0.tmp!
[:attrib] is now in /home/marek/git/rails/air/spec/models/.subleb0.tmp!
[:close_write, :close] is now in /home/marek/git/rails/air/spec/models/.subleb0.tmp!
[:attrib] is now in /home/marek/git/rails/air/spec/models/.subleb0.tmp!
[:moved_from, :move] is now in /home/marek/git/rails/air/spec/models/.subleb0.tmp!
[:moved_to, :move] is now in /home/marek/git/rails/air/spec/models/appliance_spec.rb!
nex3 commented 10 years ago

I'm not entirely sure what the issue you're seeing in. Your examples are confusing; the second one is listening to a lot more event types than the first. In addition, I'd expect some directory modification events to work differently to some extent than file modificiation events, since you're coming at them from a different perspective.

Ultimately, rb-inotify just pipes events from the underlying inotify system, so which events exist or not is rarely up to the Ruby code. If you still think we're reporting events incorrectly, though, please mention:

mkasztelnik commented 10 years ago

I'm saving the same file in both cases (/home/marek/git/rails/air/spec/models/appliance_spec.rb). When I'm listening on this file attrib event is generated. When I'm listening on the directory in which my file is located attrib event is not generated.

Additional events are not important in this case. So the code presented above can be modified into:

require 'rb-inotify'
EVENTS = [:recursive, :attrib]
notifier = INotify::Notifier.new
notifier.watch('/home/marek/git/rails/air/spec/models/', *EVENTS) do |event|
  puts "#{event.flags} is now in #{event.absolute_name}!"
end

notifier.run

Than no attrib event is generated for saved file in in SublimeText3, instead following events are generated:

[:attrib] is now in /home/marek/git/rails/air/spec/models/.subl543.tmp!
[:attrib] is now in /home/marek/git/rails/air/spec/models/.subl543.tmp!
[:attrib] is now in /home/marek/git/rails/air/spec/models/.subl543.tmp!

For saving in vim this event is generated.

If rb-notify is not source of the problem than what do you think it can be: SublimeText3, inotify?

mkasztelnik commented 10 years ago

@nex3 after applying https://github.com/guard/listen/issues/155#issuecomment-30109711 into SublimeText3 everything works as expected :)