Skip to content

Commit

Permalink
Improve crawling performance of Notifier#watch (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
nvasilevski authored and ioquatix committed May 19, 2024
1 parent 15e1007 commit ba15a05
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions lib/rb-inotify/notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ module INotify
# # Nothing happens until you run the notifier!
# notifier.run
class Notifier
# A list of directories that should never be recursively watched.
#
# * Files in `/dev/fd` sometimes register as directories, but are not enumerable.
RECURSIVE_BLACKLIST = %w[/dev/fd]
NON_RECURSIVE = "/dev/fd"

# A hash from {Watcher} ids to the instances themselves.
#
Expand Down Expand Up @@ -198,20 +196,17 @@ def to_io
def watch(path, *flags, &callback)
return Watcher.new(self, path, *flags, &callback) unless flags.include?(:recursive)

dir = Dir.new(path)
dont_follow = flags.include?(:dont_follow)

dir.each do |base|
Dir.each_child(path) do |base|
d = File.join(path, base)
binary_d = d.respond_to?(:force_encoding) ? d.dup.force_encoding('BINARY') : d
next if binary_d =~ /\/\.\.?$/ # Current or parent directory
next if RECURSIVE_BLACKLIST.include?(d)
next if flags.include?(:dont_follow) && File.symlink?(d)
next if !File.directory?(d)
next unless File.directory?(d)
next if dont_follow && File.symlink?(d)
next if NON_RECURSIVE == d

watch(d, *flags, &callback)
end

dir.close

rec_flags = [:create, :moved_to]
return watch(path, *((flags - [:recursive]) | rec_flags)) do |event|
Expand Down

0 comments on commit ba15a05

Please sign in to comment.