-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinit.go
84 lines (64 loc) · 1.93 KB
/
init.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
package encrepo
import (
"context"
sync_ds "github.com/ipfs/go-datastore/sync"
config "github.com/ipfs/kubo/config"
"github.com/pkg/errors"
)
const tableName = "ipfs"
func IsInitialized(dbPath string, key []byte, opts SQLCipherDatastoreOptions) (bool, error) {
// packageLock is held to ensure that another caller doesn't attempt to
// Init or Remove the repo while this call is in progress.
packageLock.Lock()
defer packageLock.Unlock()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
return isInitialized(ctx, dbPath, key, opts)
}
// isInitialized reports whether the repo is initialized. Caller must
// hold the packageLock.
func isInitialized(ctx context.Context, dbPath string, key []byte, opts SQLCipherDatastoreOptions) (bool, error) {
uds, err := OpenSQLCipherDatastore("sqlite3", dbPath, tableName, key, opts)
if err == ErrDatabaseNotFound {
return false, nil
}
if err != nil {
return false, err
}
ds := sync_ds.MutexWrap(uds)
initialized := isConfigInitialized(ctx, ds)
if err := uds.Close(); err != nil {
return false, err
}
return initialized, nil
}
func Init(dbPath string, key []byte, opts SQLCipherDatastoreOptions, conf *config.Config) error {
// packageLock must be held to ensure that the repo is not initialized more
// than once.
packageLock.Lock()
defer packageLock.Unlock()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
isInit, err := isInitialized(ctx, dbPath, key, opts)
if err != nil {
return err
}
if isInit {
return nil
}
uds, err := NewSQLCipherDatastore("sqlite3", dbPath, tableName, key, opts)
if err != nil {
return err
}
ds := sync_ds.MutexWrap(uds)
if err := initConfig(ctx, ds, conf); err != nil {
return err
}
if len(conf.Datastore.Spec) != 0 {
return errors.New("Config.Datastore.Spec not supported")
}
/*if err := migrations.WriteRepoVersion(repoPath, RepoVersion); err != nil {
return err
}*/
return uds.Close()
}