-
-
Notifications
You must be signed in to change notification settings - Fork 264
/
file.go
231 lines (192 loc) · 4.77 KB
/
file.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package processor
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"sync"
"github.com/dbaggerman/cuba"
"github.com/monochromegane/go-gitignore"
)
// Used as quick lookup for files with the same name to avoid some processing
// needs to be sync.Map as it potentially could be called by many GoRoutines
var extensionCache sync.Map
// A custom version of extracting extensions for a file
// which also has a case insensitive cache in order to save
// some needless processing
func getExtension(name string) string {
name = strings.ToLower(name)
extension, ok := extensionCache.Load(name)
if ok {
return extension.(string)
}
ext := filepath.Ext(name)
if ext == "" || strings.LastIndex(name, ".") == 0 {
extension = name
} else {
// Handling multiple dots or multiple extensions only needs to delete the last extension
// and then call filepath.Ext.
// If there are multiple extensions, it is the value of subExt,
// otherwise subExt is an empty string.
subExt := filepath.Ext(strings.TrimSuffix(name, ext))
extension = strings.TrimPrefix(subExt+ext, ".")
}
extensionCache.Store(name, extension)
return extension.(string)
}
type DirectoryJob struct {
root string
path string
ignores []gitignore.IgnoreMatcher
}
type DirectoryWalker struct {
buffer *cuba.Pool
output chan<- *FileJob
excludes []*regexp.Regexp
}
func NewDirectoryWalker(output chan<- *FileJob) *DirectoryWalker {
directoryWalker := &DirectoryWalker{
output: output,
}
for _, exclude := range Exclude {
directoryWalker.excludes = append(directoryWalker.excludes, regexp.MustCompile(exclude))
}
directoryWalker.buffer = cuba.New(directoryWalker.Readdir, cuba.NewStack())
return directoryWalker
}
func (dw *DirectoryWalker) Walk(root string) error {
root = filepath.Clean(root)
fileInfo, err := os.Stat(root)
if err != nil {
return err
}
if !fileInfo.IsDir() {
fileJob := newFileJob(root, filepath.Base(root), fileInfo)
if fileJob != nil {
dw.output <- fileJob
}
return nil
}
_ = dw.buffer.Push(
&DirectoryJob{
root: root,
path: root,
ignores: nil,
},
)
return nil
}
func (dw *DirectoryWalker) Run() {
dw.buffer.Finish()
close(dw.output)
}
func (dw *DirectoryWalker) Readdir(handle *cuba.Handle) {
job := handle.Item().(*DirectoryJob)
ignores := job.ignores
file, err := os.Open(job.path)
if err != nil {
printError(fmt.Sprintf("failed to open %s: %v", job.path, err))
return
}
defer file.Close()
dirents, err := file.Readdir(-1)
if err != nil {
printError(fmt.Sprintf("failed to read %s: %v", job.path, err))
return
}
for _, dirent := range dirents {
name := dirent.Name()
if (!GitIgnore && name == ".gitignore") || (!Ignore && name == ".ignore") {
path := filepath.Join(job.path, name)
ignore, err := gitignore.NewGitIgnore(path)
if err != nil {
printError(fmt.Sprintf("failed to load gitignore %s: %v", job.path, err))
}
ignores = append(ignores, ignore)
}
}
DIRENTS:
for _, dirent := range dirents {
name := dirent.Name()
path := filepath.Join(job.path, name)
isDir := dirent.IsDir()
for _, deny := range PathDenyList {
if strings.HasSuffix(path, deny) {
if Verbose {
printWarn(fmt.Sprintf("skipping directory due to being in denylist: %s", path))
}
continue DIRENTS
}
}
for _, exclude := range dw.excludes {
if exclude.Match([]byte(name)) {
if Verbose {
printWarn("skipping directory due to match exclude: " + name)
}
continue DIRENTS
}
}
for _, ignore := range ignores {
if ignore.Match(path, isDir) {
if Verbose {
printWarn("skipping directory due to ignore: " + path)
}
continue DIRENTS
}
}
if isDir {
handle.Push(
&DirectoryJob{
root: job.root,
path: path,
ignores: ignores,
},
)
} else {
fileJob := newFileJob(path, name, dirent)
if fileJob != nil {
dw.output <- fileJob
}
}
}
}
func newFileJob(path, name string, fileInfo os.FileInfo) *FileJob {
if NoLarge {
if fileInfo.Size() >= LargeByteCount {
if Verbose {
printWarn(fmt.Sprintf("skipping large file due to byte size: %s", path))
}
return nil
}
}
language, extension := DetectLanguage(name)
if len(language) != 0 {
if len(AllowListExtensions) != 0 {
ok := false
for _, x := range AllowListExtensions {
if x == extension {
ok = true
}
}
if !ok {
if Verbose {
printWarn(fmt.Sprintf("skipping file as not in allow list: %s", name))
}
return nil
}
}
for _, l := range language {
LoadLanguageFeature(l)
}
return &FileJob{
Location: path,
Filename: name,
Extension: extension,
PossibleLanguages: language,
}
} else if Verbose {
printWarn(fmt.Sprintf("skipping file unknown extension: %s", name))
}
return nil
}