Class: Archive::Tar::Minitar::Input

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/more/facets/minitar.rb

Overview

Wraps a Archive::Tar::Minitar::Reader with convenience methods and wrapped stream management; Input only works with random access data streams. See Input::new for details.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#**, #accumulate, cart, cartesian_product, #cartesian_product, #cluster_by, #collect_if, #collect_with_index, combinations, #combos, #commonality, #compact_collect, #count, #divide, #each_by, #each_combination, #each_combo, #each_pair, #each_permutation, #eachn, #elementwise, #entropy, #every, #every!, #filter_collect, #frequency, #group_by, #ideal_entropy, #inject!, #injecting, #map_send, #mash, #mode, #modulate, #none?, #nonuniq, #occur, #one?, #permutation, #permutation_number, #probability, #split, #sum, #threaded_map, #threaded_map_send, #to_elem, #to_h, #to_hash, #uniq_by

Constructor Details

#initialize(input) ⇒ Input

Creates a new Input object. If input is a stream object that responds to #read), then it will simply be wrapped. Otherwise, one will be created and opened using Kernel#open. When Input#close is called, the stream object wrapped will be closed.



685
686
687
688
689
690
691
692
# File 'lib/more/facets/minitar.rb', line 685

def initialize(input)
  if input.respond_to?(:read)
    @io = input
  else
    @io = open(input, "rb")
  end
  @tarreader = Archive::Tar::Minitar::Reader.new(@io)
end

Class Method Details

.open(input) ⇒ Object

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



668
669
670
671
672
673
674
675
676
677
678
679
# File 'lib/more/facets/minitar.rb', line 668

def self.open(input)
  stream = Input.new(input)
  return stream unless block_given?

  begin
    res = yield stream
  ensure
    stream.close
  end

  res
end

Instance Method Details

#closeObject

Closes the Reader object and the wrapped data stream.



783
784
785
786
# File 'lib/more/facets/minitar.rb', line 783

def close
  @io.close
  @tarreader.close
end

#each(&block) ⇒ Object

Iterates through each entry and rewinds to the beginning of the stream when finished.



696
697
698
699
700
# File 'lib/more/facets/minitar.rb', line 696

def each(&block)
  @tarreader.each { |entry| yield entry }
ensure
  @tarreader.rewind
end

#extract_entry(destdir, entry) ⇒ Object

Extracts the current entry to destdir. If a block is provided, it yields an action Symbol, the full name of the file being extracted (name), and a Hash of statistical information (stats).

The action will be one of:

:dir

The entry is a directory.

:file_start

The entry is a file; the extract of the file is just beginning.

:file_progress

Yielded every 4096 bytes during the extract of the entry.

:file_done

Yielded when the entry is completed.

The stats hash contains the following keys:

:current

The current total number of bytes read in the entry.

:currinc

The current number of bytes read in this read cycle.

:entry

The entry being extracted; this is a Reader::EntryStream, with all methods thereof.



721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
# File 'lib/more/facets/minitar.rb', line 721

def extract_entry(destdir, entry) # :yields action, name, stats:
  stats = {
    :current  => 0,
    :currinc  => 0,
    :entry    => entry
  }

  if entry.directory?
    dest = File.join(destdir, entry.full_name)

    yield :dir, entry.full_name, stats if block_given?

    if Archive::Tar::Minitar.dir?(dest)
      begin
        FileUtils.chmod(entry.mode, dest)
      rescue Exception
        nil
      end
    else
      FileUtils.mkdir_p(dest, :mode => entry.mode)
      FileUtils.chmod(entry.mode, dest)
    end

    fsync_dir(dest)
    fsync_dir(File.join(dest, ".."))
    return
  else # it's a file
    destdir = File.join(destdir, File.dirname(entry.full_name))
    FileUtils.mkdir_p(destdir, :mode => 0755)

    destfile = File.join(destdir, File.basename(entry.full_name))
    FileUtils.chmod(0600, destfile) rescue nil  # Errno::ENOENT

    yield :file_start, entry.full_name, stats if block_given?

    File.open(destfile, "wb", entry.mode) do |os|
      loop do
        data = entry.read(4096)
        break unless data

        stats[:currinc] = os.write(data)
        stats[:current] += stats[:currinc]

        yield :file_progress, entry.full_name, stats if block_given?
      end
      os.fsync
    end

    FileUtils.chmod(entry.mode, destfile)
    fsync_dir(File.dirname(destfile))
    fsync_dir(File.join(File.dirname(destfile), ".."))

    yield :file_done, entry.full_name, stats if block_given?
  end
end

#tarObject

Returns the Reader object for direct access.



778
779
780
# File 'lib/more/facets/minitar.rb', line 778

def tar
  @tarreader
end