/
driver_qemu_config_override.go
259 lines (207 loc) · 5.41 KB
/
driver_qemu_config_override.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
package drivers
import (
"regexp"
"sort"
"strconv"
"strings"
)
const pattern = `\s*(?m:(?:\[([^\]]+)\](?:\[(\d+)\])?)|(?:([^=]+)[ \t]*=[ \t]*(?:"([^"]*)"|([^\n]*)))$)`
var parser = regexp.MustCompile(pattern)
type rawConfigKey struct {
sectionName string
index uint
entryKey string
}
type configMap map[rawConfigKey]string
func sortedConfigKeys(cfgMap configMap) []rawConfigKey {
rv := []rawConfigKey{}
for k := range cfgMap {
rv = append(rv, k)
}
sort.SliceStable(rv, func(i, j int) bool {
return rv[i].sectionName < rv[j].sectionName ||
rv[i].index < rv[j].index ||
rv[i].entryKey < rv[j].entryKey
})
return rv
}
func parseConfOverride(confOverride string) configMap {
s := confOverride
rv := configMap{}
currentSectionName := ""
var currentIndex uint
currentEntryCount := 0
for {
loc := parser.FindStringSubmatchIndex(s)
if loc == nil {
break
}
if loc[2] > 0 {
if currentSectionName != "" && currentEntryCount == 0 {
// new section started and previous section ended without entries
k := rawConfigKey{
sectionName: currentSectionName,
index: currentIndex,
entryKey: "",
}
rv[k] = ""
}
currentEntryCount = 0
currentSectionName = strings.TrimSpace(s[loc[2]:loc[3]])
if loc[4] > 0 {
i, err := strconv.Atoi(s[loc[4]:loc[5]])
if err != nil || i < 0 {
panic("failed to parse index")
}
currentIndex = uint(i)
} else {
currentIndex = 0
}
} else {
entryKey := strings.TrimSpace(s[loc[6]:loc[7]])
var value string
if loc[8] > 0 {
// quoted value
value = s[loc[8]:loc[9]]
} else {
// unquoted value
value = strings.TrimSpace(s[loc[10]:loc[11]])
}
k := rawConfigKey{
sectionName: currentSectionName,
index: currentIndex,
entryKey: entryKey,
}
rv[k] = value
currentEntryCount++
}
s = s[loc[1]:]
}
if currentSectionName != "" && currentEntryCount == 0 {
// previous section ended without entries
k := rawConfigKey{
sectionName: currentSectionName,
index: currentIndex,
entryKey: "",
}
rv[k] = ""
}
return rv
}
func updateEntries(entries []cfgEntry, sk rawConfigKey, cfgMap configMap) []cfgEntry {
rv := []cfgEntry{}
for _, entry := range entries {
newEntry := cfgEntry{
key: entry.key,
value: entry.value,
}
ek := rawConfigKey{sk.sectionName, sk.index, entry.key}
val, ok := cfgMap[ek]
if ok {
// override
delete(cfgMap, ek)
newEntry.value = val
}
rv = append(rv, newEntry)
}
return rv
}
func appendEntries(entries []cfgEntry, sk rawConfigKey, cfgMap configMap) []cfgEntry {
// sort to have deterministic output in the appended entries
sortedKeys := sortedConfigKeys(cfgMap)
// processed all modifications for the current section, now
// handle new entries
for _, rawKey := range sortedKeys {
if rawKey.sectionName != sk.sectionName || rawKey.index != sk.index {
continue
}
newEntry := cfgEntry{
key: rawKey.entryKey,
value: cfgMap[rawKey],
}
entries = append(entries, newEntry)
delete(cfgMap, rawKey)
}
return entries
}
func updateSections(cfg []cfgSection, cfgMap configMap) []cfgSection {
newCfg := []cfgSection{}
sectionCounts := map[string]uint{}
for _, section := range cfg {
count, ok := sectionCounts[section.name]
if ok {
sectionCounts[section.name] = count + 1
} else {
sectionCounts[section.name] = 1
}
index := sectionCounts[section.name] - 1
sk := rawConfigKey{section.name, index, ""}
val, ok := cfgMap[sk]
if ok {
if val == "" {
// deleted section
delete(cfgMap, sk)
continue
}
}
newSection := cfgSection{
name: section.name,
comment: section.comment,
}
newSection.entries = updateEntries(section.entries, sk, cfgMap)
newSection.entries = appendEntries(newSection.entries, sk, cfgMap)
newCfg = append(newCfg, newSection)
}
return newCfg
}
func appendSections(newCfg []cfgSection, cfgMap configMap) []cfgSection {
tmp := map[rawConfigKey]cfgSection{}
// sort to have deterministic output in the appended entries
sortedKeys := sortedConfigKeys(cfgMap)
for _, k := range sortedKeys {
if k.entryKey == "" {
// makes no sense to process section deletions (the only case where
// entryKey == "") since we are only adding new sections now
continue
}
sectionKey := rawConfigKey{k.sectionName, k.index, ""}
section, found := tmp[sectionKey]
if !found {
section = cfgSection{
name: k.sectionName,
}
}
section.entries = append(section.entries, cfgEntry{
key: k.entryKey,
value: cfgMap[k],
})
tmp[sectionKey] = section
}
rawSections := []rawConfigKey{}
for rawSection := range tmp {
rawSections = append(rawSections, rawSection)
}
// Sort to have deterministic output in the appended sections
sort.SliceStable(rawSections, func(i, j int) bool {
return rawSections[i].sectionName < rawSections[j].sectionName ||
rawSections[i].index < rawSections[j].index
})
for _, rawSection := range rawSections {
newCfg = append(newCfg, tmp[rawSection])
}
return newCfg
}
func qemuRawCfgOverride(cfg []cfgSection, expandedConfig map[string]string) []cfgSection {
confOverride, ok := expandedConfig["raw.qemu.conf"]
if !ok {
return cfg
}
cfgMap := parseConfOverride(confOverride)
if len(cfgMap) == 0 {
// If no keys are found, we return the cfg unmodified.
return cfg
}
newCfg := updateSections(cfg, cfgMap)
newCfg = appendSections(newCfg, cfgMap)
return newCfg
}