pax_global_header00006660000000000000000000000064130446537060014522gustar00rootroot0000000000000052 comment=356374a570d3b0f3a1ab8bf4b2562ea09c9f9d19 mixlib-archive-0.4.1/000077500000000000000000000000001304465370600144275ustar00rootroot00000000000000mixlib-archive-0.4.1/.gitignore000066400000000000000000000001271304465370600164170ustar00rootroot00000000000000/.bundle/ /.yardoc /Gemfile.lock /_yardoc/ /coverage/ /doc/ /pkg/ /spec/reports/ /tmp/ mixlib-archive-0.4.1/.rspec000066400000000000000000000000371304465370600155440ustar00rootroot00000000000000--format documentation --color mixlib-archive-0.4.1/.travis.yml000066400000000000000000000002701304465370600165370ustar00rootroot00000000000000branches: only: master language: ruby rvm: - 2.1 - 2.2 - 2.3.1 before_install: gem install bundler -v 1.11.2 script: bundle exec rake ci bundler_args: --without changelog mixlib-archive-0.4.1/CHANGELOG.md000066400000000000000000000037131304465370600162440ustar00rootroot00000000000000# Change Log ## [v0.4.1](https://github.com/chef/mixlib-archive/tree/v0.4.1) (2017-02-02) [Full Changelog](https://github.com/chef/mixlib-archive/compare/v0.4.0...v0.4.1) **Merged pull requests:** - Accept a single pattern to ignore [\#8](https://github.com/chef/mixlib-archive/pull/8) ([thommay](https://github.com/thommay)) ## [v0.4.0](https://github.com/chef/mixlib-archive/tree/v0.4.0) (2017-02-02) [Full Changelog](https://github.com/chef/mixlib-archive/compare/v0.3.0...v0.4.0) **Fixed bugs:** - Ignore directory traversing file\_names in an tar [\#6](https://github.com/chef/mixlib-archive/pull/6) ([thommay](https://github.com/thommay)) ## [v0.3.0](https://github.com/chef/mixlib-archive/tree/v0.3.0) (2017-01-19) [Full Changelog](https://github.com/chef/mixlib-archive/compare/v0.2.0...v0.3.0) **Merged pull requests:** - Use Rake 11 [\#5](https://github.com/chef/mixlib-archive/pull/5) ([tas50](https://github.com/tas50)) - Create archives as well as extracting them [\#4](https://github.com/chef/mixlib-archive/pull/4) ([thommay](https://github.com/thommay)) ## [v0.2.0](https://github.com/chef/mixlib-archive/tree/v0.2.0) (2016-07-05) [Full Changelog](https://github.com/chef/mixlib-archive/compare/v0.1.0...v0.2.0) **Merged pull requests:** - Add logging [\#3](https://github.com/chef/mixlib-archive/pull/3) ([thommay](https://github.com/thommay)) - Make extraction safer [\#2](https://github.com/chef/mixlib-archive/pull/2) ([thommay](https://github.com/thommay)) ## [v0.1.0](https://github.com/chef/mixlib-archive/tree/v0.1.0) (2016-05-09) [Full Changelog](https://github.com/chef/mixlib-archive/compare/053f20d5455cc463251f91d1413b973232909dc2...v0.1.0) **Merged pull requests:** - Turns out some tar archives don't contain dirs [\#1](https://github.com/chef/mixlib-archive/pull/1) ([thommay](https://github.com/thommay)) \* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)* mixlib-archive-0.4.1/Gemfile000066400000000000000000000003331304465370600157210ustar00rootroot00000000000000source "https://rubygems.org" # Specify your gem's dependencies in mixlib-archive.gemspec gemspec group(:changelog) do gem "github_changelog_generator", git: "https://github.com/chef/github-changelog-generator" end mixlib-archive-0.4.1/LICENSE000066400000000000000000000251421304465370600154400ustar00rootroot00000000000000 Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. mixlib-archive-0.4.1/README.md000066400000000000000000000024041304465370600157060ustar00rootroot00000000000000# Mixlib::Archive [![Build Status Master](https://travis-ci.org/chef/mixlib-archive.svg?branch=master)](https://travis-ci.org/chef/mixlib-archive) A very simple gem to extract archives. ## Installation Add this line to your application's Gemfile: ```ruby gem 'mixlib-archive' ``` And then execute: $ bundle Or install it yourself as: $ gem install mixlib-archive ## Usage To extract an archive ```ruby tar = Mixlib::Artifact.new("/path/to/tar") tar.extract("/destination/directory") ``` To create an archive ```ruby tar = Mixlib::Artifact.new("/path/to/foo.tar.gz") tar.create(%w{ file.rb file2.rb }, gzip: true) ``` ## Development After checking out the repo, run `bundle` to install dependencies. Then, run `bundle exec rake spec` to run the tests. You can also run `bundle console` for an interactive prompt that will allow you to experiment. ## Contributing Bug reports and pull requests are welcome on GitHub at https://github.com/chef/mixlib-archive. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Community Guidelines](https://docs.chef.io/community_guidelines.html) code of conduct. ## License The gem is available as open source under the terms of the Apache License, v2 mixlib-archive-0.4.1/Rakefile000066400000000000000000000007021304465370600160730ustar00rootroot00000000000000require "bundler/gem_tasks" require "rspec/core/rake_task" RSpec::Core::RakeTask.new(:spec) task :default => :spec desc "Run tests for Travis CI" task ci: [:style, :spec] begin require "chefstyle" require "rubocop/rake_task" RuboCop::RakeTask.new(:style) do |task| task.options += ["--display-cop-names", "--no-color"] end rescue LoadError puts "chefstyle/rubocop is not available. gem install chefstyle to do style checking." end mixlib-archive-0.4.1/lib/000077500000000000000000000000001304465370600151755ustar00rootroot00000000000000mixlib-archive-0.4.1/lib/mixlib/000077500000000000000000000000001304465370600164615ustar00rootroot00000000000000mixlib-archive-0.4.1/lib/mixlib/archive.rb000066400000000000000000000020761304465370600204340ustar00rootroot00000000000000require "mixlib/archive/tar" require "mixlib/archive/version" require "mixlib/log" module Mixlib class Archive attr_reader :archiver alias_method :extractor, :archiver def initialize(archive, empty: false) @empty = empty archive = File.expand_path(archive) # for now we only support Tar format archives. @archiver = Mixlib::Archive::Tar.new(archive) end class Log extend Mixlib::Log end Log.level = :error def create(files = [], gzip: false) archiver.create(files, gzip: gzip) end def extract(destination, perms: true, ignore: []) ignore = [/^\.$/, /\.{2}/] + Array(ignore) create_and_empty(destination) archiver.extract(destination, perms: perms, ignore: ignore) end private def create_and_empty(destination) FileUtils.mkdir_p(destination) if @empty Dir.foreach(destination) do |entry| next if entry == "." || entry == ".." FileUtils.remove_entry_secure(File.join(destination, entry)) end end end end end mixlib-archive-0.4.1/lib/mixlib/archive/000077500000000000000000000000001304465370600201025ustar00rootroot00000000000000mixlib-archive-0.4.1/lib/mixlib/archive/tar.rb000066400000000000000000000075741304465370600212320ustar00rootroot00000000000000require "rubygems/package" require "zlib" module Mixlib class Archive class Tar TAR_LONGLINK = "././@LongLink" attr_reader :options attr_reader :archive def initialize(archive, options = {}) @archive = archive @options = options end # Extracts the archive to the given +destination+ # # === Parameters # perms:: should the extracter use permissions from the archive. # ignore[Array]:: an array of matches of file paths to ignore def extract(destination, perms: true, ignore: []) # (http://stackoverflow.com/a/31310593/506908) ignore_re = Regexp.union(ignore) reader do |tar| dest = nil tar.each do |entry| if entry.full_name == TAR_LONGLINK dest = 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.join(destination, entry.full_name) 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 # Creates an archive with the given set of +files+ # # === Parameters # gzip:: should the archive be gzipped? def create(files, gzip: false) tgt_dir = File.dirname(archive) target = Tempfile.new(File.basename(archive), tgt_dir) Gem::Package::TarWriter.new(target) do |tar| files.each do |fn| mode = File.stat(fn).mode file = File.open(fn, "rb") tar.add_file(fn, mode) do |io| io.write(file) end file.close end end target.close if gzip Zlib::GzipWriter.open(archive) do |gz| gz.mtime = File.mtime(target.path) gz.orig_name = File.basename(archive) gz.write IO.binread(target.path) end else FileUtils.mv(target.path, archive) end ensure target.close unless target.closed? end private def is_gzip_file?(path) # You cannot write "\x1F\x8B" because the default encoding of # ruby >= 1.9.3 is UTF-8 and 8B is an invalid in UTF-8. IO.binread(path, 2) == [0x1F, 0x8B].pack("C*") end def reader(&block) raw = File.open(archive, "rb") file = if is_gzip_file?(archive) Mixlib::Archive::Log.debug "gzip file detected" Zlib::GzipReader.wrap(raw) else raw end Gem::Package::TarReader.new(file, &block) ensure if file file.close unless file.closed? file = nil end if raw raw.close unless raw.closed? raw = nil end end end end end mixlib-archive-0.4.1/lib/mixlib/archive/version.rb000066400000000000000000000000761304465370600221170ustar00rootroot00000000000000module Mixlib class Archive VERSION = "0.4.1" end end mixlib-archive-0.4.1/mixlib-archive.gemspec000066400000000000000000000016351304465370600207040ustar00rootroot00000000000000# coding: utf-8 lib = File.expand_path("../lib", __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require "mixlib/archive/version" Gem::Specification.new do |spec| spec.name = "mixlib-archive" spec.version = Mixlib::Archive::VERSION spec.authors = ["Chef Software, Inc"] spec.email = ["info@chef.io"] spec.summary = %q{A simple interface to various archive formats} spec.description = spec.summary spec.homepage = "https://chef.io" spec.license = "APACHE-2.0" spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } spec.require_paths = ["lib"] spec.add_development_dependency "bundler", "~> 1.11" spec.add_development_dependency "rake", "~> 11.0" spec.add_development_dependency "rspec", "~> 3.0" spec.add_development_dependency "chefstyle" spec.add_dependency "mixlib-log" end mixlib-archive-0.4.1/spec/000077500000000000000000000000001304465370600153615ustar00rootroot00000000000000mixlib-archive-0.4.1/spec/mixlib/000077500000000000000000000000001304465370600166455ustar00rootroot00000000000000mixlib-archive-0.4.1/spec/mixlib/archive_spec.rb000066400000000000000000000073651304465370600216400ustar00rootroot00000000000000require "spec_helper" describe Mixlib::Archive do let(:target) { "../fixtures/foo.tar" } let (:destination) { Dir.mktmpdir } let (:files) { %w{ file dir/file new_file.rb } } let (:archive) { described_class.new(target) } let (:archiver) { double(Mixlib::Archive::Tar, extract: true) } before do allow(File).to receive(:expand_path).and_call_original allow(Mixlib::Archive::Tar).to receive(:new).with(any_args).and_return(archiver) end after do FileUtils.rmdir destination end describe "#initialize" do it "accepts a path" do expect { described_class.new("../foo") }.not_to raise_error end it "allows the target to be emptied" do expect { described_class.new("../foo", empty: true) }.not_to raise_error end context "creates an archiver" do let(:expanded_target) { "/my/expanded/target/fixtures/foo.tar" } before do end it "with the correct archiver" do expect(File).to receive(:expand_path).with(target).and_return(expanded_target) expect(Mixlib::Archive::Tar).to receive(:new).with(expanded_target) described_class.new(target) end end end describe "#create" do it "runs the archiver" do expect(archive).to receive(:create).with(files).and_return(true) archive.create(files) end it "passes options to the archiver" do expect(archive).to receive(:create).with(files, { gzip: true }).and_return(true) archive.create(files, gzip: true) end end describe "#extract" do it "creates the target directory" do expect(archive).to receive(:create_and_empty).with(destination).and_return(true) archive.extract(destination) end it "runs the extractor" do expect(archiver).to receive(:extract).with(destination, { perms: true, ignore: [/^\.$/, /\.{2}/] }) archive.extract(destination) end it "passes options to the extractor" do expect(archiver).to receive(:extract).with(destination, { perms: false, ignore: [/^\.$/, /\.{2}/] }) archive.extract(destination, perms: false) end it "allows the user to ignore more patterns" do expect(archiver).to receive(:extract).with(destination, { perms: false, ignore: [/^\.$/, /\.{2}/, /^$/] }) archive.extract(destination, perms: false, ignore: [/^$/]) end it "accepts a single ignore pattern" do expect(archiver).to receive(:extract).with(destination, { perms: false, ignore: [/^\.$/, /\.{2}/, /^$/] }) archive.extract(destination, perms: false, ignore: /^$/) end end describe "#create_and_empty" do it "creates the target directory" do expect(FileUtils).to receive(:mkdir_p).with(destination) archive.send(:create_and_empty, destination) end it "doesn't empty the target directory" do expect(Dir).not_to receive(:foreach) archive.send(:create_and_empty, destination) end context "optionally empties the target directory" do before do archive.instance_variable_set(:@empty, true) end it "walks the directory" do expect(Dir).to receive(:foreach).with(destination) archive.send(:create_and_empty, destination) end it "ignores . and .." do expect(Dir).to receive(:foreach).with(destination).and_yield(".").and_yield("..") expect(FileUtils).not_to receive(:remove_entry_secure) archive.send(:create_and_empty, destination) end it "deletes the entry" do deleted = File.join(destination, "foo") expect(Dir).to receive(:foreach).with(destination).and_yield(".").and_yield("..").and_yield("foo") expect(FileUtils).to receive(:remove_entry_secure).with(deleted).and_return(true) archive.send(:create_and_empty, destination) end end end end mixlib-archive-0.4.1/spec/mixlib/tar_spec.rb000066400000000000000000000033001304465370600207660ustar00rootroot00000000000000require "spec_helper" describe Mixlib::Archive::Tar do let(:tar_archive) { "../fixtures/test.tar" } let(:tgz_archive) { "../fixtures/test.tgz" } let(:tar_gz_archive) { "../fixtures/test.tar.gz" } let(:extraction) { lambda { |f| } } let(:destination) { Dir.mktmpdir } let(:gzip_header) { [0x1F, 0x8B].pack("C*") } before do allow(IO).to receive(:binread).and_return(nil) end after do FileUtils.remove_entry destination end describe "#reader" do let(:raw) { double(File, closed?: true) } before do allow(Gem::Package::TarReader).to receive(:new).with(raw, &extraction).and_return(true) end context "with a gzipped file" do before do allow(IO).to receive(:binread).and_return(gzip_header) end it "creates the correct reader for a .tgz file" do extractor = described_class.new(tgz_archive) expect(File).to receive(:open).with(tgz_archive, "rb").and_return(raw) expect(Zlib::GzipReader).to receive(:wrap).with(raw).and_return(raw) extractor.send(:reader, &extraction) end it "creates the correct reader for a .tar.gz file" do extractor = described_class.new(tar_gz_archive) expect(File).to receive(:open).with(tar_gz_archive, "rb").and_return(raw) expect(Zlib::GzipReader).to receive(:wrap).with(raw).and_return(raw) extractor.send(:reader, &extraction) end end context "with a regular tar file" do let(:extractor) { described_class.new(tar_archive) } it "creates the correct reader" do expect(File).to receive(:open).with(tar_archive, "rb").and_return(raw) extractor.send(:reader, &extraction) end end end end mixlib-archive-0.4.1/spec/spec_helper.rb000066400000000000000000000001241304465370600201740ustar00rootroot00000000000000$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__) require "mixlib/archive"