class Ki::VersionTester

Tests that a version is intact. Version can be in repository or as file. Checks that all files have correct hashes. If recursive is set to true, goes through all dependencies @see test_version

Public Instance Methods

test_version(root_version, &block) click to toggle source

Tests that a version is intact

@see VersionIterator @see RepositoryFinder @return [bool] returns true if there weren't any problems with the version

# File lib/data_access/version_helpers.rb, line 33
def test_version(root_version, &block)
  all_ok = true
  possible_hashes = KiCommand::KiExtensions.find!("/hashing")
  # iterates through all versions
  root_version.version_iterator.iterate_versions do |v|
    binaries = v.binaries
    metadata = v.metadata
    metadata.cached_data
    metadata.files.each do |file_hash|
      file_path = file_hash["path"]
      full_path = binaries.path(file_path)
      issue = nil
      if !File.exists?(full_path)
        issue="missing"
      elsif File.size(full_path) != file_hash["size"]
        issue="wrong size"
      elsif !verify_hash(file_hash, full_path, possible_hashes)
        issue="wrong hash"
      end
      if issue
        all_ok = false
        (results[issue]||=[]) << [v, file_path]
        if block
          block.call(issue, v, file_path)
        end
        if print
          puts "#{v.metadata.path}: '#{file_path}' #{issue} '#{v.binaries.path(file_path)}'"
        end
      end
    end
    if !recursive
      break
    end
  end
  all_ok
end
verify_hash(file_hash, full_path, possible_hashes) click to toggle source
# File lib/data_access/version_helpers.rb, line 70
def verify_hash(file_hash, full_path, possible_hashes)
  file_hashes = possible_hashes.service_names.select { |name| file_hash.include?(name) }
  checked_hashes = VersionMetadataFile.calculate_hashes(full_path, file_hashes)
  checked_hashes.each_pair do |id, result|
    if file_hash[id] != result
      return false
    end
  end
  true
end