Skip to content

Commit 179b462

Browse files
committedOct 12, 2021
file lister checks for allowed paths
1 parent 4c86e76 commit 179b462

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed
 

‎list/input.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,27 @@ package list
22

33
type FileTree struct {
44
InputPaths *[]string
5-
IsDir bool
6-
IsFile bool
7-
MaxNestedDir int
5+
IsDir bool
6+
HasFile bool
7+
MaxNestedDir int
88
IsNestedDir bool
99
FilePaths []string
1010
DirPaths []string
1111
HiddenFilePaths []string
1212
HiddenDirPaths []string
1313
StdinValid bool
1414
SkipHiddenDirs bool
15+
IsDirAllowed func(path string) bool
16+
IsFileAllowed func(path string) bool
1517
}
1618

1719
// maxRecursion: -1 for no limit
1820

1921
func Read(path *[]string, maxRecursion int) *FileTree {
20-
return &FileTree{InputPaths: path, MaxNestedDir: maxRecursion}
22+
return &FileTree{InputPaths: path, MaxNestedDir: maxRecursion,
23+
IsDirAllowed: returnTrue,IsFileAllowed: returnTrue}
2124
}
25+
26+
func returnTrue(_ string) bool {
27+
return true
28+
}

‎list/list.go

+11-6
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@ import (
1010
func (tree *FileTree) UpdateFiles() error {
1111
nestedLevel := 0
1212
for _, inputPath := range *tree.InputPaths {
13+
1314
inputPath = strings.TrimSpace(inputPath)
1415
file, err := os.Stat(inputPath)
1516
if err != nil {
1617
return err
1718
}
1819
path := filepath.Join(inputPath, file.Name())
19-
if file.IsDir() && tree.MaxNestedDir != nestedLevel {
20+
if file.IsDir() && tree.MaxNestedDir != nestedLevel &&
21+
tree.IsDirAllowed(path) {
22+
2023
tree.IsDir = true
2124
nestedLevel_ := nestedLevel + 1
2225
// . actually means all the dirs and files in curr directory
@@ -30,8 +33,8 @@ func (tree *FileTree) UpdateFiles() error {
3033
if err != nil {
3134
return err
3235
}
33-
} else {
34-
tree.IsFile = true
36+
} else if tree.IsFileAllowed(path) {
37+
tree.HasFile = true
3538
if CheckPathHidden(path) {
3639
tree.HiddenFilePaths = append(tree.HiddenFilePaths, path)
3740
} else {
@@ -58,15 +61,17 @@ func (tree *FileTree) iterateDir(basePath *string, nestedLevel int) error {
5861
}
5962
for _, file := range ls {
6063
path := filepath.Join(*basePath, file.Name())
61-
if file.IsDir() && tree.MaxNestedDir != nestedLevel {
64+
if file.IsDir() && tree.MaxNestedDir != nestedLevel &&
65+
tree.IsDirAllowed(path) {
66+
6267
tree.IsNestedDir = true
6368

6469
err := tree.iterateDir(&path, nestedLevel+1)
6570
if err != nil {
6671
return err
6772
}
68-
} else {
69-
tree.IsFile = true
73+
} else if tree.IsFileAllowed(path) {
74+
tree.HasFile = true
7075

7176
if CheckPathHidden(path) {
7277
tree.HiddenFilePaths = append(tree.HiddenFilePaths, path)

0 commit comments

Comments
 (0)
Please sign in to comment.