Class: BagIt::Bag

Inherits:
Object
  • Object
show all
Includes:
Fetch, Info, Manifest, Validity, Validatable
Defined in:
lib/bagit/bag.rb,
lib/bagit/valid.rb

Overview

Represents the state of a bag on a filesystem

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Fetch

#add_remote_file, #fetch!, #fetch_txt_file, #move_current_fetch_txt, #rename_old_fetch_txt

Methods included from Manifest

#add_tag_file, #delete_tag_file, #encode_filename, #fixed?, #manifest!, #manifest_file, #manifest_files, #remove_tag_file, #tagmanifest!, #tagmanifest_file, #tagmanifest_files, #write_checksum, #write_md5, #write_sha1, #write_sha256, #write_sha512

Methods included from Info

#bag_info, #bag_info_txt_file, #bagit, #bagit_txt_file, #update_bag_date, #write_bag_info, #write_bagit

Methods included from Validity

#complete?, #consistent?, #decode_filename, #manifest_type, #valid_oxum?

Constructor Details

#initialize(path, info = {}, _create = false) ⇒ Bag

Make a new Bag based at path



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/bagit/bag.rb', line 19

def initialize(path, info = {}, _create = false)
  @bag_dir = path
  # make the dir structure if it doesn't exist
  FileUtils.mkdir bag_dir unless File.directory? bag_dir
  FileUtils.mkdir data_dir unless File.directory? data_dir

  # write some tag info if its not there
  write_bagit("BagIt-Version" => SPEC_VERSION, "Tag-File-Character-Encoding" => "UTF-8") unless File.exist? bagit_txt_file

  write_bag_info(info) unless File.exist? bag_info_txt_file
end

Instance Attribute Details

#bag_dirObject (readonly)

Returns the value of attribute bag_dir.



11
12
13
# File 'lib/bagit/bag.rb', line 11

def bag_dir
  @bag_dir
end

Instance Method Details

#add_file(relative_path, src_path = nil) ⇒ Object

Add a bag file at the given path relative to data_dir



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

def add_file(relative_path, src_path = nil)
  path = File.join(data_dir, relative_path)
  raise "Bag file exists: #{relative_path}" if File.exist? path
  FileUtils.mkdir_p File.dirname(path)

  f = if src_path.nil?
        File.open(path, 'w') { |io| yield io }
      else
        FileUtils.cp src_path, path
      end
  write_bag_info
  f
end

#bag_filesObject

Return the paths to each bag file relative to bag_dir



37
38
39
# File 'lib/bagit/bag.rb', line 37

def bag_files
  Dir[File.join(data_dir, '**', '*')].select { |f| File.file? f }
end

#data_dirObject

Return the path to the data directory



32
33
34
# File 'lib/bagit/bag.rb', line 32

def data_dir
  File.join @bag_dir, 'data'
end

#empty?Boolean

Test if this bag is empty (no files)

Returns:

  • (Boolean)


83
84
85
# File 'lib/bagit/bag.rb', line 83

def empty?
  bag_files.empty?
end

#gc!Object

Remove all empty directory trees from the bag



103
104
105
106
107
108
109
110
# File 'lib/bagit/bag.rb', line 103

def gc!
  Dir.entries(data_dir).each do |f|
    unless %w[.. .].include? f
      abs_path = File.join data_dir, f
      File.clean abs_path
    end
  end
end

#get(relative_path) ⇒ Object

Retrieve the IO handle for a file in the bag at a given path relative to data_dir



76
77
78
79
80
# File 'lib/bagit/bag.rb', line 76

def get(relative_path)
  path = File.join(data_dir, relative_path)
  return nil unless File.exist?(path)
  File.open(path)
end

#pathsObject

Get all bag file paths relative to the data dir



88
89
90
# File 'lib/bagit/bag.rb', line 88

def paths
  bag_files.collect { |f| f.sub(data_dir + '/', '') }
end

#payload_oxumObject

Get the Oxum for the payload files



93
94
95
96
97
98
99
100
# File 'lib/bagit/bag.rb', line 93

def payload_oxum
  bytes = 0
  bag_files.each do |f|
    # TODO: filesystem quirks? Are we getting the stream size or the size on disk?
    bytes += File.size(f)
  end
  bytes.to_s + '.' + bag_files.count.to_s
end

#remove_file(relative_path) ⇒ Object

Remove a bag file at the given path relative to data_dir



68
69
70
71
72
# File 'lib/bagit/bag.rb', line 68

def remove_file(relative_path)
  path = File.join(data_dir, relative_path)
  raise "Bag file does not exist: #{relative_path}" unless File.exist? path
  FileUtils.rm path
end

#tag_filesObject

Return the paths to each tag file relative to bag_dir



42
43
44
45
46
47
48
49
50
# File 'lib/bagit/bag.rb', line 42

def tag_files
  files = []
  if tagmanifest_files != []
    File.open(tagmanifest_files.first) do |f|
      f.each_line { |line| files << File.join(@bag_dir, line.split(' ')[1]) }
    end
  end
  files
end