[#933] FSTree: Add NumberOfObjects
method
Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
parent
41a9261069
commit
29955a3036
1 changed files with 25 additions and 0 deletions
|
@ -3,9 +3,11 @@ package fstree
|
|||
import (
|
||||
"crypto/sha256"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
cid "github.com/nspcc-dev/neofs-api-go/pkg/container/id"
|
||||
|
@ -175,3 +177,26 @@ func (t *FSTree) Get(addr *objectSDK.Address) ([]byte, error) {
|
|||
|
||||
return os.ReadFile(p)
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue