Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix up writing tar archives with the rubygems tar #19

Merged
merged 1 commit into from
May 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/mixlib/archive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

module Mixlib
class Archive
class TarError < StandardError; end

attr_reader :archiver
alias_method :extractor, :archiver

Expand Down
27 changes: 22 additions & 5 deletions lib/mixlib/archive/tar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def extract(destination, perms: true, ignore: [])
Mixlib::Archive::Log.warn "ignoring entry #{entry.full_name}"
next
end
dest ||= File.join(destination, entry.full_name)
dest ||= File.expand_path(File.join(destination, entry.full_name))
parent = File.dirname(dest)
FileUtils.mkdir_p(parent)

Expand Down Expand Up @@ -76,11 +76,18 @@ def create(files, gzip: false)
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)
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
file.close
end
end

Expand Down Expand Up @@ -110,6 +117,14 @@ def is_gzip_file?(path)
IO.binread(path, 2) == [0x1F, 0x8B].pack("C*")
end

# tar's magic is at byte 257 and is "ustar\0"
def is_tar_archive?(io)
io.rewind
magic = io.read[257..262]
io.rewind
magic == "ustar\0"
end

def reader(&block)
raw = File.open(archive, "rb")

Expand All @@ -120,6 +135,8 @@ def reader(&block)
raw
end

raise Mixlib::Archive::TarError, "Unrecognized archive format" unless is_tar_archive?(file)

Gem::Package::TarReader.new(file, &block)
ensure
if file
Expand Down
3 changes: 2 additions & 1 deletion spec/mixlib/tar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
end

describe "#reader" do
let(:raw) { double(File, closed?: true) }
let(:raw) { double(IO, closed?: true, rewind: 0) }

before do
allow(Gem::Package::TarReader).to receive(:new).with(raw, &extraction).and_return(true)
allow_any_instance_of(Mixlib::Archive::Tar).to receive(:is_tar_archive?).and_return(true)
end

context "with a gzipped file" do
Expand Down