2019-07-10 13:23:29 +00:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
2019-07-18 14:01:39 +00:00
|
|
|
"encoding/json"
|
2019-10-30 16:38:39 +00:00
|
|
|
"fmt"
|
2019-07-10 13:23:29 +00:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2019-07-10 17:09:57 +00:00
|
|
|
"sync"
|
2019-07-10 13:23:29 +00:00
|
|
|
|
|
|
|
"github.com/ipfs/go-datastore"
|
2019-07-10 17:09:57 +00:00
|
|
|
"github.com/ipfs/go-datastore/namespace"
|
2020-01-08 18:56:45 +00:00
|
|
|
badger "github.com/ipfs/go-ds-badger2"
|
2019-07-10 13:23:29 +00:00
|
|
|
fslock "github.com/ipfs/go-fs-lock"
|
2020-01-08 19:10:57 +00:00
|
|
|
logging "github.com/ipfs/go-log/v2"
|
2019-07-10 17:09:57 +00:00
|
|
|
"github.com/mitchellh/go-homedir"
|
2019-07-18 14:01:39 +00:00
|
|
|
"github.com/multiformats/go-base32"
|
2019-07-10 13:23:29 +00:00
|
|
|
"github.com/multiformats/go-multiaddr"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
"github.com/filecoin-project/lotus/node/config"
|
2019-07-10 13:23:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
fsAPI = "api"
|
2019-07-23 18:49:09 +00:00
|
|
|
fsAPIToken = "token"
|
2019-07-10 13:23:29 +00:00
|
|
|
fsConfig = "config.toml"
|
|
|
|
fsDatastore = "datastore"
|
|
|
|
fsLock = "repo.lock"
|
2019-07-18 14:01:39 +00:00
|
|
|
fsKeystore = "keystore"
|
2019-07-10 13:23:29 +00:00
|
|
|
)
|
|
|
|
|
2019-10-30 16:38:39 +00:00
|
|
|
type RepoType int
|
|
|
|
|
|
|
|
const (
|
2019-11-12 17:59:38 +00:00
|
|
|
_ = iota // Default is invalid
|
|
|
|
FullNode RepoType = iota
|
|
|
|
StorageMiner
|
2019-10-30 16:38:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func defConfForType(t RepoType) interface{} {
|
|
|
|
switch t {
|
2019-11-12 17:59:38 +00:00
|
|
|
case FullNode:
|
2019-10-30 16:38:39 +00:00
|
|
|
return config.DefaultFullNode()
|
2019-11-12 17:59:38 +00:00
|
|
|
case StorageMiner:
|
2019-10-30 16:38:39 +00:00
|
|
|
return config.DefaultStorageMiner()
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unknown RepoType(%d)", int(t)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-10 17:09:57 +00:00
|
|
|
var log = logging.Logger("repo")
|
|
|
|
|
2019-07-26 11:45:25 +00:00
|
|
|
var ErrRepoExists = xerrors.New("repo exists")
|
2019-07-10 17:09:57 +00:00
|
|
|
|
2019-07-10 13:35:00 +00:00
|
|
|
// FsRepo is struct for repo, use NewFS to create
|
2019-07-10 13:23:29 +00:00
|
|
|
type FsRepo struct {
|
2019-10-30 16:38:39 +00:00
|
|
|
path string
|
|
|
|
repoType RepoType
|
2019-07-10 13:23:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ Repo = &FsRepo{}
|
|
|
|
|
2019-07-10 15:14:29 +00:00
|
|
|
// NewFS creates a repo instance based on a path on file system
|
2019-07-10 15:10:45 +00:00
|
|
|
func NewFS(path string) (*FsRepo, error) {
|
2019-07-10 17:09:57 +00:00
|
|
|
path, err := homedir.Expand(path)
|
|
|
|
if err != nil {
|
2019-07-10 17:36:17 +00:00
|
|
|
return nil, err
|
2019-07-10 17:09:57 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 15:10:45 +00:00
|
|
|
return &FsRepo{
|
|
|
|
path: path,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-07-23 21:54:54 +00:00
|
|
|
func (fsr *FsRepo) Exists() (bool, error) {
|
2019-10-22 17:18:06 +00:00
|
|
|
_, err := os.Stat(filepath.Join(fsr.path, fsDatastore))
|
2019-07-23 21:54:54 +00:00
|
|
|
notexist := os.IsNotExist(err)
|
|
|
|
if notexist {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
return !notexist, err
|
2019-07-19 09:23:24 +00:00
|
|
|
}
|
|
|
|
|
2019-10-30 16:38:39 +00:00
|
|
|
func (fsr *FsRepo) Init(t RepoType) error {
|
2019-07-30 23:18:21 +00:00
|
|
|
exist, err := fsr.Exists()
|
|
|
|
if err != nil {
|
2019-07-10 17:09:57 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-07-30 23:18:21 +00:00
|
|
|
if exist {
|
|
|
|
return nil
|
|
|
|
}
|
2019-07-10 17:09:57 +00:00
|
|
|
|
|
|
|
log.Infof("Initializing repo at '%s'", fsr.path)
|
2019-07-30 23:18:21 +00:00
|
|
|
err = os.Mkdir(fsr.path, 0755) //nolint: gosec
|
|
|
|
if err != nil && !os.IsExist(err) {
|
2019-07-18 14:01:39 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-10-30 16:38:39 +00:00
|
|
|
|
|
|
|
if err := fsr.initConfig(t); err != nil {
|
|
|
|
return xerrors.Errorf("init config: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fsr.initKeystore()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fsr *FsRepo) initConfig(t RepoType) error {
|
2019-11-12 21:42:26 +00:00
|
|
|
cfgP := filepath.Join(fsr.path, fsConfig)
|
|
|
|
|
|
|
|
_, err := os.Stat(cfgP)
|
|
|
|
if err == nil {
|
|
|
|
// exists
|
|
|
|
return nil
|
|
|
|
} else if !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := os.Create(cfgP)
|
2019-07-24 11:20:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-30 16:38:39 +00:00
|
|
|
comm, err := config.ConfigComment(defConfForType(t))
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("comment: %w", err)
|
|
|
|
}
|
|
|
|
_, err = c.Write(comm)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("write config: %w", err)
|
|
|
|
}
|
2019-07-10 17:09:57 +00:00
|
|
|
|
2019-10-30 16:38:39 +00:00
|
|
|
if err := c.Close(); err != nil {
|
|
|
|
return xerrors.Errorf("close config: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
2019-07-18 14:01:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (fsr *FsRepo) initKeystore() error {
|
|
|
|
kstorePath := filepath.Join(fsr.path, fsKeystore)
|
|
|
|
if _, err := os.Stat(kstorePath); err == nil {
|
|
|
|
return ErrRepoExists
|
|
|
|
} else if !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return os.Mkdir(kstorePath, 0700)
|
2019-07-10 17:09:57 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 13:35:00 +00:00
|
|
|
// APIEndpoint returns endpoint of API in this repo
|
2019-07-10 13:23:29 +00:00
|
|
|
func (fsr *FsRepo) APIEndpoint() (multiaddr.Multiaddr, error) {
|
|
|
|
p := filepath.Join(fsr.path, fsAPI)
|
|
|
|
|
2019-07-26 03:26:29 +00:00
|
|
|
f, err := os.Open(p)
|
2019-07-10 13:23:29 +00:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil, ErrNoAPIEndpoint
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close() //nolint: errcheck // Read only op
|
|
|
|
|
|
|
|
data, err := ioutil.ReadAll(f)
|
|
|
|
if err != nil {
|
2019-07-26 11:45:25 +00:00
|
|
|
return nil, xerrors.Errorf("failed to read %q: %w", p, err)
|
2019-07-10 13:23:29 +00:00
|
|
|
}
|
|
|
|
strma := string(data)
|
|
|
|
strma = strings.TrimSpace(strma)
|
|
|
|
|
|
|
|
apima, err := multiaddr.NewMultiaddr(strma)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return apima, nil
|
|
|
|
}
|
|
|
|
|
2019-07-23 18:49:09 +00:00
|
|
|
func (fsr *FsRepo) APIToken() ([]byte, error) {
|
|
|
|
p := filepath.Join(fsr.path, fsAPIToken)
|
|
|
|
f, err := os.Open(p)
|
|
|
|
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil, ErrNoAPIEndpoint
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close() //nolint: errcheck // Read only op
|
|
|
|
|
|
|
|
return ioutil.ReadAll(f)
|
|
|
|
}
|
|
|
|
|
2019-07-10 13:35:00 +00:00
|
|
|
// Lock acquires exclusive lock on this repo
|
2019-10-30 16:38:39 +00:00
|
|
|
func (fsr *FsRepo) Lock(repoType RepoType) (LockedRepo, error) {
|
2019-07-10 15:10:45 +00:00
|
|
|
locked, err := fslock.Locked(fsr.path, fsLock)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("could not check lock status: %w", err)
|
|
|
|
}
|
|
|
|
if locked {
|
|
|
|
return nil, ErrRepoAlreadyLocked
|
|
|
|
}
|
|
|
|
|
2019-07-10 13:23:29 +00:00
|
|
|
closer, err := fslock.Lock(fsr.path, fsLock)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("could not lock the repo: %w", err)
|
|
|
|
}
|
|
|
|
return &fsLockedRepo{
|
2019-10-30 16:38:39 +00:00
|
|
|
path: fsr.path,
|
|
|
|
repoType: repoType,
|
|
|
|
closer: closer,
|
2019-07-10 13:23:29 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type fsLockedRepo struct {
|
2019-10-30 16:38:39 +00:00
|
|
|
path string
|
|
|
|
repoType RepoType
|
|
|
|
closer io.Closer
|
2019-07-10 17:09:57 +00:00
|
|
|
|
2019-07-10 17:28:49 +00:00
|
|
|
ds datastore.Batching
|
|
|
|
dsErr error
|
2019-07-10 17:09:57 +00:00
|
|
|
dsOnce sync.Once
|
2019-07-10 13:23:29 +00:00
|
|
|
}
|
|
|
|
|
2019-07-12 09:59:18 +00:00
|
|
|
func (fsr *fsLockedRepo) Path() string {
|
|
|
|
return fsr.path
|
|
|
|
}
|
|
|
|
|
2019-07-10 13:23:29 +00:00
|
|
|
func (fsr *fsLockedRepo) Close() error {
|
2019-07-10 15:10:45 +00:00
|
|
|
err := os.Remove(fsr.join(fsAPI))
|
|
|
|
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
return xerrors.Errorf("could not remove API file: %w", err)
|
|
|
|
}
|
2019-09-17 14:34:22 +00:00
|
|
|
if fsr.ds != nil {
|
|
|
|
if err := fsr.ds.Close(); err != nil {
|
|
|
|
return xerrors.Errorf("could not close datastore: %w", err)
|
|
|
|
}
|
|
|
|
}
|
2019-07-10 15:10:45 +00:00
|
|
|
|
|
|
|
err = fsr.closer.Close()
|
2019-07-10 13:23:29 +00:00
|
|
|
fsr.closer = nil
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// join joins path elements with fsr.path
|
|
|
|
func (fsr *fsLockedRepo) join(paths ...string) string {
|
|
|
|
return filepath.Join(append([]string{fsr.path}, paths...)...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fsr *fsLockedRepo) stillValid() error {
|
|
|
|
if fsr.closer == nil {
|
|
|
|
return ErrClosedRepo
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-10 17:09:57 +00:00
|
|
|
func (fsr *fsLockedRepo) Datastore(ns string) (datastore.Batching, error) {
|
|
|
|
fsr.dsOnce.Do(func() {
|
2019-10-29 18:04:35 +00:00
|
|
|
opts := badger.DefaultOptions
|
|
|
|
opts.Truncate = true
|
|
|
|
|
|
|
|
fsr.ds, fsr.dsErr = badger.NewDatastore(fsr.join(fsDatastore), &opts)
|
2019-07-12 10:17:44 +00:00
|
|
|
/*if fsr.dsErr == nil {
|
|
|
|
fsr.ds = datastore.NewLogDatastore(fsr.ds, "fsrepo")
|
|
|
|
}*/
|
2019-07-10 17:09:57 +00:00
|
|
|
})
|
2019-07-11 11:48:21 +00:00
|
|
|
if fsr.dsErr != nil {
|
|
|
|
return nil, fsr.dsErr
|
|
|
|
}
|
|
|
|
return namespace.Wrap(fsr.ds, datastore.NewKey(ns)), nil
|
2019-07-10 13:23:29 +00:00
|
|
|
}
|
|
|
|
|
2019-10-30 16:38:39 +00:00
|
|
|
func (fsr *fsLockedRepo) Config() (interface{}, error) {
|
2019-07-10 13:23:29 +00:00
|
|
|
if err := fsr.stillValid(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-30 16:38:39 +00:00
|
|
|
return config.FromFile(fsr.join(fsConfig), defConfForType(fsr.repoType))
|
2019-07-10 13:23:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (fsr *fsLockedRepo) SetAPIEndpoint(ma multiaddr.Multiaddr) error {
|
|
|
|
if err := fsr.stillValid(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-11 11:52:07 +00:00
|
|
|
return ioutil.WriteFile(fsr.join(fsAPI), []byte(ma.String()), 0644)
|
2019-07-10 13:23:29 +00:00
|
|
|
}
|
|
|
|
|
2019-07-23 18:49:09 +00:00
|
|
|
func (fsr *fsLockedRepo) SetAPIToken(token []byte) error {
|
|
|
|
if err := fsr.stillValid(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return ioutil.WriteFile(fsr.join(fsAPIToken), token, 0600)
|
|
|
|
}
|
|
|
|
|
2019-07-18 14:57:49 +00:00
|
|
|
func (fsr *fsLockedRepo) KeyStore() (types.KeyStore, error) {
|
2019-07-18 14:01:39 +00:00
|
|
|
if err := fsr.stillValid(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return fsr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var kstrPermissionMsg = "permissions of key: '%s' are too relaxed, " +
|
|
|
|
"required: 0600, got: %#o"
|
|
|
|
|
|
|
|
// List lists all the keys stored in the KeyStore
|
|
|
|
func (fsr *fsLockedRepo) List() ([]string, error) {
|
|
|
|
if err := fsr.stillValid(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
kstorePath := fsr.join(fsKeystore)
|
|
|
|
dir, err := os.Open(kstorePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("opening dir to list keystore: %w", err)
|
|
|
|
}
|
|
|
|
files, err := dir.Readdir(-1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("reading keystore dir: %w", err)
|
|
|
|
}
|
|
|
|
keys := make([]string, 0, len(files))
|
|
|
|
for _, f := range files {
|
|
|
|
if f.Mode()&0077 != 0 {
|
|
|
|
return nil, xerrors.Errorf(kstrPermissionMsg, f.Name(), f.Mode())
|
|
|
|
}
|
|
|
|
name, err := base32.RawStdEncoding.DecodeString(f.Name())
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("decoding key: '%s': %w", f.Name(), err)
|
|
|
|
}
|
|
|
|
keys = append(keys, string(name))
|
|
|
|
}
|
|
|
|
return keys, nil
|
|
|
|
}
|
|
|
|
|
2019-07-18 14:57:49 +00:00
|
|
|
// Get gets a key out of keystore and returns types.KeyInfo coresponding to named key
|
|
|
|
func (fsr *fsLockedRepo) Get(name string) (types.KeyInfo, error) {
|
2019-07-18 14:01:39 +00:00
|
|
|
if err := fsr.stillValid(); err != nil {
|
2019-07-18 14:57:49 +00:00
|
|
|
return types.KeyInfo{}, err
|
2019-07-18 14:01:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
encName := base32.RawStdEncoding.EncodeToString([]byte(name))
|
|
|
|
keyPath := fsr.join(fsKeystore, encName)
|
|
|
|
|
|
|
|
fstat, err := os.Stat(keyPath)
|
|
|
|
if os.IsNotExist(err) {
|
2019-10-17 10:18:40 +00:00
|
|
|
return types.KeyInfo{}, xerrors.Errorf("opening key '%s': %w", name, types.ErrKeyInfoNotFound)
|
2019-07-18 14:01:39 +00:00
|
|
|
} else if err != nil {
|
2019-07-18 14:57:49 +00:00
|
|
|
return types.KeyInfo{}, xerrors.Errorf("opening key '%s': %w", name, err)
|
2019-07-18 14:01:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if fstat.Mode()&0077 != 0 {
|
2019-07-18 14:57:49 +00:00
|
|
|
return types.KeyInfo{}, xerrors.Errorf(kstrPermissionMsg, name, err)
|
2019-07-18 14:01:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
file, err := os.Open(keyPath)
|
|
|
|
if err != nil {
|
2019-07-18 14:57:49 +00:00
|
|
|
return types.KeyInfo{}, xerrors.Errorf("opening key '%s': %w", name, err)
|
2019-07-18 14:01:39 +00:00
|
|
|
}
|
|
|
|
defer file.Close() //nolint: errcheck // read only op
|
|
|
|
|
|
|
|
data, err := ioutil.ReadAll(file)
|
|
|
|
if err != nil {
|
2019-07-18 14:57:49 +00:00
|
|
|
return types.KeyInfo{}, xerrors.Errorf("reading key '%s': %w", name, err)
|
2019-07-18 14:01:39 +00:00
|
|
|
}
|
|
|
|
|
2019-07-18 14:57:49 +00:00
|
|
|
var res types.KeyInfo
|
2019-07-18 14:01:39 +00:00
|
|
|
err = json.Unmarshal(data, &res)
|
|
|
|
if err != nil {
|
2019-07-18 14:57:49 +00:00
|
|
|
return types.KeyInfo{}, xerrors.Errorf("decoding key '%s': %w", name, err)
|
2019-07-18 14:01:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put saves key info under given name
|
2019-07-18 14:57:49 +00:00
|
|
|
func (fsr *fsLockedRepo) Put(name string, info types.KeyInfo) error {
|
2019-07-18 14:01:39 +00:00
|
|
|
if err := fsr.stillValid(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
encName := base32.RawStdEncoding.EncodeToString([]byte(name))
|
|
|
|
keyPath := fsr.join(fsKeystore, encName)
|
|
|
|
|
|
|
|
_, err := os.Stat(keyPath)
|
|
|
|
if err == nil {
|
2019-10-18 11:39:31 +00:00
|
|
|
return xerrors.Errorf("checking key before put '%s': %w", name, types.ErrKeyExists)
|
2019-07-18 14:01:39 +00:00
|
|
|
} else if !os.IsNotExist(err) {
|
|
|
|
return xerrors.Errorf("checking key before put '%s': %w", name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
keyData, err := json.Marshal(info)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("encoding key '%s': %w", name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(keyPath, keyData, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("writing key '%s': %w", name, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fsr *fsLockedRepo) Delete(name string) error {
|
|
|
|
if err := fsr.stillValid(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
encName := base32.RawStdEncoding.EncodeToString([]byte(name))
|
|
|
|
keyPath := fsr.join(fsKeystore, encName)
|
|
|
|
|
|
|
|
_, err := os.Stat(keyPath)
|
|
|
|
if os.IsNotExist(err) {
|
2019-10-17 10:18:40 +00:00
|
|
|
return xerrors.Errorf("checking key before delete '%s': %w", name, types.ErrKeyInfoNotFound)
|
2019-07-18 14:01:39 +00:00
|
|
|
} else if err != nil {
|
|
|
|
return xerrors.Errorf("checking key before delete '%s': %w", name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.Remove(keyPath)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("deleting key '%s': %w", name, err)
|
|
|
|
}
|
|
|
|
return nil
|
2019-07-10 13:23:29 +00:00
|
|
|
}
|