lotus/node/config/storage.go

68 lines
1.2 KiB
Go
Raw Normal View History

2020-03-03 22:19:22 +00:00
package config
import (
"encoding/json"
"io"
"io/ioutil"
"os"
"golang.org/x/xerrors"
)
type LocalPath struct {
Path string
}
// .lotusstorage/storage.json
type StorageConfig struct {
StoragePaths []LocalPath
}
// [path]/metadata.json
type StorageMeta struct {
2020-03-05 19:21:06 +00:00
ID string
Weight uint64 // 0 = readonly
2020-03-03 22:19:22 +00:00
CanSeal bool
2020-03-03 22:19:22 +00:00
CanStore bool
}
func StorageFromFile(path string, def *StorageConfig) (*StorageConfig, error) {
file, err := os.Open(path)
switch {
case os.IsNotExist(err):
if def == nil {
return nil, xerrors.Errorf("couldn't load storage config: %w", err)
}
return def, nil
case err != nil:
return nil, err
}
defer file.Close() //nolint:errcheck // The file is RO
return StorageFromReader(file)
2020-03-03 22:19:22 +00:00
}
func StorageFromReader(reader io.Reader) (*StorageConfig, error) {
var cfg StorageConfig
2020-03-03 22:19:22 +00:00
err := json.NewDecoder(reader).Decode(&cfg)
if err != nil {
return nil, err
}
return &cfg, nil
}
func WriteStorageFile(path string, config StorageConfig) error {
b, err := json.MarshalIndent(config, "", " ")
2020-03-03 22:19:22 +00:00
if err != nil {
return xerrors.Errorf("marshaling storage config: %w", err)
}
if err := ioutil.WriteFile(path, b, 0644); err != nil {
return xerrors.Errorf("persisting storage config (%s): %w", path, err)
}
return nil
}