Fix: file-watcher bugs (#1897)
* Fix: file-watcher bugs * Update watcher_linux.go Co-authored-by: 杨铭哲 <yangmz@weipaitang.com> Co-authored-by: Asim Aslam <asim@aslam.me>
This commit is contained in:
@@ -39,26 +39,36 @@ func (w *watcher) Next() (*source.ChangeSet, error) {
|
||||
default:
|
||||
}
|
||||
|
||||
// try get the event
|
||||
select {
|
||||
case event, _ := <-w.fw.Events:
|
||||
if event.Op == fsnotify.Rename {
|
||||
// check existence of file, and add watch again
|
||||
_, err := os.Stat(event.Name)
|
||||
if err == nil || os.IsExist(err) {
|
||||
w.fw.Add(event.Name)
|
||||
for {
|
||||
// try get the event
|
||||
select {
|
||||
case event, _ := <-w.fw.Events:
|
||||
if event.Op == fsnotify.Rename {
|
||||
// check existence of file, and add watch again
|
||||
_, err := os.Stat(event.Name)
|
||||
if err == nil || os.IsExist(err) {
|
||||
w.fw.Add(event.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
c, err := w.f.Read()
|
||||
if err != nil {
|
||||
// ARCH: Darwin Kernel Version 18.7.0
|
||||
// ioutil.WriteFile truncates it before writing, but the problem is that
|
||||
// you will receive two events(fsnotify.Chmod and fsnotify.Write).
|
||||
// We can solve this problem by ignoring fsnotify.Chmod event.
|
||||
if event.Op&fsnotify.Write != fsnotify.Write {
|
||||
continue
|
||||
}
|
||||
|
||||
c, err := w.f.Read()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return c, nil
|
||||
case err := <-w.fw.Errors:
|
||||
return nil, err
|
||||
case <-w.exit:
|
||||
return nil, source.ErrWatcherStopped
|
||||
}
|
||||
return c, nil
|
||||
case err := <-w.fw.Errors:
|
||||
return nil, err
|
||||
case <-w.exit:
|
||||
return nil, source.ErrWatcherStopped
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user