forked from google/rpmpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsense.go
193 lines (163 loc) · 3.95 KB
/
sense.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package rpmpack
import (
"fmt"
"regexp"
"strings"
)
type rpmSense uint32
// https://github.com/rpm-software-management/rpm/blob/ab01b5eacf9ec6a07a5d9e1991ef476a12d264fd/include/rpm/rpmds.h#L27
// SenseAny (0) specifies no specific version compare
// SenseLess (2) specifies less then the specified version
// SenseGreater (4) specifies greater then the specified version
// SenseEqual (8) specifies equal to the specified version
const (
SenseAny rpmSense = 0
SenseLess = 1 << iota
SenseGreater
SenseEqual
unused1
PostTrans
PreReq
PreTrans
Interp
ScriptPre
ScriptPost
ScriptPreun
ScriptPostun
ScriptVerify
FindRequires
FindProvides
TriggerIn
TriggerUn
TriggerPostun
MissingOk
unused2
unused3
unused4
SenseRPMLIB rpmSense = 1 << 24
)
var relationMatch = regexp.MustCompile(`([^=<>\s]*)\s*((?:=|>|<)*)\s*(.*)?`)
// Relation is the structure of rpm sense relationships
type Relation struct {
Name string
Version string
Sense rpmSense
}
func SenseFromFlag(flag int32) (rpmSense, error) {
return rpmSense(flag), nil
}
// String return the string representation of the Relation
func (r *Relation) String() string {
return fmt.Sprintf("%s%v%s", r.Name, r.Sense, r.Version)
}
// Equal compare the equality of two relations
func (r *Relation) Equal(o *Relation) bool {
return r.Name == o.Name && r.Version == o.Version && r.Sense == o.Sense
}
// Relations is a slice of Relation pointers
type Relations []*Relation
// String return the string representation of the Relations
func (r *Relations) String() string {
var val []string
for _, rel := range *r {
val = append(val, rel.String())
}
return strings.Join(val, ",")
}
// Set parse a string into a Relation and append it to the Relations slice if it is missing
// this is used by the flag package
func (r *Relations) Set(value string) error {
relation, err := NewRelation(value)
if err != nil {
return err
}
r.addIfMissing(relation)
return nil
}
func (r *Relations) addIfMissing(value *Relation) {
for _, relation := range *r {
if relation.Equal(value) {
return
}
}
*r = append(*r, value)
}
// AddToIndex add the relations to the specified category on the index
func (r *Relations) AddToIndex(h *index, nameTag, versionTag, flagsTag int) error {
var (
num = len(*r)
names = make([]string, num)
versions = make([]string, num)
flags = make([]uint32, num)
)
if num == 0 {
return nil
}
for idx, relation := range *r {
names[idx] = relation.Name
versions[idx] = relation.Version
flags[idx] = uint32(relation.Sense)
}
h.Add(nameTag, EntryStringSlice(names))
h.Add(versionTag, EntryStringSlice(versions))
h.Add(flagsTag, EntryUint32(flags))
return nil
}
// NewRelation parse a string into a Relation
func NewRelation(related string) (*Relation, error) {
var (
err error
sense rpmSense
name,
version string
)
if strings.HasPrefix(related, "(") && strings.HasSuffix(related, ")") {
// This is a `rich` dependency which must be parsed at install time
// https://rpm-software-management.github.io/rpm/manual/boolean_dependencies.html
sense = SenseAny
name = related
} else {
parts := relationMatch.FindStringSubmatch(related)
if sense, err = parseSense(parts[2]); err != nil {
return nil, err
}
name = parts[1]
version = parts[3]
}
return &Relation{
Name: name,
Version: version,
Sense: sense,
}, nil
}
var stringToSense = map[string]rpmSense{
"": SenseAny,
"<": SenseLess,
">": SenseGreater,
"=": SenseEqual,
"<=": SenseLess | SenseEqual,
">=": SenseGreater | SenseEqual,
}
// String return the string representation of the rpmSense
func (r rpmSense) String() string {
var (
val rpmSense
ret string
)
for ret, val = range stringToSense {
if r == val {
return ret
}
}
return "unknown"
}
func parseSense(sense string) (rpmSense, error) {
var (
ret rpmSense
ok bool
)
if ret, ok = stringToSense[sense]; !ok {
return SenseAny, fmt.Errorf("unknown sense value: %s", sense)
}
return ret, nil
}