2019-12-24 02:06:07 +00:00
|
|
|
package manager
|
|
|
|
|
|
|
|
import (
|
2020-10-12 23:12:46 +00:00
|
|
|
"path/filepath"
|
2019-12-24 02:06:07 +00:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2020-04-24 23:32:55 +00:00
|
|
|
|
|
|
|
"github.com/stashapp/stash/pkg/logger"
|
2019-12-24 02:06:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func excludeFiles(files []string, patterns []string) ([]string, int) {
|
|
|
|
if patterns == nil {
|
|
|
|
logger.Infof("No exclude patterns in config")
|
|
|
|
return files, 0
|
|
|
|
} else {
|
|
|
|
var results []string
|
|
|
|
var exclCount int
|
|
|
|
|
|
|
|
fileRegexps := generateRegexps(patterns)
|
|
|
|
|
|
|
|
if len(fileRegexps) == 0 {
|
|
|
|
logger.Infof("Excluded 0 files from scan")
|
|
|
|
return files, 0
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(files); i++ {
|
|
|
|
if matchFileSimple(files[i], fileRegexps) {
|
|
|
|
logger.Infof("File matched pattern. Excluding:\"%s\"", files[i])
|
|
|
|
exclCount++
|
|
|
|
} else {
|
|
|
|
|
|
|
|
//if pattern doesn't match add file to list
|
|
|
|
results = append(results, files[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
logger.Infof("Excluded %d file(s) from scan", exclCount)
|
|
|
|
return results, exclCount
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func matchFile(file string, patterns []string) bool {
|
2020-04-24 23:32:55 +00:00
|
|
|
if patterns != nil {
|
2019-12-24 02:06:07 +00:00
|
|
|
fileRegexps := generateRegexps(patterns)
|
|
|
|
|
|
|
|
for _, regPattern := range fileRegexps {
|
|
|
|
if regPattern.MatchString(strings.ToLower(file)) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateRegexps(patterns []string) []*regexp.Regexp {
|
|
|
|
|
|
|
|
var fileRegexps []*regexp.Regexp
|
|
|
|
|
|
|
|
for _, pattern := range patterns {
|
|
|
|
reg, err := regexp.Compile(strings.ToLower(pattern))
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("Exclude :%v", err)
|
|
|
|
} else {
|
|
|
|
fileRegexps = append(fileRegexps, reg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(fileRegexps) == 0 {
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return fileRegexps
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func matchFileSimple(file string, regExps []*regexp.Regexp) bool {
|
|
|
|
for _, regPattern := range regExps {
|
|
|
|
if regPattern.MatchString(strings.ToLower(file)) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2020-10-12 23:12:46 +00:00
|
|
|
|
|
|
|
func matchExtension(path string, extensions []string) bool {
|
|
|
|
ext := filepath.Ext(path)
|
|
|
|
for _, e := range extensions {
|
|
|
|
if strings.ToLower(ext) == strings.ToLower("."+e) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|