@cryptotaxi247 / kubo / commits / 5c4be7514

fix: init datastore in datastore/ subdirectory

@whyrusleeping @jbenet newly initialized datastores were being dumped into ./go-ipfs. Eeek. An oversight during the FSRepo refactor. Not sure how this bug survived this long.

Brian Tiger Chow committed Jan 20, 2015 at 06:17 UTC 5c4be75146b1297fef64007ce3733521e60c772a
1 file changed +18 -13
repo/fsrepo/component/datastore.go
+18 -13
@@ -2,6 +2,8 @@ package component
2
3 import (
4 "errors"
5 + "path"
6 + "path/filepath"
7 "sync"
8
9 datastore "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
@@ -15,6 +17,10 @@ import (
17 debugerror "github.com/jbenet/go-ipfs/util/debugerror"
18 )
19
20 +const (
21 + DefaultDataStoreDirectory = "datastore"
22 +)
23 +
24 var (
25 _ Component = &DatastoreComponent{}
26 _ Initializer = InitDatastoreComponent
@@ -30,26 +36,22 @@ func init() {
36 datastores = make(map[string]ds2.ThreadSafeDatastoreCloser)
37 }
38
33 -func InitDatastoreComponent(path string, conf *config.Config) error {
39 +func InitDatastoreComponent(dspath string, conf *config.Config) error {
40 // The actual datastore contents are initialized lazily when Opened.
41 // During Init, we merely check that the directory is writeable.
36 - dspath, err := config.DataStorePath(path)
37 - if err != nil {
38 - return err
42 + if !filepath.IsAbs(dspath) {
43 + return debugerror.New("datastore filepath must be absolute") // during initialization (this isn't persisted)
44 }
40 - if err := dir.Writable(dspath); err != nil {
45 + p := path.Join(dspath, DefaultDataStoreDirectory)
46 + if err := dir.Writable(p); err != nil {
47 return debugerror.Errorf("datastore: %s", err)
48 }
49 return nil
50 }
51
52 // DatastoreComponentIsInitialized returns true if the datastore dir exists.
47 -func DatastoreComponentIsInitialized(path string) bool {
48 - dspath, err := config.DataStorePath(path)
49 - if err != nil {
50 - return false
51 - }
52 - if !util.FileExists(dspath) {
53 +func DatastoreComponentIsInitialized(dspath string) bool {
54 + if !util.FileExists(path.Join(dspath, DefaultDataStoreDirectory)) {
55 return false
56 }
57 return true
@@ -61,7 +63,10 @@ type DatastoreComponent struct {
63 ds ds2.ThreadSafeDatastoreCloser // assigned when repo is opened
64 }
65
64 -func (dsc *DatastoreComponent) SetPath(p string) { dsc.path = p }
66 +func (dsc *DatastoreComponent) SetPath(p string) {
67 + dsc.path = path.Join(p, DefaultDataStoreDirectory)
68 +}
69 +
70 func (dsc *DatastoreComponent) Datastore() datastore.ThreadSafeDatastore { return dsc.ds }
71
72 // Open returns an error if the config file is not present.
@@ -77,7 +82,7 @@ func (dsc *DatastoreComponent) Open() error {
82 Compression: ldbopts.NoCompression,
83 })
84 if err != nil {
80 - return errors.New("unable to open leveldb datastore")
85 + return debugerror.New("unable to open leveldb datastore")
86 }
87 datastores[dsc.path] = ds
88 }