/
index.go
178 lines (155 loc) · 3.8 KB
/
index.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
package rpm
import (
"time"
)
// Header index value data types.
const (
IndexDataTypeNull int = iota
IndexDataTypeChar
IndexDataTypeInt8
IndexDataTypeInt16
IndexDataTypeInt32
IndexDataTypeInt64
IndexDataTypeString
IndexDataTypeBinary
IndexDataTypeStringArray
IndexDataTypeI8NString
)
// An IndexEntry is a rpm key/value tag stored in the package header.
type IndexEntry struct {
Tag int
Type int
Offset int
ItemCount int
Value interface{}
}
// IndexEntries is an array of IndexEntry structs.
type IndexEntries []IndexEntry
// IndexByTag returns a pointer to an IndexEntry with the given tag ID or nil if
// the tag is not found.
func (c IndexEntries) IndexByTag(tag int) *IndexEntry {
for _, e := range c {
if e.Tag == tag {
return &e
}
}
return nil
}
// StringsByTag returns the slice of string values of an IndexEntry or nil if
// the tag is not found or has no value.
func (c IndexEntries) StringsByTag(tag int) []string {
i := c.IndexByTag(tag)
if i == nil || i.Value == nil {
return nil
}
s, ok := i.Value.([]string)
if !ok {
return nil
}
return s
}
// StringByTag returns the string value of an IndexEntry or an empty string if
// the tag is not found or has no value.
func (c IndexEntries) StringByTag(tag int) string {
s := c.StringsByTag(tag)
if s == nil || len(s) == 0 {
return ""
}
return s[0]
}
// IntsByTag returns the int64 values of an IndexEntry or nil if the tag is not
// found or has no value. Values with a lower range (E.g. int8) are cast as an
// int64.
func (c IndexEntries) IntsByTag(tag int) []int64 {
ix := c.IndexByTag(tag)
if ix != nil && ix.Value != nil {
vals := make([]int64, ix.ItemCount)
for i := 0; i < int(ix.ItemCount); i++ {
switch ix.Type {
case IndexDataTypeChar, IndexDataTypeInt8:
vals[i] = int64(ix.Value.([]int8)[i])
case IndexDataTypeInt16:
vals[i] = int64(ix.Value.([]int16)[i])
case IndexDataTypeInt32:
vals[i] = int64(ix.Value.([]int32)[i])
case IndexDataTypeInt64:
vals[i] = ix.Value.([]int64)[i]
}
}
return vals
}
return nil
}
// IntByTag returns the int64 value of an IndexEntry or 0 if the tag is not found
// or has no value. Values with a lower range (E.g. int8) are cast as an int64.
func (c IndexEntries) IntByTag(tag int) int64 {
i := c.IndexByTag(tag)
if i != nil && i.Value != nil {
switch i.Type {
case IndexDataTypeChar, IndexDataTypeInt8:
v, ok := i.Value.([]int8)
if !ok {
return 0
}
return int64(v[0])
case IndexDataTypeInt16:
v, ok := i.Value.([]int16)
if !ok {
return 0
}
return int64(v[0])
case IndexDataTypeInt32:
v, ok := i.Value.([]int32)
if !ok {
return 0
}
return int64(v[0])
case IndexDataTypeInt64:
v, ok := i.Value.([]int64)
if !ok {
return 0
}
return v[0]
}
}
return 0
}
// BytesByTag returns the raw value of an IndexEntry or nil if the tag is not
// found or has no value.
func (c IndexEntries) BytesByTag(tag int) []byte {
i := c.IndexByTag(tag)
if i == nil || i.Value == nil {
return nil
}
b, ok := i.Value.([]byte)
if !ok {
return nil
}
return b
}
// TimesByTag returns the value of an IndexEntry as a slice of Go native
// timestamps or nil if the tag is not found or has no value.
func (c IndexEntries) TimesByTag(tag int) []time.Time {
ix := c.IndexByTag(tag)
if ix == nil || ix.Value == nil {
return nil
}
v, ok := ix.Value.([]int32)
if !ok {
return nil
}
vals := make([]time.Time, ix.ItemCount)
for i := 0; i < ix.ItemCount; i++ {
vals[i] = time.Unix(int64(v[i]), 0)
}
return vals
}
// TimeByTag returns the value of an IndexEntry as a Go native timestamp or
// zero-time if the tag is not found or has no value.
func (c IndexEntries) TimeByTag(tag int) time.Time {
vals := c.TimesByTag(tag)
if vals == nil || len(vals) == 0 {
return time.Time{}
}
return vals[0]
}