class Archive::Tar::Minitar::Reader

The class that reads a tar format archive from a data stream. The data stream may be sequential or random access, but certain features only work with random access data streams.

Public Class Methods

Archive::Tar::Minitar::Reader.each_entry(io) → enumerator click to toggle source
Archive::Tar::Minitar::Reader.each_entry(io) { |entry| block } → obj

Iterates over each entry in the provided input. This wraps the common pattern of:

Archive::Tar::Minitar::Input.open(io) do |i|
  inp.each do |entry|
    # ...
  end
end

If a block is not provided, an enumerator will be created with the same behaviour.

# File lib/archive/tar/minitar/reader.rb, line 168
def self.each_entry(io)
  return to_enum(__method__, io) unless block_given?

  open(io) do |reader|
    reader.each_entry do |entry|
      yield entry
    end
  end
end
new(io) click to toggle source

Creates and returns a new Reader object.

# File lib/archive/tar/minitar/reader.rb, line 179
def initialize(io)
  @io = io
  @init_pos = io.pos
end
open(io) { |reader| ... } click to toggle source

With no associated block, +::open+ is a synonym for +::new+. If the optional code block is given, it will be passed the new writer as an argument and the Reader object will automatically be closed when the block terminates. In this instance, +::open+ returns the value of the block.

# File lib/archive/tar/minitar/reader.rb, line 140
def self.open(io)
  reader = new(io)
  return reader unless block_given?

  # This exception context must remain, otherwise the stream closes on open
  # even if a block is not given.
  begin
    yield reader
  ensure
    reader.close
  end
end

Public Instance Methods

close() click to toggle source
# File lib/archive/tar/minitar/reader.rb, line 249
def close
end
closed?() click to toggle source

Returns false if the reader is open (it never closes).

# File lib/archive/tar/minitar/reader.rb, line 245
def closed?
  false
end
each() click to toggle source
Alias for: each_entry
each_entry() { |entry| ... } click to toggle source

Iterates through each entry in the data stream.

# File lib/archive/tar/minitar/reader.rb, line 202
def each_entry
  return to_enum unless block_given?

  loop do
    return if @io.eof?

    header = Archive::Tar::Minitar::PosixHeader.from_stream(@io)
    return if header.empty?

    if header.long_name?
      name = @io.read(512).rstrip
      header = PosixHeader.from_stream(@io)
      return if header.empty?
      header.name = name
    end

    entry = EntryStream.new(header, @io)
    size  = entry.size

    yield entry

    skip = (512 - (size % 512)) % 512

    if Archive::Tar::Minitar.seekable?(@io, :seek)
      # avoid reading...
      @io.seek(size - entry.bytes_read, IO::SEEK_CUR)
    else
      pending = size - entry.bytes_read
      while pending > 0
        bread = @io.read([pending, 4096].min).size
        raise UnexpectedEOF if @io.eof?
        pending -= bread
      end
    end

    @io.read(skip) # discard trailing zeros
    # make sure nobody can use #read, #getc or #rewind anymore
    entry.close
  end
end
Also aliased as: each
rewind() click to toggle source

Resets the read pointer to the beginning of data stream. Do not call this during a each or each_entry iteration. This only works with random access data streams that respond to rewind and pos.

# File lib/archive/tar/minitar/reader.rb, line 187
def rewind
  if @init_pos.zero?
    unless Archive::Tar::Minitar.seekable?(@io, :rewind)
      raise Archive::Tar::Minitar::NonSeekableStream
    end
    @io.rewind
  else
    unless Archive::Tar::Minitar.seekable?(@io, :pos=)
      raise Archive::Tar::Minitar::NonSeekableStream
    end
    @io.pos = @init_pos
  end
end