Skip to content

Commit

Permalink
fix zip extraction without file entries (kubernetes-sigs#712)
Browse files Browse the repository at this point in the history
* renaming some testdata archives for symmetry
* adding new test archive (non-flat zip, without dir entry)

Signed-off-by: Ahmet Alp Balkan <[email protected]>
  • Loading branch information
ahmetb authored Aug 15, 2021
1 parent a28e951 commit c762f1f
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 15 deletions.
5 changes: 5 additions & 0 deletions internal/download/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ func extractZIP(targetDir string, read io.ReaderAt, size int64) error {
continue
}

dir := filepath.Dir(path)
klog.V(4).Infof("zip: ensuring parent dirs exist for regular file, dir=%s", dir)
if err := os.MkdirAll(dir, 0755); err != nil {
return errors.Wrap(err, "failed to create directory for zip entry")
}
src, err := f.Open()
if err != nil {
return errors.Wrap(err, "could not open inflating zip file")
Expand Down
33 changes: 20 additions & 13 deletions internal/download/downloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,23 @@ func Test_extractZIP(t *testing.T) {
files []string
}{
{
in: "test-with-directory.zip",
in: "test-flat-hierarchy.zip",
files: []string{
"/foo",
},
},
{
in: "test-with-directory-entry.zip",
files: []string{
"/test/",
"/test/foo",
},
},
{
in: "test-without-directory.zip",
in: "test-with-no-directory-entry.zip",
files: []string{
"/foo",
"/test/",
"/test/foo",
},
},
}
Expand Down Expand Up @@ -88,18 +95,18 @@ func Test_extractTARGZ(t *testing.T) {
files []string
}{
{
in: "test-without-directory.tar.gz",
in: "test-flat-hierarchy.tar.gz",
files: []string{"/foo"},
},
{
in: "test-with-nesting-with-directory-entries.tar.gz",
in: "test-with-directory-entry.tar.gz",
files: []string{
"/test/",
"/test/foo",
},
},
{
in: "test-with-nesting-without-directory-entries.tar.gz",
in: "test-with-no-directory-entry.tar.gz",
files: []string{
"/test/",
"/test/foo",
Expand Down Expand Up @@ -173,9 +180,9 @@ func TestDownloader_Get(t *testing.T) {
name: "successful get",
fields: fields{
verifier: newTrueVerifier(),
fetcher: NewFileFetcher(filepath.Join(testdataPath(), "test-with-directory.zip")),
fetcher: NewFileFetcher(filepath.Join(testdataPath(), "test-with-directory-entry.zip")),
},
uri: "foo/bar/test-with-directory.zip",
uri: "foo/bar/test-with-directory-entry.zip",
wantErr: false,
},
{
Expand All @@ -184,7 +191,7 @@ func TestDownloader_Get(t *testing.T) {
verifier: newTrueVerifier(),
fetcher: errorFetcher{},
},
uri: "foo/bar/test-with-directory.zip",
uri: "foo/bar/test-with-directory-entry.zip",
wantErr: true,
},
}
Expand All @@ -201,7 +208,7 @@ func TestDownloader_Get(t *testing.T) {
}

func Test_download(t *testing.T) {
filePath := filepath.Join(testdataPath(), "test-with-directory.zip")
filePath := filepath.Join(testdataPath(), "test-with-directory-entry.zip")
downloadOriginal, err := ioutil.ReadFile(filePath)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -302,15 +309,15 @@ func Test_detectMIMEType(t *testing.T) {
{
name: "type zip",
args: args{
file: filepath.Join(testdataPath(), "test-with-directory.zip"),
file: filepath.Join(testdataPath(), "test-with-directory-entry.zip"),
},
want: "application/zip",
wantErr: false,
},
{
name: "type tar.gz",
args: args{
file: filepath.Join(testdataPath(), "test-with-nesting-with-directory-entries.tar.gz"),
file: filepath.Join(testdataPath(), "test-with-directory-entry.tar.gz"),
},
want: "application/x-gzip",
wantErr: false,
Expand Down Expand Up @@ -434,7 +441,7 @@ func Test_extractArchive(t *testing.T) {
args: args{
filename: "",
dst: "",
file: filepath.Join(testdataPath(), "test-with-nesting-with-directory-entries.tar.gz"),
file: filepath.Join(testdataPath(), "test-with-directory-entry.tar.gz"),
},
wantErr: true,
},
Expand Down
Binary file not shown.
Empty file.
4 changes: 2 additions & 2 deletions internal/installation/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func Test_downloadAndExtract(t *testing.T) {
server := httptest.NewServer(http.FileServer(http.Dir(testdataDir)))
defer server.Close()

url := server.URL + "/test-without-directory.tar.gz"
url := server.URL + "/test-flat-hierarchy.tar.gz"
checksum := "433b9e0b6cb9f064548f451150799daadcc70a3496953490c5148c8e550d2f4e"

if err := downloadAndExtract(tmpDir.Root(), url, checksum, ""); err != nil {
Expand All @@ -241,7 +241,7 @@ func Test_downloadAndExtract(t *testing.T) {
func Test_downloadAndExtract_fileOverride(t *testing.T) {
tmpDir := testutil.NewTempDir(t)

testFile := filepath.Join(testdataPath(t), "..", "..", "download", "testdata", "test-without-directory.tar.gz")
testFile := filepath.Join(testdataPath(t), "..", "..", "download", "testdata", "test-flat-hierarchy.tar.gz")
checksum := "433b9e0b6cb9f064548f451150799daadcc70a3496953490c5148c8e550d2f4e"

if err := downloadAndExtract(tmpDir.Root(), "", checksum, testFile); err != nil {
Expand Down

0 comments on commit c762f1f

Please sign in to comment.