Repository navigation
Expand file tree
/
Copy pathmonitor.go
More file actions
235 lines (211 loc) · 4.86 KB
/
Copy pathmonitor.go
File metadata and controls
235 lines (211 loc) · 4.86 KB
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
/*
Copyright 2016 Wenhui Shen <www.webx.top>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com
import (
"errors"
"log"
"os"
"path/filepath"
"sync"
"github.com/admpub/fsnotify"
)
var (
DefaultMonitor = NewMonitor()
MonitorEventEmptyFn = func(string) {}
)
func NewMonitor() *MonitorEvent {
return &MonitorEvent{
Create: MonitorEventEmptyFn,
Delete: MonitorEventEmptyFn,
Modify: MonitorEventEmptyFn,
Chmod: MonitorEventEmptyFn,
Rename: MonitorEventEmptyFn,
filters: []func(string) bool{},
}
}
//MonitorEvent 监控事件函数
type MonitorEvent struct {
//文件事件
Create func(string) //创建
Delete func(string) //删除(包含文件夹和文件。因为已经删除,无法确定是文件夹还是文件)
Modify func(string) //修改(包含修改权限。如果是文件夹,则内部的文件被更改也会触发此事件)
Chmod func(string) //修改权限(windows不支持)
Rename func(string) //重命名
//其它
Channel chan bool //管道
Debug bool
watcher *fsnotify.Watcher
filters []func(string) bool
lock sync.RWMutex
}
func (m *MonitorEvent) AddFilter(args ...func(string) bool) *MonitorEvent {
if m.filters == nil {
m.filters = []func(string) bool{}
}
m.filters = append(m.filters, args...)
return m
}
func (m *MonitorEvent) SetFilters(args ...func(string) bool) *MonitorEvent {
m.filters = args
return m
}
func (m *MonitorEvent) Watch(args ...func(string) bool) *MonitorEvent {
m.SetFilters(args...)
go func() {
m.backendListen()
<-m.Channel
}()
return m
}
func (m *MonitorEvent) Close() error {
if m.Channel != nil {
close(m.Channel)
}
if m.watcher != nil {
return m.watcher.Close()
}
return nil
}
func (m *MonitorEvent) Watcher() *fsnotify.Watcher {
m.lock.Lock()
defer m.lock.Unlock()
if m.watcher == nil {
var err error
m.watcher, err = fsnotify.NewWatcher()
m.Channel = make(chan bool)
m.filters = []func(string) bool{}
if err != nil {
log.Panic(err)
}
}
return m.watcher
}
func (m *MonitorEvent) backendListen() *MonitorEvent {
go m.listen()
return m
}
func (m *MonitorEvent) AddDir(dir string) error {
f, err := os.Stat(dir)
if err != nil {
return err
}
if !f.IsDir() {
return errors.New(dir + ` is not dir.`)
}
err = filepath.Walk(dir, func(f string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
if m.Debug {
log.Println(`[Monitor]`, `Add Watch:`, f)
}
return m.Watcher().Add(f)
}
return nil
})
return err
}
func (m *MonitorEvent) AddFile(file string) error {
if m.Debug {
log.Println(`[Monitor]`, `Add Watch:`, file)
}
return m.Watcher().Add(file)
}
func (m *MonitorEvent) Remove(fileOrDir string) error {
if m.watcher != nil {
return m.watcher.Remove(fileOrDir)
}
return nil
}
func (m *MonitorEvent) listen() {
for {
watcher := m.Watcher()
select {
case ev, ok := <-watcher.Events:
if !ok {
return
}
if m.Debug {
log.Println(`[Monitor]`, `Trigger Event:`, ev)
}
if m.filters != nil {
var skip bool
for _, filter := range m.filters {
if !filter(ev.Name) {
skip = true
break
}
}
if skip {
break
}
}
switch {
case ev.Op&fsnotify.Create == fsnotify.Create:
if m.IsDir(ev.Name) {
watcher.Add(ev.Name)
}
if m.Create != nil {
m.Create(ev.Name)
}
case ev.Op&fsnotify.Remove == fsnotify.Remove:
if m.IsDir(ev.Name) {
watcher.Remove(ev.Name)
}
if m.Delete != nil {
m.Delete(ev.Name)
}
case ev.Op&fsnotify.Write == fsnotify.Write:
if m.Modify != nil {
m.Modify(ev.Name)
}
case ev.Op&fsnotify.Rename == fsnotify.Rename:
watcher.Remove(ev.Name)
if m.Rename != nil {
m.Rename(ev.Name)
}
case ev.Op&fsnotify.Chmod == fsnotify.Chmod:
if m.Chmod != nil {
m.Chmod(ev.Name)
}
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
if err != nil {
log.Println("Watcher error:", err)
}
}
}
}
func (m *MonitorEvent) IsDir(path string) bool {
d, e := os.Stat(path)
if e != nil {
return false
}
return d.IsDir()
}
//Monitor 文件监测
func Monitor(rootDir string, callback *MonitorEvent, args ...func(string) bool) error {
watcher := callback.Watcher()
defer watcher.Close()
callback.Watch(args...)
err := callback.AddDir(rootDir)
if err != nil {
callback.Close()
return err
}
return nil
}