-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxfg_scan_content.go
209 lines (174 loc) · 4.78 KB
/
xfg_scan_content.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"bufio"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
)
type scanFile struct {
lc int32 // line count
l string // line text
blines []line // slice for before lines
aline uint32 // the count for after lines
matchedContents []line // result
}
func (x *xfg) postMatchPath(fPath string, fInfo fs.DirEntry) (err error) {
matchedPath := path{
info: fInfo,
}
if (len(x.options.SearchGrep) > 0 || len(x.extra.searchGrepRe) > 0) && isRegularFile(fInfo) {
matchedPath.contents, err = x.scanFile(fPath)
if err != nil {
return fmt.Errorf("scanFile() : %w", err)
}
}
if x.options.extra.onlyMatchContent && len(matchedPath.contents) == 0 {
return nil // not pick up
}
return x.postScanFile(fPath, fInfo, matchedPath)
}
func (x *xfg) scanFile(fPath string) ([]line, error) {
if x.options.Stats {
x.cli.stats.IncrScannedFile()
}
fh, err := os.Open(fPath)
if err != nil {
if errors.Is(err, fs.ErrPermission) {
if !x.options.IgnorePermissionError {
x.cli.putErr(err)
}
return nil, nil
}
return nil, fmt.Errorf("path `%s` : %w", fPath, err)
}
defer fh.Close()
isBinary, err := isBinaryFile(fh)
if err != nil {
return nil, fmt.Errorf("path `%s` : %w", fPath, err)
}
if isBinary {
return nil, nil
}
if _, err := fh.Seek(0, 0); err != nil {
return nil, fmt.Errorf("could not seek `%s` : %w", fPath, err)
}
matchedContents, err := x.scanContent(bufio.NewScanner(fh), fPath)
if err != nil {
return nil, fmt.Errorf("scanContent() `%s` : %w", fPath, err)
}
return matchedContents, nil
}
func (x *xfg) postScanFile(fPath string, fInfo fs.DirEntry, matchedPath path) error {
if x.options.Abs {
absPath, err := filepath.Abs(fPath)
if err != nil {
return fmt.Errorf("failed to get abs path of `%s` : %w", fPath, err)
}
fPath = absPath
}
if fInfo.IsDir() {
fPath = fPath + string(filepath.Separator)
}
matchedPath.path = fPath
x.result.mu.Lock()
x.result.paths = append(x.result.paths, matchedPath)
x.result.outputLC = x.result.outputLC + len(matchedPath.contents) + 1
x.result.mu.Unlock()
if x.options.Stats {
x.cli.stats.AddPickedLC(len(matchedPath.contents))
}
return nil
}
func (x *xfg) scanContent(scanner *bufio.Scanner, fPath string) ([]line, error) {
gf := &scanFile{
lc: 0,
blines: make([]line, x.options.extra.actualBeforeContextLines),
}
for scanner.Scan() {
gf.lc++
gf.l = scanner.Text()
if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("could not scan file `%s` line %d: %w", fPath, gf.lc, err)
}
x.processContentLine(gf)
if x.options.FilesWithMatches && len(gf.matchedContents) > 0 {
break
}
if x.options.MaxMatchCount != 0 && int(x.options.MaxMatchCount) <= len(gf.matchedContents) {
break
}
}
if x.options.Stats {
x.cli.stats.IncrScannedLC(int(gf.lc))
}
if x.options.Quiet && !x.result.alreadyMatchContent && len(gf.matchedContents) > 0 {
x.result.alreadyMatchContent = true
}
return gf.matchedContents, nil
}
func (x *xfg) isMatchLine(line string) bool {
if x.options.IgnoreCase && len(x.extra.searchGrepi) > 0 {
for _, sgr := range x.extra.searchGrepi {
if !isMatchRegexp(line, sgr) {
return false
}
}
} else {
if len(x.options.SearchGrep) > 0 {
for _, sg := range x.options.SearchGrep {
if !isMatch(line, sg) {
return false
}
}
}
}
if len(x.extra.searchGrepRe) > 0 {
for _, re := range x.extra.searchGrepRe {
if !isMatchRegexp(line, re) {
return false
}
}
}
return true // OK, match all
}
func (x *xfg) processContentLine(gf *scanFile) {
if x.isMatchLine(gf.l) {
if !x.options.ShowMatchCount && x.options.extra.withBeforeContextLines {
for _, bl := range gf.blines {
if bl.lc == 0 {
continue // skip
}
gf.matchedContents = append(gf.matchedContents, bl)
}
gf.blines = make([]line, x.options.extra.actualBeforeContextLines)
}
if x.options.ShowMatchCount {
gf.l = ""
}
x.optimizeLine(gf)
gf.matchedContents = append(gf.matchedContents, line{lc: gf.lc, content: gf.l, matched: true})
if !x.options.ShowMatchCount && x.options.extra.withAfterContextLines {
gf.aline = x.options.extra.actualAfterContextLines // start countdown for `aline`
}
} else {
if !x.options.ShowMatchCount {
if x.options.extra.withAfterContextLines && gf.aline > 0 {
gf.aline--
x.optimizeLine(gf)
gf.matchedContents = append(gf.matchedContents, line{lc: gf.lc, content: gf.l})
} else if x.options.extra.withBeforeContextLines {
// rotate blines
// join "2nd to last elements of `blines`" and "current `line`"
x.optimizeLine(gf)
gf.blines = append(gf.blines[1:], line{lc: gf.lc, content: gf.l})
}
}
}
}
func (x *xfg) optimizeLine(gf *scanFile) {
if x.options.MaxColumns > 0 && len(gf.l) > int(x.options.MaxColumns) {
gf.l = gf.l[:x.options.MaxColumns]
}
}