Skip to content

Commit a4cc836

Browse files
committed
small reorganization
Signed-off-by: egibs <20933572+egibs@users.noreply.github.com>
1 parent 7f2393d commit a4cc836

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

pkg/action/scan.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,6 @@ func scanSinglePath(ctx context.Context, c malcontent.Config, path string, ruleF
7676
return fr, nil
7777
}
7878

79-
initReadPool.Do(func() {
80-
readPool = pool.NewBufferPool(runtime.GOMAXPROCS(0))
81-
})
82-
// create a buffer sized to the minimum of the file's size or the default ReadBuffer
83-
buf := readPool.Get(min(size, file.ReadBuffer)) //nolint:nilaway // the buffer pool is created above
84-
8579
mime := "<unknown>"
8680
kind, err := programkind.File(ctx, path)
8781
if err != nil && !interactive(c) {
@@ -137,6 +131,14 @@ func scanSinglePath(ctx context.Context, c malcontent.Config, path string, ruleF
137131
return fr, nil
138132
}
139133

134+
initReadPool.Do(func() {
135+
readPool = pool.NewBufferPool(runtime.GOMAXPROCS(0))
136+
})
137+
138+
// create a buffer sized to the minimum of the file's size or the default ReadBuffer
139+
// only do so if we actually need to retrieve the file's contents
140+
buf := readPool.Get(min(size, file.ReadBuffer)) //nolint:nilaway // the buffer pool is created above
141+
140142
// Only retrieve the file's contents and calculate its checksum if we need to generate a report
141143
fc, err := file.GetContents(f, buf)
142144
if err != nil {

pkg/programkind/programkind.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -252,28 +252,24 @@ func File(ctx context.Context, path string) (*FileType, error) {
252252
return nil, fmt.Errorf("stat: %w", err)
253253
}
254254

255-
if st.IsDir() {
255+
// ignore directories, irregular files, and empty files
256+
if st.IsDir() || st.Mode().Type() == fs.ModeIrregular || st.Size() == 0 {
256257
return nil, nil
257258
}
258-
if st.Mode().Type() == fs.ModeIrregular {
259-
return nil, nil
260-
}
261-
if st.Size() == 0 {
262-
return nil, nil
259+
260+
f, err := os.Open(path)
261+
if err != nil {
262+
return nil, fmt.Errorf("open: %w", err)
263263
}
264+
defer f.Close()
264265

266+
// initialize the header pool after we've successfully opened the file
265267
initializeHeaderPool()
266268

267269
// create a buffer sized to the minimum of the file's size or the default ReadBuffer
268270
buf := headerPool.Get(min(st.Size(), file.ReadBuffer)) //nolint:nilaway // the buffer pool is created above
269271
defer headerPool.Put(buf)
270272

271-
f, err := os.Open(path)
272-
if err != nil {
273-
return nil, fmt.Errorf("open: %w", err)
274-
}
275-
defer f.Close()
276-
277273
fc, err := file.GetContents(f, buf)
278274
if err != nil {
279275
return nil, fmt.Errorf("file contents: %w", err)

0 commit comments

Comments
 (0)