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