lib/xrefresh-server/monitor.rb in darwin-xrefresh-server-0.2.2 vs lib/xrefresh-server/monitor.rb in darwin-xrefresh-server-0.3.0
- old
+ new
@@ -1,8 +1,9 @@
require 'osx/foundation'
OSX.require_framework '/System/Library/Frameworks/CoreServices.framework/Frameworks/CarbonCore.framework'
include OSX
+require 'digest/md5'
module XRefreshServer
class Monitor
@@ -10,10 +11,11 @@
@config = config
@server = server
@modified_dirs = Set.new
@paths_info = Hash.new
@streams = []
+ @checksums = Hash.new
end
def schedule(start_event_id)
fsevents_cb = proc do |stream, ctx, numEvents, paths, marks, eventIDs|
# ctx doesn't work through rubycocoa?
@@ -49,10 +51,17 @@
@streams << stream
end
end
+ def checksum_modified?(full_path)
+ digest = Digest::MD5.hexdigest(File.read(full_path))
+ return false if (@checksums[full_path]==digest)
+ @checksums[full_path]=digest
+ true
+ end
+
# blocking call
def run_loop(start_time)
activities = {"changed" => blue('*'), "deleted" => red('-'), "created" => green('+'), "renamed" => magenta('>')}
@@ -108,15 +117,19 @@
original_time = @paths_info[full_path] || start_time
if (current_time > original_time)
OUT.puts "debug: reported #{full_path}" if @config["debug"]
relative_path = full_path[root.size+1..-1]
- buckets[root]||=[]
- buckets[root]<< {
- "action" => "changed",
- "path1" => relative_path,
- "path2" => nil
- }
+ if (checksum_modified?(full_path))
+ buckets[root]||=[]
+ buckets[root]<< {
+ "action" => "changed",
+ "path1" => relative_path,
+ "path2" => nil
+ }
+ else
+ OUT.puts "debug: not accepted - checksum is the same" if @config["debug"]
+ end
end
@paths_info[full_path] = current_time
end
else
relative_path = dir[root.size+1..-1]