Merge remote-tracking branch 'origin/master' into feat/signing-backends
This commit is contained in:
+33
-15
@@ -68,7 +68,8 @@ var ErrRepoExists = xerrors.New("repo exists")
|
||||
|
||||
// FsRepo is struct for repo, use NewFS to create
|
||||
type FsRepo struct {
|
||||
path string
|
||||
path string
|
||||
configPath string
|
||||
}
|
||||
|
||||
var _ Repo = &FsRepo{}
|
||||
@@ -81,10 +82,15 @@ func NewFS(path string) (*FsRepo, error) {
|
||||
}
|
||||
|
||||
return &FsRepo{
|
||||
path: path,
|
||||
path: path,
|
||||
configPath: filepath.Join(path, fsConfig),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (fsr *FsRepo) SetConfigPath(cfgPath string) {
|
||||
fsr.configPath = cfgPath
|
||||
}
|
||||
|
||||
func (fsr *FsRepo) Exists() (bool, error) {
|
||||
_, err := os.Stat(filepath.Join(fsr.path, fsDatastore))
|
||||
notexist := os.IsNotExist(err)
|
||||
@@ -110,7 +116,7 @@ func (fsr *FsRepo) Init(t RepoType) error {
|
||||
}
|
||||
|
||||
log.Infof("Initializing repo at '%s'", fsr.path)
|
||||
err = os.Mkdir(fsr.path, 0755) //nolint: gosec
|
||||
err = os.MkdirAll(fsr.path, 0755) //nolint: gosec
|
||||
if err != nil && !os.IsExist(err) {
|
||||
return err
|
||||
}
|
||||
@@ -124,9 +130,7 @@ func (fsr *FsRepo) Init(t RepoType) error {
|
||||
}
|
||||
|
||||
func (fsr *FsRepo) initConfig(t RepoType) error {
|
||||
cfgP := filepath.Join(fsr.path, fsConfig)
|
||||
|
||||
_, err := os.Stat(cfgP)
|
||||
_, err := os.Stat(fsr.configPath)
|
||||
if err == nil {
|
||||
// exists
|
||||
return nil
|
||||
@@ -134,7 +138,7 @@ func (fsr *FsRepo) initConfig(t RepoType) error {
|
||||
return err
|
||||
}
|
||||
|
||||
c, err := os.Create(cfgP)
|
||||
c, err := os.Create(fsr.configPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -224,16 +228,30 @@ func (fsr *FsRepo) Lock(repoType RepoType) (LockedRepo, error) {
|
||||
return nil, xerrors.Errorf("could not lock the repo: %w", err)
|
||||
}
|
||||
return &fsLockedRepo{
|
||||
path: fsr.path,
|
||||
repoType: repoType,
|
||||
closer: closer,
|
||||
path: fsr.path,
|
||||
configPath: fsr.configPath,
|
||||
repoType: repoType,
|
||||
closer: closer,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Like Lock, except datastores will work in read-only mode
|
||||
func (fsr *FsRepo) LockRO(repoType RepoType) (LockedRepo, error) {
|
||||
lr, err := fsr.Lock(repoType)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
lr.(*fsLockedRepo).readonly = true
|
||||
return lr, nil
|
||||
}
|
||||
|
||||
type fsLockedRepo struct {
|
||||
path string
|
||||
repoType RepoType
|
||||
closer io.Closer
|
||||
path string
|
||||
configPath string
|
||||
repoType RepoType
|
||||
closer io.Closer
|
||||
readonly bool
|
||||
|
||||
ds map[string]datastore.Batching
|
||||
dsErr error
|
||||
@@ -286,7 +304,7 @@ func (fsr *fsLockedRepo) Config() (interface{}, error) {
|
||||
}
|
||||
|
||||
func (fsr *fsLockedRepo) loadConfigFromDisk() (interface{}, error) {
|
||||
return config.FromFile(fsr.join(fsConfig), defConfForType(fsr.repoType))
|
||||
return config.FromFile(fsr.configPath, defConfForType(fsr.repoType))
|
||||
}
|
||||
|
||||
func (fsr *fsLockedRepo) SetConfig(c func(interface{})) error {
|
||||
@@ -315,7 +333,7 @@ func (fsr *fsLockedRepo) SetConfig(c func(interface{})) error {
|
||||
}
|
||||
|
||||
// write buffer of TOML bytes to config file
|
||||
err = ioutil.WriteFile(fsr.join(fsConfig), buf.Bytes(), 0644)
|
||||
err = ioutil.WriteFile(fsr.configPath, buf.Bytes(), 0644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
+11
-7
@@ -14,7 +14,7 @@ import (
|
||||
ldbopts "github.com/syndtr/goleveldb/leveldb/opt"
|
||||
)
|
||||
|
||||
type dsCtor func(path string) (datastore.Batching, error)
|
||||
type dsCtor func(path string, readonly bool) (datastore.Batching, error)
|
||||
|
||||
var fsDatastores = map[string]dsCtor{
|
||||
"chain": chainBadgerDs,
|
||||
@@ -26,9 +26,10 @@ var fsDatastores = map[string]dsCtor{
|
||||
"client": badgerDs, // client specific
|
||||
}
|
||||
|
||||
func chainBadgerDs(path string) (datastore.Batching, error) {
|
||||
func chainBadgerDs(path string, readonly bool) (datastore.Batching, error) {
|
||||
opts := badger.DefaultOptions
|
||||
opts.GcInterval = 0 // disable GC for chain datastore
|
||||
opts.ReadOnly = readonly
|
||||
|
||||
opts.Options = dgbadger.DefaultOptions("").WithTruncate(true).
|
||||
WithValueThreshold(1 << 10)
|
||||
@@ -36,23 +37,26 @@ func chainBadgerDs(path string) (datastore.Batching, error) {
|
||||
return badger.NewDatastore(path, &opts)
|
||||
}
|
||||
|
||||
func badgerDs(path string) (datastore.Batching, error) {
|
||||
func badgerDs(path string, readonly bool) (datastore.Batching, error) {
|
||||
opts := badger.DefaultOptions
|
||||
opts.ReadOnly = readonly
|
||||
|
||||
opts.Options = dgbadger.DefaultOptions("").WithTruncate(true).
|
||||
WithValueThreshold(1 << 10)
|
||||
|
||||
return badger.NewDatastore(path, &opts)
|
||||
}
|
||||
|
||||
func levelDs(path string) (datastore.Batching, error) {
|
||||
func levelDs(path string, readonly bool) (datastore.Batching, error) {
|
||||
return levelds.NewDatastore(path, &levelds.Options{
|
||||
Compression: ldbopts.NoCompression,
|
||||
NoSync: false,
|
||||
Strict: ldbopts.StrictAll,
|
||||
ReadOnly: readonly,
|
||||
})
|
||||
}
|
||||
|
||||
func (fsr *fsLockedRepo) openDatastores() (map[string]datastore.Batching, error) {
|
||||
func (fsr *fsLockedRepo) openDatastores(readonly bool) (map[string]datastore.Batching, error) {
|
||||
if err := os.MkdirAll(fsr.join(fsDatastore), 0755); err != nil {
|
||||
return nil, xerrors.Errorf("mkdir %s: %w", fsr.join(fsDatastore), err)
|
||||
}
|
||||
@@ -63,7 +67,7 @@ func (fsr *fsLockedRepo) openDatastores() (map[string]datastore.Batching, error)
|
||||
prefix := datastore.NewKey(p)
|
||||
|
||||
// TODO: optimization: don't init datastores we don't need
|
||||
ds, err := ctor(fsr.join(filepath.Join(fsDatastore, p)))
|
||||
ds, err := ctor(fsr.join(filepath.Join(fsDatastore, p)), readonly)
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("opening datastore %s: %w", prefix, err)
|
||||
}
|
||||
@@ -78,7 +82,7 @@ func (fsr *fsLockedRepo) openDatastores() (map[string]datastore.Batching, error)
|
||||
|
||||
func (fsr *fsLockedRepo) Datastore(ns string) (datastore.Batching, error) {
|
||||
fsr.dsOnce.Do(func() {
|
||||
fsr.ds, fsr.dsErr = fsr.openDatastores()
|
||||
fsr.ds, fsr.dsErr = fsr.openDatastores(fsr.readonly)
|
||||
})
|
||||
|
||||
if fsr.dsErr != nil {
|
||||
|
||||
Reference in New Issue
Block a user