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"
|
2022-09-28 08:10:27 +00:00
|
|
|
"syscall"
|
2021-04-08 13:53:25 +00:00
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/common"
|
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/compression"
|
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/local_object_storage/util/logicerr"
|
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/util"
|
|
|
|
apistatus "github.com/TrueCloudLab/frostfs-sdk-go/client/status"
|
|
|
|
cid "github.com/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
objectSDK "github.com/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
oid "github.com/TrueCloudLab/frostfs-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
|
2022-09-26 16:38:03 +00:00
|
|
|
Depth uint64
|
2021-04-08 13:53:25 +00:00
|
|
|
DirNameLen int
|
2022-08-23 13:04:01 +00:00
|
|
|
|
2022-10-28 09:57:36 +00:00
|
|
|
noSync bool
|
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-09-26 16:38:03 +00:00
|
|
|
func (t *FSTree) iterate(depth uint64, 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)
|
|
|
|
|
2022-09-26 16:38:03 +00:00
|
|
|
for i := 0; uint64(i) < t.Depth; i++ {
|
2021-04-08 13:53:25 +00:00
|
|
|
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) {
|
2022-10-26 12:23:12 +00:00
|
|
|
err = logicerr.Wrap(apistatus.ObjectNotFound{})
|
2022-07-08 07:09:48 +00:00
|
|
|
}
|
|
|
|
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) {
|
2022-10-26 12:23:12 +00:00
|
|
|
err = logicerr.Wrap(apistatus.ObjectNotFound{})
|
2022-08-23 12:44:51 +00:00
|
|
|
}
|
|
|
|
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-09-28 08:10:27 +00:00
|
|
|
|
2023-01-20 12:57:09 +00:00
|
|
|
tmpPath := p + "#"
|
|
|
|
err := t.writeFile(tmpPath, prm.RawData)
|
2022-09-28 08:10:27 +00:00
|
|
|
if err != nil {
|
|
|
|
var pe *fs.PathError
|
|
|
|
if errors.As(err, &pe) && pe.Err == syscall.ENOSPC {
|
|
|
|
err = common.ErrNoSpace
|
2023-01-20 12:57:09 +00:00
|
|
|
_ = os.RemoveAll(tmpPath)
|
2022-09-28 08:10:27 +00:00
|
|
|
}
|
2023-01-31 15:27:58 +00:00
|
|
|
} else {
|
|
|
|
err = os.Rename(tmpPath, p)
|
2022-09-28 08:10:27 +00:00
|
|
|
}
|
2022-10-28 09:57:36 +00:00
|
|
|
|
2023-01-31 15:27:58 +00:00
|
|
|
return common.PutRes{StorageID: []byte{}}, err
|
2021-04-08 13:53:25 +00:00
|
|
|
}
|
|
|
|
|
2022-10-28 09:57:36 +00:00
|
|
|
func (t *FSTree) writeFlags() int {
|
|
|
|
flags := os.O_WRONLY | os.O_CREATE | os.O_TRUNC
|
|
|
|
if t.noSync {
|
|
|
|
return flags
|
|
|
|
}
|
|
|
|
return flags | os.O_SYNC
|
|
|
|
}
|
|
|
|
|
|
|
|
// writeFile writes data to a file with path p.
|
|
|
|
// The code is copied from `os.WriteFile` with minor corrections for flags.
|
|
|
|
func (t *FSTree) writeFile(p string, data []byte) error {
|
|
|
|
f, err := os.OpenFile(p, t.writeFlags(), t.Permissions)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = f.Write(data)
|
|
|
|
if err1 := f.Close(); err1 != nil && err == nil {
|
|
|
|
err = err1
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-10-28 09:57:36 +00:00
|
|
|
f, err := os.OpenFile(p, t.writeFlags(), t.Permissions)
|
2022-04-28 10:19:03 +00:00
|
|
|
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-10-26 12:23:12 +00:00
|
|
|
return common.GetRes{}, logicerr.Wrap(apistatus.ObjectNotFound{})
|
2022-07-08 07:09:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2022-10-26 12:23:12 +00:00
|
|
|
return common.GetRangeRes{}, logicerr.Wrap(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
|
|
|
|
2022-09-20 12:42:56 +00:00
|
|
|
// Type is fstree storage type used in logs and configuration.
|
|
|
|
const Type = "fstree"
|
|
|
|
|
2022-07-08 11:33:49 +00:00
|
|
|
// Type implements common.Storage.
|
|
|
|
func (*FSTree) Type() string {
|
2022-09-20 12:42:56 +00:00
|
|
|
return Type
|
2022-07-08 11:33:49 +00:00
|
|
|
}
|
2022-07-11 12:34:17 +00:00
|
|
|
|
2022-10-05 12:11:12 +00:00
|
|
|
// Path implements common.Storage.
|
|
|
|
func (t *FSTree) Path() string {
|
|
|
|
return t.RootPath
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2022-11-09 10:59:24 +00:00
|
|
|
|
|
|
|
// SetReportErrorFunc implements common.Storage.
|
|
|
|
func (t *FSTree) SetReportErrorFunc(f func(string, error)) {
|
|
|
|
// Do nothing, FSTree can encounter only one error which is returned.
|
|
|
|
}
|