-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
open.go
47 lines (38 loc) · 1.13 KB
/
open.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
package encrepo
import (
"context"
"github.com/ipfs/go-datastore"
sync_ds "github.com/ipfs/go-datastore/sync"
"github.com/ipfs/kubo/repo"
"github.com/pkg/errors"
)
var (
onlyOne repo.OnlyOne
)
func Open(dbPath string, key []byte, opts SQLCipherDatastoreOptions) (repo.Repo, error) {
fn := func() (repo.Repo, error) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
return open(ctx, dbPath, key, opts)
}
return onlyOne.Open(dbPath, fn)
}
func open(ctx context.Context, dbPath string, key []byte, opts SQLCipherDatastoreOptions) (repo.Repo, error) {
packageLock.Lock()
defer packageLock.Unlock()
uroot, err := OpenSQLCipherDatastore("sqlite3", dbPath, tableName, key, opts)
if err != nil {
return nil, errors.Wrap(err, "instantiate datastore")
}
root := sync_ds.MutexWrap(uroot)
conf, err := getConfigFromDatastore(ctx, root)
if err != nil {
return nil, errors.Wrap(err, "get config")
}
return &encRepo{
root: root,
ds: NewNamespacedDatastore(root, datastore.NewKey("data")),
ks: KeystoreFromDatastore(NewNamespacedDatastore(root, datastore.NewKey("keys"))),
config: conf,
}, nil
}