Skip to content

Commit

Permalink
Added new processor to match file name
Browse files Browse the repository at this point in the history
  • Loading branch information
vinscom authored and spindriftvinay committed Jun 12, 2015
1 parent 3a1d68c commit b2b75bb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 35 deletions.
51 changes: 26 additions & 25 deletions processors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()

Expand All @@ -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
}
}
Expand Down
16 changes: 6 additions & 10 deletions zipscan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ")

Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit b2b75bb

Please sign in to comment.