Class: Mixlib::Archive::Tar
- Inherits:
-
Object
- Object
- Mixlib::Archive::Tar
- Defined in:
- lib/mixlib/archive/tar.rb
Constant Summary collapse
- TAR_LONGLINK =
"././@LongLink".freeze
Instance Attribute Summary collapse
-
#archive ⇒ Object
readonly
Returns the value of attribute archive.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#create(files, gzip: false) ⇒ Object
Creates an archive with the given set of
files. -
#extract(destination, perms: true, ignore: []) ⇒ Object
Extracts the archive to the given
destination. -
#initialize(archive, options = {}) ⇒ Tar
constructor
A new instance of Tar.
Constructor Details
#initialize(archive, options = {}) ⇒ Tar
Returns a new instance of Tar.
16 17 18 19 |
# File 'lib/mixlib/archive/tar.rb', line 16 def initialize(archive, = {}) @archive = archive = end |
Instance Attribute Details
#archive ⇒ Object (readonly)
Returns the value of attribute archive.
14 15 16 |
# File 'lib/mixlib/archive/tar.rb', line 14 def archive @archive end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
13 14 15 |
# File 'lib/mixlib/archive/tar.rb', line 13 def end |
Instance Method Details
#create(files, gzip: false) ⇒ Object
Creates an archive with the given set of files
Parameters
- gzip
should the archive be gzipped?
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 |
# File 'lib/mixlib/archive/tar.rb', line 83 def create(files, gzip: false) tgt_dir = File.dirname(archive) target = Tempfile.new(File.basename(archive), tgt_dir) target.binmode Gem::Package::TarWriter.new(target) do |tar| files.each do |fn| mode = File.stat(fn).mode if File.symlink?(fn) target = File.readlink(fn) tar.add_symlink(fn, target, mode) elsif File.directory?(fn) tar.mkdir(fn, mode) elsif File.file?(fn) file = File.open(fn, "rb") tar.add_file(fn, mode) do |io| io.write(file.read) end file.close end end end target.close if gzip Zlib::GzipWriter.open(archive, Zlib::BEST_COMPRESSION) do |gz| gz.mtime = File.mtime(target.path) gz.orig_name = File.basename(archive) File.open(target.path) do |file| while (chunk = file.read(16 * 1024)) gz.write(chunk) end end end else FileUtils.mv(target.path, archive) end ensure target.close unless target.closed? end |
#extract(destination, perms: true, ignore: []) ⇒ Object
Extracts the archive to the given destination
Parameters
- perms
should the extractor use permissions from the archive.
- ignore
an array of matches of file paths to ignore
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 |
# File 'lib/mixlib/archive/tar.rb', line 26 def extract(destination, perms: true, ignore: []) # (http://stackoverflow.com/a/31310593/506908) ignore_re = Regexp.union(ignore) destination_root = File.(destination) reader do |tar| dest = nil tar.each do |entry| if entry.full_name == TAR_LONGLINK dest = File.(File.join(destination, entry.read.strip)) next end if entry.full_name =~ ignore_re Mixlib::Archive::Log.warn "ignoring entry #{entry.full_name}" next end dest ||= File.(File.join(destination, entry.full_name)) # Reject any entry (including @LongLink-derived paths) that resolves # outside destination, closing the tar-slip / path traversal bypass. unless dest == destination_root || dest.start_with?(destination_root + File::SEPARATOR) Mixlib::Archive::Log.warn "ignoring entry #{entry.full_name}: resolves outside #{destination}" dest = nil next end parent = File.dirname(dest) FileUtils.mkdir_p(parent) if entry.directory? || (entry.header.typeflag == "" && entry.full_name.end_with?("/")) File.delete(dest) if File.file?(dest) if perms FileUtils.mkdir_p(dest, mode: entry.header.mode, verbose: false) else FileUtils.mkdir_p(dest, verbose: false) end elsif entry.file? || (entry.header.typeflag == "" && !entry.full_name.end_with?("/")) FileUtils.rm_rf(dest) if File.directory?(dest) File.open(dest, "wb") do |f| f.print(entry.read) end FileUtils.chmod(entry.header.mode, dest, verbose: false) if perms elsif entry.header.typeflag == "2" # handle symlink File.symlink(entry.header.linkname, dest) else Mixlib::Archive::Log.warn "unknown tar entry: #{entry.full_name} type: #{entry.header.typeflag}" end dest = nil end end end |