-
Notifications
You must be signed in to change notification settings - Fork 155
/
match.go
89 lines (78 loc) 路 2.18 KB
/
match.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package core
import (
"os"
"path/filepath"
"strings"
log "github.com/sirupsen/logrus"
)
type MatchFile struct {
Path string
Filename string
Extension string
Contents []byte
}
// IsSkippableDir Checks if the path is excluded
func IsSkippableDir(excludedPaths []string, path, baseDir string) bool {
for _, skippablePathIndicator := range excludedPaths {
if strings.HasPrefix(path, skippablePathIndicator) || strings.HasPrefix(path, filepath.Join(baseDir, skippablePathIndicator)) {
log.Debugf("Path %s is skippable", path)
return true
}
if strings.Contains(path, skippablePathIndicator) || strings.Contains(path, filepath.Join(baseDir, skippablePathIndicator)) {
log.Debugf("Path %s is skippable", path)
return true
}
}
log.Debugf("Path %s is not skippable", path)
return false
}
// IsSkippableFileExtension Checks if the file extension is excluded
func IsSkippableFileExtension(excludedExtensions []string, path string) bool {
extension := strings.ToLower(filepath.Ext(path))
for _, skippableExt := range excludedExtensions {
if extension == skippableExt {
return true
}
// special check of .so
if skippableExt == ".so" {
if IsSharedLibrary(path) {
return true
}
}
}
return false
}
// UpdateDirsPermissionsRW Update permissions for dirs in container images, so that they can be properly deleted
func UpdateDirsPermissionsRW(dir string) error {
return filepath.WalkDir(dir, func(path string, f os.DirEntry, err error) error {
if f.IsDir() {
err := os.Chmod(path, 0700)
if err != nil {
log.Errorf("Error updating permissions for dir %s: %s", path, err)
}
}
return nil
})
}
// IsSharedLibrary Checks if the file is a shared library
func IsSharedLibrary(path string) bool {
if path == "" {
return false
}
if strings.HasSuffix(path, ".so") {
return true
}
// check if the file is a shared library
// it could .so, .so.1, .so.1.2.3
// https://en.wikipedia.org/wiki/Shared_library
// below wouldn't work for .so.1.2.3
// extension := strings.ToLower(filepath.Ext(path))
filename := filepath.Base(path)
splitExt := strings.Split(filename, ".")
if len(splitExt) > 1 {
if splitExt[1] == "so" {
return true
}
}
return false
}