forked from canonical/snapd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend_bolt.go
288 lines (247 loc) · 5.88 KB
/
backend_bolt.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
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2018-2024 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package advisor
import (
"encoding/json"
"os"
"path/filepath"
"time"
"go.etcd.io/bbolt"
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/randutil"
)
var (
cmdBucketKey = []byte("Commands")
pkgBucketKey = []byte("Snaps")
)
type writer struct {
fn string
db *bbolt.DB
tx *bbolt.Tx
cmdBucket *bbolt.Bucket
pkgBucket *bbolt.Bucket
}
// Create opens the commands database for writing, and starts a
// transaction that drops and recreates the buckets. You should then
// call AddSnap with each snap you wish to add, and them Commit the
// results to make the changes live, or Rollback to abort; either of
// these closes the database again.
func Create() (CommandDB, error) {
var err error
t := &writer{
fn: dirs.SnapCommandsDB + "." + randutil.RandomString(12) + "~",
}
t.db, err = bbolt.Open(t.fn, 0644, &bbolt.Options{Timeout: 1 * time.Second})
if err != nil {
return nil, err
}
t.tx, err = t.db.Begin(true)
if err == nil {
t.cmdBucket, err = t.tx.CreateBucket(cmdBucketKey)
if err == nil {
t.pkgBucket, err = t.tx.CreateBucket(pkgBucketKey)
}
if err != nil {
t.tx.Rollback()
}
}
if err != nil {
t.db.Close()
return nil, err
}
return t, nil
}
func (t *writer) AddSnap(snapName, version, summary string, commands []string) error {
for _, cmd := range commands {
var sil []Package
bcmd := []byte(cmd)
row := t.cmdBucket.Get(bcmd)
if row != nil {
if err := json.Unmarshal(row, &sil); err != nil {
return err
}
}
// For the mapping of command->snap we do not need the summary, nothing is using that.
sil = append(sil, Package{Snap: snapName, Version: version})
row, err := json.Marshal(sil)
if err != nil {
return err
}
if err := t.cmdBucket.Put(bcmd, row); err != nil {
return err
}
}
// TODO: use json here as well and put the version information here
bj, err := json.Marshal(Package{
Snap: snapName,
Version: version,
Summary: summary,
})
if err != nil {
return err
}
if err := t.pkgBucket.Put([]byte(snapName), bj); err != nil {
return err
}
return nil
}
func (t *writer) Commit() error {
// either everything worked, and therefore this will fail, or something
// will fail, and that error is more important than this one if this one
// then fails as well. So, ignore the error.
defer os.Remove(t.fn)
if err := t.done(true); err != nil {
return err
}
dir, err := os.Open(filepath.Dir(dirs.SnapCommandsDB))
if err != nil {
return err
}
defer dir.Close()
if err := os.Rename(t.fn, dirs.SnapCommandsDB); err != nil {
return err
}
return dir.Sync()
}
func (t *writer) Rollback() error {
e1 := t.done(false)
e2 := os.Remove(t.fn)
if e1 == nil {
return e2
}
return e1
}
func (t *writer) done(commit bool) error {
var e1, e2 error
t.cmdBucket = nil
t.pkgBucket = nil
if t.tx != nil {
if commit {
e1 = t.tx.Commit()
} else {
e1 = t.tx.Rollback()
}
t.tx = nil
}
if t.db != nil {
e2 = t.db.Close()
t.db = nil
}
if e1 == nil {
return e2
}
return e1
}
// DumpCommands returns the whole database as a map. For use in
// testing and debugging.
func DumpCommands() (map[string]string, error) {
db, err := bbolt.Open(dirs.SnapCommandsDB, 0644, &bbolt.Options{
ReadOnly: true,
Timeout: 1 * time.Second,
})
if err != nil {
return nil, err
}
defer db.Close()
tx, err := db.Begin(false)
if err != nil {
return nil, err
}
defer tx.Rollback()
b := tx.Bucket(cmdBucketKey)
if b == nil {
return nil, nil
}
m := map[string]string{}
c := b.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
m[string(k)] = string(v)
}
return m, nil
}
type boltFinder struct {
*bbolt.DB
}
// Open the database for reading.
func Open() (Finder, error) {
// Check for missing file manually to workaround bug in bolt.
// bolt.Open() is using os.OpenFile(.., os.O_RDONLY |
// os.O_CREATE) even if ReadOnly mode is used. So we would get
// a misleading "permission denied" error without this check.
if !osutil.FileExists(dirs.SnapCommandsDB) {
return nil, os.ErrNotExist
}
db, err := bbolt.Open(dirs.SnapCommandsDB, 0644, &bbolt.Options{
ReadOnly: true,
Timeout: 1 * time.Second,
})
if err != nil {
return nil, err
}
return &boltFinder{db}, nil
}
func (f *boltFinder) FindCommand(command string) ([]Command, error) {
tx, err := f.Begin(false)
if err != nil {
return nil, err
}
defer tx.Rollback()
b := tx.Bucket(cmdBucketKey)
if b == nil {
return nil, nil
}
buf := b.Get([]byte(command))
if buf == nil {
return nil, nil
}
var sil []Package
if err := json.Unmarshal(buf, &sil); err != nil {
return nil, err
}
cmds := make([]Command, len(sil))
for i, si := range sil {
cmds[i] = Command{
Snap: si.Snap,
Version: si.Version,
Command: command,
}
}
return cmds, nil
}
func (f *boltFinder) FindPackage(pkgName string) (*Package, error) {
tx, err := f.Begin(false)
if err != nil {
return nil, err
}
defer tx.Rollback()
b := tx.Bucket(pkgBucketKey)
if b == nil {
return nil, nil
}
bj := b.Get([]byte(pkgName))
if bj == nil {
return nil, nil
}
var si Package
err = json.Unmarshal(bj, &si)
if err != nil {
return nil, err
}
return &Package{Snap: pkgName, Version: si.Version, Summary: si.Summary}, nil
}