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, detect_hidden = false) ⇒ Bag

Make a new Bag based at path



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

def initialize(path, info = {}, _create = false, detect_hidden = false)
  @bag_dir = path
  @detect_hidden = detect_hidden
  @file_finder = @detect_hidden ? StandardWithHiddenFileFinder : StandardFileFinder

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



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

def bag_dir
  @bag_dir
end

#detect_hiddenObject (readonly)

Returns the value of attribute detect_hidden.



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

def detect_hidden
  @detect_hidden
end

Instance Method Details

#add_file(relative_path, src_path = nil) ⇒ Object

Add a bag file at the given path relative to data_dir



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/bagit/bag.rb', line 77

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



61
62
63
# File 'lib/bagit/bag.rb', line 61

def bag_files
  @file_finder.find(data_dir)
end

#data_dirObject

Return the path to the data directory



56
57
58
# File 'lib/bagit/bag.rb', line 56

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

#empty?Boolean

Test if this bag is empty (no files)

Returns:

  • (Boolean)


107
108
109
# File 'lib/bagit/bag.rb', line 107

def empty?
  bag_files.empty?
end

#gc!Object

Remove all empty directory trees from the bag



127
128
129
130
131
132
133
134
# File 'lib/bagit/bag.rb', line 127

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



100
101
102
103
104
# File 'lib/bagit/bag.rb', line 100

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



112
113
114
# File 'lib/bagit/bag.rb', line 112

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

#payload_oxumObject

Get the Oxum for the payload files



117
118
119
120
121
122
123
124
# File 'lib/bagit/bag.rb', line 117

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



92
93
94
95
96
# File 'lib/bagit/bag.rb', line 92

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



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

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