Class: LyberUtils::ChecksumValidate

Inherits:
Object
  • Object
show all
Defined in:
lib/lyber_utils/checksum_validate.rb

Class Method Summary collapse

Class Method Details

.compare_hashes(hash1, hash2) ⇒ Boolean

Test the equality of two hashes

Returns:

  • (Boolean)


10
11
12
# File 'lib/lyber_utils/checksum_validate.rb', line 10

def self.compare_hashes(hash1, hash2)
  return (hash1 == hash2)
end

.get_hash_differences(hash1, hash2) ⇒ Hash

create a new hash containing: entries from hash1 where:

  • key is in hash1 but missing from hash2

  • value is different in the two hashes

entries from hash2 where:

  • key is in hash2 but missing from hash1

Returns:

  • (Hash)


21
22
23
# File 'lib/lyber_utils/checksum_validate.rb', line 21

def self.get_hash_differences(hash1, hash2)
  hash1.reject { |k, v| hash2[k] == v }.merge!(hash2.reject { |k, v| hash1.has_key?(k) })
end

.md5_hash_from_content_metadata(content_md) ⇒ Hash

Generate a filename => checksum hash from contentMetadata XML

Returns:

  • (Hash)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/lyber_utils/checksum_validate.rb', line 62

def self.(content_md)
  content_md_checksum_hash = {}
  doc = Nokogiri::XML(content_md)
  doc.xpath('/contentMetadata/resource[@type="page"]/file').each do |filenode|
    filename = filenode.attribute('id')
    if (filename)
      md5_element = filenode.xpath('checksum[@type="MD5"]').first
      if (md5_element)
        digest = md5_element.text
        if (digest)
          content_md_checksum_hash[filename.text] = digest.downcase
        end
      end
    end
  end
  return content_md_checksum_hash
end

.md5_hash_from_md5sum(md5sum) ⇒ Hash

Generate a filename => checksum hash from an output of the md5sum (or compatible) program

Returns:

  • (Hash)


28
29
30
31
32
33
34
35
36
# File 'lib/lyber_utils/checksum_validate.rb', line 28

def self.md5_hash_from_md5sum(md5sum)
  checksum_hash = {}
  md5sum.each do |line|
    line.chomp!
    digest, filename = line.split(/[ *]{2}/)
    checksum_hash[filename] = digest.downcase
  end
  return checksum_hash
end

.md5_hash_from_mets(mets) ⇒ Hash

Generate a filename => checksum hash from the contents of a METS file

Returns:

  • (Hash)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/lyber_utils/checksum_validate.rb', line 41

def self.md5_hash_from_mets(mets)
  mets_checksum_hash = {}
  doc = Nokogiri::XML(mets)
  doc.xpath('/mets:mets/mets:fileSec//mets:file', {'mets' => 'http://www.loc.gov/METS/'}).each do |filenode|
    digest = filenode.attribute('CHECKSUM')
    if (digest)
      flocat = filenode.xpath('mets:FLocat', {'mets' => 'http://www.loc.gov/METS/'}).first
      if (flocat)
        filename = flocat.attribute_with_ns('href', 'http://www.w3.org/1999/xlink')
        if (filename)
          mets_checksum_hash[filename.text] = digest.text.downcase
        end
      end
    end
  end
  return mets_checksum_hash
end

.verify_md5sum_checksums(directory, checksum_file) ⇒ Object

Verifies MD5 checksums for the files in a directory against the checksum values in the supplied file (Uses md5sum command)

Inputs:

  • directory = dirname containing the file to be checked

  • checksum_file = the name of the file containing the expected checksums

Return value:

  • The method will return true if the verification is successful.

  • The method will raise an exception if either the md5sum command fails,

or a test of the md5sum output indicates a checksum mismatch. The exception’s message will contain the explaination of the failure.



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/lyber_utils/checksum_validate.rb', line 93

def self.verify_md5sum_checksums(directory, checksum_file)
#      LyberCore::Log.debug("verifying checksums in #{directory}")
  dir_save = Dir.pwd
  Dir.chdir(directory)
  checksum_cmd = 'md5sum -c ' + checksum_file + ' | grep -v OK | wc -l'
  badcount = FileUtilities.execute(checksum_cmd).to_i
  if not (badcount==0)
    raise "#{badcount} files had bad checksums"
  end
  return true
ensure
  Dir.chdir(dir_save)
end