Skip to content
This repository has been archived by the owner on Jun 21, 2022. It is now read-only.

Commit

Permalink
fix(walker) clean for all temporary data after all analyzes
Browse files Browse the repository at this point in the history
  • Loading branch information
masahiro331 committed Dec 19, 2021
1 parent ad858d8 commit 7f2bf58
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 16 deletions.
20 changes: 7 additions & 13 deletions analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,28 +185,23 @@ func (a Analyzer) ImageConfigAnalyzerVersions() map[string]int {
}

func (a Analyzer) AnalyzeFile(ctx context.Context, wg *sync.WaitGroup, limit *semaphore.Weighted, result *AnalysisResult,
dir, filePath string, info os.FileInfo, opener Opener) (err error) {
dir, filePath string, info os.FileInfo, opener Opener) (cleaner func() error, err error) {
if info.IsDir() {
return nil
return nil, nil
}
for _, d := range a.drivers {
// filepath extracted from tar file doesn't have the prefix "/"
if !d.Required(strings.TrimLeft(filePath, "/"), info) {
continue
}
rc, cleaner, err := opener()
rc, c, err := opener()
if err != nil {
return xerrors.Errorf("unable to open a file (%s): %w", filePath, err)
return nil, xerrors.Errorf("unable to open a file (%s): %w", filePath, err)
}
defer func() {
err := cleaner()
if err != nil {
log.Logger.Warn("Clean temp directory error: %s", err)
}
}()
cleaner = c

if err = limit.Acquire(ctx, 1); err != nil {
return xerrors.Errorf("semaphore acquire: %w", err)
return nil, xerrors.Errorf("semaphore acquire: %w", err)
}
wg.Add(1)

Expand All @@ -223,9 +218,8 @@ func (a Analyzer) AnalyzeFile(ctx context.Context, wg *sync.WaitGroup, limit *se
result.Merge(ret)
}(d, AnalysisTarget{Dir: dir, FilePath: filePath, ContentReader: rc})
}
wg.Wait()

return nil
return cleaner, nil
}

func (a Analyzer) AnalyzeImageConfig(targetOS types.OS, configBlob []byte) []types.Package {
Expand Down
16 changes: 15 additions & 1 deletion artifact/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,23 +195,37 @@ func (a Artifact) inspectLayer(ctx context.Context, diffID string) (types.BlobIn
return types.BlobInfo{}, xerrors.Errorf("unable to get uncompressed layer %s: %w", diffID, err)
}

var cleaners []func() error
var wg sync.WaitGroup
result := new(analyzer.AnalysisResult)
limit := semaphore.NewWeighted(parallel)

opqDirs, whFiles, err := a.walker.Walk(r, func(filePath string, info os.FileInfo, opener analyzer.Opener) error {
if err = a.analyzer.AnalyzeFile(ctx, &wg, limit, result, "", filePath, info, opener); err != nil {
cleaner, err := a.analyzer.AnalyzeFile(ctx, &wg, limit, result, "", filePath, info, opener)
if err != nil {
return xerrors.Errorf("failed to analyze %s: %w", filePath, err)
}
if cleaner != nil {
cleaners = append(cleaners, cleaner)
}

return nil
})
if err != nil {
return types.BlobInfo{}, xerrors.Errorf("walk error: %w", err)

}

// Wait for all the goroutine to finish.
wg.Wait()

// Clean for all temporary data after all analyzers.
for _, c := range cleaners {
if err := c(); err != nil {
log.Logger.Warn("Clean temp directory error: %s", err)
}
}

// Sort the analysis result for consistent results
result.Sort()

Expand Down
16 changes: 15 additions & 1 deletion artifact/local/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/aquasecurity/fanal/cache"
"github.com/aquasecurity/fanal/config/scanner"
"github.com/aquasecurity/fanal/hook"
"github.com/aquasecurity/fanal/log"
"github.com/aquasecurity/fanal/types"
"github.com/aquasecurity/fanal/walker"
)
Expand Down Expand Up @@ -77,6 +78,7 @@ func buildAbsPaths(base string, paths []string) []string {
}

func (a Artifact) Inspect(ctx context.Context) (types.ArtifactReference, error) {
var cleaners []func() error
var wg sync.WaitGroup
result := new(analyzer.AnalysisResult)
limit := semaphore.NewWeighted(parallel)
Expand All @@ -87,9 +89,14 @@ func (a Artifact) Inspect(ctx context.Context) (types.ArtifactReference, error)
if err != nil {
return xerrors.Errorf("filepath rel (%s): %w", filePath, err)
}
if err = a.analyzer.AnalyzeFile(ctx, &wg, limit, result, a.dir, filePath, info, opener); err != nil {
cleaner, err := a.analyzer.AnalyzeFile(ctx, &wg, limit, result, a.dir, filePath, info, opener)
if err != nil {
return xerrors.Errorf("analyze file (%s): %w", filePath, err)
}
if cleaner != nil {
cleaners = append(cleaners, cleaner)
}

return nil
})
if err != nil {
Expand All @@ -99,6 +106,13 @@ func (a Artifact) Inspect(ctx context.Context) (types.ArtifactReference, error)
// Wait for all the goroutine to finish.
wg.Wait()

// Clean for all temporary data after all analyzers.
for _, c := range cleaners {
if err := c(); err != nil {
log.Logger.Warn("Clean temp directory error: %s", err)
}
}

// Sort the analysis result for consistent results
result.Sort()

Expand Down
2 changes: 1 addition & 1 deletion walker/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var (
systemDirs = []string{"proc", "sys", "dev"}
)

const ThresholdSize = int64(200) << 20
const ThresholdSize = int64(200) << 20 // 200MB

type WalkFunc func(filePath string, info os.FileInfo, opener analyzer.Opener) error

Expand Down

0 comments on commit 7f2bf58

Please sign in to comment.