diff --git a/pkg/tarutil/tarutil.go b/pkg/tarutil/tarutil.go index 357f96da..bf47bcb6 100644 --- a/pkg/tarutil/tarutil.go +++ b/pkg/tarutil/tarutil.go @@ -25,6 +25,7 @@ import ( "io" "io/ioutil" "os/exec" + "regexp" "strings" ) @@ -50,7 +51,8 @@ var ( type FilesMap map[string][]byte // ExtractFiles decompresses and extracts only the specified files from an -// io.Reader representing an archive. +// io.Reader representing an archive. The files to be extracted are specified +// by regexp func ExtractFiles(r io.Reader, filenames []string) (FilesMap, error) { data := make(map[string][]byte) @@ -78,7 +80,7 @@ func ExtractFiles(r io.Reader, filenames []string) (FilesMap, error) { // Determine if we should extract the element toBeExtracted := false for _, s := range filenames { - if strings.HasPrefix(filename, s) { + if match, err := regexp.MatchString("^"+s, filename); err == nil && match { toBeExtracted = true break } diff --git a/pkg/tarutil/tarutil_test.go b/pkg/tarutil/tarutil_test.go index 9db377ab..fd41cdc2 100644 --- a/pkg/tarutil/tarutil_test.go +++ b/pkg/tarutil/tarutil_test.go @@ -57,6 +57,26 @@ func TestExtract(t *testing.T) { } } +func TestExtractGlob(t *testing.T) { + for _, filename := range testTarballs { + f, err := os.Open(testfilepath(filename)) + assert.Nil(t, err) + defer f.Close() + + data, err := ExtractFiles(f, []string{`.+\.txt`}) + assert.Nil(t, err) + + if c, n := data["test/test.txt"]; !n { + assert.Fail(t, "test/test.txt should have been extracted") + } else { + assert.NotEqual(t, 0, len(c) > 0, "test/test.txt file is empty") + } + if _, n := data["test.txt"]; !n { + assert.Fail(t, "test.txt should also have been extracted") + } + } +} + func TestExtractUncompressedData(t *testing.T) { for _, filename := range testTarballs { f, err := os.Open(testfilepath(filename))