def watch(path, *flags, &callback)
return Watcher.new(self, path, *flags, &callback) unless flags.include?(:recursive)
dir = Dir.new(path)
dir.each do |base|
d = File.join(path, base)
binary_d = d.respond_to?(:force_encoding) ? d.dup.force_encoding('BINARY') : d
next if binary_d =~ /\/\.\.?$/
next if RECURSIVE_BLACKLIST.include?(d)
next if flags.include?(:dont_follow) && File.symlink?(d)
next if !File.directory?(d)
watch(d, *flags, &callback)
end
dir.close
rec_flags = [:create, :moved_to]
return watch(path, *((flags - [:recursive]) | rec_flags)) do |event|
callback.call(event) if flags.include?(:all_events) || !(flags & event.flags).empty?
next if (rec_flags & event.flags).empty? || !event.flags.include?(:isdir)
begin
watch(event.absolute_name, *flags, &callback)
rescue Errno::ENOENT
end
end
end