Class: Safeguard::Verifier

Inherits:
Worker
  • Object
show all
Includes:
Enumerable
Defined in:
lib/safeguard/verifier.rb

Overview

Rehashes a set of files and compares the results to the stored hashes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Worker

callbacks, has_callback

Constructor Details

#initialize(*args) ⇒ Verifier

Initializes a new verifier with the given files. Last argument can be an options hash or ribbon which specifies the hash table to verify against and the Hasher to use. If a hasher isn’t specified, a new one will be created using the files and options given. If no files were given, the hash table’s list of files will be used.

files  = %w(file1 file2 file3)
hasher = Hasher.new *files, :functions => :crc32
table  = hasher.results

Verifier.new :hasher => hasher, :hash_table => table
Verifier.new :functions => [ :crc32, :md5 ], :hash_table => table
Verifier.new *files, :functions => :crc32, :hash_table => table


34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/safeguard/verifier.rb', line 34

def initialize(*args)
  options = args.extract_ribbon!
  table = options.hash_table? do
    raise ArgumentError, 'No hash table to verify against'
  end
  self.hash_table = Repository::HashTable.new table
  args = hash_table.files if args.empty?
  self.hasher = options.hasher? do
    Hasher.new *args, options
  end
  initialize_callbacks_from options
end

Instance Attribute Details

#hash_tableObject

Hash table to compare results against.



16
17
18
# File 'lib/safeguard/verifier.rb', line 16

def hash_table
  @hash_table
end

#hasherObject

The hasher used to calculate the checksum of the files.



13
14
15
# File 'lib/safeguard/verifier.rb', line 13

def hasher
  @hasher
end

Instance Method Details

#each(&block) ⇒ Object

Yields file => comparison data pairs, or returns an enumerator.



85
86
87
# File 'lib/safeguard/verifier.rb', line 85

def each(&block)
  Ribbon.wrap(results).each &block
end

#resultsObject

Returns the cached data containing the results of the comparison betweeb the recalculated hashes of the files with the data in the hash table.

To force recomputation, call the #verify! method.



80
81
82
# File 'lib/safeguard/verifier.rb', line 80

def results
  @results ||= verify!
end

#verify!Object

Uses the hasher to recalculate the hashes of the files using the specified functions and compares the results with the data on the hash table.

If a given file is not present in the hash table, the result of all comparisons will be the :file_missing symbol. If the value of a given hash for a given file is not present in the hash table, the result of the comparison will be the :hash_missing symbol.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/safeguard/verifier.rb', line 54

def verify!
  results = Ribbon.new
  hasher.each do |file, hash_data|
    hasher.functions.each do |function|
      call_callback before_verifying, file, function
      results[file][function] = result = if hash_table.has_file? file
        if hash_table.has_hash? file, function
          hash_data[function] == hash_table[file][function]
        else
          :hash_missing
        end
      else
        :file_missing
      end
      call_callback after_verifying, file, function, result
    end
  end
  results = Ribbon.wrap results
  results.wrap_all!
  @results = results
end