Module: LogicalConstruct::Protocol::PlanValidation
- Included in:
- NodeClient::ManifestBuilder, ResolutionServer::ResolutionMethods::ResolutionMethod, ResolutionServer::States::PlanState
- Defined in:
- lib/logical-construct/protocol/plan-validation.rb
Defined Under Namespace
Classes: DigestFailure
Constant Summary collapse
- BIG_CHUNK =
The biggest digest-block-length-aligned chunk under 4MB
4 * 1024 * 1024
Instance Method Summary collapse
- #check_digest(checksum, path, target_path = nil) ⇒ Object
- #chunk_size ⇒ Object
- #digest ⇒ Object
- #file_checksum(path) ⇒ Object
- #generate_checksum(data) ⇒ Object
- #realpath(path) ⇒ Object
Instance Method Details
#check_digest(checksum, path, target_path = nil) ⇒ Object
11 12 13 14 15 |
# File 'lib/logical-construct/protocol/plan-validation.rb', line 11 def check_digest(checksum, path, target_path=nil) if file_checksum(path) != checksum raise DigestFailure, "Digest failure for #{target_path || path}" end end |
#chunk_size ⇒ Object
19 20 21 |
# File 'lib/logical-construct/protocol/plan-validation.rb', line 19 def chunk_size @chunk_size ||= (BIG_CHUNK / digest.block_length).floor * digest.block_length end |
#digest ⇒ Object
7 8 9 |
# File 'lib/logical-construct/protocol/plan-validation.rb', line 7 def digest @digest ||= OpenSSL::Digest::SHA256.new end |
#file_checksum(path) ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/logical-construct/protocol/plan-validation.rb', line 29 def file_checksum(path) digest.reset File::open(realpath(path)) do |file| while chunk = file.read(chunk_size) digest.update(chunk) end end digest.hexdigest end |
#generate_checksum(data) ⇒ Object
39 40 41 42 43 |
# File 'lib/logical-construct/protocol/plan-validation.rb', line 39 def generate_checksum(data) digest.reset digest << data digest.hexdigest end |
#realpath(path) ⇒ Object
23 24 25 26 27 |
# File 'lib/logical-construct/protocol/plan-validation.rb', line 23 def realpath(path) File::readlink(path.to_s) rescue Errno::EINVAL, Errno::ENOENT path end |