Proper executable detection (looks for #! or ELF at the beginning of the file)

This commit is contained in:
Belac Darkstorm
2016-09-13 02:54:59 -05:00
parent be42963441
commit 08a453b5d5
2 changed files with 35 additions and 16 deletions
+31 -8
View File
@@ -4,6 +4,7 @@ import (
"bufio"
"os"
"path"
"reflect"
"sort"
"strings"
@@ -79,18 +80,40 @@ func processApp(fi *os.File) (out prtap) {
if out.cat == "" {
out.cat = "other"
}
//executable detection
wd, _ := os.Getwd()
var rdr *bufio.Reader
for _, v := range fis {
if !v.IsDir() && strings.HasSuffix(strings.ToLower(v.Name()), ".sh") {
//do os check here for possible cross platform support
out.ex = fi.Name() + "/" + v.Name()
return
fil, err := os.Open(wd + "/" + fi.Name() + "/" + v.Name())
if err == nil {
stat, _ := fil.Stat()
if !stat.IsDir() {
rdr = bufio.NewReader(fil)
shebang := []byte{'#', '!'}
two := make([]byte, 2)
rdr.Read(two)
if reflect.DeepEqual(shebang, two) {
out.ex = wd + "/" + fi.Name() + "/" + v.Name()
rdr.Reset(fil)
return
}
}
}
}
for _, v := range fis {
if !v.IsDir() && strings.HasSuffix(strings.ToLower(v.Name()), ".appimage") {
//do os check here for possible cross platform support
out.ex = fi.Name() + "/" + v.Name()
return
fil, err := os.Open(wd + "/" + fi.Name() + "/" + v.Name())
if err == nil {
stat, _ := fil.Stat()
if !stat.IsDir() {
rdr = bufio.NewReader(fil)
thr := make([]byte, 4)
rdr.Read(thr)
if strings.Contains(string(thr), "ELF") {
out.ex = wd + "/" + fi.Name() + "/" + v.Name()
rdr.Reset(fil)
return
}
}
}
}
return prtap{}