1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 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 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
# frozen_string_literal: true
require "fileutils"
require "rbconfig"
require "rbconfig/sizeof"
# == Synopsis
#
# Using minitar is easy. The simplest case is:
#
# require 'zlib'
# require 'minitar'
#
# # Packs everything that matches Find.find('tests'). test.tar will automatically be
# # closed by Minitar.pack.
# Minitar.pack('tests', File.open('test.tar', 'wb'))
#
# # Unpacks 'test.tar' to 'x', creating 'x' if necessary.
# Minitar.unpack('test.tar', 'x')
#
# A gzipped tar can be written with:
#
# # test.tgz will be closed automatically.
# Minitar.pack('tests', Zlib::GzipWriter.new(File.open('test.tgz', 'wb'))
#
# # test.tgz will be closed automatically.
# Minitar.unpack(Zlib::GzipReader.new(File.open('test.tgz', 'rb')), 'x')
#
# As the case above shows, one need not write to a file. However, it will sometimes
# require that one dive a little deeper into the API, as in the case of StringIO objects.
# Note that I'm not providing a block with Minitar::Output, as Minitar::Output#close
# automatically closes both the Output object and the wrapped data stream object.
#
# begin
# sgz = Zlib::GzipWriter.new(StringIO.new(""))
# tar = Minitar::Output.new(sgz)
# Find.find('tests') do |entry|
# Minitar.pack_file(entry, tar)
# end
# ensure
# # Closes both tar and sgz.
# tar.close
# end
class Minitar
# The base class for any minitar error.
Error = Class.new(::StandardError)
# Raised when a wrapped data stream class is not seekable.
NonSeekableStream = Class.new(Error)
# The exception raised when operations are performed on a stream that has previously
# been closed.
ClosedStream = Class.new(Error)
# The exception raised when a filename exceeds 256 bytes in length, the maximum
# supported by the standard Tar format.
FileNameTooLong = Class.new(Error)
# The exception raised when a data stream ends before the amount of data expected in the
# archive's PosixHeader.
UnexpectedEOF = Class.new(StandardError)
# The exception raised when a file contains a relative path in secure mode (the default
# for this version).
SecureRelativePathError = Class.new(Error)
# The exception raised when a file contains an invalid Posix header.
InvalidTarStream = Class.new(Error)
end
class << Minitar
# Tests if +path+ refers to a directory. Fixes an apparently corrupted <tt>stat()</tt>
# call on Windows.
def dir?(path)
path = "#{path}/" unless path.to_s.end_with?("/")
File.directory?(path)
end
# A convenience method for wrapping Minitar::Input.open (mode +r+ or +rb) and
# Minitar::Output.open (mode +w+ or +wb+). No other modes are currently supported.
def open(dest, mode = "r", &)
case mode
when "r", "rb"
Minitar::Input.open(dest, &)
when "w", "wb"
Minitar::Output.open(dest, &)
else
raise ArgumentError, "Unknown open mode for Minitar.open."
end
end
def windows? = # :nodoc:
RUBY_PLATFORM =~ /^(?:cyginw|mingw|mswin)/i ||
RbConfig::CONFIG["host_os"].to_s =~ /^(cygwin|mingw|mswin|windows)/i ||
File::ALT_SEPARATOR == "\\"
# A convenience method to pack the provided +data+ as a file named +entry+. +entry+ may
# either be a name or a Hash with the fields described below. When only a name is
# provided, or only some Hash fields are provided, the default values will apply.
#
# <tt>:name</tt>:: The filename to be packed into the archive. Required.
# <tt>:mode</tt>:: The mode to be applied. Defaults to 0o644 for files and 0o755 for
# directories.
# <tt>:uid</tt>:: The user owner of the file. Default is +nil+.
# <tt>:gid</tt>:: The group owner of the file. Default is +nil+.
# <tt>:mtime</tt>:: The modification Time of the file. Default is +Time.now+.
#
# If +data+ is +nil+, a directory will be created. Use an empty String for a normal
# empty file.
def pack_as_file(entry, data, outputter) # :yields: action, name, stats
outputter = outputter.tar if outputter.is_a?(Minitar::Output)
stats = {
gid: nil,
uid: nil,
mtime: Time.now,
size: data&.size || 0,
mode: data ? 0o644 : 0o755
}
if entry.is_a?(Hash)
name = entry[:name]
entry.each_pair {
next if _1 == :name
stats[_1] = _2 unless _2.nil?
}
else
name = entry
end
if data.nil? # Create a directory
yield :dir, name, stats if block_given?
outputter.mkdir(name, stats)
else
outputter.add_file_simple(name, stats) do |os|
stats[:current] = 0
yield :file_start, name, stats if block_given?
StringIO.open(data, "rb") do |ff|
until ff.eof?
stats[:currinc] = os.write(ff.read(4096))
stats[:current] += stats[:currinc]
yield :file_progress, name, stats if block_given?
end
end
yield :file_done, name, stats if block_given?
end
end
end
# A convenience method to pack the file provided. +entry+ may either be a filename (in
# which case various values for the file (see below) will be obtained from
# <tt>File#stat(entry)</tt> or a Hash with the fields:
#
# <tt>:name</tt>:: The filename to be packed into the archive. Required.
# <tt>:mode</tt>:: The mode to be applied.
# <tt>:uid</tt>:: The user owner of the file. (Ignored on Windows.)
# <tt>:gid</tt>:: The group owner of the file. (Ignored on Windows.)
# <tt>:mtime</tt>:: The modification Time of the file.
#
# During packing, if a block is provided, #pack_file yields an +action+ Symbol, the full
# name of the file being packed, and a Hash of statistical information, just as with
# Minitar::Input#extract_entry.
#
# The +action+ will be one of:
# <tt>:dir</tt>:: The +entry+ is a directory.
# <tt>:file_start</tt>:: The +entry+ is a file; the extract of the file is just
# beginning.
# <tt>:file_progress</tt>:: Yielded every 4096 bytes during the extract of the +entry+.
# <tt>:file_done</tt>:: Yielded when the +entry+ is completed.
#
# The +stats+ hash contains the following keys:
# <tt>:current</tt>:: The current total number of bytes read in the +entry+.
# <tt>:currinc</tt>:: The current number of bytes read in this read cycle.
# <tt>:name</tt>:: The filename to be packed into the tarchive. *REQUIRED*.
# <tt>:mode</tt>:: The mode to be applied.
# <tt>:uid</tt>:: The user owner of the file. (+nil+ on Windows.)
# <tt>:gid</tt>:: The group owner of the file. (+nil+ on Windows.)
# <tt>:mtime</tt>:: The modification Time of the file.
def pack_file(entry, outputter) # :yields: action, name, stats
outputter = outputter.tar if outputter.is_a?(Minitar::Output)
stats = {}
if entry.is_a?(Hash)
name = entry[:name]
entry.each { |kk, vv| stats[kk] = vv unless vv.nil? }
else
name = entry
end
name = name.sub(%r{\./}, "")
stat = File.stat(name)
stats[:mode] ||= stat.mode
stats[:mtime] ||= stat.mtime
stats[:size] = stat.size
if windows?
stats[:uid] = nil
stats[:gid] = nil
else
stats[:uid] ||= stat.uid
stats[:gid] ||= stat.gid
end
if File.file?(name)
outputter.add_file_simple(name, stats) do |os|
stats[:current] = 0
yield :file_start, name, stats if block_given?
File.open(name, "rb") do |ff|
until ff.eof?
stats[:currinc] = os.write(ff.read(4096))
stats[:current] += stats[:currinc]
yield :file_progress, name, stats if block_given?
end
end
yield :file_done, name, stats if block_given?
end
elsif dir?(name)
yield :dir, name, stats if block_given?
outputter.mkdir(name, stats)
else
raise "Don't yet know how to pack this type of file."
end
end
# A convenience method to pack files specified by +src+ into +dest+. If +src+ is an
# Array, then each file detailed therein will be packed into the resulting
# Minitar::Output stream; if +recurse_dirs+ is true, then directories will be recursed.
#
# If +src+ is not an Array, it will be treated as the result of Find.find; all files
# matching will be packed.
def pack(src, dest, recurse_dirs = true, &block)
require "find"
Minitar::Output.open(dest) do |outp|
if src.is_a?(Array)
src.each do |entry|
if dir?(entry) && recurse_dirs
Find.find(entry) do |ee|
pack_file(ee, outp, &block)
end
else
pack_file(entry, outp, &block)
end
end
else
Find.find(src) do |entry|
pack_file(entry, outp, &block)
end
end
end
end
# A convenience method to unpack files from +src+ into the directory specified by
# +dest+. Only those files named explicitly in +files+ will be extracted.
def unpack(src, dest, files = [], options = {}, &block)
Minitar::Input.open(src) do |inp|
if File.exist?(dest) && !dir?(dest)
raise "Can't unpack to a non-directory."
end
FileUtils.mkdir_p(dest) unless File.exist?(dest)
inp.each do |entry|
if files.empty? || files.include?(entry.full_name)
inp.extract_entry(dest, entry, options, &block)
end
end
end
end
# Check whether +io+ can seek without errors.
def seekable?(io, methods = nil)
# The IO class throws an exception at runtime if we try to change position on
# a non-regular file.
if io.respond_to?(:stat)
io.stat.file?
else
# Duck-type the rest of this.
methods ||= [:pos, :pos=, :seek, :rewind]
methods = [methods] unless methods.is_a?(Array)
methods.all? { |m| io.respond_to?(m) }
end
end
end
require "minitar/posix_header"
require "minitar/pax_header"
require "minitar/input"
require "minitar/output"
require "minitar/version"
|