-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkernelrelease.go
84 lines (67 loc) · 1.89 KB
/
kernelrelease.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
package kernelrelease
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"strconv"
p "github.com/maxgio92/krawler/pkg/packages"
)
type Arch string
type Archs map[Arch]string
type KernelRelease struct {
Fullversion string `json:"full_version"`
Version int `json:"version"`
PatchLevel int `json:"patch_level"`
Sublevel int `json:"sublevel"`
Extraversion string `json:"extra_version"`
FullExtraversion string `json:"full_extra_version"`
Architecture Arch `json:"architecture"`
PackageName string `json:"package_name"`
PackageURL string `json:"package_url"`
CompilerVersion string `json:"compiler_version"`
}
//nolint:cyclop
func (k *KernelRelease) BuildFromPackage(pkg p.Package) error {
k.PackageName = pkg.GetName()
k.PackageURL = pkg.URL()
k.Architecture = Arch(pkg.GetArch())
kernelVersion := versionStringFromPackage(pkg)
match := kernelVersionPattern.FindStringSubmatch(kernelVersion)
identifiers := make(map[string]string)
for i, name := range kernelVersionPattern.SubexpNames() {
if i > 0 && i <= len(match) {
identifiers[name] = match[i]
switch name {
case "fullversion":
k.Fullversion = match[i]
case "version":
k.Version, _ = strconv.Atoi(match[i])
case "patchlevel":
k.PatchLevel, _ = strconv.Atoi(match[i])
case "sublevel":
k.Sublevel, _ = strconv.Atoi(match[i])
case "extraversion":
k.Extraversion = match[i]
case "fullextraversion":
k.FullExtraversion = match[i]
}
}
}
compilerVersion, err := GetCompilerVersionFromKernelPackage(pkg)
if err != nil {
k.CompilerVersion = ""
}
k.CompilerVersion = compilerVersion
return nil
}
func (k *KernelRelease) SHA256Sum() string {
sha256.New()
s := fmt.Sprintf("%s%s%s%s",
k.Fullversion,
k.FullExtraversion,
k.PackageName,
string(k.Architecture),
)
hash := sha256.Sum256([]byte(s))
return hex.EncodeToString(hash[:])
}