44require "rbconfig"
55require "rbconfig/sizeof"
66
7- # == Synopsis
7+ # ## Synopsis
88#
99# Using minitar is easy. The simplest case is:
1010#
11- # require 'zlib'
12- # require 'minitar'
11+ # ```ruby
12+ # require 'zlib'
13+ # require 'minitar'
1314#
14- # # Packs everything that matches Find.find('tests'). test.tar will automatically be
15- # # closed by Minitar.pack.
16- # 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'))
1718#
18- # # Unpacks 'test.tar' to 'x', creating 'x' if necessary.
19- # Minitar.unpack('test.tar', 'x')
19+ # # Unpacks 'test.tar' to 'x', creating 'x' if necessary.
20+ # Minitar.unpack('test.tar', 'x')
21+ # ```
2022#
2123# A gzipped tar can be written with:
2224#
23- # # test.tgz will be closed automatically.
24- # Minitar.pack('tests', Zlib::GzipWriter.new(File.open('test.tgz', 'wb'))
2525#
26- # # test.tgz will be closed automatically.
27- # 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+ # ```
2833#
2934# As the case above shows, one need not write to a file. However, it will sometimes
3035# require that one dive a little deeper into the API, as in the case of StringIO objects.
3136# Note that I'm not providing a block with Minitar::Output, as Minitar::Output#close
3237# automatically closes both the Output object and the wrapped data stream object.
3338#
34- # begin
35- # sgz = Zlib::GzipWriter.new(StringIO.new(""))
36- # tar = Minitar::Output.new(sgz)
37- # Find.find('tests') do |entry|
38- # Minitar.pack_file(entry, tar)
39- # end
40- # ensure
41- # # Closes both tar and sgz.
42- # tar.close
43- # 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+ # ```
4451class Minitar
45- # The base class for any minitar error.
52+ # The base class for any Minitar error.
4653 Error = Class . new ( ::StandardError )
4754 # Raised when a wrapped data stream class is not seekable.
4855 NonSeekableStream = Class . new ( Error )
@@ -54,7 +61,7 @@ class Minitar
5461 FileNameTooLong = Class . new ( Error )
5562 # The exception raised when a data stream ends before the amount of data expected in the
5663 # archive's PosixHeader.
57- UnexpectedEOF = Class . new ( StandardError )
64+ UnexpectedEOF = Class . new ( Error )
5865 # The exception raised when a file contains a relative path in secure mode (the default
5966 # for this version).
6067 SecureRelativePathError = Class . new ( Error )
@@ -63,23 +70,23 @@ class Minitar
6370end
6471
6572class << Minitar
66- # Tests if + path+ refers to a directory. Fixes an apparently corrupted <tt> stat()</tt>
67- # call on Windows.
68- def dir? ( path )
73+ # Tests if ` path` refers to a directory. Fixes an apparently corrupted ` stat()` call on
74+ # Windows.
75+ def dir? ( path ) # :nodoc:
6976 path = "#{ path } /" unless path . to_s . end_with? ( "/" )
7077 File . directory? ( path )
7178 end
7279
73- # A convenience method for wrapping Minitar::Input.open (mode +r+ or +rb ) and
74- # Minitar::Output.open (mode +w+ or +wb+ ). No other modes are currently supported.
80+ # A convenience method for wrapping Minitar::Input.open (mode `r` or `rb` ) and
81+ # Minitar::Output.open (mode `w` or `wb` ). No other modes are currently supported.
7582 def open ( dest , mode = "r" , &)
7683 case mode
7784 when "r" , "rb"
7885 Minitar ::Input . open ( dest , &)
7986 when "w" , "wb"
8087 Minitar ::Output . open ( dest , &)
8188 else
82- raise ArgumentError , "Unknown open mode for Minitar.open."
89+ raise ArgumentError , "Unknown open mode #{ mode . inspect } for Minitar.open."
8390 end
8491 end
8592
@@ -88,19 +95,19 @@ def windows? = # :nodoc:
8895 RbConfig ::CONFIG [ "host_os" ] . to_s =~ /^(cygwin|mingw|mswin|windows)/i ||
8996 File ::ALT_SEPARATOR == "\\ "
9097
91- # A convenience method to pack the provided + data+ as a file named + entry+. +entry+ may
92- # either be a name or a Hash with the fields described below. When only a name is
98+ # A convenience method to pack the provided ` data` as a file named ` entry`, which may
99+ # either be a name String or a Hash with the fields described below. When only a name is
93100 # provided, or only some Hash fields are provided, the default values will apply.
94101 #
95- # <tt> :name</tt>:: The filename to be packed into the archive. Required .
96- # <tt> :mode</tt>:: The mode to be applied. Defaults to 0o644 for files and 0o755 for
97- # directories.
98- # <tt> :uid</tt>:: The user owner of the file. Default is + nil+ .
99- # <tt> :gid</tt>:: The group owner of the file. Default is + nil+ .
100- # <tt> :mtime</tt>:: The modification Time of the file. Default is + Time.now+ .
102+ # - ` :name` (**required**): The filename to be packed into the archive.
103+ # - ` :mode`: The mode to be applied. Defaults to ` 0o644` for files and ` 0o755` for
104+ # directories.
105+ # - ` :uid`: The user owner of the file. Default is ` nil` .
106+ # - ` :gid`: The group owner of the file. Default is ` nil` .
107+ # - ` :mtime`: The modification Time of the file. Default is ` Time.now` .
101108 #
102- # If + data+ is + nil+ , a directory will be created. Use an empty String for a normal
103- # empty file.
109+ # If ` data` is ` nil` , a directory will be created. Use an empty String for an empty
110+ # file.
104111 def pack_as_file ( entry , data , outputter ) # :yields: action, name, stats
105112 outputter = outputter . tar if outputter . is_a? ( Minitar ::Output )
106113
@@ -143,35 +150,36 @@ def pack_as_file(entry, data, outputter) # :yields: action, name, stats
143150 end
144151 end
145152
146- # A convenience method to pack the file provided. + entry+ may either be a filename (in
147- # which case various values for the file (see below) will be obtained from
148- # <tt> File#stat(entry)</tt> or a Hash with the fields:
153+ # A convenience method to pack the file provided. ` entry` may either be a filename
154+ # string ( which will in which case various values for the file will be obtained from
155+ # ` File#stat(entry)` or a Hash with the fields:
149156 #
150- # <tt> :name</tt>:: The filename to be packed into the archive. Required .
151- # <tt> :mode</tt>:: The mode to be applied.
152- # <tt> :uid</tt>:: The user owner of the file. (Ignored on Windows.)
153- # <tt> :gid</tt>:: The group owner of the file. (Ignored on Windows.)
154- # <tt> :mtime</tt>: : The modification Time of the file.
157+ # - ` :name` (**required**): The filename to be packed into the archive.
158+ # - ` :mode`: The mode to be applied.
159+ # - ` :uid`: The user owner of the file, ignored on Windows.
160+ # - ` :gid`: The group owner of the file, ignored on Windows.
161+ # - ` :mtime` : The modification Time of the file.
155162 #
156- # During packing, if a block is provided, #pack_file yields an + action+ Symbol, the full
163+ # During packing, if a block is provided, #pack_file yields an ` action` Symbol, the full
157164 # name of the file being packed, and a Hash of statistical information, just as with
158165 # Minitar::Input#extract_entry.
159166 #
160- # The + action+ will be one of:
161- # <tt>:dir</tt>:: The +entry+ is a directory.
162- # <tt>:file_start</tt>:: The + entry+ is a file; the extract of the file is just
163- # beginning.
164- # <tt> :file_progress</tt>:: Yielded every 4096 bytes during the extract of the + entry+ .
165- # <tt>: file_done</tt>:: Yielded when the + entry+ is completed.
167+ # The ` action` will be one of:
168+ #
169+ # - `:dir`: The ` entry` is a directory.
170+ # - `:file_start`: The `entry` is a file; the extract of the file is just beginning.
171+ # - ` :file_progress`: Yielded every 4096 bytes during the extract of the file ` entry` .
172+ # - ` file_done`: Yielded when the file ` entry` is completed.
166173 #
167- # The +stats+ hash contains the following keys:
168- # <tt>:current</tt>:: The current total number of bytes read in the +entry+.
169- # <tt>:currinc</tt>:: The current number of bytes read in this read cycle.
170- # <tt>:name</tt>:: The filename to be packed into the tarchive. *REQUIRED*.
171- # <tt>:mode</tt>:: The mode to be applied.
172- # <tt>:uid</tt>:: The user owner of the file. (+nil+ on Windows.)
173- # <tt>:gid</tt>:: The group owner of the file. (+nil+ on Windows.)
174- # <tt>:mtime</tt>:: The modification Time of the file.
174+ # The +stats+ hash may contain the following keys:
175+ #
176+ # - `:current`: The current total number of bytes read in the `entry`.
177+ # - `:currinc`: The current number of bytes read in this read cycle.
178+ # - `:name`: The filename packed into the tarchive.
179+ # - `:mode`: The mode to be applied.
180+ # - `:uid`: The user owner of the file, `nil` on Windows.
181+ # - `:gid`: The group owner of the file, `nil` on Windows.
182+ # - `:mtime`: The modification Time of the file.
175183 def pack_file ( entry , outputter ) # :yields: action, name, stats
176184 outputter = outputter . tar if outputter . is_a? ( Minitar ::Output )
177185
@@ -219,11 +227,12 @@ def pack_file(entry, outputter) # :yields: action, name, stats
219227 end
220228 end
221229
222- # A convenience method to pack files specified by + src+ into + dest+ . If + src+ is an
230+ # A convenience method to pack files specified by ` src` into ` dest` . If ` src` is an
223231 # Array, then each file detailed therein will be packed into the resulting
224- # Minitar::Output stream; if +recurse_dirs+ is true, then directories will be recursed.
232+ # Minitar::Output stream. If `recurse_dirs` is `true` (the default), then directories
233+ # will be recursed.
225234 #
226- # If + src+ is not an Array, it will be treated as the result of Find.find; all files
235+ # If ` src` is not an Array, it will be treated as the source of Find.find; all files
227236 # matching will be packed.
228237 def pack ( src , dest , recurse_dirs = true , &block )
229238 require "find"
@@ -246,8 +255,10 @@ def pack(src, dest, recurse_dirs = true, &block)
246255 end
247256 end
248257
249- # A convenience method to unpack files from +src+ into the directory specified by
250- # +dest+. Only those files named explicitly in +files+ will be extracted.
258+ # A convenience method to unpack files from `src` into the directory specified by
259+ # `dest`.
260+ #
261+ # If `files` is not empty, it will act as a full name filter to restrict extraction.
251262 def unpack ( src , dest , files = [ ] , options = { } , &block )
252263 Minitar ::Input . open ( src ) do |inp |
253264 if File . exist? ( dest ) && !dir? ( dest )
@@ -264,8 +275,8 @@ def unpack(src, dest, files = [], options = {}, &block)
264275 end
265276 end
266277
267- # Check whether +io+ can seek without errors.
268- def seekable? ( io , methods = nil )
278+ # Check whether `io` can seek without errors.
279+ def seekable? ( io , methods = nil ) # :nodoc:
269280 # The IO class throws an exception at runtime if we try to change position on
270281 # a non-regular file.
271282 if io . respond_to? ( :stat )
@@ -283,4 +294,6 @@ def seekable?(io, methods = nil)
283294require "minitar/pax_header"
284295require "minitar/input"
285296require "minitar/output"
297+ require "minitar/reader"
298+ require "minitar/writer"
286299require "minitar/version"
0 commit comments