-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathpackage.go
397 lines (335 loc) · 10.4 KB
/
package.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
package rpm
import (
"bufio"
"fmt"
"io"
"os"
"sort"
"strings"
"syscall"
"time"
)
// A Package is an rpm package file.
type Package struct {
Lead Lead
Signature Header
Header Header
}
var _ Version = &Package{}
// Read reads an rpm package from r.
//
// When this function returns, the reader will be positioned at the start of the
// package payload. Use Package.PayloadFormat and Package.PayloadCompression to
// determine how to decompress and unarchive the payload.
func Read(r io.Reader) (*Package, error) {
lead, err := readLead(r)
if err != nil {
return nil, err
}
sig, err := readHeader(r, true)
if err != nil {
return nil, err
}
hdr, err := readHeader(r, false)
if err != nil {
return nil, err
}
return &Package{
Lead: *lead,
Signature: *sig,
Header: *hdr,
}, nil
}
// Open opens an rpm package from the file system.
//
// Once the package headers are read, the underlying reader is closed and cannot
// be used to read the package payload. To read the package payload, open the
// package with os.Open and read the headers with Read. You may then use the
// same reader to read the payload.
func Open(name string) (*Package, error) {
f, err := os.Open(name)
if err != nil {
return nil, err
}
defer f.Close()
return Read(bufio.NewReader(f))
}
// dependencies translates the given tag values into a slice of package
// relationships such as provides, conflicts, obsoletes and requires.
func (c *Package) dependencies(nevrsTagID, flagsTagID, namesTagID, versionsTagID int) []Dependency {
// TODO: Implement NEVRS tags
// TODO: error handling
flgs := c.Header.GetTag(flagsTagID).Int64Slice()
names := c.Header.GetTag(namesTagID).StringSlice()
vers := c.Header.GetTag(versionsTagID).StringSlice()
deps := make([]Dependency, len(names))
for i := 0; i < len(names); i++ {
epoch, ver, rel := parseVersion(vers[i])
deps[i] = &dependency{
flags: int(flgs[i]),
name: names[i],
epoch: epoch,
version: ver,
release: rel,
}
}
return deps
}
// String returns the package identifier in the form
// '[name]-[version]-[release].[architecture]'.
func (c *Package) String() string {
return fmt.Sprintf("%s-%s-%s.%s", c.Name(), c.Version(), c.Release(), c.Architecture())
}
func (c *Package) GPGSignature() GPGSignature {
return c.Signature.GetTag(1002).Bytes()
}
// For tag definitions, see:
// https://github.com/rpm-software-management/rpm/blob/master/include/rpm/rpmtag.h#L34
func (c *Package) Name() string {
return c.Header.GetTag(1000).String()
}
func (c *Package) Version() string {
return c.Header.GetTag(1001).String()
}
func (c *Package) Release() string {
return c.Header.GetTag(1002).String()
}
func (c *Package) Epoch() int {
return int(c.Header.GetTag(1003).Int64())
}
// HeaderRange returns the byte offsets of the RPM header.
func (c *Package) HeaderRange() (start, end int) {
start = 96 + c.Signature.Size
end = start + c.Header.Size
return start, end
}
func (c *Package) Requires() []Dependency {
return c.dependencies(5041, 1048, 1049, 1050)
}
func (c *Package) Provides() []Dependency {
return c.dependencies(5042, 1112, 1047, 1113)
}
func (c *Package) Conflicts() []Dependency {
return c.dependencies(5044, 1053, 1054, 1055)
}
func (c *Package) Obsoletes() []Dependency {
return c.dependencies(5043, 1114, 1090, 1115)
}
func (c *Package) Suggests() []Dependency {
return c.dependencies(5059, 5051, 5049, 5050)
}
func (c *Package) Enhances() []Dependency {
return c.dependencies(5061, 5057, 5055, 5056)
}
func (c *Package) Recommends() []Dependency {
return c.dependencies(5058, 5048, 5046, 5047)
}
func (c *Package) Supplements() []Dependency {
return c.dependencies(5060, 5051, 5052, 5053)
}
// Files returns file information for each file that is installed by this RPM
// package.
func (c *Package) Files() []FileInfo {
ixs := c.Header.GetTag(1116).Int64Slice()
names := c.Header.GetTag(1117).StringSlice()
dirs := c.Header.GetTag(1118).StringSlice()
modes := c.Header.GetTag(1030).Int64Slice()
sizes := c.Header.GetTag(1028).Int64Slice()
times := c.Header.GetTag(1034).Int64Slice()
flags := c.Header.GetTag(1037).Int64Slice()
owners := c.Header.GetTag(1039).StringSlice()
groups := c.Header.GetTag(1040).StringSlice()
digests := c.Header.GetTag(1035).StringSlice()
linknames := c.Header.GetTag(1036).StringSlice()
a := make([]FileInfo, len(names))
for i := 0; i < len(names); i++ {
a[i] = FileInfo{
name: dirs[ixs[i]] + names[i],
mode: fileModeFromInt64(modes[i]),
size: sizes[i],
modTime: time.Unix(times[i], 0),
flags: flags[i],
owner: owners[i],
group: groups[i],
digest: digests[i],
linkname: linknames[i],
}
}
return a
}
// fileModeFromInt64 converts the 16 bit value returned from a typical
// unix/linux stat call to the bitmask that go uses to produce an os
// neutral representation. It is incorrect to just cast the 16 bit
// value directly to a os.FileMode. The result of stat is 4 bits to
// specify the type of the object, this is a value in the range 0 to
// 15, rather than a bitfield, 3 bits to note suid, sgid and sticky,
// and 3 sets of 3 bits for rwx permissions for user, group and other.
// An os.FileMode has the same 9 bits for permissions, but rather than
// using an enum for the type it has individual bits. As a concrete
// example, a block device has the 1<<26 bit set (os.ModeDevice) in
// the os.FileMode, but has type 0x6000 (syscall.S_IFBLK). A regular
// file is represented in os.FileMode by not having any of the bits in
// os.ModeType set (i.e. is not a directory, is not a symlink, is not
// a named pipe...) whilst a regular file has value syscall.S_IFREG
// (0x8000) in the mode field from stat.
func fileModeFromInt64(mode int64) os.FileMode {
fm := os.FileMode(mode & 0777)
switch mode & syscall.S_IFMT {
case syscall.S_IFBLK:
fm |= os.ModeDevice
case syscall.S_IFCHR:
fm |= os.ModeDevice | os.ModeCharDevice
case syscall.S_IFDIR:
fm |= os.ModeDir
case syscall.S_IFIFO:
fm |= os.ModeNamedPipe
case syscall.S_IFLNK:
fm |= os.ModeSymlink
case syscall.S_IFREG:
// nothing to do
case syscall.S_IFSOCK:
fm |= os.ModeSocket
}
if mode&syscall.S_ISGID != 0 {
fm |= os.ModeSetgid
}
if mode&syscall.S_ISUID != 0 {
fm |= os.ModeSetuid
}
if mode&syscall.S_ISVTX != 0 {
fm |= os.ModeSticky
}
return fm
}
func (c *Package) Summary() string {
return strings.Join(c.Header.GetTag(1004).StringSlice(), "\n")
}
func (c *Package) Description() string {
return strings.Join(c.Header.GetTag(1005).StringSlice(), "\n")
}
func (c *Package) BuildTime() time.Time {
return time.Unix(c.Header.GetTag(1006).Int64(), 0)
}
func (c *Package) BuildHost() string {
return c.Header.GetTag(1007).String()
}
func (c *Package) InstallTime() time.Time {
return time.Unix(c.Header.GetTag(1008).Int64(), 0)
}
// Size specifies the disk space consumed by installation of the package.
func (c *Package) Size() uint64 {
if i := uint64(c.Header.GetTag(5009).Int64()); i > 0 {
return i
}
return uint64(c.Header.GetTag(1009).Int64())
}
// ArchiveSize specifies the size of the archived payload of the package in
// bytes.
func (c *Package) ArchiveSize() uint64 {
if i := uint64(c.Signature.GetTag(271).Int64()); i > 0 {
return i
}
if i := uint64(c.Signature.GetTag(1007).Int64()); i > 0 {
return i
}
if i := uint64(c.Header.GetTag(271).Int64()); i > 0 {
return i
}
return uint64(c.Header.GetTag(1046).Int64())
}
func (c *Package) Distribution() string {
return c.Header.GetTag(1010).String()
}
func (c *Package) Vendor() string {
return c.Header.GetTag(1011).String()
}
func (c *Package) GIFImage() []byte {
return c.Header.GetTag(1012).Bytes()
}
func (c *Package) XPMImage() []byte {
return c.Header.GetTag(1013).Bytes()
}
func (c *Package) License() string {
return c.Header.GetTag(1014).String()
}
func (c *Package) Packager() string {
return c.Header.GetTag(1015).String()
}
func (c *Package) Groups() []string {
return c.Header.GetTag(1016).StringSlice()
}
func (c *Package) ChangeLog() []string {
return c.Header.GetTag(1017).StringSlice()
}
func (c *Package) Source() []string {
return c.Header.GetTag(1018).StringSlice()
}
func (c *Package) Patch() []string {
return c.Header.GetTag(1019).StringSlice()
}
func (c *Package) URL() string {
return c.Header.GetTag(1020).String()
}
func (c *Package) OperatingSystem() string {
return c.Header.GetTag(1021).String()
}
func (c *Package) Architecture() string {
return c.Header.GetTag(1022).String()
}
func (c *Package) PreInstallScript() string {
return c.Header.GetTag(1023).String()
}
func (c *Package) PostInstallScript() string {
return c.Header.GetTag(1024).String()
}
func (c *Package) PreUninstallScript() string {
return c.Header.GetTag(1025).String()
}
func (c *Package) PostUninstallScript() string {
return c.Header.GetTag(1026).String()
}
func (c *Package) OldFilenames() []string {
return c.Header.GetTag(1027).StringSlice()
}
func (c *Package) Icon() []byte {
return c.Header.GetTag(1043).Bytes()
}
func (c *Package) SourceRPM() string {
return c.Header.GetTag(1044).String()
}
func (c *Package) RPMVersion() string {
return c.Header.GetTag(1064).String()
}
func (c *Package) Platform() string {
return c.Header.GetTag(1132).String()
}
// PayloadFormat returns the name of the format used for the package payload.
// Typically cpio.
func (c *Package) PayloadFormat() string {
return c.Header.GetTag(1124).String()
}
// PayloadCompression returns the name of the compression used for the package
// payload. Typically xz.
func (c *Package) PayloadCompression() string {
return c.Header.GetTag(1125).String()
}
// Sort sorts a slice of packages lexically by name ascending and then by
// version descending. Version is evaluated first by epoch, then by version
// string, then by release.
func Sort(x []*Package) { sort.Sort(PackageSlice(x)) }
// PackageSlice implements sort.Interface for a slice of packages. Packages are
// sorted lexically by name ascending and then by version descending. Version is
// evaluated first by epoch, then by version string, then by release.
type PackageSlice []*Package
// Sort is a convenience method: x.Sort() calls sort.Sort(x).
func (x PackageSlice) Sort() { sort.Sort(x) }
func (x PackageSlice) Len() int { return len(x) }
func (x PackageSlice) Less(i, j int) bool {
a, b := x[i].Name(), x[j].Name()
if a == b {
return Compare(x[i], x[j]) == 1
}
return a < b
}
func (x PackageSlice) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
var _ sort.Interface = PackageSlice{}