[#970] fstree: Move delete implementation to a separate file
Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
parent
7f692409cf
commit
fb74524ac7
5 changed files with 52 additions and 25 deletions
|
@ -19,7 +19,7 @@ func (t *FSTree) Init() error {
|
|||
if !t.readOnly {
|
||||
f := newSpecificWriteData(t.fileCounter, t.RootPath, t.Permissions, t.noSync)
|
||||
if f != nil {
|
||||
t.writeData = f
|
||||
t.writer = f
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -57,7 +57,7 @@ type FSTree struct {
|
|||
fileCounter FileCounter
|
||||
fileCounterEnabled bool
|
||||
|
||||
writeData func(string, []byte) error
|
||||
writer writer
|
||||
}
|
||||
|
||||
// Info groups the information about file storage.
|
||||
|
@ -95,7 +95,7 @@ func New(opts ...Option) *FSTree {
|
|||
for i := range opts {
|
||||
opts[i](f)
|
||||
}
|
||||
f.writeData = newGenericWriteData(f)
|
||||
f.writer = newGenericWriteData(f)
|
||||
|
||||
return f
|
||||
}
|
||||
|
@ -266,21 +266,7 @@ func (t *FSTree) Delete(ctx context.Context, prm common.DeletePrm) (common.Delet
|
|||
}
|
||||
|
||||
p := t.treePath(prm.Address)
|
||||
|
||||
if t.fileCounterEnabled {
|
||||
t.fileGuard.Lock(p)
|
||||
err = os.Remove(p)
|
||||
t.fileGuard.Unlock(p)
|
||||
if err == nil {
|
||||
t.fileCounter.Dec()
|
||||
}
|
||||
} else {
|
||||
err = os.Remove(p)
|
||||
}
|
||||
|
||||
if err != nil && os.IsNotExist(err) {
|
||||
err = logicerr.Wrap(new(apistatus.ObjectNotFound))
|
||||
}
|
||||
err = t.writer.removeFile(p)
|
||||
return common.DeleteRes{}, err
|
||||
}
|
||||
|
||||
|
@ -357,7 +343,7 @@ func (t *FSTree) Put(ctx context.Context, prm common.PutPrm) (common.PutRes, err
|
|||
}
|
||||
|
||||
size = len(prm.RawData)
|
||||
return common.PutRes{StorageID: []byte{}}, t.writeData(p, prm.RawData)
|
||||
return common.PutRes{StorageID: []byte{}}, t.writer.writeData(p, prm.RawData)
|
||||
}
|
||||
|
||||
// Get returns an object from the storage by address.
|
||||
|
|
|
@ -9,8 +9,15 @@ import (
|
|||
"syscall"
|
||||
|
||||
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/common"
|
||||
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/util/logicerr"
|
||||
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
||||
)
|
||||
|
||||
type writer interface {
|
||||
writeData(string, []byte) error
|
||||
removeFile(string) error
|
||||
}
|
||||
|
||||
type genericWriter struct {
|
||||
perm fs.FileMode
|
||||
flags int
|
||||
|
@ -19,7 +26,7 @@ type genericWriter struct {
|
|||
suffix atomic.Uint64
|
||||
}
|
||||
|
||||
func newGenericWriteData(t *FSTree) func(string, []byte) error {
|
||||
func newGenericWriteData(t *FSTree) writer {
|
||||
flags := os.O_WRONLY | os.O_CREATE | os.O_TRUNC | os.O_EXCL
|
||||
if !t.noSync {
|
||||
flags |= os.O_SYNC
|
||||
|
@ -29,7 +36,7 @@ func newGenericWriteData(t *FSTree) func(string, []byte) error {
|
|||
flags: flags,
|
||||
t: t,
|
||||
}
|
||||
return w.writeData
|
||||
return w
|
||||
}
|
||||
|
||||
func (w *genericWriter) writeData(p string, data []byte) error {
|
||||
|
@ -86,3 +93,22 @@ func (w *genericWriter) writeFile(p string, data []byte) error {
|
|||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (w *genericWriter) removeFile(p string) error {
|
||||
var err error
|
||||
if w.t.fileCounterEnabled {
|
||||
w.t.fileGuard.Lock(p)
|
||||
err = os.Remove(p)
|
||||
w.t.fileGuard.Unlock(p)
|
||||
if err == nil {
|
||||
w.t.fileCounter.Dec()
|
||||
}
|
||||
} else {
|
||||
err = os.Remove(p)
|
||||
}
|
||||
|
||||
if err != nil && os.IsNotExist(err) {
|
||||
err = logicerr.Wrap(new(apistatus.ObjectNotFound))
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -8,6 +8,8 @@ import (
|
|||
"strconv"
|
||||
|
||||
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/common"
|
||||
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/util/logicerr"
|
||||
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
|
@ -19,7 +21,7 @@ type linuxWriter struct {
|
|||
counter FileCounter
|
||||
}
|
||||
|
||||
func newSpecificWriteData(c FileCounter, root string, perm fs.FileMode, noSync bool) func(string, []byte) error {
|
||||
func newSpecificWriteData(c FileCounter, root string, perm fs.FileMode, noSync bool) writer {
|
||||
flags := unix.O_WRONLY | unix.O_TMPFILE | unix.O_CLOEXEC
|
||||
if !noSync {
|
||||
flags |= unix.O_DSYNC
|
||||
|
@ -37,7 +39,7 @@ func newSpecificWriteData(c FileCounter, root string, perm fs.FileMode, noSync b
|
|||
flags: flags,
|
||||
counter: c,
|
||||
}
|
||||
return w.writeData
|
||||
return w
|
||||
}
|
||||
|
||||
func (w *linuxWriter) writeData(p string, data []byte) error {
|
||||
|
@ -74,3 +76,14 @@ func (w *linuxWriter) writeFile(p string, data []byte) error {
|
|||
}
|
||||
return errClose
|
||||
}
|
||||
|
||||
func (w *linuxWriter) removeFile(p string) error {
|
||||
err := unix.Unlink(p)
|
||||
if err != nil && err == unix.ENOENT {
|
||||
return logicerr.Wrap(new(apistatus.ObjectNotFound))
|
||||
}
|
||||
if err == nil {
|
||||
w.counter.Dec()
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -2,8 +2,10 @@
|
|||
|
||||
package fstree
|
||||
|
||||
import "io/fs"
|
||||
import (
|
||||
"io/fs"
|
||||
)
|
||||
|
||||
func newSpecificWriteData(_ FileCounter, _ string, _ fs.FileMode, _ bool) func(string, []byte) error {
|
||||
func newSpecificWriteData(_ FileCounter, _ string, _ fs.FileMode, _ bool) writer {
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue