| 1 |
|
| 2 |
package watcher |
| 3 |
|
| 4 |
import ( |
| 5 |
|
| 6 |
|
| 7 |
"os" |
| 8 |
"path/filepath" |
| 9 |
"strings" |
| 10 |
"sync" |
| 11 |
"time" |
| 12 |
|
| 13 |
"github.com/mgutz/str" |
| 14 |
"gopkg.in/godo.v2/watcher/fswatch" |
| 15 |
) |
| 16 |
|
| 17 |
const ( |
| 18 |
|
| 19 |
|
| 20 |
IgnoreThresholdRange = 50 * 1000000 |
| 21 |
) |
| 22 |
|
| 23 |
|
| 24 |
func SetWatchDelay(delay time.Duration) { |
| 25 |
fswatch.WatchDelay = delay |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
type Watcher struct { |
| 36 |
*fswatch.Watcher |
| 37 |
Event chan *FileEvent |
| 38 |
Error chan error |
| 39 |
|
| 40 |
IgnorePathFn func(path string) bool |
| 41 |
|
| 42 |
ErrorHandler func(err error) |
| 43 |
isClosed bool |
| 44 |
quit chan bool |
| 45 |
cache map[string]*os.FileInfo |
| 46 |
mu sync.Mutex |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
func NewWatcher(bufferSize int) (watcher *Watcher, err error) { |
| 51 |
|
| 52 |
fswatcher := fswatch.NewAutoWatcher() |
| 53 |
|
| 54 |
if err != nil { |
| 55 |
return nil, err |
| 56 |
} |
| 57 |
watcher = &Watcher{ |
| 58 |
Watcher: fswatcher, |
| 59 |
Error: make(chan error, 10), |
| 60 |
Event: make(chan *FileEvent, bufferSize), |
| 61 |
IgnorePathFn: DefaultIgnorePathFn, |
| 62 |
cache: map[string]*os.FileInfo{}, |
| 63 |
} |
| 64 |
return |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
func (w *Watcher) Close() error { |
| 69 |
if w.isClosed { |
| 70 |
return nil |
| 71 |
} |
| 72 |
w.Watcher.Stop() |
| 73 |
w.quit <- true |
| 74 |
w.isClosed = true |
| 75 |
return nil |
| 76 |
} |
| 77 |
|
| 78 |
func (w *Watcher) eventLoop() { |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
coutput := w.Watcher.Start() |
| 83 |
for { |
| 84 |
event, ok := <-coutput |
| 85 |
if !ok { |
| 86 |
return |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
if w.IgnorePathFn(event.Path) { |
| 91 |
continue |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
if event.Event == fswatch.DELETED || event.Event == fswatch.NOEXIST { |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
w.Event <- newFileEvent(event.Event, event.Path, time.Now().UnixNano()-10) |
| 100 |
continue |
| 101 |
} |
| 102 |
|
| 103 |
fi, err := os.Stat(event.Path) |
| 104 |
if os.IsNotExist(err) { |
| 105 |
|
| 106 |
continue |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
w.mu.Lock() |
| 115 |
oldFI := w.cache[event.Path] |
| 116 |
w.cache[event.Path] = &fi |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
if oldFI != nil && fi.ModTime().UnixNano() < (*oldFI).ModTime().UnixNano()+IgnoreThresholdRange { |
| 124 |
w.mu.Unlock() |
| 125 |
continue |
| 126 |
} |
| 127 |
w.mu.Unlock() |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
w.Event <- newFileEvent(event.Event, event.Path, fi.ModTime().UnixNano()) |
| 133 |
|
| 134 |
if err != nil { |
| 135 |
|
| 136 |
if os.IsNotExist(err) { |
| 137 |
continue |
| 138 |
} |
| 139 |
w.errorHandle(err) |
| 140 |
continue |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
} |
| 148 |
} |
| 149 |
func (w *Watcher) errorHandle(err error) { |
| 150 |
if w.ErrorHandler == nil { |
| 151 |
w.Error <- err |
| 152 |
return |
| 153 |
} |
| 154 |
w.ErrorHandler(err) |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
func (w *Watcher) GetErrorChan() chan error { |
| 159 |
return w.Error |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
func (w *Watcher) GetEventChan() chan *FileEvent { |
| 164 |
return w.Event |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
func (w *Watcher) WatchRecursive(path string) error { |
| 170 |
path, err := filepath.Abs(path) |
| 171 |
if err != nil { |
| 172 |
return err |
| 173 |
} |
| 174 |
|
| 175 |
_, err = os.Stat(path) |
| 176 |
if err != nil { |
| 177 |
return err |
| 178 |
} |
| 179 |
|
| 180 |
w.Watcher.Add(path) |
| 181 |
|
| 182 |
|
| 183 |
return nil |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
func (w *Watcher) Start() { |
| 188 |
go w.eventLoop() |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
func DefaultIgnorePathFn(path string) bool { |
| 212 |
if strings.HasPrefix(path, ".") || strings.Contains(path, "/.") { |
| 213 |
return true |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
if strings.HasPrefix(path, "node_modules") || strings.Contains(path, "/node_modules") { |
| 218 |
return true |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
base := filepath.Base(path) |
| 223 |
if str.IsNumeric(base) { |
| 224 |
return true |
| 225 |
} |
| 226 |
return false |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
func (w *Watcher) SetIgnorePathFn(fn func(string) bool) { |
| 232 |
w.Watcher.IgnorePathFn = fn |
| 233 |
} |