minitar 1.1.0 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,276 +1,276 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class Minitar
4
- # The class that reads a tar format archive from a data stream. The data stream may be
5
- # sequential or random access, but certain features only work with random access data
6
- # streams.
7
- class Reader
8
- include Enumerable
3
+ # The class that reads a tar format archive from a data stream. The data stream may be
4
+ # sequential or random access, but certain features only work with random access data
5
+ # streams.
6
+ class Minitar::Reader
7
+ include Enumerable
9
8
 
10
- # This marks the EntryStream closed for reading without closing the actual data
11
- # stream.
12
- module InvalidEntryStream
13
- def read(*) = raise ClosedStream # :nodoc:
9
+ # This marks the EntryStream closed for reading without closing the actual data
10
+ # stream.
11
+ module InvalidEntryStream
12
+ def read(*) = raise Minitar::ClosedStream # :nodoc:
14
13
 
15
- def getc = raise ClosedStream # :nodoc:
14
+ def getc = raise Minitar::ClosedStream # :nodoc:
16
15
 
17
- def rewind = raise ClosedStream # :nodoc:
16
+ def rewind = raise Minitar::ClosedStream # :nodoc:
18
17
 
19
- def closed? = true # :nodoc:
20
- end
18
+ def closed? = true # :nodoc:
19
+ end
21
20
 
22
- # EntryStreams are pseudo-streams on top of the main data stream.
23
- class EntryStream
24
- Minitar::PosixHeader::FIELDS.each do |field|
25
- attr_reader field.to_sym
26
- end
21
+ # EntryStreams are pseudo-streams on top of the main data stream.
22
+ class EntryStream
23
+ Minitar::PosixHeader::FIELDS.each do |field|
24
+ attr_reader field.to_sym
25
+ end
27
26
 
28
- def initialize(header, io)
29
- @io = io
30
- @name = header.name
31
- @mode = header.mode
32
- @uid = header.uid
33
- @gid = header.gid
34
- @size = header.size
35
- @mtime = header.mtime
36
- @checksum = header.checksum
37
- @typeflag = header.typeflag
38
- @linkname = header.linkname
39
- @magic = header.magic
40
- @version = header.version
41
- @uname = header.uname
42
- @gname = header.gname
43
- @devmajor = header.devmajor
44
- @devminor = header.devminor
45
- @prefix = header.prefix
46
- @read = 0
47
- @orig_pos =
48
- if Minitar.seekable?(@io)
49
- @io.pos
50
- else
51
- 0
52
- end
53
- end
27
+ def initialize(header, io)
28
+ @io = io
29
+ @name = header.name
30
+ @mode = header.mode
31
+ @uid = header.uid
32
+ @gid = header.gid
33
+ @size = header.size
34
+ @mtime = header.mtime
35
+ @checksum = header.checksum
36
+ @typeflag = header.typeflag
37
+ @linkname = header.linkname
38
+ @magic = header.magic
39
+ @version = header.version
40
+ @uname = header.uname
41
+ @gname = header.gname
42
+ @devmajor = header.devmajor
43
+ @devminor = header.devminor
44
+ @prefix = header.prefix
45
+ @read = 0
46
+ @orig_pos =
47
+ if Minitar.seekable?(@io)
48
+ @io.pos
49
+ else
50
+ 0
51
+ end
52
+ end
54
53
 
55
- # Reads +len+ bytes (or all remaining data) from the entry. Returns +nil+ if there
56
- # is no more data to read.
57
- def read(len = nil)
58
- return nil if @read >= @size
59
- len ||= @size - @read
60
- max_read = [len, @size - @read].min
61
- ret = @io.read(max_read)
62
- @read += ret.bytesize
63
- ret
64
- end
54
+ # Reads `len` bytes (or all remaining data) from the entry. Returns `nil` if there
55
+ # is no more data to read.
56
+ def read(len = nil)
57
+ return nil if @read >= @size
58
+ len ||= @size - @read
59
+ max_read = [len, @size - @read].min
60
+ ret = @io.read(max_read)
61
+ @read += ret.bytesize
62
+ ret
63
+ end
65
64
 
66
- # Reads one byte from the entry. Returns +nil+ if there is no more data to read.
67
- def getc
68
- return nil if @read >= @size
69
- ret = @io.getc
70
- @read += 1 if ret
71
- ret
72
- end
65
+ # Reads one byte from the entry. Returns `nil` if there is no more data to read.
66
+ def getc
67
+ return nil if @read >= @size
68
+ ret = @io.getc
69
+ @read += 1 if ret
70
+ ret
71
+ end
73
72
 
74
- # Returns +true+ if the entry represents a directory.
75
- def directory?
76
- case @typeflag
77
- when "5"
78
- true
79
- when "0", "\0"
80
- # If the name ends with a slash, treat it as a directory. This is what other
81
- # major tar implementations do for interoperability and compatibility with older
82
- # tar variants and some new ones.
83
- @name.end_with?("/")
84
- else
85
- false
86
- end
73
+ # Returns `true` if the entry represents a directory.
74
+ #
75
+ # This is primarily controlled by #typeflag `5`, but if the #typeflag is `0` or `\0`,
76
+ # filenames that end with a forward slash (`/`) will be treated as directories for
77
+ # compatibility purposes.
78
+ def directory?
79
+ case @typeflag
80
+ when "5"
81
+ true
82
+ when "0", "\0"
83
+ @name.end_with?("/")
84
+ else
85
+ false
87
86
  end
88
- alias_method :directory, :directory?
87
+ end
88
+ alias_method :directory, :directory?
89
89
 
90
- # Returns +true+ if the entry represents a plain file.
91
- def file?
92
- (@typeflag == "0" || @typeflag == "\0") && !@name.end_with?("/")
93
- end
94
- alias_method :file, :file?
90
+ # Returns `true` if the entry represents a plain file.
91
+ def file?
92
+ (@typeflag == "0" || @typeflag == "\0") && !@name.end_with?("/")
93
+ end
94
+ alias_method :file, :file?
95
95
 
96
- # Returns +true+ if the current read pointer is at the end of the EntryStream data.
97
- def eof? = @read >= @size
96
+ # Returns `true` if the current read pointer is at the end of the EntryStream data.
97
+ def eof? = @read >= @size
98
98
 
99
- # Returns the current read pointer in the EntryStream.
100
- def pos = @read
99
+ # Returns the current read pointer in the EntryStream.
100
+ def pos = @read
101
101
 
102
- alias_method :bytes_read, :pos
102
+ alias_method :bytes_read, :pos
103
103
 
104
- # Sets the current read pointer to the beginning of the EntryStream.
105
- def rewind
106
- unless Minitar.seekable?(@io, :pos=)
107
- raise Minitar::NonSeekableStream
108
- end
109
- @io.pos = @orig_pos
110
- @read = 0
104
+ # Sets the current read pointer to the beginning of the EntryStream.
105
+ def rewind
106
+ unless Minitar.seekable?(@io, :pos=)
107
+ raise Minitar::NonSeekableStream
111
108
  end
109
+ @io.pos = @orig_pos
110
+ @read = 0
111
+ end
112
112
 
113
- # Returns the full and proper name of the entry.
114
- def full_name
115
- if @prefix != ""
116
- File.join(@prefix, @name)
117
- else
118
- @name
119
- end
113
+ # Returns the full and proper name of the entry.
114
+ def full_name
115
+ if @prefix != ""
116
+ File.join(@prefix, @name)
117
+ else
118
+ @name
120
119
  end
120
+ end
121
121
 
122
- # Returns false if the entry stream is valid.
123
- def closed? = false
122
+ # Returns false if the entry stream is valid.
123
+ def closed? = false
124
124
 
125
- # Closes the entry.
126
- def close = invalidate
125
+ # Closes the entry.
126
+ def close = invalidate
127
127
 
128
- private
128
+ private
129
129
 
130
- def invalidate
131
- extend InvalidEntryStream
132
- end
130
+ def invalidate
131
+ extend InvalidEntryStream
133
132
  end
133
+ end
134
134
 
135
- # With no associated block, +Reader::open+ is a synonym for +Reader::new+. If the
136
- # optional code block is given, it will be passed the new _writer_ as an argument and
137
- # the Reader object will automatically be closed when the block terminates. In this
138
- # instance, +Reader::open+ returns the value of the block.
139
- def self.open(io)
140
- reader = new(io)
141
- return reader unless block_given?
142
-
143
- # This exception context must remain, otherwise the stream closes on open even if
144
- # a block is not given.
145
- begin
146
- yield reader
147
- ensure
148
- reader.close
149
- end
135
+ # With no associated block, Reader::open is a synonym for Reader::new. If the optional
136
+ # code block is given, it will be passed the new _reader_ as an argument and the Reader
137
+ # object will automatically be closed when the block terminates. In this
138
+ # instance, Reader::open returns the value of the block.
139
+ def self.open(io)
140
+ reader = new(io)
141
+ return reader unless block_given?
142
+
143
+ # This exception context must remain, otherwise the stream closes on open even if
144
+ # a block is not given.
145
+ begin
146
+ yield reader
147
+ ensure
148
+ reader.close
150
149
  end
150
+ end
151
151
 
152
- # Iterates over each entry in the provided input. This wraps the common pattern of:
153
- #
154
- # Minitar::Input.open(io) do |i|
155
- # inp.each do |entry|
156
- # # ...
157
- # end
158
- # end
159
- #
160
- # If a block is not provided, an enumerator will be created with the same behaviour.
161
- #
162
- # :call-seq:
163
- # Minitar::Reader.each_entry(io) -> enumerator
164
- # Minitar::Reader.each_entry(io) { |entry| block } -> obj
165
- def self.each_entry(io)
166
- return to_enum(__method__, io) unless block_given?
167
-
168
- Input.open(io) do |reader|
169
- reader.each_entry do |entry|
170
- yield entry
171
- end
152
+ # Iterates over each entry in the provided input. This wraps the common pattern of:
153
+ #
154
+ # ```ruby
155
+ # Minitar::Input.open(io) do |i|
156
+ # inp.each do |entry|
157
+ # # ...
158
+ # end
159
+ # end
160
+ # ```
161
+ #
162
+ # If a block is not provided, an enumerator will be created with the same behaviour.
163
+ #
164
+ # :call-seq:
165
+ # Minitar::Reader.each_entry(io) -> enumerator
166
+ # Minitar::Reader.each_entry(io) { |entry| block } -> obj
167
+ def self.each_entry(io)
168
+ return to_enum(__method__, io) unless block_given?
169
+
170
+ Input.open(io) do |reader|
171
+ reader.each_entry do |entry|
172
+ yield entry
172
173
  end
173
174
  end
175
+ end
174
176
 
175
- # Creates and returns a new Reader object.
176
- def initialize(io)
177
- @io = io
178
- @init_pos = begin
179
- io.pos
180
- rescue
181
- nil
182
- end
177
+ # Creates and returns a new Reader object.
178
+ def initialize(io)
179
+ @io = io
180
+ @init_pos = begin
181
+ io.pos
182
+ rescue
183
+ nil
183
184
  end
185
+ end
184
186
 
185
- # Resets the read pointer to the beginning of data stream. Do not call this during
186
- # a #each or #each_entry iteration. This only works with random access data streams
187
- # that respond to #rewind and #pos.
188
- def rewind
189
- if @init_pos.zero?
190
- raise Minitar::NonSeekableStream unless Minitar.seekable?(@io, :rewind)
191
- @io.rewind
192
- else
193
- raise Minitar::NonSeekableStream unless Minitar.seekable?(@io, :pos=)
194
- @io.pos = @init_pos
195
- end
187
+ # Resets the read pointer to the beginning of data stream. Do not call this during
188
+ # a #each or #each_entry iteration. This only works with random access data streams that
189
+ # respond to #rewind and #pos.
190
+ def rewind
191
+ if @init_pos.zero?
192
+ raise Minitar::NonSeekableStream unless Minitar.seekable?(@io, :rewind)
193
+ @io.rewind
194
+ else
195
+ raise Minitar::NonSeekableStream unless Minitar.seekable?(@io, :pos=)
196
+ @io.pos = @init_pos
196
197
  end
198
+ end
197
199
 
198
- # Iterates through each entry in the data stream.
199
- def each_entry
200
- return to_enum unless block_given?
200
+ # Iterates through each entry in the data stream.
201
+ def each_entry
202
+ return to_enum unless block_given?
201
203
 
202
- loop do
203
- return if @io.eof?
204
+ loop do
205
+ return if @io.eof?
204
206
 
205
- header = Minitar::PosixHeader.from_stream(@io)
206
- raise Minitar::InvalidTarStream unless header.valid?
207
- return if header.empty?
207
+ header = Minitar::PosixHeader.from_stream(@io)
208
+ raise Minitar::InvalidTarStream unless header.valid?
209
+ return if header.empty?
208
210
 
209
- raise Minitar::InvalidTarStream if header.size < 0
211
+ raise Minitar::InvalidTarStream if header.size < 0
210
212
 
211
- if header.long_name?
212
- name_block = (header.size / 512.0).ceil * 512
213
+ if header.long_name?
214
+ name_block = (header.size / 512.0).ceil * 512
213
215
 
214
- long_name = @io.read(name_block).rstrip
215
- header = PosixHeader.from_stream(@io)
216
+ long_name = @io.read(name_block).rstrip
217
+ header = Minitar::PosixHeader.from_stream(@io)
216
218
 
217
- return if header.empty?
218
- header.long_name = long_name
219
- elsif header.pax_header?
220
- pax_header = PaxHeader.from_stream(@io, header)
219
+ return if header.empty?
220
+ header.long_name = long_name
221
+ elsif header.pax_header?
222
+ pax_header = Minitar::PaxHeader.from_stream(@io, header)
221
223
 
222
- header = PosixHeader.from_stream(@io)
223
- return if header.empty?
224
+ header = Minitar::PosixHeader.from_stream(@io)
225
+ return if header.empty?
224
226
 
225
- header.size = pax_header.size if pax_header.size
226
- end
227
+ header.size = pax_header.size if pax_header.size
228
+ end
227
229
 
228
- entry = EntryStream.new(header, @io)
229
- size = entry.size
230
+ entry = EntryStream.new(header, @io)
231
+ size = entry.size
230
232
 
231
- yield entry
233
+ yield entry
232
234
 
233
- skip = (512 - (size % 512)) % 512
235
+ skip = (512 - (size % 512)) % 512
234
236
 
235
- if Minitar.seekable?(@io, :seek)
236
- # avoid reading...
237
- try_seek(size - entry.bytes_read)
238
- else
239
- pending = size - entry.bytes_read
240
- while pending > 0
241
- bread = @io.read([pending, 4096].min).bytesize
242
- raise UnexpectedEOF if @io.eof?
243
- pending -= bread
244
- end
237
+ if Minitar.seekable?(@io, :seek)
238
+ # avoid reading...
239
+ try_seek(size - entry.bytes_read)
240
+ else
241
+ pending = size - entry.bytes_read
242
+ while pending > 0
243
+ bread = @io.read([pending, 4096].min).bytesize
244
+ raise Minitar::UnexpectedEOF if @io.eof?
245
+ pending -= bread
245
246
  end
246
-
247
- @io.read(skip) # discard trailing zeros
248
- # make sure nobody can use #read, #getc or #rewind anymore
249
- entry.close
250
247
  end
251
- end
252
- alias_method :each, :each_entry
253
-
254
- # Returns false if the reader is open (it never closes).
255
- def closed? = false
256
248
 
257
- def close
249
+ @io.read(skip) # discard trailing zeros
250
+ # make sure nobody can use #read, #getc or #rewind anymore
251
+ entry.close
258
252
  end
259
-
260
- private
261
-
262
- def try_seek(bytes)
263
- @io.seek(bytes, IO::SEEK_CUR)
264
- rescue RangeError
265
- # This happens when skipping the large entry and the skipping entry size exceeds
266
- # maximum allowed size (varies by platform and underlying IO object).
267
- max = RbConfig::LIMITS.fetch("INT_MAX", 2147483647)
268
- skipped = 0
269
- while skipped < bytes
270
- to_skip = [bytes - skipped, max].min
271
- @io.seek(to_skip, IO::SEEK_CUR)
272
- skipped += to_skip
273
- end
253
+ end
254
+ alias_method :each, :each_entry
255
+
256
+ # Returns `false` if the reader is open (it never closes).
257
+ def closed? = false
258
+
259
+ def close = nil
260
+
261
+ private
262
+
263
+ def try_seek(bytes)
264
+ @io.seek(bytes, IO::SEEK_CUR)
265
+ rescue RangeError
266
+ # This happens when skipping the large entry and the skipping entry size exceeds
267
+ # maximum allowed size (varies by platform and underlying IO object).
268
+ max = RbConfig::LIMITS.fetch("INT_MAX", 2147483647)
269
+ skipped = 0
270
+ while skipped < bytes
271
+ to_skip = [bytes - skipped, max].min
272
+ @io.seek(to_skip, IO::SEEK_CUR)
273
+ skipped += to_skip
274
274
  end
275
275
  end
276
276
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Minitar
4
- VERSION = "1.1.0"
4
+ VERSION = "1.1.1"
5
5
  end