always trim './'

Signed-off-by: liangchenye <liangchenye@huawei.com>
This commit is contained in:
liangchenye 2015-12-15 11:34:22 +08:00
parent d402ae818e
commit 354c4b3672
3 changed files with 8 additions and 5 deletions

View File

@ -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

View File

@ -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 {
@ -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)
}
}

View File

@ -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)
}