Skip to content

Commit

Permalink
Merge pull request #9216 from ndr-qef/core/verify
Browse files Browse the repository at this point in the history
Separate verification from download
  • Loading branch information
ndr committed Jan 25, 2015
2 parents 9b7fa81 + eb25dc5 commit 9103324
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 25 deletions.
1 change: 1 addition & 0 deletions lib/hbc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Hbc; end
require 'hbc/url'
require 'hbc/url_checker'
require 'hbc/utils'
require 'hbc/verify'
require 'hbc/version'

require 'vendor/plist'
Expand Down
29 changes: 4 additions & 25 deletions lib/hbc/download.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
require 'digest'
require 'hbc/verify'

class Hbc::Download
attr_reader :cask

include Hbc::Verify

def initialize(cask)
@cask = cask
end
Expand All @@ -27,31 +30,7 @@ def perform(force=false)
HOMEBREW_CACHE_CASKS.join(downloaded_path.basename)
rescue StandardError => e
end
_compare_sha256(downloaded_path, cask)
Hbc::Verify.checksum(downloaded_path, cask)
downloaded_path
end

private
def _calc_sha256(path)
require 'digest/sha2'
Digest::SHA2.file(path).hexdigest
end

def _compare_sha256(path, cask)
begin
expected = cask.sha256
rescue RuntimeError => e
end
return if expected == :no_check
computed = _calc_sha256(path)
if expected.nil? or expected.empty?
raise Hbc::CaskSha256MissingError.new("sha256 required: sha256 '#{computed}'")
end
if expected == computed
odebug "SHA256 checksums match"
else
ohai 'Note: running "brew update" may fix sha256 checksum errors'
raise Hbc::CaskSha256MismatchError.new(path, expected, computed)
end
end
end
7 changes: 7 additions & 0 deletions lib/hbc/installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'hbc/cask_dependencies'
require 'hbc/staged'
require 'hbc/verify'

class Hbc::Installer

Expand All @@ -11,6 +12,7 @@ class Hbc::Installer
# should be explicit checks on whether staged state is valid in
# every method.
include Hbc::Staged
include Hbc::Verify

PERSISTENT_METADATA_SUBDIRS = [ 'gpg' ]

Expand Down Expand Up @@ -59,6 +61,7 @@ def install(force=false, skip_cask_deps=false)
begin
satisfy_dependencies(skip_cask_deps)
download
verify
extract_primary_container
install_artifacts
save_caskfile force
Expand Down Expand Up @@ -88,6 +91,10 @@ def download
@downloaded_path
end

def verify
Hbc::Verify.all(@downloaded_path, @cask)
end

def extract_primary_container
odebug "Extracting primary container"
FileUtils.mkdir_p @cask.staged_path
Expand Down
25 changes: 25 additions & 0 deletions lib/hbc/verify.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'digest'

module Hbc::Verify
def self.all(path, cask)
checksum(path, cask)
end

def self.checksum(path, cask)
begin
expected = cask.sha256
rescue RuntimeError => e
end
return if expected == :no_check
computed = Digest::SHA2.file(path).hexdigest
if expected.nil? or expected.empty?
raise Hbc::CaskSha256MissingError.new("sha256 required: sha256 '#{computed}'")
end
if expected == computed
odebug "SHA256 checksums match"
else
ohai 'Note: running "brew update" may fix sha256 checksum errors'
raise Hbc::CaskSha256MismatchError.new(path, expected, computed)
end
end
end

0 comments on commit 9103324

Please sign in to comment.