提交 c5da1561 作者: Brian Tiger Chow

check datastore directory when opening the repo for use

上级 0d88f70c
......@@ -145,18 +145,15 @@ func addTheWelcomeFile(conf *config.Config) error {
return nil
}
func datastoreConfig() (config.Datastore, error) {
ds := config.Datastore{}
func datastoreConfig() (*config.Datastore, error) {
dspath, err := config.DataStorePath("")
if err != nil {
return ds, err
}
ds.Path = dspath
ds.Type = "leveldb"
if err := initCheckDir(dspath); err != nil {
return ds, debugerror.Errorf("datastore: %s", err)
return nil, err
}
return ds, nil
return &config.Datastore{
Path: dspath,
Type: "leveldb",
}, nil
}
func initConfig(nBitsForKeypair int) (*config.Config, error) {
......@@ -193,7 +190,7 @@ func initConfig(nBitsForKeypair int) (*config.Config, error) {
},
Bootstrap: bootstrapPeers,
Datastore: ds,
Datastore: *ds,
Logs: logConfig,
Identity: identity,
......
......@@ -2,9 +2,12 @@ package fsrepo
import (
"io"
"os"
"path/filepath"
config "github.com/jbenet/go-ipfs/repo/config"
util "github.com/jbenet/go-ipfs/util"
"github.com/jbenet/go-ipfs/util/debugerror"
)
type FSRepo struct {
......@@ -19,8 +22,22 @@ func At(path string) *FSRepo {
}
func (r *FSRepo) Open() error {
// TODO may need to check that directory is writeable
// check repo path, then check all constituent parts.
// TODO acquire repo lock
// TODO if err := initCheckDir(logpath); err != nil { // }
if err := initCheckDir(r.path); err != nil {
return err
}
// datastore
dspath, err := config.DataStorePath("")
if err != nil {
return err
}
if err := initCheckDir(dspath); err != nil {
return debugerror.Errorf("datastore: %s", err)
}
return nil
}
......@@ -53,3 +70,18 @@ func ConfigIsInitialized(path string) bool {
}
return true
}
// initCheckDir ensures the directory exists and is writable
func initCheckDir(path string) error {
// Construct the path if missing
if err := os.MkdirAll(path, os.ModePerm); err != nil {
return err
}
// Check the directory is writeable
if f, err := os.Create(filepath.Join(path, "._check_writeable")); err == nil {
os.Remove(f.Name())
} else {
return debugerror.New("'" + path + "' is not writeable")
}
return nil
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论