mirror of
https://github.com/ipfs/kubo.git
synced 2025-09-10 05:52:20 +08:00
check datastore directory when opening the repo for use
This commit is contained in:
@ -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
|
||||
return nil, err
|
||||
}
|
||||
ds.Path = dspath
|
||||
ds.Type = "leveldb"
|
||||
if err := initCheckDir(dspath); err != nil {
|
||||
return ds, debugerror.Errorf("datastore: %s", 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
|
||||
}
|
||||
|
Reference in New Issue
Block a user