forked from remyoudompheng/go-alpm
-
Notifications
You must be signed in to change notification settings - Fork 14
/
package.go
319 lines (258 loc) · 8.08 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
// package.go - libalpm package type and methods.
//
// Copyright (c) 2013 The go-alpm Authors
//
// MIT Licensed. See LICENSE for details.
package alpm
/*
#include <alpm.h>
int pkg_cmp(const void *v1, const void *v2)
{
alpm_pkg_t *p1 = (alpm_pkg_t *)v1;
alpm_pkg_t *p2 = (alpm_pkg_t *)v2;
off_t s1 = alpm_pkg_get_isize(p1);
off_t s2 = alpm_pkg_get_isize(p2);
if (s1 > s2)
return -1;
else if (s1 < s2)
return 1;
else
return 0;
}
*/
import "C"
import (
"time"
"unsafe"
)
// Package describes a single package and associated handle.
type Package struct {
pmpkg *C.alpm_pkg_t
handle Handle
}
// PackageList describes a linked list of packages and associated handle.
type PackageList struct {
*list
handle Handle
}
// ForEach executes an action on each package of the PackageList.
func (l PackageList) ForEach(f func(IPackage) error) error {
return l.forEach(func(p unsafe.Pointer) error {
return f(&Package{(*C.alpm_pkg_t)(p), l.handle})
})
}
// Slice converts the PackageList to a Package Slice.
func (l PackageList) Slice() []IPackage {
slice := []IPackage{}
_ = l.ForEach(func(p IPackage) error {
slice = append(slice, p)
return nil
})
return slice
}
// DependList describes a linkedlist of dependency type packages.
type DependList struct{ *list }
// ForEach executes an action on each package of the DependList.
func (l DependList) ForEach(f func(*Depend) error) error {
return l.forEach(func(p unsafe.Pointer) error {
dep := convertDepend((*C.alpm_depend_t)(p))
return f(dep)
})
}
// Slice converts the DependList to a Depend Slice.
func (l DependList) Slice() []Depend {
slice := []Depend{}
_ = l.ForEach(func(dep *Depend) error {
slice = append(slice, *dep)
return nil
})
return slice
}
func (pkg *Package) FileName() string {
return C.GoString(C.alpm_pkg_get_filename(pkg.pmpkg))
}
func (pkg *Package) Base() string {
return C.GoString(C.alpm_pkg_get_base(pkg.pmpkg))
}
func (pkg *Package) Base64Signature() string {
return C.GoString(C.alpm_pkg_get_base64_sig(pkg.pmpkg))
}
func (pkg *Package) Validation() Validation {
return Validation(C.alpm_pkg_get_validation(pkg.pmpkg))
}
// Architecture returns the package target Architecture.
func (pkg *Package) Architecture() string {
return C.GoString(C.alpm_pkg_get_arch(pkg.pmpkg))
}
// Backup returns a list of package backups.
func (pkg *Package) Backup() BackupList {
ptr := unsafe.Pointer(C.alpm_pkg_get_backup(pkg.pmpkg))
return BackupList{(*list)(ptr)}
}
// BuildDate returns the BuildDate of the package.
func (pkg *Package) BuildDate() time.Time {
t := C.alpm_pkg_get_builddate(pkg.pmpkg)
return time.Unix(int64(t), 0)
}
// Conflicts returns the conflicts of the package as a DependList.
func (pkg *Package) Conflicts() IDependList {
ptr := unsafe.Pointer(C.alpm_pkg_get_conflicts(pkg.pmpkg))
return DependList{(*list)(ptr)}
}
// DB returns the package's origin database.
func (pkg *Package) DB() IDB {
ptr := C.alpm_pkg_get_db(pkg.pmpkg)
if ptr == nil {
return nil
}
return &DB{ptr, pkg.handle}
}
// Depends returns the package's dependency list.
func (pkg *Package) Depends() IDependList {
ptr := unsafe.Pointer(C.alpm_pkg_get_depends(pkg.pmpkg))
return DependList{(*list)(ptr)}
}
// Depends returns the package's optional dependency list.
func (pkg *Package) OptionalDepends() IDependList {
ptr := unsafe.Pointer(C.alpm_pkg_get_optdepends(pkg.pmpkg))
return DependList{(*list)(ptr)}
}
// Depends returns the package's check dependency list.
func (pkg *Package) CheckDepends() IDependList {
ptr := unsafe.Pointer(C.alpm_pkg_get_checkdepends(pkg.pmpkg))
return DependList{(*list)(ptr)}
}
// Depends returns the package's make dependency list.
func (pkg *Package) MakeDepends() IDependList {
ptr := unsafe.Pointer(C.alpm_pkg_get_makedepends(pkg.pmpkg))
return DependList{(*list)(ptr)}
}
// Description returns the package's description.
func (pkg *Package) Description() string {
return C.GoString(C.alpm_pkg_get_desc(pkg.pmpkg))
}
// Files returns the file list of the package.
func (pkg *Package) Files() []File {
cFiles := C.alpm_pkg_get_files(pkg.pmpkg)
return convertFilelist(cFiles)
}
// ContainsFile checks if the path is in the package filelist
func (pkg *Package) ContainsFile(path string) (File, error) {
return convertFile(C.alpm_filelist_contains(C.alpm_pkg_get_files(pkg.pmpkg), C.CString(path)))
}
// Groups returns the groups the package belongs to.
func (pkg *Package) Groups() StringList {
ptr := unsafe.Pointer(C.alpm_pkg_get_groups(pkg.pmpkg))
return StringList{(*list)(ptr)}
}
// ISize returns the package installed size.
func (pkg *Package) ISize() int64 {
t := C.alpm_pkg_get_isize(pkg.pmpkg)
return int64(t)
}
// InstallDate returns the package install date.
func (pkg *Package) InstallDate() time.Time {
t := C.alpm_pkg_get_installdate(pkg.pmpkg)
return time.Unix(int64(t), 0)
}
// Licenses returns the package license list.
func (pkg *Package) Licenses() StringList {
ptr := unsafe.Pointer(C.alpm_pkg_get_licenses(pkg.pmpkg))
return StringList{(*list)(ptr)}
}
// SHA256Sum returns package SHA256Sum.
func (pkg *Package) SHA256Sum() string {
return C.GoString(C.alpm_pkg_get_sha256sum(pkg.pmpkg))
}
// MD5Sum returns package MD5Sum.
func (pkg *Package) MD5Sum() string {
return C.GoString(C.alpm_pkg_get_md5sum(pkg.pmpkg))
}
// Name returns package name.
func (pkg *Package) Name() string {
return C.GoString(C.alpm_pkg_get_name(pkg.pmpkg))
}
// Packager returns package packager name.
func (pkg *Package) Packager() string {
return C.GoString(C.alpm_pkg_get_packager(pkg.pmpkg))
}
// Provides returns DependList of packages provides by package.
func (pkg *Package) Provides() IDependList {
ptr := unsafe.Pointer(C.alpm_pkg_get_provides(pkg.pmpkg))
return DependList{(*list)(ptr)}
}
// Reason returns package install reason.
func (pkg *Package) Reason() PkgReason {
reason := C.alpm_pkg_get_reason(pkg.pmpkg)
return PkgReason(reason)
}
// Origin returns package origin.
func (pkg *Package) Origin() PkgFrom {
origin := C.alpm_pkg_get_origin(pkg.pmpkg)
return PkgFrom(origin)
}
// Replaces returns a DependList with the packages this package replaces.
func (pkg *Package) Replaces() IDependList {
ptr := unsafe.Pointer(C.alpm_pkg_get_replaces(pkg.pmpkg))
return DependList{(*list)(ptr)}
}
// Size returns the packed package size.
func (pkg *Package) Size() int64 {
t := C.alpm_pkg_get_size(pkg.pmpkg)
return int64(t)
}
// URL returns the upstream URL of the package.
func (pkg *Package) URL() string {
return C.GoString(C.alpm_pkg_get_url(pkg.pmpkg))
}
// Version returns the package version.
func (pkg *Package) Version() string {
return C.GoString(C.alpm_pkg_get_version(pkg.pmpkg))
}
// ComputeRequiredBy returns the names of reverse dependencies of a package
func (pkg *Package) ComputeRequiredBy() []string {
result := C.alpm_pkg_compute_requiredby(pkg.pmpkg)
requiredby := make([]string, 0)
for i := (*list)(unsafe.Pointer(result)); i != nil; i = i.Next {
if i.Data != nil {
name := C.GoString((*C.char)(i.Data))
requiredby = append(requiredby, name)
C.free(i.Data)
}
C.free(unsafe.Pointer(i))
}
return requiredby
}
// ComputeOptionalFor returns the names of packages that optionally require the given package
func (pkg *Package) ComputeOptionalFor() []string {
result := C.alpm_pkg_compute_optionalfor(pkg.pmpkg)
optionalfor := make([]string, 0)
for i := (*list)(unsafe.Pointer(result)); i != nil; i = i.Next {
if i.Data != nil {
name := C.GoString((*C.char)(i.Data))
optionalfor = append(optionalfor, name)
C.free(i.Data)
}
C.free(unsafe.Pointer(i))
}
return optionalfor
}
func (pkg *Package) ShouldIgnore() bool {
result := C.alpm_pkg_should_ignore(pkg.handle.ptr, pkg.pmpkg)
return result == 1
}
func (pkg *Package) Type() string {
return "alpm"
}
// SortBySize returns a PackageList sorted by size.
func (l PackageList) SortBySize() IPackageList {
pkgList := (*C.struct__alpm_list_t)(unsafe.Pointer(l.list))
pkgCache := (*list)(unsafe.Pointer(
C.alpm_list_msort(pkgList, //nolint
C.alpm_list_count(pkgList),
C.alpm_list_fn_cmp(C.pkg_cmp))))
if pkgCache == nil {
return nil
}
return PackageList{pkgCache, l.handle}
}