Skip to content

Commit 6872afa

Browse files
committed
Fix zipFiler crash on dirs with subdirs
This happens since nodes for directories don't have their "file" field initialized. Signed-off-by: Tal Einat <[email protected]>
1 parent 63bc934 commit 6872afa

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

licensedb/filer/filer.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,9 +323,13 @@ func (filer *zipFiler) ReadDir(path string) ([]File, error) {
323323
}
324324
result := make([]File, 0, len(node.children))
325325
for name, child := range node.children {
326+
isDir := true
327+
if child.file != nil {
328+
isDir = child.file.FileInfo().IsDir()
329+
}
326330
result = append(result, File{
327331
Name: name,
328-
IsDir: child.file.FileInfo().IsDir(),
332+
IsDir: isDir,
329333
})
330334
}
331335
return result, nil

licensedb/filer/filer_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,27 @@ func TestZipFiler(t *testing.T) {
7979
filer, err := FromZIP("test_data/local.zip")
8080
assert.Nil(t, err)
8181
testFiler(t, filer)
82+
8283
filer, err = FromZIP("test_data/local2.zip")
8384
assert.Nil(t, filer)
8485
assert.NotNil(t, err)
86+
87+
filer, err = FromZIP("test_data/empty_sub_dir.zip")
88+
assert.Nil(t, err)
89+
defer filer.Close()
90+
files, err := filer.ReadDir("")
91+
assert.Nil(t, err)
92+
assert.Len(t, files, 1)
93+
assert.Equal(t, "dir", files[0].Name)
94+
assert.True(t, files[0].IsDir)
95+
files, err = filer.ReadDir("dir")
96+
assert.Nil(t, err)
97+
assert.Len(t, files, 1)
98+
assert.Equal(t, "sub_dir", files[0].Name)
99+
assert.True(t, files[0].IsDir)
100+
files, err = filer.ReadDir("dir/sub_dir")
101+
assert.Nil(t, err)
102+
assert.Len(t, files, 0)
85103
}
86104

87105
func TestNestedFiler(t *testing.T) {
310 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)