forked from celestiaorg/celestia-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.go
107 lines (89 loc) · 2.15 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package nodebuilder
import (
"os"
"path/filepath"
"github.com/celestiaorg/celestia-node/libs/fslock"
"github.com/celestiaorg/celestia-node/libs/utils"
"github.com/celestiaorg/celestia-node/nodebuilder/node"
)
// Init initializes the Node FileSystem Store for the given Node Type 'tp' in the directory under
// 'path'.
func Init(cfg Config, path string, tp node.Type) error {
path, err := storePath(path)
if err != nil {
return err
}
log.Infof("Initializing %s Node Store over '%s'", tp, path)
err = initRoot(path)
if err != nil {
return err
}
flock, err := fslock.Lock(lockPath(path))
if err != nil {
if err == fslock.ErrLocked {
return ErrOpened
}
return err
}
defer flock.Unlock() //nolint: errcheck
err = initDir(keysPath(path))
if err != nil {
return err
}
err = initDir(dataPath(path))
if err != nil {
return err
}
cfgPath := configPath(path)
err = SaveConfig(cfgPath, &cfg)
if err != nil {
return err
}
log.Infow("Saving config", "path", cfgPath)
log.Info("Node Store initialized")
return nil
}
// IsInit checks whether FileSystem Store was setup under given 'path'.
// If any required file/subdirectory does not exist, then false is reported.
func IsInit(path string) bool {
path, err := storePath(path)
if err != nil {
log.Errorw("parsing store path", "path", path, "err", err)
return false
}
_, err = LoadConfig(configPath(path)) // load the Config and implicitly check for its existence
if err != nil {
log.Errorw("loading config", "path", path, "err", err)
return false
}
if utils.Exists(keysPath(path)) &&
utils.Exists(dataPath(path)) {
return true
}
return false
}
const perms = 0755
// initRoot initializes(creates) directory if not created and check if it is writable
func initRoot(path string) error {
err := initDir(path)
if err != nil {
return err
}
// check for writing permissions
f, err := os.Create(filepath.Join(path, ".check"))
if err != nil {
return err
}
err = f.Close()
if err != nil {
return err
}
return os.Remove(f.Name())
}
// initDir creates a dir if not exist
func initDir(path string) error {
if utils.Exists(path) {
return nil
}
return os.Mkdir(path, perms)
}