-
Notifications
You must be signed in to change notification settings - Fork 0
/
watcher.go
179 lines (151 loc) · 3.31 KB
/
watcher.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
package file
import (
"fmt"
"log"
"os"
"path/filepath"
"sync"
"time"
"github.com/fsnotify/fsnotify"
"github.com/lanceryou/confd/loader"
)
type Event struct {
Op loader.OpType
Path string
}
type EventHandler func(event Event) error
type FileWatcher struct {
watcher *fsnotify.Watcher
watchedMu sync.RWMutex
watched map[string]EventHandler
noExistPath chan string
eventsDone chan struct{}
closeOnce sync.Once
}
func NewFileWatcher() (fileWatcher *FileWatcher, err error) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return
}
fileWatcher = &FileWatcher{
watcher: watcher,
watched: make(map[string]EventHandler),
eventsDone: make(chan struct{}, 1),
noExistPath: make(chan string, 1),
}
go fileWatcher.runEvents()
go fileWatcher.runWatchPath()
return
}
func (w *FileWatcher) runWatchPath() {
noExistPaths := make(map[string]struct{})
for {
select {
case p := <-w.noExistPath:
noExistPaths[p] = struct{}{}
case <-time.After(time.Second):
var add []string
for k := range noExistPaths {
if !isExists(k) {
continue
}
add = append(add, k)
}
for _, p := range add {
if err := w.addWatch(p); err == nil {
delete(noExistPaths, p)
}
}
}
}
}
func isExists(path string) bool {
_, err := os.Stat(path) // os.Stat获取文件信息
if err != nil && !os.IsExist(err) {
return false
}
return true
}
func (w *FileWatcher) runEvents() {
defer close(w.eventsDone)
go func() {
for err := range w.watcher.Errors {
log.Println(err)
}
}()
for e := range w.watcher.Events {
switch {
case e.Op&fsnotify.Create == fsnotify.Create:
w.sendEvent(Event{loader.Create, e.Name})
case e.Op&fsnotify.Write == fsnotify.Write,
e.Op&fsnotify.Chmod == fsnotify.Chmod:
w.sendEvent(Event{loader.Update, e.Name})
case e.Op&fsnotify.Remove == fsnotify.Remove:
w.sendEvent(Event{loader.Delete, e.Name})
case e.Op&fsnotify.Rename == fsnotify.Rename:
// Rename is only issued on the original file path; the new name receives a Create event
w.sendEvent(Event{loader.Delete, e.Name})
default:
panic(fmt.Sprintf("unknown op type %v", e.Op))
}
}
}
func (w *FileWatcher) sendEvent(event Event) {
w.watchedMu.RLock()
watch, ok := w.watched[event.Path]
w.watchedMu.RUnlock()
if !ok {
d := filepath.Dir(event.Path)
w.watchedMu.RLock()
watch, ok = w.watched[d]
w.watchedMu.RUnlock()
if !ok {
return
}
}
watch(event)
}
func (w *FileWatcher) AddWatch(path string, fn EventHandler) (err error) {
absPath, err := filepath.Abs(path)
if err != nil {
return err
}
w.watchedMu.RLock()
_, ok := w.watched[absPath]
w.watchedMu.RUnlock()
if ok {
return
}
w.watchedMu.Lock()
defer w.watchedMu.Unlock()
w.watched[absPath] = fn
err = w.addWatch(absPath)
if err != nil {
delete(w.watched, absPath)
}
return
}
func (w *FileWatcher) addWatch(path string) error {
if !isExists(path) {
w.noExistPath <- path
return nil
}
err := w.watcher.Add(path)
if err != nil && os.IsPermission(err) {
return fmt.Errorf(fmt.Sprintf("path %v IsPermission", path))
}
return err
}
func (w *FileWatcher) RemoveWatch(path string) {
w.watchedMu.Lock()
delete(w.watched, path)
w.watchedMu.Unlock()
}
func (w *FileWatcher) Close() {
w.closeOnce.Do(func() {
if w.watcher != nil {
w.watcher.Close()
<-w.eventsDone
}
})
}