-
Notifications
You must be signed in to change notification settings - Fork 46
/
path.go
279 lines (237 loc) · 6.26 KB
/
path.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package guard
import (
"github.com/yaklang/yaklang/common/log"
"github.com/yaklang/yaklang/common/utils"
"io/ioutil"
"os"
"regexp"
"sync"
)
type GuardFileInfo struct {
os.FileInfo
Path string
Content []byte
}
type pathGuardCallback func(old *GuardFileInfo, new *GuardFileInfo)
type PathGuardTarget struct {
guardTargetBase
Path string
Recursive bool
recordOrigin bool
cacheFileSize int
contentChangeCallback pathGuardCallback
callback pathGuardCallback
fileCountLimit int
// include/exclude
includedRegexps []*regexp.Regexp
excludedRegexps []*regexp.Regexp
// map[string(Name)]os.FileInfo
cache *sync.Map
isFirst *utils.AtomicBool
disallowNewFile *utils.AtomicBool
origin *sync.Map
recordOriginOnce *sync.Once
}
type PathGuardTargetOption func(p *PathGuardTarget) error
func SetPathGuardCacheFileSize(byteSize int) PathGuardTargetOption {
return func(p *PathGuardTarget) error {
p.cacheFileSize = byteSize
return nil
}
}
func SetPathUnserRecovered(r bool) PathGuardTargetOption {
return func(p *PathGuardTarget) error {
p.recordOrigin = r
return nil
}
}
func SetPathGuardCallback(f pathGuardCallback) PathGuardTargetOption {
return func(p *PathGuardTarget) error {
p.callback = f
return nil
}
}
func SetPathGuardContentChangeCallback(f pathGuardCallback) PathGuardTargetOption {
return func(p *PathGuardTarget) error {
if p.cacheFileSize <= 0 {
return utils.Errorf("cache file size is not set")
}
p.contentChangeCallback = f
return nil
}
}
func SetPathGuardFileCountLimit(i int) PathGuardTargetOption {
return func(p *PathGuardTarget) error {
p.fileCountLimit = i
return nil
}
}
func SetPathGuardFirstToNotify() PathGuardTargetOption {
return func(p *PathGuardTarget) error {
p.isFirst = utils.NewBool(false)
return nil
}
}
func SetPathNameIncludes(s ...string) PathGuardTargetOption {
return func(p *PathGuardTarget) error {
for _, sub := range s {
r, err := utils.StarAsWildcardToRegexp("", sub)
if err != nil {
return utils.Errorf("compile include rule[%s] failed: %s", sub, err)
}
p.includedRegexps = append(p.includedRegexps, r)
}
return nil
}
}
func SetDisallowNewFile(s ...string) PathGuardTargetOption {
return func(p *PathGuardTarget) error {
p.disallowNewFile.Set()
return nil
}
}
func SetPathNameExcludes(s ...string) PathGuardTargetOption {
return func(p *PathGuardTarget) error {
for _, sub := range s {
r, err := utils.StarAsWildcardToRegexp("", sub)
if err != nil {
return utils.Errorf("compile exclude rule[%s] failed: %s", sub, err)
}
log.Info(r.String())
p.excludedRegexps = append(p.excludedRegexps, r)
}
return nil
}
}
func (p *PathGuardTarget) shouldContinueByPath(path string) bool {
// 如果设置了 include, 则只检查 include 里面的
if p.includedRegexps != nil {
shouldContinue := false
for _, include := range p.includedRegexps {
if include.MatchString(path) {
shouldContinue = true
break
}
}
if !shouldContinue {
return false
}
}
for _, exclude := range p.excludedRegexps {
if exclude.MatchString(path) {
return false
}
}
return true
}
func (p *PathGuardTarget) do() {
if (p.callback == nil && (p.contentChangeCallback == nil || p.cacheFileSize <= 0)) || p.disallowNewFile.IsSet() {
return
}
state, e := os.Stat(p.Path)
if e != nil {
return
}
var raw []byte
if !state.IsDir() && state.Size() <= int64(p.cacheFileSize) {
raw, _ = ioutil.ReadFile(p.Path)
}
var (
infos = []*GuardFileInfo{{FileInfo: state, Path: p.Path, Content: raw}}
err error
)
if state.IsDir() {
var infosRaw []*utils.FileInfo
if p.Recursive {
infosRaw, err = utils.ReadFilesRecursivelyWithLimit(p.Path, p.fileCountLimit)
if err != nil {
log.Errorf("read %v's recursive failed: %s", p.Path, err)
return
}
} else {
infosRaw, err = utils.ReadDirWithLimit(p.Path, p.fileCountLimit)
if err != nil {
log.Errorf("read dir[%s] failed: %s", p.Path, err)
return
}
}
for _, r := range infosRaw {
if !p.shouldContinueByPath(r.Path) {
continue
}
var raw []byte
if !r.BuildIn.IsDir() && r.BuildIn.Size() <= int64(p.cacheFileSize) {
raw, _ = ioutil.ReadFile(p.Path)
}
infos = append(infos, &GuardFileInfo{
FileInfo: r.BuildIn,
Path: r.Path,
Content: raw,
})
}
}
// 监控文件内容了
if len(infos) > 0 && p.isFirst.IsSet() {
defer p.isFirst.UnSet()
}
for _, info := range infos {
//log.Infof("monitor path: %s", info.Path)
data, ok := p.cache.Load(FileInfoToHash(info))
if !ok {
// 如果不允许创建新文件了,就不做缓存,直接删除新建的文件
if !p.isFirst.IsSet() && p.disallowNewFile.IsSet() {
log.Infof("disallow to create new file: %v auto deleted", info.Path)
err := os.RemoveAll(info.Path)
if err != nil {
log.Errorf("remove path failed: %s", err)
}
continue
}
// 如果允许创建新文件
// 如果有新文件就直接汇报
p.cache.Store(FileInfoToHash(info), info)
if (!p.isFirst.IsSet()) && p.callback != nil {
p.callback(nil, info)
}
continue
}
oldData := data.(*GuardFileInfo)
newData := info
if FileInfoEqual(oldData, newData) {
continue
} else {
p.cache.Store(FileInfoToHash(info), info)
}
// 第一次执行,就不要执行 callback 了,不然监控的文件太多会炸掉
if !p.isFirst.IsSet() {
if p.callback != nil {
p.callback(oldData, newData)
}
if p.cacheFileSize > 0 && p.contentChangeCallback != nil &&
utils.CalcSha1(oldData.Content) != utils.CalcSha1(newData.Content) {
p.contentChangeCallback(oldData, newData)
}
}
}
// 缓存第一次启动时候的监控文件目录上
if p.recordOrigin {
p.recordOriginOnce.Do(func() {
// 监控文件内容了
for _, info := range infos {
if info.IsDir() {
continue
}
if p.cacheFileSize > 0 && info.Size() <= int64(p.cacheFileSize) {
p.origin.Store(info.Path, info)
}
}
})
}
}
func FileInfoToHash(c *GuardFileInfo) string {
return utils.CalcSha1(c.Path, c.IsDir())
}
func FileInfoEqual(old, current *GuardFileInfo) bool {
return old.Path == current.Path && old.IsDir() == current.IsDir() &&
old.Mode() == current.Mode() && old.ModTime().Equal(current.ModTime())
}