Skip to content

Commit c762f1f

Browse files
authored
fix zip extraction without file entries (#712)
* renaming some testdata archives for symmetry * adding new test archive (non-flat zip, without dir entry) Signed-off-by: Ahmet Alp Balkan <[email protected]>
1 parent a28e951 commit c762f1f

10 files changed

+27
-15
lines changed

β€Žinternal/download/downloader.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ func extractZIP(targetDir string, read io.ReaderAt, size int64) error {
7070
continue
7171
}
7272

73+
dir := filepath.Dir(path)
74+
klog.V(4).Infof("zip: ensuring parent dirs exist for regular file, dir=%s", dir)
75+
if err := os.MkdirAll(dir, 0755); err != nil {
76+
return errors.Wrap(err, "failed to create directory for zip entry")
77+
}
7378
src, err := f.Open()
7479
if err != nil {
7580
return errors.Wrap(err, "could not open inflating zip file")

β€Žinternal/download/downloader_test.go

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,23 @@ func Test_extractZIP(t *testing.T) {
4646
files []string
4747
}{
4848
{
49-
in: "test-with-directory.zip",
49+
in: "test-flat-hierarchy.zip",
50+
files: []string{
51+
"/foo",
52+
},
53+
},
54+
{
55+
in: "test-with-directory-entry.zip",
5056
files: []string{
5157
"/test/",
5258
"/test/foo",
5359
},
5460
},
5561
{
56-
in: "test-without-directory.zip",
62+
in: "test-with-no-directory-entry.zip",
5763
files: []string{
58-
"/foo",
64+
"/test/",
65+
"/test/foo",
5966
},
6067
},
6168
}
@@ -88,18 +95,18 @@ func Test_extractTARGZ(t *testing.T) {
8895
files []string
8996
}{
9097
{
91-
in: "test-without-directory.tar.gz",
98+
in: "test-flat-hierarchy.tar.gz",
9299
files: []string{"/foo"},
93100
},
94101
{
95-
in: "test-with-nesting-with-directory-entries.tar.gz",
102+
in: "test-with-directory-entry.tar.gz",
96103
files: []string{
97104
"/test/",
98105
"/test/foo",
99106
},
100107
},
101108
{
102-
in: "test-with-nesting-without-directory-entries.tar.gz",
109+
in: "test-with-no-directory-entry.tar.gz",
103110
files: []string{
104111
"/test/",
105112
"/test/foo",
@@ -173,9 +180,9 @@ func TestDownloader_Get(t *testing.T) {
173180
name: "successful get",
174181
fields: fields{
175182
verifier: newTrueVerifier(),
176-
fetcher: NewFileFetcher(filepath.Join(testdataPath(), "test-with-directory.zip")),
183+
fetcher: NewFileFetcher(filepath.Join(testdataPath(), "test-with-directory-entry.zip")),
177184
},
178-
uri: "foo/bar/test-with-directory.zip",
185+
uri: "foo/bar/test-with-directory-entry.zip",
179186
wantErr: false,
180187
},
181188
{
@@ -184,7 +191,7 @@ func TestDownloader_Get(t *testing.T) {
184191
verifier: newTrueVerifier(),
185192
fetcher: errorFetcher{},
186193
},
187-
uri: "foo/bar/test-with-directory.zip",
194+
uri: "foo/bar/test-with-directory-entry.zip",
188195
wantErr: true,
189196
},
190197
}
@@ -201,7 +208,7 @@ func TestDownloader_Get(t *testing.T) {
201208
}
202209

203210
func Test_download(t *testing.T) {
204-
filePath := filepath.Join(testdataPath(), "test-with-directory.zip")
211+
filePath := filepath.Join(testdataPath(), "test-with-directory-entry.zip")
205212
downloadOriginal, err := ioutil.ReadFile(filePath)
206213
if err != nil {
207214
t.Fatal(err)
@@ -302,15 +309,15 @@ func Test_detectMIMEType(t *testing.T) {
302309
{
303310
name: "type zip",
304311
args: args{
305-
file: filepath.Join(testdataPath(), "test-with-directory.zip"),
312+
file: filepath.Join(testdataPath(), "test-with-directory-entry.zip"),
306313
},
307314
want: "application/zip",
308315
wantErr: false,
309316
},
310317
{
311318
name: "type tar.gz",
312319
args: args{
313-
file: filepath.Join(testdataPath(), "test-with-nesting-with-directory-entries.tar.gz"),
320+
file: filepath.Join(testdataPath(), "test-with-directory-entry.tar.gz"),
314321
},
315322
want: "application/x-gzip",
316323
wantErr: false,
@@ -434,7 +441,7 @@ func Test_extractArchive(t *testing.T) {
434441
args: args{
435442
filename: "",
436443
dst: "",
437-
file: filepath.Join(testdataPath(), "test-with-nesting-with-directory-entries.tar.gz"),
444+
file: filepath.Join(testdataPath(), "test-with-directory-entry.tar.gz"),
438445
},
439446
wantErr: true,
440447
},
Binary file not shown.

β€Žinternal/download/testdata/test/foo

Whitespace-only changes.

β€Žinternal/installation/install_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ func Test_downloadAndExtract(t *testing.T) {
223223
server := httptest.NewServer(http.FileServer(http.Dir(testdataDir)))
224224
defer server.Close()
225225

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

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

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

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

0 commit comments

Comments
Β (0)