-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinder.go
168 lines (136 loc) · 3.15 KB
/
finder.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"bufio"
"errors"
"io"
"os"
"path/filepath"
"regexp"
"strings"
"sync"
)
var wg sync.WaitGroup = sync.WaitGroup{}
var running chan bool = nil
var stopWalk chan bool = nil
type Finder struct {
Msg OutputHandler
Explorer *PathWalk
Options *Options
}
type Options struct {
MatchAll bool
Pattern *regexp.Regexp
Exclude []string
Include []string
LimitBytes int
}
func (app *Finder) Run(maxWorkers int) {
running = make(chan bool, maxWorkers)
stopWalk = make(chan bool)
app.Explorer.Walk(app.getWalkFunction())
wg.Wait()
close(stopWalk)
}
func (app *Finder) Stop() {
stopWalk <- true
<-stopWalk
}
func (app *Finder) getWalkFunction() filepath.WalkFunc {
return func(pathname string, info os.FileInfo, err error) error {
select {
case <-stopWalk:
return errors.New("walk ended")
default:
e := FileInfo{FileInfo: info, Path: pathname}
if err == nil {
return app.processEntry(e)
}
app.Msg.AddPathError(e.Path, err)
return nil
}
}
}
// Determines if the path entry should be skipped
func (app *Finder) shouldSkip(f FileInfo) bool {
// skipping regular files
if !f.IsDir() && !f.Mode().IsRegular() {
return true
}
isExcluded := matchCriteria(f, app.Options.Exclude)
isIncluded := matchCriteria(f, app.Options.Include)
exceedSize := (int64(app.Options.LimitBytes) != 0 && f.Size() > int64(app.Options.LimitBytes) && !f.IsDir())
if exceedSize || isExcluded {
return true
}
if len(app.Options.Include) != 0 && !f.IsDir() {
return !isIncluded
}
return false
}
// formatting text line in order to trim it
func formatMatchLine(line string) string {
return strings.TrimSpace(strings.Trim(line, "\t"))
}
// Process path and enqueues if ok for match checking
func (app *Finder) processEntry(f FileInfo) error {
if app.shouldSkip(f) {
if f.IsDir() {
return filepath.SkipDir
} else {
return nil
}
}
if f.IsDir() {
return nil
}
running <- true // hangs if the buffer is full
wg.Add(1)
go func() {
match, err := app.match(f, app.Options.MatchAll)
if err == nil && match != nil && len(match) != 0 {
app.Msg.AddMatches(f.Path, match)
}
<-running // frees one position in the buffer
wg.Done()
}()
return nil
}
func (app *Finder) match(f FileInfo, all bool) ([]*Match, error) {
var m []*Match = nil
file, err := os.Open(f.Path)
// generally this is due to permission errors
if err != nil {
return m, err
}
defer file.Close()
bufread := bufio.NewReader(file)
m = []*Match{}
count := 1 // counts line
for {
line, err := bufread.ReadBytes('\n')
if err == io.EOF {
break
}
if app.Options.Pattern.Match(line) {
m = append(m, NewMatch(count, formatMatchLine(string(line))))
if !all {
// just return the first one if all is false
return m, nil
}
}
count += 1
}
return m, nil
}
// matches a file with a set of patterns and returns true when
// a match is found, false otherwise
func matchCriteria(f FileInfo, criteria []string) bool {
for _, n := range criteria {
fullMatch, _ := filepath.Match(n, f.Path)
envMatch, _ := filepath.Match(n, filepath.Base(f.Path))
if fullMatch || envMatch {
return true
}
}
return false
}