Class: Checksum::Tools::Local

Inherits:
Base
  • Object
show all
Defined in:
lib/checksum-tools/local.rb

Constant Summary collapse

@@digests =
{}

Instance Attribute Summary collapse

Attributes inherited from Base

#host

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#create_digest_file, #create_digest_files, #digest_filename, #digest_files, #verify_digest_file, #verify_digest_files, #verify_file

Constructor Details

#initialize(*args) ⇒ Local

Returns a new instance of Local.


38
39
40
41
# File 'lib/checksum-tools/local.rb', line 38

def initialize(*args)
  super(*args)
  @digest_types.each { |type| self.class.digest_for(type) }
end

Instance Attribute Details

#digest_typesObject (readonly)

Returns the value of attribute digest_types


35
36
37
# File 'lib/checksum-tools/local.rb', line 35

def digest_types
  @digest_types
end

#optsObject (readonly)

Returns the value of attribute opts


36
37
38
# File 'lib/checksum-tools/local.rb', line 36

def opts
  @opts
end

Class Method Details

.digest_for(key) ⇒ Object


22
23
24
25
26
27
28
# File 'lib/checksum-tools/local.rb', line 22

def digest_for(key)
  handler = @@digests[key]
  if handler.nil?
    raise ArgumentError, "undefined digest class: #{key.to_s}"
  end
  return handler.call()
end

.digestsObject


30
31
32
# File 'lib/checksum-tools/local.rb', line 30

def digests
  @@digests.keys
end

.register_digest(key, file, &block) ⇒ Object


8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/checksum-tools/local.rb', line 8

def register_digest(key, file, &block)
  if @@digests.has_key?(key)
    return false
  else
    begin
      require file
      @@digests[key] = block
      return key
    rescue LoadError
      return false
    end
  end
end

Instance Method Details

#digest_file(filename) ⇒ Object


51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/checksum-tools/local.rb', line 51

def digest_file(filename)
  if File.exists?(filename)
    size = file_size(filename)
    block = block_given? ? lambda { |pos| yield(filename, size, pos) } : nil
    File.open(filename, 'r') do |io|
      yield(filename, size, 0) if block_given?
      return digest_stream(io, &block)
    end
  else
    raise Errno::ENOENT, filename
  end
end

#digest_length(type) ⇒ Object


47
48
49
# File 'lib/checksum-tools/local.rb', line 47

def digest_length(type)
  self.class.digest_for(type).hexdigest.length
end

#digest_stream(io) ⇒ Object


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/checksum-tools/local.rb', line 64

def digest_stream(io)
  output = digest_types.inject({}) { |collector,key| collector[key] = self.class.digest_for(key); collector }
  io.rewind
  while chunk = io.read(CHUNK_SIZE)
    output.values.each do |digest|
      digest << chunk
    end
    if block_given?
      yield(io.pos)
    end
  end
  output.each_pair do |type,digest|
    output[type] = digest.hexdigest
  end
  return output
end

#digestsObject


43
44
45
# File 'lib/checksum-tools/local.rb', line 43

def digests
  self.class.digests
end

#verify_stream(io, hashes, &block) ⇒ Object


81
82
83
84
85
86
87
88
89
90
# File 'lib/checksum-tools/local.rb', line 81

def verify_stream(io, hashes, &block)
  with_types(hashes.keys) do
    actual = digest_stream(io, &block)
    result = {}
    actual.each_pair do |hash,digest|
      result[hash] = hashes[hash] == digest
    end
    result
  end
end