lib/bagit/bag.rb in bagit-0.2.0 vs lib/bagit/bag.rb in bagit-0.3.0.pre
- old
+ new
@@ -15,11 +15,11 @@
include Info # bagit & bag info functionality
include Manifest # manifest related functionality
include Fetch # fetch related functionality
# Make a new Bag based at path
- def initialize(path)
+ def initialize(path, info={})
@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
@@ -28,11 +28,11 @@
unless File.exist? bagit_txt_file
write_bagit("BagIt-Version" => SPEC_VERSION, "Tag-File-Character-Encoding" => "UTF-8")
end
unless File.exist? bag_info_txt_file
- write_bag_info('Bag-Software-Agent' => "BagIt Ruby Gem (http://bagit.rubyforge.org)")
+ write_bag_info(info)
end
end
# Return the path to the data directory
def data_dir
@@ -44,24 +44,32 @@
Dir[File.join(data_dir, '**', '*')].select { |f| File.file? f }
end
# Return the paths to each tag file relative to bag_dir
def tag_files
- Dir[File.join(@bag_dir, '*')].select { |f| File.file? f }
+ 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
# Add a bag file
def add_file(base_path, src_path=nil)
path = File.join(data_dir, base_path)
raise "Bag file exists: #{base_path}" if File.exist? path
FileUtils::mkdir_p File.dirname(path)
if src_path.nil?
- open(path, 'w') { |io| yield io }
+ f = open(path, 'w') { |io| yield io }
else
- FileUtils::cp src_path, path
+ f = FileUtils::cp src_path, path
end
+ write_bag_info
+ return f
end
# Remove a bag file
def remove_file(base_path)
path = File.join(data_dir, base_path)
@@ -82,9 +90,19 @@
end
# Get all bag file paths relative to the data dir
def paths
self.bag_files.collect { |f| f.sub(data_dir + '/', '') }
+ end
+
+ # Get the Oxum for the payload files
+ 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
+ return bytes.to_s + '.' + bag_files.count.to_s
end
# Remove all empty directory trees from the bag
def gc!
Dir.entries(data_dir).each do |f|