Skip to content

Commit

Permalink
Added counting of nested Jars 1 level down
Browse files Browse the repository at this point in the history
  • Loading branch information
pivotal-david-osullivan committed May 20, 2022
1 parent b13c052 commit 0418754
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
54 changes: 49 additions & 5 deletions count/count_classes.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ package count

import (
"archive/zip"
"bytes"
"errors"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
Expand Down Expand Up @@ -68,20 +70,22 @@ func JarClasses(path string) (int, error) {
z, err := zip.OpenReader(path)
if err != nil {
if !(errors.Is(err, zip.ErrFormat)) {
return fmt.Errorf("unable to open ZIP %s\n%w", path, err)
return fmt.Errorf("unable to open Jar %s\n%w", path, err)
} else {
return nil
}
}
defer z.Close()

for _, f := range z.File {
for _, e := range ClassExtensions {
if strings.HasSuffix(f.Name, e) {
count++
break
if strings.HasSuffix(f.FileInfo().Name(), ".jar") {
c, err := nestedJarContents(f)
if err != nil {
return fmt.Errorf("unable to counted nested jar%w\n", err)
}
count += c
}
count += jarContents(f)
}

return nil
Expand Down Expand Up @@ -136,3 +140,43 @@ func JarClassesFrom(paths ...string) (int, int, error) {
}
return agentClassCount, skippedPaths, nil
}

func jarContents(file *zip.File) int {
var count = 0
for _, e := range ClassExtensions {
if strings.HasSuffix(file.Name, e) {
count++
break
}
}
return count
}

func nestedJarContents(jarFile *zip.File) (int, error) {
var count = 0

reader, err := jarFile.Open()
if err != nil {
return 0, fmt.Errorf("unable to open nested jar%w\n", err)
}
defer reader.Close()

var b bytes.Buffer
size, err := io.Copy(&b, reader)
if err != nil {
return 0, fmt.Errorf("error copying nested Jar \n%w", err)
}
br := bytes.NewReader(b.Bytes())
nj, err := zip.NewReader(br, size)
if err != nil {
if !(errors.Is(err, zip.ErrFormat)) {
return 0, fmt.Errorf("error reading nested Jar contents\n%w", err)
} else {
return 0, nil
}
}
for _, nestedJar := range nj.File {
count += jarContents(nestedJar)
}
return count, nil
}
12 changes: 10 additions & 2 deletions count/count_classes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,16 @@ func testCountClasses(t *testing.T, context spec.G, it spec.S) {
Expect(count.Classes(path)).To(Equal(2))
})

it("counts files in archives", func() {
Expect(count.Classes("testdata")).To(Equal(2))
it("counts files in single archive", func() {
Expect(count.JarClasses("testdata/stub-dependency.jar")).To(Equal(2))
})

it("counts files with nested archives 1 level down", func() {
Expect(count.Classes("testdata/nested")).To(Equal(4))
})

it("counts files including any nested 1 level down", func() {
Expect(count.Classes("testdata")).To(Equal(6))
})

it("skips empty zip/jar files with none in the name", func() {
Expand Down
Binary file added count/testdata/nested/stub-dependency.jar
Binary file not shown.

0 comments on commit 0418754

Please sign in to comment.