Class: Checksum::Tools::Base

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

Direct Known Subclasses

Local, Remote

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Base

Returns a new instance of Base.


35
36
37
38
39
40
41
42
43
# File 'lib/checksum-tools.rb', line 35

def initialize(*args)
  @opts = DEFAULT_OPTS
  if args.last.is_a?(Hash)
    @opts.merge!(args.pop)
  end
  @opts[:exclude] << digest_filename("*")
  @opts[:exclude].uniq!
  @digest_types = args
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host


33
34
35
# File 'lib/checksum-tools.rb', line 33

def host
  @host
end

Instance Method Details

#create_digest_file(filename, &block) ⇒ Object


56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/checksum-tools.rb', line 56

def create_digest_file(filename, &block)
  digest_file = digest_filename(filename)
  if File.expand_path(filename) == File.expand_path(digest_file)
    raise ArgumentError, "Digest file #{digest_file} will clobber content file #{filename}!"
  end
  if opts[:overwrite] or not file_exists?(digest_file)
    result = digest_file(filename, &block)
    file_open(digest_file,'w') do |cksum|
      result.each_pair do |type,hexdigest| 
        cksum.puts("#{type.to_s.upcase}(#{File.basename(filename)})= #{hexdigest}")
      end
    end
  end
end

#create_digest_files(base_dir, file_masks, &block) ⇒ Object


49
50
51
52
53
54
# File 'lib/checksum-tools.rb', line 49

def create_digest_files(base_dir, file_masks, &block)
  process_files(base_dir, file_masks) do |filename|
    yield(filename, -1, -1) if block_given?
    create_digest_file(filename, &block)
  end
end

#digest_filename(filename) ⇒ Object


45
46
47
# File 'lib/checksum-tools.rb', line 45

def digest_filename(filename)
  "#{filename}.#{opts[:extension].sub(/^\.+/,'')}"
end

#digest_files(base_dir, file_masks, &block) ⇒ Object


71
72
73
74
75
76
# File 'lib/checksum-tools.rb', line 71

def digest_files(base_dir, file_masks, &block)
  process_files(base_dir, file_masks) do |filename|
    yield(filename, -1, -1) if block_given?
    digest_file(filename, &block)
  end
end

#verify_digest_file(filename, &block) ⇒ Object


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/checksum-tools.rb', line 87

def verify_digest_file(filename, &block)
  digest_file = digest_filename(filename)
  unless file_exists?(digest_file)
    return { :digest_file => false }
  end

  hashes = {}
  digest_data = file_read(digest_file)

  ext_type = File.extname(digest_file)[1..-1].downcase.to_sym
  # Check to see if the digest file extension implies a specific digest type (e.g., .md5)
  if digests.include?(ext_type)
    # Find a hex value of the correct length in the digest file
    len = self.digest_length(ext_type)
    digest = digest_data.scan(/\b((?:[0-9a-f]{4})+)\b/im).flatten.find { |d| d.length == len }
    hashes[ext_type] = digest
  else
    digest_data = digest_data.split(/\n/)
    hashes = digest_data.inject({}) do |collector,sum|
      (hash,hashed_fname,digest) = sum.scan(/(.+)\((.+)\)= (.+)/).flatten
      unless File.basename(hashed_fname) == File.basename(filename)
        warn "WARNING: Filename mismatch in #{digest_file}: #{File.basename(hashed_fname)}"
      end
      collector[hash.downcase.to_sym] = digest
      collector
    end
  end
  verify_file(filename, hashes, &block)
end

#verify_digest_files(base_dir, file_masks, &block) ⇒ Object


78
79
80
81
82
83
84
85
# File 'lib/checksum-tools.rb', line 78

def verify_digest_files(base_dir, file_masks, &block)
  process_files(base_dir, file_masks) do |filename|
    yield(filename, -1, -1) if block_given?
    result = verify_digest_file(filename, &block)
    yield(filename, -1, 0, result)
    result
  end
end

#verify_file(filename, hashes, &block) ⇒ Object


117
118
119
120
121
122
123
124
125
126
# File 'lib/checksum-tools.rb', line 117

def verify_file(filename, hashes, &block)
  with_types(hashes.keys) do
    actual = digest_file(filename, &block)
    result = {}
    actual.each_pair do |hash,digest|
      result[hash] = hashes[hash].downcase == digest.downcase
    end
    result
  end
end