-
Notifications
You must be signed in to change notification settings - Fork 21
/
maven_jar_listing.go
157 lines (128 loc) · 3.35 KB
/
maven_jar_listing.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* Copyright 2018-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package libjvm
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"sort"
"sync"
)
var maven = regexp.MustCompile(`.+/(.*)-([\d].*)\.jar`)
// MavenJAR is metadata about a JRE entry that follows Maven naming conventions.
type MavenJAR struct {
// Name is the name of the JAR, without the version or extension.
Name string `toml:"name"`
// Version is the version of the JAR, without the name or extension.
Version string `toml:"version"`
// SHA256 is the SHA256 hash of the JAR.
SHA256 string `toml:"sha256"`
}
type result struct {
err error
value MavenJAR
}
// NewMavenJARListing generates a listing of all JAR that follow Maven naming convention under the roots.
func NewMavenJARListing(roots ...string) ([]MavenJAR, error) {
paths := make(chan string)
results := make(chan result)
go func() {
for _, root := range roots {
p, err := filepath.EvalSymlinks(root)
if os.IsNotExist(err) {
continue
} else if err != nil {
results <- result{err: fmt.Errorf("unable to resolve %s\n%w", root, err)}
return
}
if err := filepath.Walk(p, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if filepath.Ext(path) != ".jar" {
return nil
}
paths <- path
return nil
}); err != nil {
results <- result{err: fmt.Errorf("error walking path %s\n%w", root, err)}
return
}
}
close(paths)
}()
go func() {
var workers sync.WaitGroup
for i := 0; i < 128; i++ {
workers.Add(1)
go worker(paths, results, &workers)
}
workers.Wait()
close(results)
}()
var m []MavenJAR
for r := range results {
if r.err != nil {
return nil, fmt.Errorf("unable to create file listing: %s", r.err)
}
m = append(m, r.value)
}
sort.Slice(m, func(i, j int) bool {
if m[i].Name != m[j].Name {
return m[i].Name < m[j].Name
}
if m[i].Version != m[j].Version {
return m[i].Version < m[j].Version
}
return m[i].SHA256 < m[j].SHA256
})
return m, nil
}
func worker(paths chan string, results chan result, wg *sync.WaitGroup) {
for path := range paths {
m, err := process(path)
results <- result{value: m, err: err}
}
wg.Done()
}
func process(path string) (MavenJAR, error) {
m := MavenJAR{
Name: filepath.Base(path),
Version: "unknown",
}
if p := maven.FindStringSubmatch(path); p != nil {
m.Name = p[1]
m.Version = p[2]
}
s := sha256.New()
in, err := os.Open(path)
if err != nil {
return MavenJAR{}, fmt.Errorf("unable to open file %s\n%w", path, err)
}
defer in.Close()
if _, err := io.Copy(s, in); err != nil {
return MavenJAR{}, fmt.Errorf("unable to hash file %s\n%w", path, err)
}
m.SHA256 = hex.EncodeToString(s.Sum(nil))
return m, nil
}