2009-09-03 05:42:59 +00:00
|
|
|
require 'yaml'
|
2009-08-30 18:37:24 +00:00
|
|
|
class FSSM::State
|
2009-09-06 05:59:30 +00:00
|
|
|
def initialize(path)
|
2009-08-30 18:37:24 +00:00
|
|
|
@path = path
|
2009-09-06 05:59:30 +00:00
|
|
|
@cache = FSSM::Tree::Cache.new
|
2009-09-03 05:42:59 +00:00
|
|
|
end
|
|
|
|
|
2009-09-06 05:59:30 +00:00
|
|
|
def refresh(base=nil, skip_callbacks=false)
|
2009-09-03 05:42:59 +00:00
|
|
|
previous, current = recache(base || @path.to_pathname)
|
|
|
|
|
2009-09-06 05:59:30 +00:00
|
|
|
unless skip_callbacks
|
|
|
|
deleted(previous, current)
|
|
|
|
created(previous, current)
|
|
|
|
modified(previous, current)
|
|
|
|
end
|
2009-08-30 18:37:24 +00:00
|
|
|
end
|
2009-09-03 05:42:59 +00:00
|
|
|
|
2009-08-30 18:37:24 +00:00
|
|
|
private
|
2009-09-03 05:42:59 +00:00
|
|
|
|
2009-08-30 18:37:24 +00:00
|
|
|
def created(previous, current)
|
|
|
|
(current.keys - previous.keys).each {|created| @path.create(created)}
|
|
|
|
end
|
2009-09-03 05:42:59 +00:00
|
|
|
|
2009-08-30 18:37:24 +00:00
|
|
|
def deleted(previous, current)
|
|
|
|
(previous.keys - current.keys).each {|deleted| @path.delete(deleted)}
|
|
|
|
end
|
2009-09-03 05:42:59 +00:00
|
|
|
|
2009-08-30 18:37:24 +00:00
|
|
|
def modified(previous, current)
|
|
|
|
(current.keys & previous.keys).each do |file|
|
|
|
|
@path.update(file) if (current[file] <=> previous[file]) != 0
|
|
|
|
end
|
|
|
|
end
|
2009-09-03 05:42:59 +00:00
|
|
|
|
|
|
|
def recache(base)
|
|
|
|
base = Pathname.for(base)
|
|
|
|
previous = @cache.files
|
|
|
|
snapshot(base)
|
|
|
|
current = @cache.files
|
|
|
|
[previous, current]
|
|
|
|
end
|
|
|
|
|
|
|
|
def snapshot(base)
|
|
|
|
base = Pathname.for(base)
|
|
|
|
@cache.unset(base)
|
|
|
|
@path.glob.each {|glob| add_glob(base, glob)}
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_glob(base, glob)
|
2009-11-19 18:53:20 +00:00
|
|
|
Pathname.glob(base.join(glob).to_s).each do |fn|
|
2009-09-03 05:42:59 +00:00
|
|
|
@cache.set(fn)
|
2009-08-30 18:37:24 +00:00
|
|
|
end
|
|
|
|
end
|
2009-09-03 05:42:59 +00:00
|
|
|
|
2009-08-30 18:37:24 +00:00
|
|
|
end
|