2016-02-01 19:11:34 -05:00
|
|
|
# frozen_string_literal: true
|
2008-03-31 18:40:06 -04:00
|
|
|
#--
|
2010-04-22 04:24:42 -04:00
|
|
|
# Copyright (C) 2004 Mauricio Julio Fernández Pradier
|
|
|
|
# See LICENSE.txt for additional licensing information.
|
|
|
|
#++
|
2008-03-31 18:40:06 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# TarReader reads tar files and allows iteration over their items
|
2008-03-31 18:40:06 -04:00
|
|
|
|
|
|
|
class Gem::Package::TarReader
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
include Enumerable
|
2008-03-31 18:40:06 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# Raised if the tar IO is not seekable
|
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
class UnexpectedEOF < StandardError; end
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# Creates a new TarReader on +io+ and yields it to the block, if given.
|
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
def self.new(io)
|
|
|
|
reader = super
|
|
|
|
|
|
|
|
return reader unless block_given?
|
|
|
|
|
|
|
|
begin
|
|
|
|
yield reader
|
|
|
|
ensure
|
|
|
|
reader.close
|
|
|
|
end
|
|
|
|
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# Creates a new tar file reader on +io+ which needs to respond to #pos,
|
|
|
|
# #eof?, #read, #getc and #pos=
|
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
def initialize(io)
|
|
|
|
@io = io
|
|
|
|
@init_pos = io.pos
|
|
|
|
end
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# Close the tar file
|
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
def close
|
|
|
|
end
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# Iterates over files in the tarball yielding each entry
|
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
def each
|
2012-11-29 01:52:18 -05:00
|
|
|
return enum_for __method__ unless block_given?
|
2008-03-31 18:40:06 -04:00
|
|
|
|
2019-10-01 07:16:32 -04:00
|
|
|
use_seek = @io.respond_to?(:seek)
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
until @io.eof? do
|
2008-03-31 18:40:06 -04:00
|
|
|
header = Gem::Package::TarHeader.from @io
|
|
|
|
return if header.empty?
|
|
|
|
|
|
|
|
entry = Gem::Package::TarReader::Entry.new header, @io
|
|
|
|
size = entry.header.size
|
|
|
|
|
|
|
|
yield entry
|
|
|
|
|
|
|
|
skip = (512 - (size % 512)) % 512
|
2008-09-25 06:13:50 -04:00
|
|
|
pending = size - entry.bytes_read
|
2008-03-31 18:40:06 -04:00
|
|
|
|
2019-10-01 07:16:32 -04:00
|
|
|
if use_seek
|
|
|
|
begin
|
|
|
|
# avoid reading if the @io supports seeking
|
|
|
|
@io.seek pending, IO::SEEK_CUR
|
|
|
|
pending = 0
|
|
|
|
rescue Errno::EINVAL
|
2008-03-31 18:40:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-01 07:16:32 -04:00
|
|
|
# if seeking isn't supported or failed
|
|
|
|
while pending > 0 do
|
|
|
|
bytes_read = @io.read([pending, 4096].min).size
|
|
|
|
raise UnexpectedEOF if @io.eof?
|
|
|
|
pending -= bytes_read
|
|
|
|
end
|
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
@io.read skip # discard trailing zeros
|
|
|
|
|
|
|
|
# make sure nobody can use #read, #getc or #rewind anymore
|
|
|
|
entry.close
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
alias each_entry each
|
|
|
|
|
|
|
|
##
|
|
|
|
# NOTE: Do not call #rewind during #each
|
|
|
|
|
|
|
|
def rewind
|
2018-11-21 05:20:47 -05:00
|
|
|
if @init_pos == 0
|
2008-03-31 18:40:06 -04:00
|
|
|
@io.rewind
|
|
|
|
else
|
|
|
|
@io.pos = @init_pos
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
##
|
|
|
|
# Seeks through the tar file until it finds the +entry+ with +name+ and
|
|
|
|
# yields it. Rewinds the tar file to the beginning when the block
|
|
|
|
# terminates.
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def seek(name) # :yields: entry
|
2012-11-29 01:52:18 -05:00
|
|
|
found = find do |entry|
|
|
|
|
entry.full_name == name
|
|
|
|
end
|
|
|
|
|
|
|
|
return unless found
|
|
|
|
|
|
|
|
return yield found
|
|
|
|
ensure
|
|
|
|
rewind
|
|
|
|
end
|
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
end
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
require 'rubygems/package/tar_reader/entry'
|