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
|
|
|
|
2021-07-28 16:19:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/util"
|
2021-11-10 07:08:33 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
2022-01-26 12:11:13 +00:00
|
|
|
addressSDK "github.com/nspcc-dev/neofs-sdk-go/object/address"
|
|
|
|
oidSDK "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2021-04-08 13:53:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// FSTree represents object storage as filesystem tree.
|
|
|
|
type FSTree struct {
|
|
|
|
Info
|
|
|
|
|
|
|
|
Depth int
|
|
|
|
DirNameLen int
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
)
|
|
|
|
|
|
|
|
// ErrFileNotFound is returned when file is missing.
|
|
|
|
var ErrFileNotFound = errors.New("file not found")
|
|
|
|
|
2022-01-26 12:11:13 +00:00
|
|
|
func stringifyAddress(addr *addressSDK.Address) string {
|
2021-05-19 14:53:51 +00:00
|
|
|
return addr.ObjectID().String() + "." + addr.ContainerID().String()
|
|
|
|
}
|
|
|
|
|
2022-01-26 12:11:13 +00:00
|
|
|
func addressFromString(s string) (*addressSDK.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-01-26 12:11:13 +00:00
|
|
|
oid := oidSDK.NewID()
|
2021-05-19 14:53:51 +00:00
|
|
|
if err := oid.Parse(ss[0]); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-05-31 11:03:17 +00:00
|
|
|
id := cid.New()
|
|
|
|
if err := id.Parse(ss[1]); err != nil {
|
2021-05-19 14:53:51 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-01-26 12:11:13 +00:00
|
|
|
addr := addressSDK.NewAddress()
|
2021-05-19 14:53:51 +00:00
|
|
|
addr.SetObjectID(oid)
|
2021-05-31 11:03:17 +00:00
|
|
|
addr.SetContainerID(id)
|
2021-05-19 14:53:51 +00:00
|
|
|
|
|
|
|
return addr, nil
|
2021-04-08 13:53:25 +00:00
|
|
|
}
|
|
|
|
|
2022-01-20 15:53:29 +00:00
|
|
|
// IterationPrm contains iteraction parameters.
|
|
|
|
type IterationPrm struct {
|
2022-01-26 12:11:13 +00:00
|
|
|
handler func(addr *addressSDK.Address, data []byte) error
|
2022-01-20 15:53:29 +00:00
|
|
|
ignoreErrors bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithHandler sets a function to call on each object.
|
2022-01-26 12:11:13 +00:00
|
|
|
func (p *IterationPrm) WithHandler(f func(addr *addressSDK.Address, data []byte) error) *IterationPrm {
|
2022-01-20 15:53:29 +00:00
|
|
|
p.handler = f
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithIgnoreErrors sets a flag indicating whether errors should be ignored.
|
|
|
|
func (p *IterationPrm) WithIgnoreErrors(ignore bool) *IterationPrm {
|
|
|
|
p.ignoreErrors = ignore
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2021-04-06 10:56:06 +00:00
|
|
|
// Iterate iterates over all stored objects.
|
2022-01-20 15:53:29 +00:00
|
|
|
func (t *FSTree) Iterate(prm *IterationPrm) error {
|
|
|
|
return t.iterate(0, []string{t.RootPath}, prm)
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
|
|
|
|
2022-01-20 15:53:29 +00:00
|
|
|
func (t *FSTree) iterate(depth int, curPath []string, prm *IterationPrm) 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-01-20 15:53:29 +00:00
|
|
|
if prm.ignoreErrors {
|
|
|
|
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-02-02 13:28:08 +00:00
|
|
|
data, err := os.ReadFile(filepath.Join(curPath...))
|
2021-04-06 10:56:06 +00:00
|
|
|
if err != nil {
|
2022-01-20 15:53:29 +00:00
|
|
|
if prm.ignoreErrors {
|
|
|
|
continue
|
|
|
|
}
|
2021-04-06 10:56:06 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-20 15:53:29 +00:00
|
|
|
if err := prm.handler(addr, data); err != nil {
|
|
|
|
// Error occurred in handler, outside of our scope, needs to be reported.
|
2021-04-06 10:56:06 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-26 12:11:13 +00:00
|
|
|
func (t *FSTree) treePath(addr *addressSDK.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
|
|
|
}
|
|
|
|
|
|
|
|
// Delete removes object with the specified address from storage.
|
2022-01-26 12:11:13 +00:00
|
|
|
func (t *FSTree) Delete(addr *addressSDK.Address) error {
|
2021-04-08 13:53:25 +00:00
|
|
|
p, err := t.Exists(addr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return os.Remove(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exists returns path to file with object contents if it exists in storage
|
|
|
|
// and an error otherwise.
|
2022-01-26 12:11:13 +00:00
|
|
|
func (t *FSTree) Exists(addr *addressSDK.Address) (string, error) {
|
2021-04-08 13:53:25 +00:00
|
|
|
p := t.treePath(addr)
|
|
|
|
|
|
|
|
_, err := os.Stat(p)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
err = ErrFileNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return p, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put puts object in storage.
|
2022-01-26 12:11:13 +00:00
|
|
|
func (t *FSTree) Put(addr *addressSDK.Address, data []byte) error {
|
2021-04-08 13:53:25 +00:00
|
|
|
p := t.treePath(addr)
|
|
|
|
|
2022-02-02 13:28:08 +00:00
|
|
|
if err := util.MkdirAllX(filepath.Dir(p), t.Permissions); err != nil {
|
2021-04-08 13:53:25 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-28 14:01:31 +00:00
|
|
|
return os.WriteFile(p, data, t.Permissions)
|
2021-04-08 13:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns object from storage by address.
|
2022-01-26 12:11:13 +00:00
|
|
|
func (t *FSTree) Get(addr *addressSDK.Address) ([]byte, error) {
|
2021-04-08 13:53:25 +00:00
|
|
|
p := t.treePath(addr)
|
|
|
|
|
|
|
|
if _, err := os.Stat(p); os.IsNotExist(err) {
|
|
|
|
return nil, ErrFileNotFound
|
|
|
|
}
|
|
|
|
|
2021-06-28 14:01:31 +00:00
|
|
|
return os.ReadFile(p)
|
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
|
|
|
|
}
|