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
data/lib/minitar.rb CHANGED
@@ -1,109 +1,125 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "fileutils"
2
4
  require "rbconfig"
5
+ require "rbconfig/sizeof"
3
6
 
4
- # == Synopsis
7
+ # ## Synopsis
5
8
  #
6
9
  # Using minitar is easy. The simplest case is:
7
10
  #
8
- # require 'zlib'
9
- # require 'minitar'
11
+ # ```ruby
12
+ # require 'zlib'
13
+ # require 'minitar'
10
14
  #
11
- # # Packs everything that matches Find.find('tests').
12
- # # test.tar will automatically be closed by Minitar.pack.
13
- # Minitar.pack('tests', File.open('test.tar', 'wb'))
15
+ # # Packs everything that matches Find.find('tests'). test.tar will automatically be
16
+ # # closed by Minitar.pack.
17
+ # Minitar.pack('tests', File.open('test.tar', 'wb'))
14
18
  #
15
- # # Unpacks 'test.tar' to 'x', creating 'x' if necessary.
16
- # Minitar.unpack('test.tar', 'x')
19
+ # # Unpacks 'test.tar' to 'x', creating 'x' if necessary.
20
+ # Minitar.unpack('test.tar', 'x')
21
+ # ```
17
22
  #
18
23
  # A gzipped tar can be written with:
19
24
  #
20
- # # test.tgz will be closed automatically.
21
- # Minitar.pack('tests', Zlib::GzipWriter.new(File.open('test.tgz', 'wb'))
22
25
  #
23
- # # test.tgz will be closed automatically.
24
- # Minitar.unpack(Zlib::GzipReader.new(File.open('test.tgz', 'rb')), 'x')
26
+ # ```ruby
27
+ # # test.tgz will be closed automatically.
28
+ # Minitar.pack('tests', Zlib::GzipWriter.new(File.open('test.tgz', 'wb'))
29
+ #
30
+ # # test.tgz will be closed automatically.
31
+ # Minitar.unpack(Zlib::GzipReader.new(File.open('test.tgz', 'rb')), 'x')
32
+ # ```
25
33
  #
26
- # As the case above shows, one need not write to a file. However, it will
27
- # sometimes require that one dive a little deeper into the API, as in the case
28
- # of StringIO objects. Note that I'm not providing a block with
29
- # Minitar::Output, as Minitar::Output#close automatically closes both the
30
- # Output object and the wrapped data stream object.
34
+ # As the case above shows, one need not write to a file. However, it will sometimes
35
+ # require that one dive a little deeper into the API, as in the case of StringIO objects.
36
+ # Note that I'm not providing a block with Minitar::Output, as Minitar::Output#close
37
+ # automatically closes both the Output object and the wrapped data stream object.
31
38
  #
32
- # begin
33
- # sgz = Zlib::GzipWriter.new(StringIO.new(""))
34
- # tar = Minitar::Output.new(sgz)
35
- # Find.find('tests') do |entry|
36
- # Minitar.pack_file(entry, tar)
37
- # end
38
- # ensure
39
- # # Closes both tar and sgz.
40
- # tar.close
41
- # end
39
+ # ```ruby
40
+ # begin
41
+ # sgz = Zlib::GzipWriter.new(StringIO.new(""))
42
+ # tar = Minitar::Output.new(sgz)
43
+ # Find.find('tests') do |entry|
44
+ # Minitar.pack_file(entry, tar)
45
+ # end
46
+ # ensure
47
+ # # Closes both tar and sgz.
48
+ # tar.close
49
+ # end
50
+ # ```
42
51
  class Minitar
43
- VERSION = "1.0.2".freeze # :nodoc:
44
-
45
- # The base class for any minitar error.
52
+ # The base class for any Minitar error.
46
53
  Error = Class.new(::StandardError)
47
54
  # Raised when a wrapped data stream class is not seekable.
48
55
  NonSeekableStream = Class.new(Error)
49
- # The exception raised when operations are performed on a stream that has
50
- # previously been closed.
56
+ # The exception raised when operations are performed on a stream that has previously
57
+ # been closed.
51
58
  ClosedStream = Class.new(Error)
52
- # The exception raised when a filename exceeds 256 bytes in length, the
53
- # maximum supported by the standard Tar format.
59
+ # The exception raised when a filename exceeds 256 bytes in length, the maximum
60
+ # supported by the standard Tar format.
54
61
  FileNameTooLong = Class.new(Error)
55
- # The exception raised when a data stream ends before the amount of data
56
- # expected in the archive's PosixHeader.
57
- UnexpectedEOF = Class.new(StandardError)
58
- # The exception raised when a file contains a relative path in secure mode
59
- # (the default for this version).
62
+ # The exception raised when a data stream ends before the amount of data expected in the
63
+ # archive's PosixHeader.
64
+ UnexpectedEOF = Class.new(Error)
65
+ # The exception raised when a file contains a relative path in secure mode (the default
66
+ # for this version).
60
67
  SecureRelativePathError = Class.new(Error)
68
+ # The exception raised when an entry name cannot be safely written on the current
69
+ # platform (for example, a name containing `:` on Windows, which may address an NTFS
70
+ # alternate data stream).
71
+ UnsafeFileNameError = Class.new(Error)
61
72
  # The exception raised when a file contains an invalid Posix header.
62
73
  InvalidTarStream = Class.new(Error)
63
74
  end
64
75
 
65
76
  class << Minitar
66
- # Tests if +path+ refers to a directory. Fixes an apparently
67
- # corrupted <tt>stat()</tt> call on Windows.
68
- def dir?(path)
69
- File.directory?((path[-1] == "/") ? path : "#{path}/")
77
+ # Tests if `path` refers to a directory. Fixes an apparently corrupted `stat()` call on
78
+ # Windows.
79
+ def dir?(path) # :nodoc:
80
+ path = "#{path}/" unless path.to_s.end_with?("/")
81
+ File.directory?(path)
70
82
  end
71
83
 
72
- # A convenience method for wrapping Minitar::Input.open
73
- # (mode +r+) and Minitar::Output.open (mode +w+). No other
74
- # modes are currently supported.
84
+ # A convenience method for wrapping Minitar::Input.open (mode `r` or `rb`) and
85
+ # Minitar::Output.open (mode `w` or `wb`). No other modes are currently supported.
75
86
  def open(dest, mode = "r", &)
76
87
  case mode
77
- when "r"
88
+ when "r", "rb"
78
89
  Minitar::Input.open(dest, &)
79
- when "w"
80
- Minitar::Output.open(dest, &block)
90
+ when "w", "wb"
91
+ Minitar::Output.open(dest, &)
81
92
  else
82
- raise "Unknown open mode for Minitar.open."
93
+ raise ArgumentError, "Unknown open mode #{mode.inspect} for Minitar.open."
83
94
  end
84
95
  end
85
96
 
86
- def windows? # :nodoc:
87
- RbConfig::CONFIG["host_os"] =~ /^(mswin|mingw|cygwin)/
88
- end
97
+ WINDOWS_CONFIG = RbConfig::CONFIG["host_os"].to_s =~ /^(cygwin|mingw|mswin|windows)/i
98
+ WINDOWS_PLATFORM = RUBY_PLATFORM =~ /^(?:cygwin|mingw|mswin)/i
99
+ WINDOWS_SEPARATOR = File::ALT_SEPARATOR == "\\"
100
+
101
+ private_constant :WINDOWS_CONFIG, :WINDOWS_PLATFORM, :WINDOWS_SEPARATOR
102
+
103
+ def windows? = windows_platform? || windows_config? || File::ALT_SEPARATOR == "\\" # :nodoc:
104
+ def windows_config? = WINDOWS_CONFIG # :nodoc:
105
+ def windows_platform? = WINDOWS_PLATFORM # :nodoc:
106
+ def windows_separator? = WINDOWS_SEPARATOR # :nodoc:
89
107
 
90
- # A convenience method to pack the provided +data+ as a file named +entry+. +entry+ may
91
- # either be a name or a Hash with the fields described below. When only a name is
108
+ # A convenience method to pack the provided `data` as a file named `entry`, which may
109
+ # either be a name String or a Hash with the fields described below. When only a name is
92
110
  # provided, or only some Hash fields are provided, the default values will apply.
93
111
  #
94
- # <tt>:name</tt>:: The filename to be packed into the archive. Required.
95
- # <tt>:mode</tt>:: The mode to be applied. Defaults to 0o644 for files and 0o755 for
96
- # directories.
97
- # <tt>:uid</tt>:: The user owner of the file. Default is +nil+.
98
- # <tt>:gid</tt>:: The group owner of the file. Default is +nil+.
99
- # <tt>:mtime</tt>:: The modification Time of the file. Default is +Time.now+.
112
+ # - `:name` (**required**): The filename to be packed into the archive.
113
+ # - `:mode`: The mode to be applied. Defaults to `0o644` for files and `0o755` for
114
+ # directories.
115
+ # - `:uid`: The user owner of the file. Default is `nil`.
116
+ # - `:gid`: The group owner of the file. Default is `nil`.
117
+ # - `:mtime`: The modification Time of the file. Default is `Time.now`.
100
118
  #
101
- # If +data+ is +nil+, a directory will be created. Use an empty String for a normal
102
- # empty file.
103
- def pack_as_file(entry, data, outputter) # :yields action, name, stats:
104
- if outputter.is_a?(Minitar::Output)
105
- outputter = outputter.tar
106
- end
119
+ # If `data` is `nil`, a directory will be created. Use an empty String for an empty
120
+ # file.
121
+ def pack_as_file(entry, data, outputter) # :yields: action, name, stats
122
+ outputter = outputter.tar if outputter.is_a?(Minitar::Output)
107
123
 
108
124
  stats = {
109
125
  gid: nil,
@@ -114,8 +130,11 @@ class << Minitar
114
130
  }
115
131
 
116
132
  if entry.is_a?(Hash)
117
- name = entry.delete(:name)
118
- entry.each_pair { stats[_1] = _2 unless _2.nil? }
133
+ name = entry[:name]
134
+ entry.each_pair {
135
+ next if _1 == :name
136
+ stats[_1] = _2 unless _2.nil?
137
+ }
119
138
  else
120
139
  name = entry
121
140
  end
@@ -141,43 +160,38 @@ class << Minitar
141
160
  end
142
161
  end
143
162
 
144
- # A convenience method to pack the file provided. +entry+ may either be a filename (in
145
- # which case various values for the file (see below) will be obtained from
146
- # <tt>File#stat(entry)</tt> or a Hash with the fields:
163
+ # A convenience method to pack the file provided. `entry` may either be a filename
164
+ # string (which will in which case various values for the file will be obtained from
165
+ # `File#stat(entry)` or a Hash with the fields:
147
166
  #
148
- # <tt>:name</tt>:: The filename to be packed into the archive. Required.
149
- # <tt>:mode</tt>:: The mode to be applied.
150
- # <tt>:uid</tt>:: The user owner of the file. (Ignored on Windows.)
151
- # <tt>:gid</tt>:: The group owner of the file. (Ignored on Windows.)
152
- # <tt>:mtime</tt>:: The modification Time of the file.
167
+ # - `:name` (**required**): The filename to be packed into the archive.
168
+ # - `:mode`: The mode to be applied.
169
+ # - `:uid`: The user owner of the file, ignored on Windows.
170
+ # - `:gid`: The group owner of the file, ignored on Windows.
171
+ # - `:mtime`: The modification Time of the file.
153
172
  #
154
- # During packing, if a block is provided, #pack_file yields an +action+ Symol, the
155
- # full name of the file being packed, and a Hash of statistical information, just as
156
- # with Minitar::Input#extract_entry.
173
+ # During packing, if a block is provided, #pack_file yields an `action` Symbol, the full
174
+ # name of the file being packed, and a Hash of statistical information, just as with
175
+ # Minitar::Input#extract_entry.
157
176
  #
158
- # The +action+ will be one of:
159
- # <tt>:dir</tt>:: The +entry+ is a directory.
160
- # <tt>:file_start</tt>:: The +entry+ is a file; the extract of the
161
- # file is just beginning.
162
- # <tt>:file_progress</tt>:: Yielded every 4096 bytes during the extract
163
- # of the +entry+.
164
- # <tt>:file_done</tt>:: Yielded when the +entry+ is completed.
177
+ # The `action` will be one of:
165
178
  #
166
- # The +stats+ hash contains the following keys:
167
- # <tt>:current</tt>:: The current total number of bytes read in the
168
- # +entry+.
169
- # <tt>:currinc</tt>:: The current number of bytes read in this read
170
- # cycle.
171
- # <tt>:name</tt>:: The filename to be packed into the tarchive.
172
- # *REQUIRED*.
173
- # <tt>:mode</tt>:: The mode to be applied.
174
- # <tt>:uid</tt>:: The user owner of the file. (+nil+ on Windows.)
175
- # <tt>:gid</tt>:: The group owner of the file. (+nil+ on Windows.)
176
- # <tt>:mtime</tt>:: The modification Time of the file.
177
- def pack_file(entry, outputter) # :yields action, name, stats:
178
- if outputter.is_a?(Minitar::Output)
179
- outputter = outputter.tar
180
- end
179
+ # - `:dir`: The `entry` is a directory.
180
+ # - `:file_start`: The `entry` is a file; the extract of the file is just beginning.
181
+ # - `:file_progress`: Yielded every 4096 bytes during the extract of the file `entry`.
182
+ # - `file_done`: Yielded when the file `entry` is completed.
183
+ #
184
+ # The +stats+ hash may contain the following keys:
185
+ #
186
+ # - `:current`: The current total number of bytes read in the `entry`.
187
+ # - `:currinc`: The current number of bytes read in this read cycle.
188
+ # - `:name`: The filename packed into the tarchive.
189
+ # - `:mode`: The mode to be applied.
190
+ # - `:uid`: The user owner of the file, `nil` on Windows.
191
+ # - `:gid`: The group owner of the file, `nil` on Windows.
192
+ # - `:mtime`: The modification Time of the file.
193
+ def pack_file(entry, outputter) # :yields: action, name, stats
194
+ outputter = outputter.tar if outputter.is_a?(Minitar::Output)
181
195
 
182
196
  stats = {}
183
197
 
@@ -223,13 +237,13 @@ class << Minitar
223
237
  end
224
238
  end
225
239
 
226
- # A convenience method to pack files specified by +src+ into +dest+. If
227
- # +src+ is an Array, then each file detailed therein will be packed into
228
- # the resulting Minitar::Output stream; if +recurse_dirs+ is
229
- # true, then directories will be recursed.
240
+ # A convenience method to pack files specified by `src` into `dest`. If `src` is an
241
+ # Array, then each file detailed therein will be packed into the resulting
242
+ # Minitar::Output stream. If `recurse_dirs` is `true` (the default), then directories
243
+ # will be recursed.
230
244
  #
231
- # If +src+ is not an Array, it will be treated as the result of Find.find;
232
- # all files matching will be packed.
245
+ # If `src` is not an Array, it will be treated as the source of Find.find; all files
246
+ # matching will be packed.
233
247
  def pack(src, dest, recurse_dirs = true, &block)
234
248
  require "find"
235
249
  Minitar::Output.open(dest) do |outp|
@@ -251,9 +265,10 @@ class << Minitar
251
265
  end
252
266
  end
253
267
 
254
- # A convenience method to unpack files from +src+ into the directory
255
- # specified by +dest+. Only those files named explicitly in +files+
256
- # will be extracted.
268
+ # A convenience method to unpack files from `src` into the directory specified by
269
+ # `dest`.
270
+ #
271
+ # If `files` is not empty, it will act as a full name filter to restrict extraction.
257
272
  def unpack(src, dest, files = [], options = {}, &block)
258
273
  Minitar::Input.open(src) do |inp|
259
274
  if File.exist?(dest) && !dir?(dest)
@@ -270,10 +285,10 @@ class << Minitar
270
285
  end
271
286
  end
272
287
 
273
- # Check whether +io+ can seek without errors.
274
- def seekable?(io, methods = nil)
275
- # The IO class throws an exception at runtime if we try to change
276
- # position on a non-regular file.
288
+ # Check whether `io` can seek without errors.
289
+ def seekable?(io, methods = nil) # :nodoc:
290
+ # The IO class throws an exception at runtime if we try to change position on
291
+ # a non-regular file.
277
292
  if io.respond_to?(:stat)
278
293
  io.stat.file?
279
294
  else
@@ -286,5 +301,9 @@ class << Minitar
286
301
  end
287
302
 
288
303
  require "minitar/posix_header"
304
+ require "minitar/pax_header"
289
305
  require "minitar/input"
290
306
  require "minitar/output"
307
+ require "minitar/reader"
308
+ require "minitar/writer"
309
+ require "minitar/version"
data/licenses/bsdl.txt ADDED
@@ -0,0 +1,20 @@
1
+ Redistribution and use in source and binary forms, with or without
2
+ modification, are permitted provided that the following conditions are met:
3
+
4
+ 1. Redistributions of source code must retain the above copyright notice, this
5
+ list of conditions and the following disclaimer.
6
+
7
+ 2. Redistributions in binary form must reproduce the above copyright notice,
8
+ this list of conditions and the following disclaimer in the documentation
9
+ and/or other materials provided with the distribution.
10
+
11
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
12
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
13
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
14
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
15
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
16
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
17
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
18
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
19
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
20
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/licenses/dco.txt ADDED
@@ -0,0 +1,34 @@
1
+ Developer Certificate of Origin
2
+ Version 1.1
3
+
4
+ Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
5
+
6
+ Everyone is permitted to copy and distribute verbatim copies of this
7
+ license document, but changing it is not allowed.
8
+
9
+
10
+ Developer's Certificate of Origin 1.1
11
+
12
+ By making a contribution to this project, I certify that:
13
+
14
+ (a) The contribution was created in whole or in part by me and I
15
+ have the right to submit it under the open source license
16
+ indicated in the file; or
17
+
18
+ (b) The contribution is based upon previous work that, to the best
19
+ of my knowledge, is covered under an appropriate open source
20
+ license and I have the right under that license to submit that
21
+ work with modifications, whether created in whole or in part
22
+ by me, under the same open source license (unless I am
23
+ permitted to submit under a different license), as indicated
24
+ in the file; or
25
+
26
+ (c) The contribution was provided directly to me by some other
27
+ person who certified (a), (b) or (c) and I have not modified
28
+ it.
29
+
30
+ (d) I understand and agree that this project and the contribution
31
+ are public and that a record of the contribution (including all
32
+ personal information I submit with it, including my sign-off) is
33
+ maintained indefinitely and may be redistributed consistent with
34
+ this project or the open source license(s) involved.
data/licenses/ruby.txt ADDED
@@ -0,0 +1,52 @@
1
+ 1. You may make and give away verbatim copies of the source form of the
2
+ software without restriction, provided that you duplicate all of the
3
+ original copyright notices and associated disclaimers.
4
+
5
+ 2. You may modify your copy of the software in any way, provided that
6
+ you do at least ONE of the following:
7
+
8
+ a. place your modifications in the Public Domain or otherwise
9
+ make them Freely Available, such as by posting said
10
+ modifications to Usenet or an equivalent medium, or by allowing
11
+ the author to include your modifications in the software.
12
+
13
+ b. use the modified software only within your corporation or
14
+ organization.
15
+
16
+ c. give non-standard binaries non-standard names, with
17
+ instructions on where to get the original software distribution.
18
+
19
+ d. make other distribution arrangements with the author.
20
+
21
+ 3. You may distribute the software in object code or binary form,
22
+ provided that you do at least ONE of the following:
23
+
24
+ a. distribute the binaries and library files of the software,
25
+ together with instructions (in the manual page or equivalent)
26
+ on where to get the original distribution.
27
+
28
+ b. accompany the distribution with the machine-readable source of
29
+ the software.
30
+
31
+ c. give non-standard binaries non-standard names, with
32
+ instructions on where to get the original software distribution.
33
+
34
+ d. make other distribution arrangements with the author.
35
+
36
+ 4. You may modify and include the part of the software into any other
37
+ software (possibly commercial). But some files in the distribution
38
+ are not written by the author, so that they are not under these terms.
39
+
40
+ For the list of those files and their copying conditions, see the
41
+ file LEGAL.
42
+
43
+ 5. The scripts and library files supplied as input to or produced as
44
+ output from the software do not automatically fall under the
45
+ copyright of the software, but belong to whomever generated them,
46
+ and may be sold commercially, and may be aggregated with this
47
+ software.
48
+
49
+ 6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
50
+ IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
51
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
52
+ PURPOSE.
Binary file
Binary file
Binary file
Binary file
@@ -1,10 +1,21 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "fileutils"
2
4
  require "minitar"
5
+ require "pathname"
6
+ require "stringio"
7
+ require "zlib"
3
8
 
4
9
  gem "minitest"
5
10
  require "minitest/autorun"
6
11
  require "minitest/focus"
7
12
 
8
- Dir.glob(File.join(File.dirname(__FILE__), "support/*.rb")).sort.each do |support|
13
+ if ENV["STRICT"] != "false"
14
+ $VERBOSE = true
15
+ Warning[:deprecated] = true
16
+ require "minitest/error_on_warning"
17
+ end
18
+
19
+ Dir.glob(File.join(__dir__, "support/**/*.rb")).sort.each do |support|
9
20
  require support
10
21
  end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Minitar::TestHelpers::Fixtures
4
+ def Fixture(name) = FIXTURES.fetch(name)
5
+
6
+ def open_fixture(name)
7
+ fixture = Fixture(name)
8
+
9
+ io = fixture.open("rb").then {
10
+ if %w[.gz .tgz].include?(fixture.extname.to_s)
11
+ Zlib::GzipReader.new(_1)
12
+ else
13
+ _1
14
+ end
15
+ }.tap {
16
+ yield _1 if block_given?
17
+ }
18
+ ensure
19
+ io&.close if block_given?
20
+ end
21
+
22
+ FIXTURES = Pathname(__dir__)
23
+ .join("../../fixtures")
24
+ .expand_path
25
+ .glob("**")
26
+ .each_with_object({}) {
27
+ next if _1.directory?
28
+
29
+ name = _1.dup
30
+ name = name.basename(name.extname) until name.nil? || name.extname.empty?
31
+
32
+ _2[name.to_s] = _1
33
+ }
34
+ .freeze
35
+ private_constant :FIXTURES
36
+
37
+ Minitest::Test.send(:include, self)
38
+ end
@@ -0,0 +1,130 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Test assertions and helpers for working with header objects.
4
+ module Minitar::TestHelpers::Header
5
+ private
6
+
7
+ # Assert that the +actual+ header is equal to +expected+.
8
+ def assert_headers_equal(expected, actual)
9
+ actual = actual.to_s
10
+ __field_order.each do |field|
11
+ message =
12
+ if field == "checksum"
13
+ "Header checksums are expected to match."
14
+ else
15
+ "Header field #{field} is expected to match."
16
+ end
17
+
18
+ offset = __fields[field].offset
19
+ length = __fields[field].length
20
+
21
+ assert_equal expected[offset, length], actual[offset, length], message
22
+ end
23
+ end
24
+
25
+ def assert_modes_equal(expected, actual, filename)
26
+ return if Minitar.windows?
27
+
28
+ assert_equal mode_string(expected), mode_string(actual), "Mode for #{filename} does not match"
29
+ end
30
+
31
+ def build_raw_header(type, name, prefix, size, mode, link_name = "") =
32
+ [
33
+ name, mode, z(octal(nil, 7)), z(octal(nil, 7)), size, z(octal(0, 11)),
34
+ BLANK_CHECKSUM, type, asciiz(link_name, 100), USTAR, DOUBLE_ZERO, asciiz("", 32),
35
+ asciiz("", 32), z(octal(nil, 7)), z(octal(nil, 7)), prefix
36
+ ].join.bytes.to_a.pack("C100C8C8C8C12C12C8CC100C6C2C32C32C8C8C155").then {
37
+ "#{_1}#{"\0" * (512 - _1.bytesize)}"
38
+ }.tap { assert_equal 512, _1.bytesize }
39
+
40
+ def build_header(type, name, prefix, size, mode, link_name = "") =
41
+ build_raw_header(
42
+ type,
43
+ asciiz(name, 100),
44
+ asciiz(prefix, 155),
45
+ z(octal(size, 11)),
46
+ z(octal(mode, 7)),
47
+ asciiz(link_name, 100)
48
+ )
49
+
50
+ def build_tar_file_header(name, prefix, mode, size) =
51
+ build_header("0", name, prefix, size, mode).then {
52
+ update_header_checksum(_1)
53
+ }
54
+
55
+ def build_tar_dir_header(name, prefix, mode) =
56
+ build_header("5", name, prefix, 0, mode).then {
57
+ update_header_checksum(_1)
58
+ }
59
+
60
+ def build_tar_symlink_header(name, prefix, mode, target) =
61
+ build_header("2", name, prefix, 0, mode, target).then {
62
+ update_header_checksum(_1)
63
+ }
64
+
65
+ def build_tar_pax_header(name, prefix, content_size) =
66
+ build_header("x", name, prefix, content_size, 0o644).then {
67
+ update_header_checksum(_1)
68
+ }
69
+
70
+ def update_header_checksum(header) =
71
+ header.tap { |h|
72
+ checksum = __fields["checksum"]
73
+ h[checksum.offset, checksum.length] =
74
+ h.unpack("C*")
75
+ .inject(:+)
76
+ .then { octal(_1, 6) }
77
+ .then { z(_1) }
78
+ .then { sp(_1) }
79
+ }
80
+
81
+ def octal(n, pad_size) = n.nil? ? "\0" * pad_size : "%0#{pad_size}o" % n
82
+
83
+ def asciiz(str, size) = "#{str}#{"\0" * (size - str.bytesize)}"
84
+
85
+ def sp(s) = "#{s} "
86
+
87
+ def z(s) = "#{s}\0"
88
+
89
+ def mode_string(value) = "%04o" % (value & 0o777)
90
+
91
+ def __field_order = FIELD_ORDER
92
+
93
+ def __fields = FIELDS
94
+
95
+ FIELD_ORDER = []
96
+ private_constant :FIELD_ORDER
97
+
98
+ FIELDS = {}
99
+ private_constant :FIELDS
100
+
101
+ Field = Struct.new(:name, :offset, :length)
102
+ private_constant :Field
103
+
104
+ BLANK_CHECKSUM = (" " * 8).freeze
105
+ private_constant :BLANK_CHECKSUM
106
+
107
+ DOUBLE_ZERO = "00"
108
+ private_constant :DOUBLE_ZERO
109
+
110
+ NULL_100 = ("\0" * 100).freeze
111
+ private_constant :NULL_100
112
+
113
+ USTAR = "ustar\0"
114
+ private_constant :USTAR
115
+
116
+ fields = [
117
+ ["name", 100], ["mode", 8], ["uid", 8], ["gid", 8], ["size", 12], ["mtime", 12],
118
+ ["checksum", 8], ["typeflag", 1], ["linkname", 100], ["magic", 6], ["version", 2],
119
+ ["uname", 32], ["gname", 32], ["devmajor", 8], ["devminor", 8], ["prefix", 155]
120
+ ]
121
+ offset = 0
122
+
123
+ fields.each do |(name, length)|
124
+ FIELDS[name] = Field.new(name, offset, length)
125
+ FIELD_ORDER << name
126
+ offset += length
127
+ end
128
+
129
+ Minitest::Test.send(:include, self)
130
+ end