Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions pkg/programkind/programkind.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ type FileType struct {
var (
headerPool *pool.BufferPool
initializeOnce sync.Once
versionRegex = regexp.MustCompile(`\d+\.\d+\.\d+$`)
// Magic byte constants for common file signatures.
elfMagic = []byte{0x7f, 'E', 'L', 'F'}
gzipMagic = []byte{0x1f, 0x8b}
ZMagic = []byte{0x78, 0x5E}
)

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

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

ext := filepath.Ext(base)

if ext != "" && strings.Contains(base, ".") {
parts := strings.Split(base, ".")
if len(parts) > 2 {
subExt := fmt.Sprintf(".%s%s", parts[len(parts)-2], ext)
if isValidExt := func(ext string) bool {
_, ok := ArchiveMap[ext]
return ok
}(subExt); isValidExt {
if _, ok := ArchiveMap[subExt]; ok {
return subExt
}
}
Expand Down Expand Up @@ -228,8 +229,6 @@ func makeFileType(path string, ext string, mime string) *FileType {
}

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

initializeOnce.Do(func() {
headerPool = pool.NewBufferPool(runtime.GOMAXPROCS(0))
})
initializeHeaderPool()

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

switch {
case hdr[0] == '\x7f' && (hdr[1] == 'E' && hdr[2] == 'L' && hdr[3] == 'F'):
case bytes.HasPrefix(hdr, elfMagic):
return Path(".elf"), nil
case bytes.Contains(hdr, []byte("<?php")):
return Path(".php"), nil
Expand All @@ -316,14 +313,20 @@ func File(path string) (*FileType, error) {
return Path(".c"), nil
case bytes.Contains(hdr, []byte("BEAMAtU8")):
return Path(".beam"), nil
case hdr[0] == '\x1f' && hdr[1] == '\x8b':
case bytes.HasPrefix(hdr, gzipMagic):
return Path(".gzip"), nil
case hdr[0] == '\x78' && hdr[1] == '\x5E':
case bytes.HasPrefix(hdr, ZMagic):
return Path(".Z"), nil
}
return nil, nil
}

func initializeHeaderPool() {
initializeOnce.Do(func() {
headerPool = pool.NewBufferPool(runtime.GOMAXPROCS(0))
})
}

// Path returns a filetype based strictly on file path.
func Path(path string) *FileType {
ext := strings.ReplaceAll(filepath.Ext(path), ".", "")
Expand Down
Loading
Loading