[#970] fstree: Move delete implementation to a separate file

Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
Evgenii Stratonikov 2024-02-08 17:53:27 +03:00 committed by Evgenii Stratonikov
parent 7f692409cf
commit fb74524ac7
5 changed files with 52 additions and 25 deletions

View file

@ -19,7 +19,7 @@ func (t *FSTree) Init() error {
if !t.readOnly { if !t.readOnly {
f := newSpecificWriteData(t.fileCounter, t.RootPath, t.Permissions, t.noSync) f := newSpecificWriteData(t.fileCounter, t.RootPath, t.Permissions, t.noSync)
if f != nil { if f != nil {
t.writeData = f t.writer = f
} }
} }

View file

@ -57,7 +57,7 @@ type FSTree struct {
fileCounter FileCounter fileCounter FileCounter
fileCounterEnabled bool fileCounterEnabled bool
writeData func(string, []byte) error writer writer
} }
// Info groups the information about file storage. // Info groups the information about file storage.
@ -95,7 +95,7 @@ func New(opts ...Option) *FSTree {
for i := range opts { for i := range opts {
opts[i](f) opts[i](f)
} }
f.writeData = newGenericWriteData(f) f.writer = newGenericWriteData(f)
return f return f
} }
@ -266,21 +266,7 @@ func (t *FSTree) Delete(ctx context.Context, prm common.DeletePrm) (common.Delet
} }
p := t.treePath(prm.Address) p := t.treePath(prm.Address)
err = t.writer.removeFile(p)
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))
}
return common.DeleteRes{}, err 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) 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. // Get returns an object from the storage by address.

View file

@ -9,8 +9,15 @@ import (
"syscall" "syscall"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/common" "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 { type genericWriter struct {
perm fs.FileMode perm fs.FileMode
flags int flags int
@ -19,7 +26,7 @@ type genericWriter struct {
suffix atomic.Uint64 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 flags := os.O_WRONLY | os.O_CREATE | os.O_TRUNC | os.O_EXCL
if !t.noSync { if !t.noSync {
flags |= os.O_SYNC flags |= os.O_SYNC
@ -29,7 +36,7 @@ func newGenericWriteData(t *FSTree) func(string, []byte) error {
flags: flags, flags: flags,
t: t, t: t,
} }
return w.writeData return w
} }
func (w *genericWriter) writeData(p string, data []byte) error { func (w *genericWriter) writeData(p string, data []byte) error {
@ -86,3 +93,22 @@ func (w *genericWriter) writeFile(p string, data []byte) error {
} }
return err 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
}

View file

@ -8,6 +8,8 @@ import (
"strconv" "strconv"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/common" "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" "golang.org/x/sys/unix"
) )
@ -19,7 +21,7 @@ type linuxWriter struct {
counter FileCounter 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 flags := unix.O_WRONLY | unix.O_TMPFILE | unix.O_CLOEXEC
if !noSync { if !noSync {
flags |= unix.O_DSYNC flags |= unix.O_DSYNC
@ -37,7 +39,7 @@ func newSpecificWriteData(c FileCounter, root string, perm fs.FileMode, noSync b
flags: flags, flags: flags,
counter: c, counter: c,
} }
return w.writeData return w
} }
func (w *linuxWriter) writeData(p string, data []byte) error { func (w *linuxWriter) writeData(p string, data []byte) error {
@ -74,3 +76,14 @@ func (w *linuxWriter) writeFile(p string, data []byte) error {
} }
return errClose 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
}

View file

@ -2,8 +2,10 @@
package fstree 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 return nil
} }