From 0f3553c8ab231d94cbc7093a257109fa5784c947 Mon Sep 17 00:00:00 2001 From: Ibrahim Serdar Acikgoz Date: Fri, 22 Dec 2023 19:26:57 +0100 Subject: [PATCH] shared/filestore/localstore.go: remove unnecessary warning (#25801) --- .../shared/filestore/filesstore_test.go | 2 +- .../platform/shared/filestore/localstore.go | 24 +++++++++++++++---- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/server/platform/shared/filestore/filesstore_test.go b/server/platform/shared/filestore/filesstore_test.go index 83bdd4df0c..6162172aa4 100644 --- a/server/platform/shared/filestore/filesstore_test.go +++ b/server/platform/shared/filestore/filesstore_test.go @@ -391,7 +391,7 @@ func (s *FileBackendTestSuite) TestListDirectoryRecursively() { b := []byte("test") path1 := "19700101/" + randomString() path2 := "19800101/" + randomString() - longPath := "19800102/this/is/a/way/too/long/path/for/this/function/to/handle" + randomString() + longPath := "19800102" + strings.Repeat("/toomuch", MaxRecursionDepth+1) + randomString() paths, err := s.backend.ListDirectoryRecursively("19700101") s.Nil(err) diff --git a/server/platform/shared/filestore/localstore.go b/server/platform/shared/filestore/localstore.go index 893dac58a2..5d4e514f4c 100644 --- a/server/platform/shared/filestore/localstore.go +++ b/server/platform/shared/filestore/localstore.go @@ -16,7 +16,8 @@ import ( ) const ( - TestFilePath = "/testfile" + TestFilePath = "/testfile" + MaxRecursionDepth = 50 ) type LocalFileBackend struct { @@ -211,7 +212,7 @@ func appendRecursively(basePath, path string, maxDepth int) ([]string, error) { } if dirEntry.IsDir() { if maxDepth <= 0 { - mlog.Warn("Max Depth reached", mlog.String("path", entryPath)) + mlog.Warn("Max depth reached, skipping any further directories", mlog.Int("depth", maxDepth), mlog.String("path", entryPath)) results = append(results, entryPath) continue // we'll ignore it if max depth is reached. } @@ -228,11 +229,26 @@ func appendRecursively(basePath, path string, maxDepth int) ([]string, error) { } func (b *LocalFileBackend) ListDirectory(path string) ([]string, error) { - return appendRecursively(b.directory, path, 0) + results := []string{} + dirEntries, err := os.ReadDir(filepath.Join(b.directory, path)) + if err != nil { + if os.IsNotExist(err) { + // ideally os.ErrNotExist should've been returned but to keep the + // consistency, leaving it as is before. + return results, nil + } + // same here, ideally we shouldn't return the empty slice + return results, errors.Wrapf(err, "unable to list the directory %s", path) + } + for _, dirEntry := range dirEntries { + results = append(results, filepath.Join(path, dirEntry.Name())) + } + + return results, nil } func (b *LocalFileBackend) ListDirectoryRecursively(path string) ([]string, error) { - return appendRecursively(b.directory, path, 10) + return appendRecursively(b.directory, path, MaxRecursionDepth) } func (b *LocalFileBackend) RemoveDirectory(path string) error {