Module: BagIt::Validity

Included in:
Bag
Defined in:
lib/bagit/valid.rb

Instance Method Summary collapse

Instance Method Details

#complete?Boolean

Return true if the manifest cover all files and all files are covered.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/bagit/valid.rb', line 24

def complete?
  logger = Logger.new(STDOUT)

  errors.add :completeness, "there are no manifest files" if manifest_files == []

  unmanifested_files.each do |file|
    logger.error("#{file} is present but not manifested".red)
    errors.add :completeness, "#{file} is present but not manifested"
  end

  empty_manifests.each do |file|
    logger.error("#{file} is manifested but not present".red)
    error_message = "#{file} is manifested but not present"
    if !detect_hidden && file.start_with?(File.join("data", "."))
      error_message += "; consider turning on hidden file detection"
    end
    errors.add :completeness, error_message
  end
  tag_empty_manifests.each do |file|
    logger.error("#{file} is a manifested tag but not present".red)
    errors.add :completeness, "#{file} is a manifested tag but not present"
  end

  errors.on(:completeness).nil?
end

#consistent?Boolean

Return true if all manifested files message digests match.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/bagit/valid.rb', line 68

def consistent?
  (manifest_files | tagmanifest_files).each do |mf|
    # get the algorithm implementation
    File.basename(mf) =~ /manifest-(.+).txt$/
    type = Regexp.last_match(1)
    algo = manifest_type(type)
    # Check every file in the manifest
    File.open(mf) do |io|
      io.each_line do |line|
        expected, path = line.chomp.split(/\s+/, 2)
        file = File.join(bag_dir, decode_filename(path))

        next unless File.exist? file
        actual = algo.file(file).hexdigest
        errors.add :consistency, "expected #{file} to have #{algo}: #{expected}, actual is #{actual}" if expected.downcase != actual
      end
    end
  end

  errors.on(:consistency).nil?
end

#decode_filename(s) ⇒ Object



16
17
18
19
20
# File 'lib/bagit/valid.rb', line 16

def decode_filename(s)
  s = s.gsub("%0D", "\r")
  s = s.gsub("%0A", "\n")
  s
end

#manifest_type(type) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/bagit/valid.rb', line 50

def manifest_type(type)
  case type
  when /sha1/i
    Digest::SHA1
  when /md5/i
    Digest::MD5
  when /sha256/i
    Digest::SHA256
  when /sha384/i
    Digest::SHA384
  when /sha512/i
    Digest::SHA512
  else
    raise ArgumentError, "Algorithm #{manifest_type} is not supported."
  end
end

#valid_oxum?Boolean

Checks for validity against Payload-Oxum



91
92
93
# File 'lib/bagit/valid.rb', line 91

def valid_oxum?
  bag_info["Payload-Oxum"] == payload_oxum
end