Skip to content

Commit

Permalink
don't unpack extended tar headers as regular files
Browse files Browse the repository at this point in the history
Extended headers should not be extracted as regular files.

Most popular tar implementations will create directories as needed, even
if they don't exist in the archive. This was already fixed in master, but
left the test case for completeness.

extended_header.tar contains an global extended header like git uses.
implied_dir.tar contains 2 files in multiple directories with no
directory entries.
  • Loading branch information
jbardin committed Sep 1, 2017
1 parent 6aae8e4 commit 668c725
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
29 changes: 29 additions & 0 deletions decompress_tar.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ func untar(input io.Reader, dst, src string, dir bool) error {
return err
}

if hdr.Typeflag == tar.TypeXGlobalHeader || hdr.Typeflag == tar.TypeXHeader {
// don't unpack extended headers as files
continue
}

path := dst
if dir {
path = filepath.Join(path, hdr.Name)
Expand Down Expand Up @@ -81,3 +86,27 @@ func untar(input io.Reader, dst, src string, dir bool) error {
}
}
}

// tarDecompressor is an implementation of Decompressor that can
// unpack tar files.
type tarDecompressor struct{}

func (d *tarDecompressor) Decompress(dst, src string, dir bool) error {
// If we're going into a directory we should make that first
mkdir := dst
if !dir {
mkdir = filepath.Dir(dst)
}
if err := os.MkdirAll(mkdir, 0755); err != nil {
return err
}

// File first
f, err := os.Open(src)
if err != nil {
return err
}
defer f.Close()

return untar(f, dst, src, dir)
}
31 changes: 31 additions & 0 deletions decompress_tar_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package getter

import (
"path/filepath"
"testing"
)

func TestTar(t *testing.T) {
cases := []TestDecompressCase{
{
"extended_header.tar",
true,
false,
[]string{"directory/", "directory/a", "directory/b"},
"",
},
{
"implied_dir.tar",
true,
false,
[]string{"directory/", "directory/sub/", "directory/sub/a", "directory/sub/b"},
"",
},
}

for i, tc := range cases {
cases[i].Input = filepath.Join("./test-fixtures", "decompress-tar", tc.Input)
}

TestDecompressor(t, new(tarDecompressor), cases)
}
Binary file added test-fixtures/decompress-tar/extended_header.tar
Binary file not shown.
Binary file added test-fixtures/decompress-tar/implied_dir.tar
Binary file not shown.

0 comments on commit 668c725

Please sign in to comment.