Skip to content

Commit 75df672

Browse files
Ritish134egibs
andauthored
Clean up Generate function in report.go (#992)
* Clean up Generate function in report.go Broken down the functionality of the generate function into modular functions: `buildIgnoreMap()` — creates the ignore tag map `trimDisplayPath()` — handles trimming file paths `initFileReport()` — initializes the report struct `createMatchRulesMap()` — Store match rules in a map `processMatchedStrings()` — processes all pattern matches `buildBehavior()` — creates a behavior object from rule data `handleMetadata()` — processes metadata and applies overrides `updateBehavior()` — updates behavior entries in the file report Signed-off-by: Ritish Srivastava <121374890+Ritish134@users.noreply.github.com> * formatting fix Signed-off-by: Ritish Srivastava <121374890+Ritish134@users.noreply.github.com> * fix typecheck error Signed-off-by: Ritish Srivastava <121374890+Ritish134@users.noreply.github.com> * fix formatting using gofumpt Signed-off-by: Ritish Srivastava <121374890+Ritish134@users.noreply.github.com> * Update programkind.go - Moved the regex for checking the version outside `getExt` func for better performance - Created slices to check for `.elf, .gzip, .Z` files using `bytes.hasPrefix` - Wrote Equivalent code without an anonymous function in `GetExt` for extension of size greater than 2, which is more idiomatic. Signed-off-by: Ritish Srivastava <121374890+Ritish134@users.noreply.github.com> * fix missing pledges syscalls and caps * fix golangci-lint Signed-off-by: Ritish Srivastava <121374890+Ritish134@users.noreply.github.com> * WIP: saving before rebase --------- Signed-off-by: Ritish Srivastava <121374890+Ritish134@users.noreply.github.com> Co-authored-by: Evan Gibler <20933572+egibs@users.noreply.github.com>
1 parent f6569b2 commit 75df672

2 files changed

Lines changed: 190 additions & 156 deletions

File tree

pkg/programkind/programkind.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ type FileType struct {
112112
var (
113113
headerPool *pool.BufferPool
114114
initializeOnce sync.Once
115+
versionRegex = regexp.MustCompile(`\d+\.\d+\.\d+$`)
116+
// Magic byte constants for common file signatures.
117+
elfMagic = []byte{0x7f, 'E', 'L', 'F'}
118+
gzipMagic = []byte{0x1f, 0x8b}
119+
ZMagic = []byte{0x78, 0x5E}
115120
)
116121

117122
const headerSize int = 512
@@ -137,19 +142,15 @@ func GetExt(path string) string {
137142

138143
// Handle files with version numbers in the name
139144
// e.g. file1.2.3.tar.gz -> .tar.gz
140-
re := regexp.MustCompile(`\d+\.\d+\.\d+$`)
141-
base = re.ReplaceAllString(base, "")
145+
base = versionRegex.ReplaceAllString(base, "")
142146

143147
ext := filepath.Ext(base)
144148

145149
if ext != "" && strings.Contains(base, ".") {
146150
parts := strings.Split(base, ".")
147151
if len(parts) > 2 {
148152
subExt := fmt.Sprintf(".%s%s", parts[len(parts)-2], ext)
149-
if isValidExt := func(ext string) bool {
150-
_, ok := ArchiveMap[ext]
151-
return ok
152-
}(subExt); isValidExt {
153+
if _, ok := ArchiveMap[subExt]; ok {
153154
return subExt
154155
}
155156
}
@@ -228,8 +229,6 @@ func makeFileType(path string, ext string, mime string) *FileType {
228229
}
229230

230231
// File detects what kind of program this file might be.
231-
//
232-
//nolint:cyclop // ignore complexity of 38
233232
func File(path string) (*FileType, error) {
234233
// Follow symlinks and return cleanly if the target does not exist
235234
_, err := filepath.EvalSymlinks(path)
@@ -252,9 +251,7 @@ func File(path string) (*FileType, error) {
252251
return nil, nil
253252
}
254253

255-
initializeOnce.Do(func() {
256-
headerPool = pool.NewBufferPool(runtime.GOMAXPROCS(0))
257-
})
254+
initializeHeaderPool()
258255

259256
buf := headerPool.Get(int64(headerSize))
260257
defer headerPool.Put(buf)
@@ -289,7 +286,7 @@ func File(path string) (*FileType, error) {
289286
}
290287

291288
switch {
292-
case hdr[0] == '\x7f' && (hdr[1] == 'E' && hdr[2] == 'L' && hdr[3] == 'F'):
289+
case bytes.HasPrefix(hdr, elfMagic):
293290
return Path(".elf"), nil
294291
case bytes.Contains(hdr, []byte("<?php")):
295292
return Path(".php"), nil
@@ -316,14 +313,20 @@ func File(path string) (*FileType, error) {
316313
return Path(".c"), nil
317314
case bytes.Contains(hdr, []byte("BEAMAtU8")):
318315
return Path(".beam"), nil
319-
case hdr[0] == '\x1f' && hdr[1] == '\x8b':
316+
case bytes.HasPrefix(hdr, gzipMagic):
320317
return Path(".gzip"), nil
321-
case hdr[0] == '\x78' && hdr[1] == '\x5E':
318+
case bytes.HasPrefix(hdr, ZMagic):
322319
return Path(".Z"), nil
323320
}
324321
return nil, nil
325322
}
326323

324+
func initializeHeaderPool() {
325+
initializeOnce.Do(func() {
326+
headerPool = pool.NewBufferPool(runtime.GOMAXPROCS(0))
327+
})
328+
}
329+
327330
// Path returns a filetype based strictly on file path.
328331
func Path(path string) *FileType {
329332
ext := strings.ReplaceAll(filepath.Ext(path), ".", "")

0 commit comments

Comments
 (0)