From 29955a30362ce92df495222fbd9d0414b4515696 Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Tue, 19 Oct 2021 19:20:53 +0300 Subject: [PATCH] [#933] FSTree: Add `NumberOfObjects` method Signed-off-by: Pavel Karpy --- .../blobstor/fstree/fstree.go | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/pkg/local_object_storage/blobstor/fstree/fstree.go b/pkg/local_object_storage/blobstor/fstree/fstree.go index 8fcb7a385..f3bdcfd0c 100644 --- a/pkg/local_object_storage/blobstor/fstree/fstree.go +++ b/pkg/local_object_storage/blobstor/fstree/fstree.go @@ -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 +}