2021-04-08 13:53:25 +00:00
|
|
|
package fstree
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
|
|
|
"errors"
|
2021-10-19 16:20:53 +00:00
|
|
|
"fmt"
|
2021-06-28 14:01:31 +00:00
|
|
|
"io/fs"
|
2021-04-08 13:53:25 +00:00
|
|
|
"os"
|
2021-10-19 16:20:53 +00:00
|
|
|
"path/filepath"
|
2021-04-06 10:56:06 +00:00
|
|
|
"strings"
|
2021-04-08 13:53:25 +00:00
|
|
|
|
2022-07-05 14:07:40 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor/common"
|
2022-07-08 07:09:48 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor/compression"
|
2021-07-28 16:19:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/util"
|
2022-07-08 07:09:48 +00:00
|
|
|
apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status"
|
2021-11-10 07:08:33 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
2022-07-08 07:09:48 +00:00
|
|
|
objectSDK "github.com/nspcc-dev/neofs-sdk-go/object"
|
2022-05-31 17:00:41 +00:00
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2021-04-08 13:53:25 +00:00
|
|
|
)
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// FSTree represents an object storage as a filesystem tree.
|
2021-04-08 13:53:25 +00:00
|
|
|
type FSTree struct {
|
|
|
|
Info
|
|
|
|
|
2022-08-19 14:29:53 +00:00
|
|
|
*compression.Config
|
2021-04-08 13:53:25 +00:00
|
|
|
Depth int
|
|
|
|
DirNameLen int
|
2022-08-23 13:04:01 +00:00
|
|
|
|
|
|
|
readOnly bool
|
2021-04-08 13:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Info groups the information about file storage.
|
|
|
|
type Info struct {
|
|
|
|
// Permission bits of the root directory.
|
2021-06-28 14:01:31 +00:00
|
|
|
Permissions fs.FileMode
|
2021-04-08 13:53:25 +00:00
|
|
|
|
|
|
|
// Full path to the root directory.
|
|
|
|
RootPath string
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
// DirNameLen is how many bytes is used to group keys into directories.
|
|
|
|
DirNameLen = 1 // in bytes
|
|
|
|
// MaxDepth is maximum depth of nested directories.
|
|
|
|
MaxDepth = (sha256.Size - 1) / DirNameLen
|
|
|
|
)
|
|
|
|
|
2022-07-08 07:09:48 +00:00
|
|
|
var _ common.Storage = (*FSTree)(nil)
|
2021-04-08 13:53:25 +00:00
|
|
|
|
2022-07-11 12:34:17 +00:00
|
|
|
func New(opts ...Option) *FSTree {
|
|
|
|
f := &FSTree{
|
|
|
|
Info: Info{
|
|
|
|
Permissions: 0700,
|
|
|
|
RootPath: "./",
|
|
|
|
},
|
2022-08-19 14:29:53 +00:00
|
|
|
Config: nil,
|
2022-07-11 12:34:17 +00:00
|
|
|
Depth: 4,
|
|
|
|
DirNameLen: DirNameLen,
|
|
|
|
}
|
|
|
|
for i := range opts {
|
|
|
|
opts[i](f)
|
|
|
|
}
|
|
|
|
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func stringifyAddress(addr oid.Address) string {
|
|
|
|
return addr.Object().EncodeToString() + "." + addr.Container().EncodeToString()
|
2021-05-19 14:53:51 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func addressFromString(s string) (*oid.Address, error) {
|
2021-05-19 14:53:51 +00:00
|
|
|
ss := strings.SplitN(s, ".", 2)
|
|
|
|
if len(ss) != 2 {
|
|
|
|
return nil, errors.New("invalid address")
|
|
|
|
}
|
2021-04-08 13:53:25 +00:00
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
var obj oid.ID
|
|
|
|
if err := obj.DecodeString(ss[0]); err != nil {
|
2021-05-19 14:53:51 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
var cnr cid.ID
|
|
|
|
if err := cnr.DecodeString(ss[1]); err != nil {
|
2021-05-19 14:53:51 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
var addr oid.Address
|
|
|
|
addr.SetObject(obj)
|
|
|
|
addr.SetContainer(cnr)
|
2021-05-19 14:53:51 +00:00
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
return &addr, nil
|
2021-04-08 13:53:25 +00:00
|
|
|
}
|
|
|
|
|
2021-04-06 10:56:06 +00:00
|
|
|
// Iterate iterates over all stored objects.
|
2022-07-07 12:03:45 +00:00
|
|
|
func (t *FSTree) Iterate(prm common.IteratePrm) (common.IterateRes, error) {
|
|
|
|
return common.IterateRes{}, t.iterate(0, []string{t.RootPath}, prm)
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
|
|
|
|
2022-07-07 12:03:45 +00:00
|
|
|
func (t *FSTree) iterate(depth int, curPath []string, prm common.IteratePrm) error {
|
2021-04-06 10:56:06 +00:00
|
|
|
curName := strings.Join(curPath[1:], "")
|
2022-02-02 13:28:08 +00:00
|
|
|
des, err := os.ReadDir(filepath.Join(curPath...))
|
2021-04-06 10:56:06 +00:00
|
|
|
if err != nil {
|
2022-07-07 12:03:45 +00:00
|
|
|
if prm.IgnoreErrors {
|
2022-01-20 15:53:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-04-06 10:56:06 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
isLast := depth >= t.Depth
|
|
|
|
l := len(curPath)
|
|
|
|
curPath = append(curPath, "")
|
|
|
|
|
|
|
|
for i := range des {
|
|
|
|
curPath[l] = des[i].Name()
|
|
|
|
|
|
|
|
if !isLast && des[i].IsDir() {
|
2022-01-20 15:53:29 +00:00
|
|
|
err := t.iterate(depth+1, curPath, prm)
|
2021-04-06 10:56:06 +00:00
|
|
|
if err != nil {
|
2022-01-20 15:53:29 +00:00
|
|
|
// Must be error from handler in case errors are ignored.
|
|
|
|
// Need to report.
|
2021-04-06 10:56:06 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-19 14:53:51 +00:00
|
|
|
if depth != t.Depth {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
addr, err := addressFromString(curName + des[i].Name())
|
2021-04-06 10:56:06 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-07-07 12:03:45 +00:00
|
|
|
if prm.LazyHandler != nil {
|
|
|
|
err = prm.LazyHandler(*addr, func() ([]byte, error) {
|
2022-05-31 14:11:48 +00:00
|
|
|
return os.ReadFile(filepath.Join(curPath...))
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
var data []byte
|
|
|
|
data, err = os.ReadFile(filepath.Join(curPath...))
|
2022-07-08 07:09:48 +00:00
|
|
|
if err == nil {
|
|
|
|
data, err = t.Decompress(data)
|
|
|
|
}
|
2022-05-31 14:11:48 +00:00
|
|
|
if err != nil {
|
2022-07-07 12:03:45 +00:00
|
|
|
if prm.IgnoreErrors {
|
|
|
|
if prm.ErrorHandler != nil {
|
|
|
|
return prm.ErrorHandler(*addr, err)
|
2022-06-21 07:52:03 +00:00
|
|
|
}
|
2022-05-31 14:11:48 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
return err
|
2022-01-20 15:53:29 +00:00
|
|
|
}
|
2022-07-07 12:03:45 +00:00
|
|
|
|
|
|
|
err = prm.Handler(common.IterationElement{
|
|
|
|
Address: *addr,
|
|
|
|
ObjectData: data,
|
|
|
|
StorageID: []byte{},
|
|
|
|
})
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 14:11:48 +00:00
|
|
|
if err != nil {
|
2021-04-06 10:56:06 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func (t *FSTree) treePath(addr oid.Address) string {
|
2021-04-08 13:53:25 +00:00
|
|
|
sAddr := stringifyAddress(addr)
|
|
|
|
|
|
|
|
dirs := make([]string, 0, t.Depth+1+1) // 1 for root, 1 for file
|
|
|
|
dirs = append(dirs, t.RootPath)
|
|
|
|
|
|
|
|
for i := 0; i < t.Depth; i++ {
|
|
|
|
dirs = append(dirs, sAddr[:t.DirNameLen])
|
|
|
|
sAddr = sAddr[t.DirNameLen:]
|
|
|
|
}
|
|
|
|
|
|
|
|
dirs = append(dirs, sAddr)
|
|
|
|
|
2022-02-02 13:28:08 +00:00
|
|
|
return filepath.Join(dirs...)
|
2021-04-08 13:53:25 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// Delete removes the object with the specified address from the storage.
|
2022-07-08 07:09:48 +00:00
|
|
|
func (t *FSTree) Delete(prm common.DeletePrm) (common.DeleteRes, error) {
|
2022-08-23 13:04:01 +00:00
|
|
|
if t.readOnly {
|
|
|
|
return common.DeleteRes{}, common.ErrReadOnly
|
|
|
|
}
|
|
|
|
|
2022-07-08 07:09:48 +00:00
|
|
|
p, err := t.getPath(prm.Address)
|
2021-04-08 13:53:25 +00:00
|
|
|
if err != nil {
|
2022-07-08 07:09:48 +00:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
var errNotFound apistatus.ObjectNotFound
|
|
|
|
err = errNotFound
|
|
|
|
}
|
|
|
|
return common.DeleteRes{}, err
|
2021-04-08 13:53:25 +00:00
|
|
|
}
|
|
|
|
|
2022-08-23 12:44:51 +00:00
|
|
|
err = os.Remove(p)
|
|
|
|
if err != nil && os.IsNotExist(err) {
|
|
|
|
err = apistatus.ObjectNotFound{}
|
|
|
|
}
|
|
|
|
return common.DeleteRes{}, err
|
2021-04-08 13:53:25 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// Exists returns the path to the file with object contents if it exists in the storage
|
2021-04-08 13:53:25 +00:00
|
|
|
// and an error otherwise.
|
2022-07-08 07:09:48 +00:00
|
|
|
func (t *FSTree) Exists(prm common.ExistsPrm) (common.ExistsRes, error) {
|
|
|
|
_, err := t.getPath(prm.Address)
|
|
|
|
found := err == nil
|
2021-04-08 13:53:25 +00:00
|
|
|
if os.IsNotExist(err) {
|
2022-07-08 07:09:48 +00:00
|
|
|
err = nil
|
2021-04-08 13:53:25 +00:00
|
|
|
}
|
2022-07-08 07:09:48 +00:00
|
|
|
return common.ExistsRes{Exists: found}, err
|
|
|
|
}
|
2021-04-08 13:53:25 +00:00
|
|
|
|
2022-07-08 07:09:48 +00:00
|
|
|
func (t *FSTree) getPath(addr oid.Address) (string, error) {
|
|
|
|
p := t.treePath(addr)
|
|
|
|
|
|
|
|
_, err := os.Stat(p)
|
2021-04-08 13:53:25 +00:00
|
|
|
return p, err
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// Put puts an object in the storage.
|
2022-07-06 13:41:35 +00:00
|
|
|
func (t *FSTree) Put(prm common.PutPrm) (common.PutRes, error) {
|
2022-08-23 13:04:01 +00:00
|
|
|
if t.readOnly {
|
|
|
|
return common.PutRes{}, common.ErrReadOnly
|
|
|
|
}
|
|
|
|
|
2022-07-06 13:41:35 +00:00
|
|
|
p := t.treePath(prm.Address)
|
2021-04-08 13:53:25 +00:00
|
|
|
|
2022-02-02 13:28:08 +00:00
|
|
|
if err := util.MkdirAllX(filepath.Dir(p), t.Permissions); err != nil {
|
2022-07-06 13:41:35 +00:00
|
|
|
return common.PutRes{}, err
|
2021-04-08 13:53:25 +00:00
|
|
|
}
|
2022-07-08 07:09:48 +00:00
|
|
|
if !prm.DontCompress {
|
|
|
|
prm.RawData = t.Compress(prm.RawData)
|
|
|
|
}
|
2022-08-22 14:16:35 +00:00
|
|
|
return common.PutRes{StorageID: []byte{}}, os.WriteFile(p, prm.RawData, t.Permissions)
|
2021-04-08 13:53:25 +00:00
|
|
|
}
|
|
|
|
|
2022-04-28 10:19:03 +00:00
|
|
|
// PutStream puts executes handler on a file opened for write.
|
2022-05-31 17:00:41 +00:00
|
|
|
func (t *FSTree) PutStream(addr oid.Address, handler func(*os.File) error) error {
|
2022-08-23 13:04:01 +00:00
|
|
|
if t.readOnly {
|
|
|
|
return common.ErrReadOnly
|
|
|
|
}
|
|
|
|
|
2022-04-28 10:19:03 +00:00
|
|
|
p := t.treePath(addr)
|
|
|
|
|
|
|
|
if err := util.MkdirAllX(filepath.Dir(p), t.Permissions); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.OpenFile(p, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, t.Permissions)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
return handler(f)
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// Get returns an object from the storage by address.
|
2022-07-08 07:09:48 +00:00
|
|
|
func (t *FSTree) Get(prm common.GetPrm) (common.GetRes, error) {
|
2022-07-05 14:07:40 +00:00
|
|
|
p := t.treePath(prm.Address)
|
2021-04-08 13:53:25 +00:00
|
|
|
|
|
|
|
if _, err := os.Stat(p); os.IsNotExist(err) {
|
2022-07-08 07:09:48 +00:00
|
|
|
var errNotFound apistatus.ObjectNotFound
|
|
|
|
return common.GetRes{}, errNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := os.ReadFile(p)
|
|
|
|
if err != nil {
|
|
|
|
return common.GetRes{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err = t.Decompress(data)
|
|
|
|
if err != nil {
|
|
|
|
return common.GetRes{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
obj := objectSDK.New()
|
|
|
|
if err := obj.Unmarshal(data); err != nil {
|
|
|
|
return common.GetRes{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return common.GetRes{Object: obj, RawData: data}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRange implements common.Storage.
|
|
|
|
func (t *FSTree) GetRange(prm common.GetRangePrm) (common.GetRangeRes, error) {
|
|
|
|
res, err := t.Get(common.GetPrm{Address: prm.Address})
|
|
|
|
if err != nil {
|
|
|
|
return common.GetRangeRes{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
payload := res.Object.Payload()
|
|
|
|
from := prm.Range.GetOffset()
|
|
|
|
to := from + prm.Range.GetLength()
|
|
|
|
|
|
|
|
if pLen := uint64(len(payload)); to < from || pLen < from || pLen < to {
|
|
|
|
return common.GetRangeRes{}, apistatus.ObjectOutOfRange{}
|
2021-04-08 13:53:25 +00:00
|
|
|
}
|
|
|
|
|
2022-07-08 07:09:48 +00:00
|
|
|
return common.GetRangeRes{
|
|
|
|
Data: payload[from:to],
|
|
|
|
}, nil
|
2021-04-08 13:53:25 +00:00
|
|
|
}
|
2021-10-19 16:20:53 +00:00
|
|
|
|
|
|
|
// NumberOfObjects walks the file tree rooted at FSTree's root
|
|
|
|
// and returns number of stored objects.
|
|
|
|
func (t *FSTree) NumberOfObjects() (uint64, error) {
|
|
|
|
var counter uint64
|
|
|
|
|
|
|
|
// it is simpler to just consider every file
|
|
|
|
// that is not directory as an object
|
|
|
|
err := filepath.WalkDir(t.RootPath,
|
|
|
|
func(_ string, d fs.DirEntry, _ error) error {
|
|
|
|
if !d.IsDir() {
|
|
|
|
counter++
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("could not walk through %s directory: %w", t.RootPath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return counter, nil
|
|
|
|
}
|
2022-07-08 11:33:49 +00:00
|
|
|
|
|
|
|
// Type implements common.Storage.
|
|
|
|
func (*FSTree) Type() string {
|
|
|
|
return "fstree"
|
|
|
|
}
|
2022-07-11 12:34:17 +00:00
|
|
|
|
|
|
|
// SetCompressor implements common.Storage.
|
2022-08-19 14:29:53 +00:00
|
|
|
func (t *FSTree) SetCompressor(cc *compression.Config) {
|
|
|
|
t.Config = cc
|
2022-07-11 12:34:17 +00:00
|
|
|
}
|