Class: Archive::FileFixity
- Inherits:
-
Object
- Object
- Archive::FileFixity
- Defined in:
- lib/archive/file_fixity.rb
Overview
Copyright © 2014 by The Board of Trustees of the Leland Stanford Junior University. All rights reserved. See LICENSE for details.
The fixity properties of a file, used to determine file content equivalence. Placing this data in a class by itself facilitates using the MD5, SHA1, etc checksums (and optionally the file size) as a single key when doing comparisons against other file instances. The design assumes that this file fixity is sufficiently unique to act as a comparator for determining file equality or verifying checksum manifests.
Instance Attribute Summary collapse
-
#bytes ⇒ Integer
The size of the file in bytes.
-
#checksums ⇒ Hash<Symbol,String>
The MD5, SHA1, SHA256, etc checksum values of the file.
-
#file_id ⇒ String
The name of the file, relative to its base directory (for payload files, path relative to the data folder. For tag files, path relative to the bag home folder).
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Returns true if self and other have comparable fixity data.
-
#diff(other, left = 'base', right = 'other') ⇒ Hash<symbol,Hash<String,String>] details of the checksum differences between fixity objects
Hash<symbol,Hash<String,String>] details of the checksum differences between fixity objects.
-
#eql?(other) ⇒ Boolean
Returns true if self and other have comparable fixity data.
-
#get_checksum(type) ⇒ String
The value of the file digest.
-
#hash ⇒ Fixnum
Compute a hash-code for the fixity value array.
-
#initialize(options = nil) ⇒ FileFixity
constructor
A new instance of FileFixity.
-
#set_checksum(type, value) ⇒ void
Set the value for the specified checksum type in the checksum hash.
Constructor Details
#initialize(options = nil) ⇒ FileFixity
Returns a new instance of FileFixity.
16 17 18 19 20 21 22 23 |
# File 'lib/archive/file_fixity.rb', line 16 def initialize(=nil) @checksums=Hash.new = {} if .nil? .each do |key,value| #instance_variable_set("@#{key}", value) send "#{key}=", value end end |
Instance Attribute Details
#bytes ⇒ Integer
Returns The size of the file in bytes.
30 31 32 |
# File 'lib/archive/file_fixity.rb', line 30 def bytes @bytes end |
#checksums ⇒ Hash<Symbol,String>
Returns The MD5, SHA1, SHA256, etc checksum values of the file.
33 34 35 |
# File 'lib/archive/file_fixity.rb', line 33 def checksums @checksums end |
#file_id ⇒ String
Returns The name of the file, relative to its base directory (for payload files, path relative to the data folder. For tag files, path relative to the bag home folder).
27 28 29 |
# File 'lib/archive/file_fixity.rb', line 27 def file_id @file_id end |
Instance Method Details
#==(other) ⇒ Boolean
Returns true if self and other have comparable fixity data.
63 64 65 |
# File 'lib/archive/file_fixity.rb', line 63 def ==(other) eql?(other) end |
#diff(other, left = 'base', right = 'other') ⇒ Hash<symbol,Hash<String,String>] details of the checksum differences between fixity objects
Returns Hash<symbol,Hash<String,String>] details of the checksum differences between fixity objects.
82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/archive/file_fixity.rb', line 82 def diff(other,left='base',right='other') diff_hash = Hash.new matching_checksum_types = (self.checksums.keys & other.checksums.keys) matching_checksum_types = (self.checksums.keys | other.checksums.keys) if matching_checksum_types.empty? matching_checksum_types.each do |type| base_checksum = self.checksums[type] other_checksum = other.checksums[type] if base_checksum != other_checksum diff_hash[type] = {left => base_checksum, right => other_checksum } end end return diff_hash.size > 0 ? diff_hash : nil end |
#eql?(other) ⇒ Boolean
Returns true if self and other have comparable fixity data.
53 54 55 56 57 58 59 60 |
# File 'lib/archive/file_fixity.rb', line 53 def eql?(other) matching_checksum_types = self.checksums.keys & other.checksums.keys return false if matching_checksum_types.size == 0 matching_checksum_types.each do |type| return false if self.checksums[type] != other.checksums[type] end true end |
#get_checksum(type) ⇒ String
Returns The value of the file digest.
37 38 39 40 |
# File 'lib/archive/file_fixity.rb', line 37 def get_checksum(type) checksum_type = type.to_s.downcase.to_sym self.checksums[checksum_type] end |
#hash ⇒ Fixnum
Returns Compute a hash-code for the fixity value array. Two file instances with the same content will have the same hash code (and will compare using eql?).
74 75 76 |
# File 'lib/archive/file_fixity.rb', line 74 def hash [self.file_id].hash end |
#set_checksum(type, value) ⇒ void
This method returns an undefined value.
Returns Set the value for the specified checksum type in the checksum hash.
45 46 47 48 49 |
# File 'lib/archive/file_fixity.rb', line 45 def set_checksum(type,value) checksum_type = type.to_s.downcase.to_sym Fixity.validate_checksum_types(checksum_type) self.checksums[checksum_type] = value end |