Class: Minitar::Reader
- Inherits:
-
Object
- Object
- Minitar::Reader
- Includes:
- Enumerable
- Defined in:
- lib/minitar/reader.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
-
.each_entry(io) ⇒ Object
Iterates over each entry in the provided input.
-
.open(io) ⇒ Object
With no associated block,
Reader::open
is a synonym forReader::new
.
Instance Method Summary collapse
- #close ⇒ Object
-
#closed? ⇒ Boolean
Returns false if the reader is open (it never closes).
-
#each_entry ⇒ Object
(also: #each)
Iterates through each entry in the data stream.
-
#initialize(io) ⇒ Reader
constructor
Creates and returns a new Reader object.
-
#rewind ⇒ Object
Resets the read pointer to the beginning of data stream.
Constructor Details
#initialize(io) ⇒ Reader
Creates and returns a new Reader object.
176 177 178 179 180 181 182 183 |
# File 'lib/minitar/reader.rb', line 176 def initialize(io) @io = io @init_pos = begin io.pos rescue nil end end |
Class Method Details
.each_entry(io) ⇒ Object
Iterates over each entry in the provided input. This wraps the common pattern of:
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.
:call-seq:
Minitar::Reader.each_entry(io) -> enumerator
Minitar::Reader.each_entry(io) { |entry| block } -> obj
165 166 167 168 169 170 171 172 173 |
# File 'lib/minitar/reader.rb', line 165 def self.each_entry(io) return to_enum(__method__, io) unless block_given? Input.open(io) do |reader| reader.each_entry do |entry| yield entry end end end |
.open(io) ⇒ 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.
139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/minitar/reader.rb', line 139 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 |
Instance Method Details
#close ⇒ Object
257 258 |
# File 'lib/minitar/reader.rb', line 257 def close end |
#closed? ⇒ Boolean
Returns false if the reader is open (it never closes).
255 |
# File 'lib/minitar/reader.rb', line 255 def closed? = false |
#each_entry ⇒ Object Also known as: each
Iterates through each entry in the data stream.
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/minitar/reader.rb', line 199 def each_entry return to_enum unless block_given? loop do return if @io.eof? header = Minitar::PosixHeader.from_stream(@io) raise Minitar::InvalidTarStream unless header.valid? return if header.empty? raise Minitar::InvalidTarStream if header.size < 0 if header.long_name? name_block = (header.size / 512.0).ceil * 512 long_name = @io.read(name_block).rstrip header = PosixHeader.from_stream(@io) return if header.empty? header.long_name = long_name elsif header.pax_header? pax_header = PaxHeader.from_stream(@io, header) header = PosixHeader.from_stream(@io) return if header.empty? header.size = pax_header.size if pax_header.size end entry = EntryStream.new(header, @io) size = entry.size yield entry skip = (512 - (size % 512)) % 512 if Minitar.seekable?(@io, :seek) # avoid reading... try_seek(size - entry.bytes_read) else pending = size - entry.bytes_read while pending > 0 bread = @io.read([pending, 4096].min).bytesize 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 |
#rewind ⇒ Object
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.
188 189 190 191 192 193 194 195 196 |
# File 'lib/minitar/reader.rb', line 188 def rewind if @init_pos.zero? raise Minitar::NonSeekableStream unless Minitar.seekable?(@io, :rewind) @io.rewind else raise Minitar::NonSeekableStream unless Minitar.seekable?(@io, :pos=) @io.pos = @init_pos end end |