Skip to content

Commit 3a16bf6

Browse files
[filesystem] Added LsRecursive in filesystem to browse file system 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]>
1 parent 427207d commit 3a16bf6

18 files changed

+1299
-378
lines changed

changes/20240828133012.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
✨ `[filesystem]` Added `LsRecursive` to browse file system trees and list their content

utils/filesystem/files.go

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,6 @@ func (fs *VFS) walk(ctx context.Context, path string, info os.FileInfo, exclusio
200200
}
201201
}
202202
return nil
203-
204203
}
205204

206205
func (fs *VFS) GetType() FilesystemType {
@@ -1129,6 +1128,53 @@ func (fs *VFS) LsWithExclusionPatterns(dir string, exclusionPatterns ...string)
11291128
return
11301129
}
11311130

1131+
func (fs *VFS) LsRecursive(ctx context.Context, dir string, includeDirectories bool) (files []string, err error) {
1132+
return fs.LsRecursiveWithExtensionPatternsAndLimits(ctx, dir, NoLimits(), includeDirectories)
1133+
}
1134+
1135+
func (fs *VFS) LsRecursiveWithExtensionPatterns(ctx context.Context, dir string, includeDirectories bool, exclusionPatterns ...string) (files []string, err error) {
1136+
return fs.LsRecursiveWithExtensionPatternsAndLimits(ctx, dir, NoLimits(), includeDirectories, exclusionPatterns...)
1137+
}
1138+
1139+
func (fs *VFS) LsRecursiveWithExtensionPatternsAndLimits(ctx context.Context, dir string, limits ILimits, includeDirectories bool, exclusionPatterns ...string) (files []string, err error) {
1140+
err = parallelisation.DetermineContextError(ctx)
1141+
if err != nil {
1142+
return
1143+
}
1144+
if limits == nil {
1145+
err = fmt.Errorf("%w: missing file system limits", commonerrors.ErrUndefined)
1146+
return
1147+
}
1148+
1149+
fn := func(path string, info os.FileInfo, err error) error {
1150+
if err != nil {
1151+
return err
1152+
}
1153+
1154+
currentDepth, err := FileTreeDepth(fs, dir, path)
1155+
if err != nil {
1156+
return err
1157+
}
1158+
1159+
if limits.Apply() && currentDepth >= limits.GetMaxDepth() {
1160+
return filepath.SkipDir
1161+
}
1162+
1163+
if limits.Apply() && int64(len(files)) >= limits.GetMaxFileCount() {
1164+
return fmt.Errorf("number of files exceeds the limit of %d: %w", limits.GetMaxFileCount(), commonerrors.ErrTooLarge)
1165+
}
1166+
1167+
if includeDirectories || !info.IsDir() {
1168+
files = append(files, path)
1169+
}
1170+
1171+
return nil
1172+
}
1173+
1174+
err = fs.WalkWithContextAndExclusionPatterns(ctx, dir, fn, exclusionPatterns...)
1175+
return
1176+
}
1177+
11321178
func LsWithExclusionPatterns(fs FS, dir string, regexes []*regexp.Regexp) (names []string, err error) {
11331179
if isDir, subErr := fs.IsDir(dir); !isDir || subErr != nil {
11341180
err = fmt.Errorf("path [%v] is not a directory: %w", dir, commonerrors.ErrInvalid)
@@ -1146,7 +1192,6 @@ func LsWithExclusionPatterns(fs FS, dir string, regexes []*regexp.Regexp) (names
11461192
}
11471193
names, err = ExcludeFiles(AllNames, regexes)
11481194
return
1149-
11501195
}
11511196

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

1208+
func (fs *VFS) LsRecursiveFromOpenedDirectory(ctx context.Context, dir File, includeDirectories bool) (files []string, err error) {
1209+
err = parallelisation.DetermineContextError(ctx)
1210+
if err != nil {
1211+
return
1212+
}
1213+
if dir == nil {
1214+
return nil, fmt.Errorf("%w: supplied directory was undefined", commonerrors.ErrUndefined)
1215+
}
1216+
err = fs.checkWhetherUnderlyingResourceIsClosed()
1217+
if err != nil {
1218+
return nil, fmt.Errorf("%w: underlying resource is closed", commonerrors.ErrUndefined)
1219+
}
1220+
1221+
return fs.LsRecursive(ctx, dir.Name(), includeDirectories)
1222+
}
1223+
11631224
func (fs *VFS) Lls(dir string) (files []os.FileInfo, err error) {
11641225
err = fs.checkWhetherUnderlyingResourceIsClosed()
11651226
if err != nil {

0 commit comments

Comments
 (0)