require 'yaml' require 'yaml/store' require 'digest/md5' module Cbt class Digester # initialize from the configuration def initialize store_file @store_file = store_file @store = YAML::Store.new @store_file end def file_digest file return "" unless File.exists? file d = Digest::MD5.hexdigest(File.read(file)) end # add file copy history def add_history file, digest = nil digest = file_digest file unless digest @store.transaction do @store[file] = Array.new unless @store[file] @store[file] << { 'datetime' => DateTime.now.strftime("%F %T"), 'digest' => digest } end end # return 'datetime' => '', 'digest' => '' or nil for no store def last_change file @store.transaction do @store[file][-1] if @store[file] end end # if the digest is different from last time def changed? file, digest = nil digest = file_digest file unless digest @store.transaction do not (@store[file] and @store[file][-1] and @store[file][-1]['digest'] == digest) end end # if the current digest exists in file store def known? file, digest = nil digest = file_digest file unless digest @store.transaction do return false unless @store[file] @store[file].each do |h| return true if h['digest'] == digest end end false end end end