Skip to content

Commit

Permalink
Add a AfterFileSeen Collector method for exporting file stats from …
Browse files Browse the repository at this point in the history
…plugins

PiperOrigin-RevId: 638675789
  • Loading branch information
SCALIBR Team authored and copybara-github committed May 30, 2024
1 parent 089b980 commit e165e7b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
10 changes: 9 additions & 1 deletion extractor/filesystem/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ type ScanInput struct {
// A reader for accessing contents of the file.
// Note that the file is closed by the core library, not the plugin.
Reader io.Reader
// Stats collector to export stats internally from a plugin.
Stats stats.Collector
}

// Config stores the config settings for an extraction run.
Expand Down Expand Up @@ -279,7 +281,13 @@ func (wc *walkContext) runExtractor(ex Extractor, path string, mode fs.FileMode)
wc.extractCalls++

start := time.Now()
results, err := ex.Extract(wc.ctx, &ScanInput{Path: path, ScanRoot: wc.scanRoot, Info: info, Reader: rc})
results, err := ex.Extract(wc.ctx, &ScanInput{
Path: path,
ScanRoot: wc.scanRoot,
Info: info,
Reader: rc,
Stats: wc.stats,
})
wc.stats.AfterExtractorRun(ex.Name(), time.Since(start), err)
if err != nil {
addErrToMap(wc.errors, ex.Name(), fmt.Errorf("%s: %w", path, err))
Expand Down
16 changes: 16 additions & 0 deletions stats/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ type Collector interface {
// AfterResultsExported is called after results have been exported. destination should merely be
// a category of where the result was written to (e.g. 'file', 'http'), not the precise location.
AfterResultsExported(destination string, bytes int, err error)
AfterFileSeen(pluginName string, filestats *FileStats)
}

// FileStats is a struct containing stats about a file that was extracted. If
// the file was skipped due to an error during extraction, `Error` will be
// populated.
type FileStats struct {
Path string
Error error
FileSizeBytes int64
// For extractors that unarchive a compressed files, this reports the bytes
// that were opened during the unarchiving process.
UncompressedBytes int64
}

// NoopCollector implements Collector by doing nothing.
Expand All @@ -51,3 +64,6 @@ func (c NoopCollector) AfterScan(runtime time.Duration, status *plugin.ScanStatu

// AfterResultsExported implements Collector by doing nothing.
func (c NoopCollector) AfterResultsExported(destination string, bytes int, err error) {}

// AfterFileSeen implements Collector by doing nothing.
func (c NoopCollector) AfterFileSeen(name string, filestats *FileStats) {}

0 comments on commit e165e7b

Please sign in to comment.