forked from remyoudompheng/go-alpm
-
Notifications
You must be signed in to change notification settings - Fork 14
/
types.go
308 lines (249 loc) · 6.08 KB
/
types.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
// types.go - libalpm types.
//
// Copyright (c) 2013 The go-alpm Authors
//
// MIT Licensed. See LICENSE for details.
package alpm
// #cgo CFLAGS: -D_FILE_OFFSET_BITS=64
// #include <alpm.h>
import "C"
import (
"errors"
"fmt"
"unsafe"
)
// Depend provides a description of a dependency.
type Depend struct {
Name string
Version string
Description string
NameHash uint
Mod DepMod
}
func convertDepend(dep *C.alpm_depend_t) *Depend {
return &Depend{
Name: C.GoString(dep.name),
Version: C.GoString(dep.version),
Mod: DepMod(dep.mod),
Description: C.GoString(dep.desc),
NameHash: uint(dep.name_hash),
}
}
func convertCDepend(dep Depend) *C.alpm_depend_t {
cName := C.CString(dep.Name)
cVersion := C.CString(dep.Version)
cDesc := C.CString(dep.Description)
cDep := C.alpm_depend_t{
name: cName,
version: cVersion,
desc: cDesc,
name_hash: C.ulong(dep.NameHash),
mod: C.alpm_depmod_t(dep.Mod),
}
return &cDep
}
func freeCDepend(dep *C.alpm_depend_t) {
C.free(unsafe.Pointer(dep.name))
C.free(unsafe.Pointer(dep.version))
C.free(unsafe.Pointer(dep.desc))
}
func (dep Depend) String() string {
return dep.Name + dep.Mod.String() + dep.Version
}
// File provides a description of package files.
type File struct {
Name string
Size int64
Mode uint32
}
func convertFile(file *C.alpm_file_t) (File, error) {
if file == nil {
return File{}, errors.New("no file")
}
return File{
Name: C.GoString(file.name),
Size: int64(file.size),
Mode: uint32(file.mode),
}, nil
}
func convertFilelist(files *C.alpm_filelist_t) []File {
size := int(files.count)
items := make([]File, size)
cFiles := unsafe.Slice(files.files, size)
for i := 0; i < size; i++ {
if file, err := convertFile(&cFiles[i]); err == nil {
items[i] = file
}
}
return items
}
// Internal alpm list structure.
type list struct {
Data unsafe.Pointer
Prev *list
Next *list
}
// Iterates a function on a list and stop on error.
func (l *list) forEach(f func(unsafe.Pointer) error) error {
for ; l != nil; l = l.Next {
err := f(l.Data)
if err != nil {
return err
}
}
return nil
}
func (l *list) Len() int {
count := 0
for ; l != nil; l = l.Next {
count++
}
return count
}
func (l *list) Empty() bool {
return l == nil
}
type StringList struct {
*list
}
func (l StringList) ForEach(f func(string) error) error {
return l.forEach(func(p unsafe.Pointer) error {
return f(C.GoString((*C.char)(p)))
})
}
func (l StringList) Slice() []string {
slice := []string{}
_ = l.ForEach(func(s string) error {
slice = append(slice, s)
return nil
})
return slice
}
type BackupFile struct {
Name string
Hash string
}
type BackupList struct {
*list
}
func (l BackupList) ForEach(f func(BackupFile) error) error {
return l.forEach(func(p unsafe.Pointer) error {
bf := (*C.alpm_backup_t)(p)
return f(BackupFile{
Name: C.GoString(bf.name),
Hash: C.GoString(bf.hash),
})
})
}
func (l BackupList) Slice() (slice []BackupFile) {
_ = l.ForEach(func(f BackupFile) error {
slice = append(slice, f)
return nil
})
return
}
type QuestionAny struct {
ptr *C.alpm_question_any_t
}
func (question QuestionAny) SetAnswer(answer bool) {
if answer {
question.ptr.answer = 1
} else {
question.ptr.answer = 0
}
}
type QuestionInstallIgnorepkg struct {
ptr *C.alpm_question_install_ignorepkg_t
}
func (question QuestionAny) Type() QuestionType {
return QuestionType(question.ptr._type)
}
func (question QuestionAny) Answer() bool {
return question.ptr.answer == 1
}
func (question QuestionAny) QuestionInstallIgnorepkg() (QuestionInstallIgnorepkg, error) {
if question.Type() == QuestionTypeInstallIgnorepkg {
return *(*QuestionInstallIgnorepkg)(unsafe.Pointer(&question)), nil
}
return QuestionInstallIgnorepkg{}, fmt.Errorf("cannot convert to QuestionInstallIgnorepkg")
}
func (question QuestionAny) QuestionSelectProvider() (QuestionSelectProvider, error) {
if question.Type() == QuestionTypeSelectProvider {
return *(*QuestionSelectProvider)(unsafe.Pointer(&question)), nil
}
return QuestionSelectProvider{}, fmt.Errorf("cannot convert to QuestionInstallIgnorepkg")
}
func (question QuestionAny) QuestionReplace() (QuestionReplace, error) {
if question.Type() == QuestionTypeReplacePkg {
return *(*QuestionReplace)(unsafe.Pointer(&question)), nil
}
return QuestionReplace{}, fmt.Errorf("cannot convert to QuestionReplace")
}
func (question QuestionInstallIgnorepkg) SetInstall(install bool) {
if install {
question.ptr.install = 1
} else {
question.ptr.install = 0
}
}
func (question QuestionInstallIgnorepkg) Type() QuestionType {
return QuestionType(question.ptr._type)
}
func (question QuestionInstallIgnorepkg) Install() bool {
return question.ptr.install == 1
}
func (question QuestionInstallIgnorepkg) Pkg(h *Handle) IPackage {
return &Package{
question.ptr.pkg,
*h,
}
}
type QuestionReplace struct {
ptr *C.alpm_question_replace_t
}
func (question QuestionReplace) Type() QuestionType {
return QuestionType(question.ptr._type)
}
func (question QuestionReplace) SetReplace(replace bool) {
if replace {
question.ptr.replace = 1
} else {
question.ptr.replace = 0
}
}
func (question QuestionReplace) Replace() bool {
return question.ptr.replace == 1
}
func (question QuestionReplace) NewPkg(h *Handle) IPackage {
return &Package{
question.ptr.newpkg,
*h,
}
}
func (question QuestionReplace) OldPkg(h *Handle) IPackage {
return &Package{
question.ptr.oldpkg,
*h,
}
}
type QuestionSelectProvider struct {
ptr *C.alpm_question_select_provider_t
}
func (question QuestionSelectProvider) Type() QuestionType {
return QuestionType(question.ptr._type)
}
func (question QuestionSelectProvider) SetUseIndex(index int) {
question.ptr.use_index = C.int(index)
}
func (question QuestionSelectProvider) UseIndex() int {
return int(question.ptr.use_index)
}
func (question QuestionSelectProvider) Providers(h *Handle) IPackageList {
return PackageList{
(*list)(unsafe.Pointer(question.ptr.providers)),
*h,
}
}
func (question QuestionSelectProvider) Dep() *Depend {
return convertDepend(question.ptr.depend)
}