forked from mrosset/go-alpm
-
Notifications
You must be signed in to change notification settings - Fork 4
/
conf.go
354 lines (335 loc) · 7.77 KB
/
conf.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
package alpm
import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"reflect"
"strings"
"syscall"
)
// Parsing routines for pacman.conf format.
type PacmanOption uint
const (
ConfUseSyslog PacmanOption = 1 << iota
ConfColor
ConfShowSize
ConfUseDelta
ConfTotalDownload
ConfCheckSpace
ConfVerbosePkgLists
ConfILoveCandy
)
var optionsMap = map[string]PacmanOption{
"UseSyslog": ConfUseSyslog,
"Color": ConfColor,
"ShowSize": ConfShowSize,
"UseDelta": ConfUseDelta,
"TotalDownload": ConfTotalDownload,
"CheckSpace": ConfCheckSpace,
"VerbosePkgLists": ConfVerbosePkgLists,
"ILoveCandy": ConfILoveCandy,
}
type PacmanConfig struct {
CacheDir []string
HoldPkg []string
SyncFirst []string
IgnoreGroup []string
IgnorePkg []string
NoExtract []string
NoUpgrade []string
RootDir string
DBPath string
GPGDir string
LogFile string
Architecture string
XferCommand string
CleanMethod string
SigLevel SigLevel
LocalFileSigLevel SigLevel
RemoteFileSigLevel SigLevel
Options PacmanOption
Repos []RepoConfig
}
type RepoConfig struct {
Name string
SigLevel SigLevel
Servers []string
}
const (
tokenSection = iota
tokenKey
tokenComment
)
type iniToken struct {
Type uint
Name string
Values []string
}
type confReader struct {
*bufio.Reader
Lineno uint
}
func newConfReader(r io.Reader) confReader {
if buf, ok := r.(*bufio.Reader); ok {
return confReader{buf, 0}
}
buf := bufio.NewReader(r)
return confReader{buf, 0}
}
func (rdr *confReader) ParseLine() (tok iniToken, err error) {
line, overflow, err := rdr.ReadLine()
switch {
case err != nil:
return
case overflow:
err = fmt.Errorf("line %d too long", rdr.Lineno)
return
}
rdr.Lineno++
line = bytes.TrimSpace(line)
if len(line) == 0 {
tok.Type = tokenComment
return
}
switch line[0] {
case '#':
tok.Type = tokenComment
return
case '[':
closing := bytes.IndexByte(line, ']')
if closing < 0 {
err = fmt.Errorf("missing ']' is section name at line %d", rdr.Lineno)
return
}
tok.Name = string(line[1:closing])
if closing+1 < len(line) {
err = fmt.Errorf("trailing characters %q after section name %s",
line[closing+1:], tok.Name)
return
}
return
default:
tok.Type = tokenKey
if idx := bytes.IndexByte(line, '='); idx >= 0 {
optname := bytes.TrimSpace(line[:idx])
values := bytes.Split(line[idx+1:], []byte{' '})
tok.Name = string(optname)
tok.Values = make([]string, 0, len(values))
for _, word := range values {
word = bytes.TrimSpace(word)
if len(word) > 0 {
tok.Values = append(tok.Values, string(word))
}
}
} else {
// boolean option
tok.Name = string(line)
tok.Values = nil
}
return
}
}
func ParseConfig(r io.Reader) (conf PacmanConfig, err error) {
rdr := newConfReader(r)
rdrStack := []confReader{rdr}
conf.SetDefaults()
confReflect := reflect.ValueOf(&conf).Elem()
var currentSection string
var curRepo *RepoConfig
lineloop:
for {
line, err := rdr.ParseLine()
// fmt.Printf("%+v\n", line)
switch err {
case io.EOF:
// pop reader stack.
l := len(rdrStack)
if l == 1 {
return conf, nil
} else {
rdr = rdrStack[l-2]
rdrStack = rdrStack[:l-1]
}
default:
return conf, err
case nil:
// Ok.
}
switch line.Type {
case tokenComment:
case tokenSection:
currentSection = line.Name
if currentSection != "options" {
conf.Repos = append(conf.Repos, RepoConfig{})
curRepo = &conf.Repos[len(conf.Repos)-1]
curRepo.Name = line.Name
curRepo.SigLevel = SigUseDefault // FIXME
}
case tokenKey:
if line.Name == "Include" {
f, err := os.Open(line.Values[0])
if err != nil {
err = fmt.Errorf("error while processing Include directive at line %d: %s",
rdr.Lineno, err)
return conf, err
}
rdr = newConfReader(f)
rdrStack = append(rdrStack, rdr)
continue lineloop
}
if currentSection != "options" {
switch line.Name {
case "SigLevel":
curRepo.SigLevel, err = parseSigLevel(SigUseDefault, line.Values)
if err != nil {
err = fmt.Errorf("invalid SigLevel for repo %q: %q",
currentSection, err)
return conf, err
}
continue lineloop
case "Server":
curRepo.Servers = append(curRepo.Servers, line.Values...)
continue lineloop
case "Usage":
// TODO: parse Usage
continue lineloop
default:
err = fmt.Errorf("option %s outside of [options] section, at line %d",
line.Name, rdr.Lineno)
return conf, err
}
}
// main options.
if opt, ok := optionsMap[line.Name]; ok {
// boolean option.
conf.Options |= opt
} else {
// key-value option.
fld := confReflect.FieldByName(line.Name)
if !fld.IsValid() {
err = fmt.Errorf("unknown option at line %d: %s", rdr.Lineno, line.Name)
return conf, err
}
switch field_p := fld.Addr().Interface().(type) {
case *string:
// single valued option.
*field_p = strings.Join(line.Values, " ")
case *[]string:
//many valued option.
*field_p = append(*field_p, line.Values...)
case *SigLevel:
*field_p, err = parseSigLevel(SigUseDefault, line.Values)
if err != nil {
err = fmt.Errorf("invalid value at line %d: %s",
rdr.Lineno, err)
return conf, err
}
}
}
}
}
}
func parseSigLevel(base SigLevel, tokens []string) (lvl SigLevel, err error) {
lvl = base
for i, tok := range tokens {
lvl &^= SigUseDefault
onPkg, onDB := true, true
if strings.HasPrefix(tok, "Package") {
tok = strings.TrimPrefix(tok, "Package")
onDB = false
} else if strings.HasPrefix(tok, "Database") {
tok = strings.TrimPrefix(tok, "Database")
onPkg = false
}
switch tok {
case "Never":
if onDB {
lvl &^= SigDatabase
}
if onPkg {
lvl &^= SigPackage
}
case "Optional":
if onDB {
lvl |= SigDatabase | SigDatabaseOptional
}
if onPkg {
lvl |= SigPackage | SigPackageOptional
}
case "Required":
if onDB {
lvl |= SigDatabase
lvl &^= SigDatabaseOptional
}
if onPkg {
lvl |= SigPackage
lvl &^= SigPackageOptional
}
case "TrustedOnly":
if onDB {
lvl &^= SigDatabaseMarginalOk | SigDatabaseUnknownOk
}
if onPkg {
lvl &^= SigPackageMarginalOk | SigPackageUnknownOk
}
case "TrustAll":
if onDB {
lvl |= SigDatabaseMarginalOk | SigDatabaseUnknownOk
}
if onPkg {
lvl |= SigPackageMarginalOk | SigPackageUnknownOk
}
default:
return 0, fmt.Errorf("invalid signature level %s", tokens[i])
}
}
return lvl, nil
}
func (conf *PacmanConfig) SetDefaults() {
conf.RootDir = "/"
conf.DBPath = "/var/lib/pacman"
conf.LocalFileSigLevel = SigUseDefault
conf.RemoteFileSigLevel = SigUseDefault
}
func getArch() (string, error) {
var uname syscall.Utsname
err := syscall.Uname(&uname)
if err != nil {
return "", err
}
var arch [65]byte
for i, c := range uname.Machine {
if c == 0 {
return string(arch[:i]), nil
}
arch[i] = byte(c)
}
return string(arch[:]), nil
}
func (conf *PacmanConfig) CreateHandle() (*Handle, error) {
h, err := Init(conf.RootDir, conf.DBPath)
if err != nil {
return nil, err
}
if conf.Architecture == "auto" {
conf.Architecture, err = getArch()
if err != nil {
return nil, fmt.Errorf("architecture is 'auto' but couldn't uname()")
}
}
for _, repoconf := range conf.Repos {
// TODO: set SigLevel
db, err := h.RegisterSyncDb(repoconf.Name, 0)
if err == nil {
for i, addr := range repoconf.Servers {
addr = strings.Replace(addr, "$repo", repoconf.Name, -1)
addr = strings.Replace(addr, "$arch", conf.Architecture, -1)
repoconf.Servers[i] = addr
}
db.SetServers(repoconf.Servers)
}
}
return h, nil
}