Class: Fingerprint::Checker

Inherits:
Object
  • Object
show all
Defined in:
lib/fingerprint/checker.rb

Overview

Given two fingerprints (master and copy) ensures that the copy has at least everything contained in master: For every file in the master, a corresponding file must exist in the copy. This means that there may be extraneous files in the copy, but ensures that every file in the master has been replicated accurately.

At this time, this implementation may require a large amount of memory, proportional to the number of files being checked.

Master and copy are IO objects corresponding to the output produced by Fingerprint::Scanner.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(master, copy, options = {}) ⇒ Checker

Returns a new instance of Checker.



34
35
36
37
38
39
40
41
42
43
# File 'lib/fingerprint/checker.rb', line 34

def initialize(master, copy, options = {})
	@master = master
	@copy = copy
	
	@mismatches = []
	
	@options = options
	
	@failures = []
end

Instance Attribute Details

#failuresObject (readonly)

A list of files which either did not exist in the copy, or had the wrong checksum.



81
82
83
# File 'lib/fingerprint/checker.rb', line 81

def failures
  @failures
end

#file_hashesObject (readonly)

A hash of all files in copy file hash => [file1, file2, …]



90
91
92
# File 'lib/fingerprint/checker.rb', line 90

def file_hashes
  @file_hashes
end

#file_pathsObject (readonly)

A hash of all files in copy path => file hash



87
88
89
# File 'lib/fingerprint/checker.rb', line 87

def file_paths
  @file_paths
end

#filesObject (readonly)

An array of all files in the copy



84
85
86
# File 'lib/fingerprint/checker.rb', line 84

def files
  @files
end

Class Method Details

.check_files(master, copy, &block) ⇒ Object

Helper function to check two fingerprint files.



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/fingerprint/checker.rb', line 93

def self.check_files(master, copy, &block)
	error_count = 0 
	
	master = File.open(master) unless master.respond_to? :read
	copy = File.open(copy) unless copy.respond_to? :read
	
	checker = Checker.new(master, copy)

	checker.check do |hash, path|
		error_count += 1

		if !checker.file_paths[path]
			$stderr.puts "File #{path.dump} is missing!"
		elsif checker.file_paths[path] != hash
			$stderr.puts "File #{path.dump} is different!"
		else
			$stderr.puts "Unknown error for path #{path.dump}"
		end
	end

	return error_count
end

Instance Method Details

#check(options = {}, &block) ⇒ Object

Run the checking process.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/fingerprint/checker.rb', line 46

def check (options = {}, &block)
	@files = Set.new
	@file_paths = {}
	@file_hashes = {}

	# Parse original fingerprint
	@copy.each_line do |line|
		# Skip comments
		next if line.match(/^\s+#/)
		
		if line.chomp.match(/^([a-fA-F0-9]{32}): (.*)$/)
			@files.add([$1, $2])

			@file_paths[$2] = $1
			@file_hashes[$1] ||= Set.new
			@file_hashes[$1].add($2)
		end
	end

	# For every file in the src, we check that it exists
	# in the destination:
	@master.each_line do |line|
		# Skip comments
		next if line.match(/^\s+#/)
		
		if line.chomp.match(/^([a-fA-F0-9]{32}): (.*)$/)
			unless @files.include?([$1, $2])
				yield($1, $2) if block_given?
				@failures << [$1, $2]
			end
		end
	end
end