diff --git a/processors.go b/processors.go index 97748f9..8f25e4f 100644 --- a/processors.go +++ b/processors.go @@ -52,8 +52,25 @@ func NewFileNameFilterProcessor(pFileNameMatchList []string) processorFn { } } +func NewMatchFileNameProcessor(pFnFileNameMatcher stringFinder,pEnable bool) processorFn { + if pEnable { + return func(f fileInfo, out fileInfoChannel) { + if pFnFileNameMatcher(f.name) { + f.foundPathMatch = true + } + out <- f + + } + } else { + return func(f fileInfo, out fileInfoChannel) { + out <- f + + } + } +} + //Scan Zip File -func NewZipFileProcessor(pFnFileNameMatcher stringFinder, pFnFileContentMatcher contentFinder, pContentSearchEnabled bool) processorFn { +func NewZipFileProcessor(pFnFileContentMatcher contentFinder, pContentSearchEnabled bool) processorFn { return func(f fileInfo, out fileInfoChannel) { fInfoList := make([]fileInfo, 0) @@ -67,19 +84,11 @@ func NewZipFileProcessor(pFnFileNameMatcher stringFinder, pFnFileContentMatcher f.processed = true fInfoList = append(fInfoList, f) - - if pFnFileNameMatcher(f.name) { - fInfoList[0].foundPathMatch = true - } for _, zFile := range zipFile.File { zFileInfo := fileInfo{false,f.path + "@" + zFile.FileInfo().Name(),zFile.FileInfo().Name(),zFile.FileInfo().IsDir(),false,false} - if pFnFileNameMatcher(zFile.Name) { - zFileInfo.foundPathMatch = true - } - if pContentSearchEnabled { zFileReader, _ := zFile.Open() @@ -102,30 +111,22 @@ func NewZipFileProcessor(pFnFileNameMatcher stringFinder, pFnFileContentMatcher } //Scan Noraml File -func NewNormalFileProcessor(pFnFileNameMatcher stringFinder, pFnFileContentMatcher contentFinder, pContentSearchEnabled bool) processorFn { +func NewNormalFileProcessor(pFnFileContentMatcher contentFinder, pContentSearchEnabled bool) processorFn { return func(f fileInfo, out fileInfoChannel) { - if f.processed { + if f.processed || !pContentSearchEnabled { out <- f return } + + file, err := os.Open(f.path) + defer file.Close() - //Match File Name - if pFnFileNameMatcher(f.name) { - f.foundPathMatch = true - } - - if pContentSearchEnabled { - file, err := os.Open(f.path) - defer file.Close() - - if err == nil && pFnFileContentMatcher(file) { - f.foundContentMatch = true - } - } + if err == nil && pFnFileContentMatcher(file) { + f.foundContentMatch = true + } f.processed = true - out <- f } } diff --git a/zipscan.go b/zipscan.go index 6c7c644..94dde56 100644 --- a/zipscan.go +++ b/zipscan.go @@ -45,7 +45,7 @@ func main() { flag.Var(&argFileFilterPattern, "f", "Comma seperated list of patterns of name. Only matching names will be search for content or name") argTargetDirectory := flag.String("d", ".", "Directory to scan (Symbolic Links are not followed)") - argSearchContent := flag.Bool("s", false, "Enable content search. To eanble add -s=true or -s If this is enabled then Content and File Name patterns become same") + argSearchContent := flag.Bool("s", false, "Enable content search. To eanble add -s=true or -s") argNamePattern := flag.String("p", ".*", "Regular expression of name of file or directory") argContentPattern := flag.String("c", "^$", "Regular expression we are looking in file ") @@ -57,13 +57,8 @@ func main() { patternContent, _ := regexp.Compile(*argContentPattern) //If content search is enabled then content pattern is also file pattern - var patternName *regexp.Regexp - - if *argSearchContent { - patternName = patternContent - } else { - patternName, _ = regexp.Compile(*argNamePattern) - } + patternName, _ := regexp.Compile(*argNamePattern) + //Setup Environment Config chain := make([]processorInfo, 0) @@ -74,8 +69,9 @@ func main() { chain = append(chain, processorInfo{NewProcessor(NewTraverseDirectoryProcessor(*argTargetDirectory)), 1}, processorInfo{NewProcessor(NewFileNameFilterProcessor(argFileFilterPattern)), 1}, - processorInfo{NewProcessor(NewZipFileProcessor(fnFileNameMatcher, fnFileContentMatcher, *argSearchContent)), runtime.NumCPU()}, - processorInfo{NewProcessor(NewNormalFileProcessor(fnFileNameMatcher, fnFileContentMatcher, *argSearchContent)), runtime.NumCPU()}, + processorInfo{NewProcessor(NewZipFileProcessor(fnFileContentMatcher, *argSearchContent)), runtime.NumCPU()}, + processorInfo{NewProcessor(NewNormalFileProcessor(fnFileContentMatcher, *argSearchContent)), runtime.NumCPU()}, + processorInfo{NewProcessor(NewMatchFileNameProcessor(fnFileNameMatcher, !(*argSearchContent))), 1}, processorInfo{NewProcessor(PrintToConsoleProcessor), 1}) done := SetupSystem(&chain)