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

update tar magic to identify oldgnu style tar headers #21

Merged
merged 3 commits into from
May 8, 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
9 changes: 7 additions & 2 deletions lib/mixlib/archive/tar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,16 @@ def is_gzip_file?(path)
end

# tar's magic is at byte 257 and is "ustar\0"
# OLDGNU_MAGIC "ustar \0" /* 7 chars and a null */
def is_tar_archive?(io)
!(read_tar_magic(io) =~ /ustar\s{0,2}\x00/).nil?
end

def read_tar_magic(io)
io.rewind
magic = io.read[257..262]
magic = io.read[257..264]
io.rewind
magic == "ustar\0"
magic
end

def reader(&block)
Expand Down
26 changes: 26 additions & 0 deletions spec/mixlib/tar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,30 @@
end

end

describe "#is_tar_archive?" do
let(:raw) { double(IO, closed?: true, rewind: 0, read: data) }
context "oldgnu style header" do
let(:data) { "testdir/#{Array.new(249) { "\x00" }.join}ustar \x00" }
it "identifies an oldgnu style tar header" do
extractor = described_class.new(tgz_archive)
expect(extractor.send(:is_tar_archive?, raw)).to eq(true)
end
end
context "gnu style header" do
let(:data) { "testdir/#{Array.new(249) { "\x00" }.join}ustar\x00" }
it "identifies an gnu style tar header" do
extractor = described_class.new(tgz_archive)
expect(extractor.send(:is_tar_archive?, raw)).to eq(true)
end
end
context "invalid header" do
let(:data) { "testdir/#{Array.new(249) { "\x00" }.join}notavalidheader" }
it "does not identify an invalid header" do
extractor = described_class.new(tgz_archive)
expect(extractor.send(:is_tar_archive?, raw)).to eq(false)
end
end
end

end