[#1132] *: Use path/filepath package when working with files

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2022-02-02 16:28:08 +03:00 committed by LeL
parent 0decb95591
commit 674f520da7
20 changed files with 73 additions and 77 deletions

View file

@ -4,7 +4,7 @@ import (
"bytes"
"errors"
"fmt"
"path"
"path/filepath"
"strconv"
"sync"
@ -88,7 +88,7 @@ var errPutFailed = errors.New("could not save the object in any blobovnicza")
func newBlobovniczaTree(c *cfg) (blz *blobovniczas) {
cache, err := simplelru.NewLRU(c.openedCacheSize, func(key interface{}, value interface{}) {
if _, ok := blz.active[path.Dir(key.(string))]; ok {
if _, ok := blz.active[filepath.Dir(key.(string))]; ok {
return
} else if err := value.(*blobovnicza.Blobovnicza).Close(); err != nil {
c.log.Error("could not close Blobovnicza",
@ -156,7 +156,7 @@ func (b *blobovniczas) put(addr *objectSDK.Address, data []byte) (*blobovnicza.I
// check if blobovnicza is full
if errors.Is(err, blobovnicza.ErrFull) {
b.log.Debug("blobovnicza overflowed",
zap.String("path", path.Join(p, u64ToHexString(active.ind))),
zap.String("path", filepath.Join(p, u64ToHexString(active.ind))),
)
if err := b.updateActive(p, &active.ind); err != nil {
@ -172,14 +172,14 @@ func (b *blobovniczas) put(addr *objectSDK.Address, data []byte) (*blobovnicza.I
}
b.log.Debug("could not put object to active blobovnicza",
zap.String("path", path.Join(p, u64ToHexString(active.ind))),
zap.String("path", filepath.Join(p, u64ToHexString(active.ind))),
zap.String("error", err.Error()),
)
return false, nil
}
p = path.Join(p, u64ToHexString(active.ind))
p = filepath.Join(p, u64ToHexString(active.ind))
id = blobovnicza.NewIDFromBytes([]byte(p))
@ -217,7 +217,7 @@ func (b *blobovniczas) get(prm *GetSmallPrm) (res *GetSmallRes, err error) {
activeCache := make(map[string]struct{})
err = b.iterateSortedLeaves(prm.addr, func(p string) (bool, error) {
dirPath := path.Dir(p)
dirPath := filepath.Dir(p)
_, ok := activeCache[dirPath]
@ -267,7 +267,7 @@ func (b *blobovniczas) delete(prm *DeleteSmallPrm) (res *DeleteSmallRes, err err
activeCache := make(map[string]struct{})
err = b.iterateSortedLeaves(prm.addr, func(p string) (bool, error) {
dirPath := path.Dir(p)
dirPath := filepath.Dir(p)
// don't process active blobovnicza of the level twice
_, ok := activeCache[dirPath]
@ -319,7 +319,7 @@ func (b *blobovniczas) getRange(prm *GetRangeSmallPrm) (res *GetRangeSmallRes, e
activeCache := make(map[string]struct{})
err = b.iterateSortedLeaves(prm.addr, func(p string) (bool, error) {
dirPath := path.Dir(p)
dirPath := filepath.Dir(p)
_, ok := activeCache[dirPath]
@ -351,7 +351,7 @@ func (b *blobovniczas) getRange(prm *GetRangeSmallPrm) (res *GetRangeSmallRes, e
//
// returns no error if object was removed from some blobovnicza of the same level.
func (b *blobovniczas) deleteObjectFromLevel(prm *blobovnicza.DeletePrm, blzPath string, tryActive bool, dp *DeleteSmallPrm) (*DeleteSmallRes, error) {
lvlPath := path.Dir(blzPath)
lvlPath := filepath.Dir(blzPath)
log := b.log.With(
zap.String("path", blzPath),
@ -394,7 +394,7 @@ func (b *blobovniczas) deleteObjectFromLevel(prm *blobovnicza.DeletePrm, blzPath
// check if it makes sense to try to open the blob
// (blobovniczas "after" the active one are empty anyway,
// and it's pointless to open them).
if u64FromHexString(path.Base(blzPath)) > active.ind {
if u64FromHexString(filepath.Base(blzPath)) > active.ind {
log.Debug("index is too big")
return nil, object.ErrNotFound
}
@ -412,7 +412,7 @@ func (b *blobovniczas) deleteObjectFromLevel(prm *blobovnicza.DeletePrm, blzPath
//
// returns error if object could not be read from any blobovnicza of the same level.
func (b *blobovniczas) getObjectFromLevel(prm *blobovnicza.GetPrm, blzPath string, tryActive bool) (*GetSmallRes, error) {
lvlPath := path.Dir(blzPath)
lvlPath := filepath.Dir(blzPath)
log := b.log.With(
zap.String("path", blzPath),
@ -456,7 +456,7 @@ func (b *blobovniczas) getObjectFromLevel(prm *blobovnicza.GetPrm, blzPath strin
// check if it makes sense to try to open the blob
// (blobovniczas "after" the active one are empty anyway,
// and it's pointless to open them).
if u64FromHexString(path.Base(blzPath)) > active.ind {
if u64FromHexString(filepath.Base(blzPath)) > active.ind {
log.Debug("index is too big")
return nil, object.ErrNotFound
}
@ -474,7 +474,7 @@ func (b *blobovniczas) getObjectFromLevel(prm *blobovnicza.GetPrm, blzPath strin
//
// returns error if object could not be read from any blobovnicza of the same level.
func (b *blobovniczas) getRangeFromLevel(prm *GetRangeSmallPrm, blzPath string, tryActive bool) (*GetRangeSmallRes, error) {
lvlPath := path.Dir(blzPath)
lvlPath := filepath.Dir(blzPath)
log := b.log.With(
zap.String("path", blzPath),
@ -528,7 +528,7 @@ func (b *blobovniczas) getRangeFromLevel(prm *GetRangeSmallPrm, blzPath string,
// check if it makes sense to try to open the blob
// (blobovniczas "after" the active one are empty anyway,
// and it's pointless to open them).
if u64FromHexString(path.Base(blzPath)) > active.ind {
if u64FromHexString(filepath.Base(blzPath)) > active.ind {
log.Debug("index is too big")
return nil, object.ErrNotFound
}
@ -653,7 +653,7 @@ func (b *blobovniczas) iterateSortedLeaves(addr *objectSDK.Address, f func(strin
addr,
make([]string, 0, b.blzShallowDepth),
b.blzShallowDepth,
func(p []string) (bool, error) { return f(path.Join(p...)) },
func(p []string) (bool, error) { return f(filepath.Join(p...)) },
)
return err
@ -670,7 +670,7 @@ func (b *blobovniczas) iterateDeepest(addr *objectSDK.Address, f func(string) (b
addr,
make([]string, 0, depth),
depth,
func(p []string) (bool, error) { return f(path.Join(p...)) },
func(p []string) (bool, error) { return f(filepath.Join(p...)) },
)
return err
@ -680,7 +680,7 @@ func (b *blobovniczas) iterateDeepest(addr *objectSDK.Address, f func(string) (b
func (b *blobovniczas) iterateSorted(addr *objectSDK.Address, curPath []string, execDepth uint64, f func([]string) (bool, error)) (bool, error) {
indices := indexSlice(b.blzShallowWidth)
hrw.SortSliceByValue(indices, addressHash(addr, path.Join(curPath...)))
hrw.SortSliceByValue(indices, addressHash(addr, filepath.Join(curPath...)))
exec := uint64(len(curPath)) == execDepth
@ -754,7 +754,7 @@ func (b *blobovniczas) updateAndGet(p string, old *uint64) (blobovniczaWithIndex
}
var err error
if active.blz, err = b.openBlobovnicza(path.Join(p, u64ToHexString(active.ind))); err != nil {
if active.blz, err = b.openBlobovnicza(filepath.Join(p, u64ToHexString(active.ind))); err != nil {
return active, err
}
@ -773,7 +773,7 @@ func (b *blobovniczas) updateAndGet(p string, old *uint64) (blobovniczaWithIndex
b.active[p] = active
b.log.Debug("blobovnicza successfully activated",
zap.String("path", path.Join(p, u64ToHexString(active.ind))),
zap.String("path", filepath.Join(p, u64ToHexString(active.ind))),
)
return active, nil
@ -887,7 +887,7 @@ func (b *blobovniczas) openBlobovnicza(p string) (*blobovnicza.Blobovnicza, erro
}
blz := blobovnicza.New(append(b.blzOpts,
blobovnicza.WithPath(path.Join(b.blzRootPath, p)),
blobovnicza.WithPath(filepath.Join(b.blzRootPath, p)),
)...)
if err := blz.Open(); err != nil {

View file

@ -3,7 +3,7 @@ package blobstor
import (
"encoding/hex"
"io/fs"
"path"
"path/filepath"
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobovnicza"
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor/fstree"
@ -132,7 +132,7 @@ func WithUncompressableContentTypes(values []string) Option {
func WithRootPath(rootDir string) Option {
return func(c *cfg) {
c.fsTree.RootPath = rootDir
c.blzRootPath = path.Join(rootDir, blobovniczaDir)
c.blzRootPath = filepath.Join(rootDir, blobovniczaDir)
}
}

View file

@ -6,7 +6,6 @@ import (
"fmt"
"io/fs"
"os"
"path"
"path/filepath"
"strings"
@ -94,7 +93,7 @@ func (t *FSTree) Iterate(prm *IterationPrm) error {
func (t *FSTree) iterate(depth int, curPath []string, prm *IterationPrm) error {
curName := strings.Join(curPath[1:], "")
des, err := os.ReadDir(path.Join(curPath...))
des, err := os.ReadDir(filepath.Join(curPath...))
if err != nil {
if prm.ignoreErrors {
return nil
@ -127,7 +126,7 @@ func (t *FSTree) iterate(depth int, curPath []string, prm *IterationPrm) error {
continue
}
data, err := os.ReadFile(path.Join(curPath...))
data, err := os.ReadFile(filepath.Join(curPath...))
if err != nil {
if prm.ignoreErrors {
continue
@ -157,7 +156,7 @@ func (t *FSTree) treePath(addr *objectSDK.Address) string {
dirs = append(dirs, sAddr)
return path.Join(dirs...)
return filepath.Join(dirs...)
}
// Delete removes object with the specified address from storage.
@ -187,7 +186,7 @@ func (t *FSTree) Exists(addr *objectSDK.Address) (string, error) {
func (t *FSTree) Put(addr *objectSDK.Address, data []byte) error {
p := t.treePath(addr)
if err := util.MkdirAllX(path.Dir(p), t.Permissions); err != nil {
if err := util.MkdirAllX(filepath.Dir(p), t.Permissions); err != nil {
return err
}

View file

@ -5,7 +5,6 @@ import (
"crypto/sha256"
"errors"
"os"
"path"
"path/filepath"
"testing"
@ -43,7 +42,7 @@ func TestAddressToString(t *testing.T) {
}
func TestFSTree(t *testing.T) {
tmpDir := path.Join(os.TempDir(), "neofs.fstree.test")
tmpDir := filepath.Join(os.TempDir(), "neofs.fstree.test")
require.NoError(t, os.Mkdir(tmpDir, os.ModePerm))
t.Cleanup(func() { require.NoError(t, os.RemoveAll(tmpDir)) })