Skip to content

Commit

Permalink
Fixed bugs in file collector (guacsec#348)
Browse files Browse the repository at this point in the history
- Fixed bugs in file collector.

Signed-off-by: naveensrinivasan <[email protected]>

Signed-off-by: naveensrinivasan <[email protected]>
  • Loading branch information
naveensrinivasan authored Jan 26, 2023
1 parent fbeba32 commit 49b181b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
20 changes: 11 additions & 9 deletions pkg/handler/collector/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package file

import (
"context"
"errors"
"fmt"
"io/fs"
"os"
Expand Down Expand Up @@ -53,7 +52,7 @@ func NewFileCollector(ctx context.Context, path string, poll bool, interval time
// for new artifacts as they are being uploaded by polling on an interval or run once and
// grab all the artifacts and end.
func (f *fileCollector) RetrieveArtifacts(ctx context.Context, docChannel chan<- *processor.Document) error {
if _, err := os.Stat(f.path); os.IsExist(err) {
if _, err := os.Stat(f.path); os.IsNotExist(err) {
return fmt.Errorf("path: %s does not exist", f.path)
}

Expand Down Expand Up @@ -98,15 +97,18 @@ func (f *fileCollector) RetrieveArtifacts(ctx context.Context, docChannel chan<-

if f.poll {
for {
err := filepath.WalkDir(f.path, readFunc)
if err != nil {
if errors.Is(err, ctx.Err()) {
return nil
select {
// If the context has been canceled it contains an err which we can throw.
case <-ctx.Done():
return ctx.Err()
default:
err := filepath.WalkDir(f.path, readFunc)
if err != nil {
return err
}
return err
f.lastChecked = time.Now()
time.Sleep(f.interval)
}
f.lastChecked = time.Now()
time.Sleep(f.interval)
}
} else {
err := filepath.WalkDir(f.path, readFunc)
Expand Down
2 changes: 1 addition & 1 deletion pkg/handler/collector/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func Test_fileCollector_RetrieveArtifacts(t *testing.T) {
Source: "file:///testdata/hello",
}},
},
wantErr: false,
wantErr: true,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 49b181b

Please sign in to comment.