-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleanup.go
70 lines (60 loc) · 1.18 KB
/
cleanup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"debug/elf"
"debug/macho"
"fmt"
"os"
"path/filepath"
"runtime"
"io/fs"
)
func cleanup(tempdir string) error {
var verifyFile func(file *os.File) error
switch runtime.GOOS {
case "linux":
verifyFile = func(file *os.File) error {
_, err := elf.NewFile(file)
return err
}
case "darwin":
verifyFile = func(file *os.File) error {
_, err := macho.NewFile(file)
return err
}
}
err := filepath.WalkDir(tempdir, func(binpath string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if binpath == tempdir {
return nil
}
// If a directory - skip
if d.IsDir() {
// fmt.Println("Not regular: ", binpath) // comment
return nil
}
// Open the file
f, err := os.Open(binpath)
defer f.Close()
if err != nil {
return err
}
// Verify if the open file is either an ELF or Mach-O
if err := verifyFile(f); err == nil {
err = os.Chmod(binpath, 0755)
// fmt.Println("Binary: ", binpath) //comment
if err != nil {
return err
}
} else {
// fmt.Println("Removing: ", binpath) // comment
os.Remove(binpath)
}
return nil
})
if err != nil {
fmt.Println(err)
}
return nil
}