Class: Archive::FileFixity

Inherits:
Object
  • Object
show all
Defined in:
lib/archive/file_fixity.rb

Overview

Note:

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

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ FileFixity

Returns a new instance of FileFixity.

Parameters:

  • options (Hash<Symbol,Object>) (defaults to: nil)

    Key,Value pairs specifying initial values of attributes



16
17
18
19
20
21
22
23
# File 'lib/archive/file_fixity.rb', line 16

def initialize(options=nil)
  @checksums=Hash.new
  options = {} if options.nil?
  options.each do |key,value|
    #instance_variable_set("@#{key}", value)
    send "#{key}=", value
  end
end

Instance Attribute Details

#bytesInteger

Returns The size of the file in bytes.

Returns:

  • (Integer)

    The size of the file in bytes



30
31
32
# File 'lib/archive/file_fixity.rb', line 30

def bytes
  @bytes
end

#checksumsHash<Symbol,String>

Returns The MD5, SHA1, SHA256, etc checksum values of the file.

Returns:

  • (Hash<Symbol,String>)

    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_idString

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).

Returns:

  • (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)



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.

Parameters:

  • other (FileFixity)

    The other file fixity being compared to this fixity

Returns:

  • (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.

Parameters:

  • other (FileFixity)

    The other FileFixity object being compared to this one

  • left (String) (defaults to: 'base')

    The label to use for values from this base FileFixity object

  • right (String) (defaults to: 'other')

    he label to use for values from the other FileFixity object

Returns:

  • (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



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.

Parameters:

  • other (FileFixity)

    The other file fixity being compared to this fixity

Returns:

  • (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.

Parameters:

  • type (Symbol, String)

    The type of checksum (e.g. :md5, :sha1, :sha256)

Returns:

  • (String)

    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

#hashFixnum

Note:

The hash and eql? methods override the methods inherited from Object. These methods ensure that instances of this class can be used as Hash keys. See

Also overriden is #== so that equality tests in other contexts will also return the expected result.

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?).

Returns:

  • (Fixnum)

    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.

Parameters:

  • type (Symbol, String)

    The type of checksum

  • value (String)

    value of the file digest



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