Class: Archive::Tar::Minitar::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/gems/archive-tar-minitar-0.5.2/lib/archive/tar/minitar.rb

Overview

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.

Defined Under Namespace

Modules: InvalidEntryStream Classes: EntryStream

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(anIO) ⇒ Reader

Creates and returns a new Reader object.



580
581
582
583
# File 'lib/gems/archive-tar-minitar-0.5.2/lib/archive/tar/minitar.rb', line 580

def initialize(anIO)
  @io     = anIO
  @init_pos = anIO.pos
end

Class Method Details

.open(anIO) ⇒ Object

With no associated block, Reader::open is a synonym for Reader::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, Reader::open returns the value of the block.



565
566
567
568
569
570
571
572
573
574
575
576
577
# File 'lib/gems/archive-tar-minitar-0.5.2/lib/archive/tar/minitar.rb', line 565

def self.open(anIO)
  reader = Reader.new(anIO)

  return reader unless block_given?

  begin
    res = yield reader
  ensure
    reader.close
  end
  
  res
end

Instance Method Details

#closeObject



635
636
# File 'lib/gems/archive-tar-minitar-0.5.2/lib/archive/tar/minitar.rb', line 635

def close
end

#each(&block) ⇒ Object

Iterates through each entry in the data stream.



586
587
588
# File 'lib/gems/archive-tar-minitar-0.5.2/lib/archive/tar/minitar.rb', line 586

def each(&block)
  each_entry(&block)
end

#each_entryObject

Iterates through each entry in the data stream.



604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
# File 'lib/gems/archive-tar-minitar-0.5.2/lib/archive/tar/minitar.rb', line 604

def each_entry
  loop do
    return if @io.eof?

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

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

    yield entry

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

    if @io.respond_to?(: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

#rewindObject

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.



593
594
595
596
597
598
599
600
601
# File 'lib/gems/archive-tar-minitar-0.5.2/lib/archive/tar/minitar.rb', line 593

def rewind
  if @init_pos == 0
    raise NonSeekableStream unless @io.respond_to?(:rewind)
    @io.rewind
  else
    raise NonSeekableStream unless @io.respond_to?(:pos=)
    @io.pos = @init_pos
  end
end