Skip to content

Commit

Permalink
[filesystem] Added LsRecursive in filesystem to browse file sys…
Browse files Browse the repository at this point in the history
…tem trees (#497)

<!--
Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors.
All rights reserved.
SPDX-License-Identifier: Apache-2.0
-->
### Description

- Introduced a way to perform recursive `ls`

### Test Coverage

<!--
Please put an `x` in the correct box e.g. `[x]` to indicate the testing
coverage of this change.
-->

- [x]  This change is covered by existing or additional automated tests.
- [ ] Manual testing has been performed (and evidence provided) as
automated testing was not feasible.
- [ ] Additional tests are not required for this change (e.g.
documentation update).

---------

Co-authored-by: Adrien CABARBAYE <[email protected]>
  • Loading branch information
ERICwangyiquan and acabarbaye authored Aug 30, 2024
1 parent 427207d commit 3a16bf6
Show file tree
Hide file tree
Showing 18 changed files with 1,299 additions and 378 deletions.
1 change: 1 addition & 0 deletions changes/20240828133012.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
✨ `[filesystem]` Added `LsRecursive` to browse file system trees and list their content
65 changes: 63 additions & 2 deletions utils/filesystem/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ func (fs *VFS) walk(ctx context.Context, path string, info os.FileInfo, exclusio
}
}
return nil

}

func (fs *VFS) GetType() FilesystemType {
Expand Down Expand Up @@ -1129,6 +1128,53 @@ func (fs *VFS) LsWithExclusionPatterns(dir string, exclusionPatterns ...string)
return
}

func (fs *VFS) LsRecursive(ctx context.Context, dir string, includeDirectories bool) (files []string, err error) {
return fs.LsRecursiveWithExtensionPatternsAndLimits(ctx, dir, NoLimits(), includeDirectories)
}

func (fs *VFS) LsRecursiveWithExtensionPatterns(ctx context.Context, dir string, includeDirectories bool, exclusionPatterns ...string) (files []string, err error) {
return fs.LsRecursiveWithExtensionPatternsAndLimits(ctx, dir, NoLimits(), includeDirectories, exclusionPatterns...)
}

func (fs *VFS) LsRecursiveWithExtensionPatternsAndLimits(ctx context.Context, dir string, limits ILimits, includeDirectories bool, exclusionPatterns ...string) (files []string, err error) {
err = parallelisation.DetermineContextError(ctx)
if err != nil {
return
}
if limits == nil {
err = fmt.Errorf("%w: missing file system limits", commonerrors.ErrUndefined)
return
}

fn := func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

currentDepth, err := FileTreeDepth(fs, dir, path)
if err != nil {
return err
}

if limits.Apply() && currentDepth >= limits.GetMaxDepth() {
return filepath.SkipDir
}

if limits.Apply() && int64(len(files)) >= limits.GetMaxFileCount() {
return fmt.Errorf("number of files exceeds the limit of %d: %w", limits.GetMaxFileCount(), commonerrors.ErrTooLarge)
}

if includeDirectories || !info.IsDir() {
files = append(files, path)
}

return nil
}

err = fs.WalkWithContextAndExclusionPatterns(ctx, dir, fn, exclusionPatterns...)
return
}

func LsWithExclusionPatterns(fs FS, dir string, regexes []*regexp.Regexp) (names []string, err error) {
if isDir, subErr := fs.IsDir(dir); !isDir || subErr != nil {
err = fmt.Errorf("path [%v] is not a directory: %w", dir, commonerrors.ErrInvalid)
Expand All @@ -1146,7 +1192,6 @@ func LsWithExclusionPatterns(fs FS, dir string, regexes []*regexp.Regexp) (names
}
names, err = ExcludeFiles(AllNames, regexes)
return

}

func (fs *VFS) LsFromOpenedDirectory(dir File) ([]string, error) {
Expand All @@ -1160,6 +1205,22 @@ func (fs *VFS) LsFromOpenedDirectory(dir File) ([]string, error) {
return dir.Readdirnames(-1)
}

func (fs *VFS) LsRecursiveFromOpenedDirectory(ctx context.Context, dir File, includeDirectories bool) (files []string, err error) {
err = parallelisation.DetermineContextError(ctx)
if err != nil {
return
}
if dir == nil {
return nil, fmt.Errorf("%w: supplied directory was undefined", commonerrors.ErrUndefined)
}
err = fs.checkWhetherUnderlyingResourceIsClosed()
if err != nil {
return nil, fmt.Errorf("%w: underlying resource is closed", commonerrors.ErrUndefined)
}

return fs.LsRecursive(ctx, dir.Name(), includeDirectories)
}

func (fs *VFS) Lls(dir string) (files []os.FileInfo, err error) {
err = fs.checkWhetherUnderlyingResourceIsClosed()
if err != nil {
Expand Down
Loading

0 comments on commit 3a16bf6

Please sign in to comment.