Class: Minitar::Reader::EntryStream

Inherits:
Object
  • Object
show all
Defined in:
lib/minitar/reader.rb

Overview

EntryStreams are pseudo-streams on top of the main data stream.

Instance Method Summary collapse

Constructor Details

#initialize(header, io) ⇒ EntryStream

Returns a new instance of EntryStream.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/minitar/reader.rb', line 28

def initialize(header, io)
  @io = io
  @name = header.name
  @mode = header.mode
  @uid = header.uid
  @gid = header.gid
  @size = header.size
  @mtime = header.mtime
  @checksum = header.checksum
  @typeflag = header.typeflag
  @linkname = header.linkname
  @magic = header.magic
  @version = header.version
  @uname = header.uname
  @gname = header.gname
  @devmajor = header.devmajor
  @devminor = header.devminor
  @prefix = header.prefix
  @read = 0
  @orig_pos =
    if Minitar.seekable?(@io)
      @io.pos
    else
      0
    end
end

Instance Method Details

#closeObject

Closes the entry.



126
# File 'lib/minitar/reader.rb', line 126

def close = invalidate

#closed?Boolean

Returns false if the entry stream is valid.

Returns:

  • (Boolean)


123
# File 'lib/minitar/reader.rb', line 123

def closed? = false

#directory?Boolean Also known as: directory

Returns true if the entry represents a directory.

Returns:

  • (Boolean)


75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/minitar/reader.rb', line 75

def directory?
  case @typeflag
  when "5"
    true
  when "0", "\0"
    # If the name ends with a slash, treat it as a directory. This is what other
    # major tar implementations do for interoperability and compatibility with older
    # tar variants and some new ones.
    @name.end_with?("/")
  else
    false
  end
end

#eof?Boolean

Returns true if the current read pointer is at the end of the EntryStream data.

Returns:

  • (Boolean)


97
# File 'lib/minitar/reader.rb', line 97

def eof? = @read >= @size

#file?Boolean Also known as: file

Returns true if the entry represents a plain file.

Returns:

  • (Boolean)


91
92
93
# File 'lib/minitar/reader.rb', line 91

def file?
  (@typeflag == "0" || @typeflag == "\0") && !@name.end_with?("/")
end

#full_nameObject

Returns the full and proper name of the entry.



114
115
116
117
118
119
120
# File 'lib/minitar/reader.rb', line 114

def full_name
  if @prefix != ""
    File.join(@prefix, @name)
  else
    @name
  end
end

#getcObject

Reads one byte from the entry. Returns nil if there is no more data to read.



67
68
69
70
71
72
# File 'lib/minitar/reader.rb', line 67

def getc
  return nil if @read >= @size
  ret = @io.getc
  @read += 1 if ret
  ret
end

#posObject Also known as: bytes_read

Returns the current read pointer in the EntryStream.



100
# File 'lib/minitar/reader.rb', line 100

def pos = @read

#read(len = nil) ⇒ Object

Reads len bytes (or all remaining data) from the entry. Returns nil if there is no more data to read.



57
58
59
60
61
62
63
64
# File 'lib/minitar/reader.rb', line 57

def read(len = nil)
  return nil if @read >= @size
  len ||= @size - @read
  max_read = [len, @size - @read].min
  ret = @io.read(max_read)
  @read += ret.bytesize
  ret
end

#rewindObject

Sets the current read pointer to the beginning of the EntryStream.



105
106
107
108
109
110
111
# File 'lib/minitar/reader.rb', line 105

def rewind
  unless Minitar.seekable?(@io, :pos=)
    raise Minitar::NonSeekableStream
  end
  @io.pos = @orig_pos
  @read = 0
end