Skip to content

Commit

Permalink
always trim './'
Browse files Browse the repository at this point in the history
Signed-off-by: liangchenye <[email protected]>
  • Loading branch information
liangchenye committed Dec 15, 2015
1 parent d402ae8 commit 354c4b3
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
5 changes: 4 additions & 1 deletion utils/tar.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ func SelectivelyExtractArchive(r io.Reader, prefix string, toExtract []string, m

// Get element filename
filename := hdr.Name
filename = strings.TrimPrefix(filename, prefix)
filename = strings.TrimPrefix(filename, "./")
if prefix != "" {
filename = strings.TrimPrefix(filename, prefix)
}

// Determine if we should extract the element
toBeExtracted := false
Expand Down
6 changes: 3 additions & 3 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ func TestTar(t *testing.T) {
testArchivePath := path.Join(path.Dir(filepath)) + filename

// Extract non compressed data
data, err = SelectivelyExtractArchive(bytes.NewReader([]byte("that string does not represent a tar or tar-gzip file")), "./", []string{}, 0)
data, err = SelectivelyExtractArchive(bytes.NewReader([]byte("that string does not represent a tar or tar-gzip file")), "", []string{}, 0)
assert.Error(t, err, "Extracting non compressed data should return an error")

// Extract an archive
f, _ := os.Open(testArchivePath)
defer f.Close()
data, err = SelectivelyExtractArchive(f, "./", []string{"test/"}, 0)
data, err = SelectivelyExtractArchive(f, "", []string{"test/"}, 0)
assert.Nil(t, err)

if c, n := data["test/test.txt"]; !n {
Expand All @@ -86,7 +86,7 @@ func TestTar(t *testing.T) {
// File size limit
f, _ = os.Open(testArchivePath)
defer f.Close()
data, err = SelectivelyExtractArchive(f, "./", []string{"test"}, 50)
data, err = SelectivelyExtractArchive(f, "", []string{"test"}, 50)
assert.Equal(t, ErrExtractedFileTooBig, err)
}
}
Expand Down
2 changes: 1 addition & 1 deletion worker/detectors/data/tar.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ func (detector *TarDataDetector) Supported(path string, format string) bool {
}

func (detector *TarDataDetector) Detect(layerReader io.ReadCloser, toExtract []string, maxFileSize int64) (map[string][]byte, error) {
return utils.SelectivelyExtractArchive(layerReader, "./", toExtract, maxFileSize)
return utils.SelectivelyExtractArchive(layerReader, "", toExtract, maxFileSize)
}

0 comments on commit 354c4b3

Please sign in to comment.