-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
89 lines (75 loc) · 1.81 KB
/
file.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
/*
Copyright © 2022-2023 Macaroni OS Linux
See AUTHORS and LICENSE for the license details and contributors.
*/
package mask
import (
"errors"
gentoo "github.com/geaaru/pkgs-checker/pkg/gentoo"
"gopkg.in/yaml.v2"
)
func NewPackageMaskFile(file string) *PackageMaskFile {
return &PackageMaskFile{
File: file,
Enabled: true,
Rules: []string{},
pkgsMap: make(map[string][]*gentoo.GentooPackage, 0),
}
}
func NewPackageMaskFileFromData(file string, data []byte) (*PackageMaskFile, error) {
ans := NewPackageMaskFile(file)
err := yaml.Unmarshal(data, ans)
if err != nil {
return nil, err
}
return ans, nil
}
func (f *PackageMaskFile) BuildMap() error {
for _, rule := range f.Rules {
p, err := gentoo.ParsePackageStr(rule)
if err != nil {
return err
}
pkgstr := f.getPkgStr(p)
if val, ok := f.pkgsMap[pkgstr]; ok {
f.pkgsMap[pkgstr] = append(val, p)
} else {
f.pkgsMap[pkgstr] = []*gentoo.GentooPackage{p}
}
}
return nil
}
func (f *PackageMaskFile) getPkgStr(p *gentoo.GentooPackage) string {
ans := p.Category
if p.Slot != "" && p.Slot != "0" {
ans += "-" + p.Slot
}
ans += "/" + p.Name
return ans
}
func (f *PackageMaskFile) Mask(repo string, p *gentoo.GentooPackage) (bool, error) {
if p == nil {
return false, errors.New("Invalid package")
}
// Check if the package p is available in the map
pkgstr := f.getPkgStr(p)
if pkgs, ok := f.pkgsMap[pkgstr]; ok {
for _, pm := range pkgs {
if pm.Repository != "" && pm.Repository != repo {
// Ignoring mask related to a different repository.
continue
}
admit, err := pm.Admit(p)
if err != nil {
return false, err
}
if admit {
// POST: The package in input match with the existing
// rule selector.
return true, nil
}
}
}
// POST: The package is not masked.
return false, nil
}