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
39
40
41
42
43
44
45
|
# File 'lib/feather_watch/core/linux_watcher.rb', line 3
def initialize(directories, callback, verbose= false, silence_exceptions= false)
@verbose = verbose
@silence_exceptions = silence_exceptions
puts "Initializing linux watcher" if @verbose
@notifiers = []
directories = [directories] if directories.is_a?(String)
directories.each do |dir|
notifier = INotify::Notifier.new
@notifiers << notifier
notifier.watch(dir, :recursive, :create, :attrib, :delete, :close_write, :delete_self, :modify, :move_self, :moved_from, :moved_to) do |event|
begin
if !([:attrib, :close_write, :modify] & event.flags ).empty?
puts "Change on file: #{event.absolute_name}" if @verbose
callback.call({status: :modified, file: event.absolute_name, event: event})
elsif !([:moved_to] & event.flags ).empty?
puts "File added: #{event.absolute_name}" if @verbose
callback.call({status: :added, file: event.absolute_name, event: event})
elsif !([:moved_from] & event.flags ).empty?
puts "File removed: #{event.absolute_name}" if @verbose
callback.call({status: :removed, file: event.absolute_name, event: event})
elsif !([:create] & event.flags ).empty?
puts "File added: #{event.absolute_name}" if @verbose
callback.call({status: :added, file: event.absolute_name, event: event})
elsif !([:delete, :delete_self] & event.flags ).empty?
puts "File removed: #{event.absolute_name}" if @verbose
callback.call({status: :removed, file: event.absolute_name, event: event})
else
STDERR.puts "Unhandled status flags: #{event.flags} for file #{event.absolute_name}" if @verbose
end
rescue Exception => e
unless @silence_exceptions
STDERR.puts "----------------------------"
STDERR.puts "Error in Feather Watch callback"
STDERR.puts "Message: #{e.message}"
STDERR.puts "backtrace: #{e.backtrace * "\n\t >"}"
end
end
end
end
end
|