check datastore directory when opening the repo for use
Brian Tiger Chow committed
Jan 12, 2015 at 12:28 UTC
c5da1561b9ad887335fb8b84360a7a7900ac1ef4
2 files changed
+40
-11
cmd/ipfs/init.go
+7
-10
@@ -145,18 +145,15 @@ func addTheWelcomeFile(conf *config.Config) error {
145
return nil
146
}
147
148
-func datastoreConfig() (config.Datastore, error) {
149
- ds := config.Datastore{}
148
+func datastoreConfig() (*config.Datastore, error) {
149
dspath, err := config.DataStorePath("")
150
if err != nil {
152
- return ds, err
153
- }
154
- ds.Path = dspath
155
- ds.Type = "leveldb"
156
- if err := initCheckDir(dspath); err != nil {
157
- return ds, debugerror.Errorf("datastore: %s", err)
151
+ return nil, err
152
}
159
- return ds, nil
153
+ return &config.Datastore{
154
+ Path: dspath,
155
+ Type: "leveldb",
156
+ }, nil
157
}
158
159
func initConfig(nBitsForKeypair int) (*config.Config, error) {
@@ -193,7 +190,7 @@ func initConfig(nBitsForKeypair int) (*config.Config, error) {
190
},
191
192
Bootstrap: bootstrapPeers,
196
- Datastore: ds,
193
+ Datastore: *ds,
194
Logs: logConfig,
195
Identity: identity,
196
repo/fsrepo/fsrepo.go
+33
-1
@@ -2,9 +2,12 @@ package fsrepo
2
3
import (
4
"io"
5
+ "os"
6
+ "path/filepath"
7
8
config "github.com/jbenet/go-ipfs/repo/config"
9
util "github.com/jbenet/go-ipfs/util"
10
+ "github.com/jbenet/go-ipfs/util/debugerror"
11
)
12
13
type FSRepo struct {
@@ -19,8 +22,22 @@ func At(path string) *FSRepo {
22
}
23
24
func (r *FSRepo) Open() error {
22
- // TODO may need to check that directory is writeable
25
+ // check repo path, then check all constituent parts.
26
// TODO acquire repo lock
27
+ // TODO if err := initCheckDir(logpath); err != nil { // }
28
+ if err := initCheckDir(r.path); err != nil {
29
+ return err
30
+ }
31
+
32
+ // datastore
33
+ dspath, err := config.DataStorePath("")
34
+ if err != nil {
35
+ return err
36
+ }
37
+ if err := initCheckDir(dspath); err != nil {
38
+ return debugerror.Errorf("datastore: %s", err)
39
+ }
40
+
41
return nil
42
}
43
@@ -53,3 +70,18 @@ func ConfigIsInitialized(path string) bool {
70
}
71
return true
72
}
73
+
74
+// initCheckDir ensures the directory exists and is writable
75
+func initCheckDir(path string) error {
76
+ // Construct the path if missing
77
+ if err := os.MkdirAll(path, os.ModePerm); err != nil {
78
+ return err
79
+ }
80
+ // Check the directory is writeable
81
+ if f, err := os.Create(filepath.Join(path, "._check_writeable")); err == nil {
82
+ os.Remove(f.Name())
83
+ } else {
84
+ return debugerror.New("'" + path + "' is not writeable")
85
+ }
86
+ return nil
87
+}