vendor: Update github.com/syncthing/notify (fixes #4854) (#5032)

This commit is contained in:
Simon Frei 2018-06-26 10:13:39 +02:00 committed by Jakob Borg
parent 7b0d8c2e77
commit 406b394704
8 changed files with 93 additions and 108 deletions

View File

@ -1,4 +1,4 @@
// Copyright (c) 2014-2015 The Notify Authors. All rights reserved. // Copyright (c) 2014-2018 The Notify Authors. All rights reserved.
// Use of this source code is governed by the MIT license that can be // Use of this source code is governed by the MIT license that can be
// found in the LICENSE file. // found in the LICENSE file.
@ -6,4 +6,4 @@
package notify package notify
var debugTag bool = true var debugTag = true

View File

@ -1,4 +1,4 @@
// Copyright (c) 2014-2015 The Notify Authors. All rights reserved. // Copyright (c) 2014-2018 The Notify Authors. All rights reserved.
// Use of this source code is governed by the MIT license that can be // Use of this source code is governed by the MIT license that can be
// found in the LICENSE file. // found in the LICENSE file.
@ -6,4 +6,4 @@
package notify package notify
var debugTag bool = false var debugTag = false

View File

@ -19,34 +19,10 @@
package notify package notify
import "fmt" var defaultTree = newTree()
var defaultTree tree // lazy init
type DoNotWatchFn func(string) bool type DoNotWatchFn func(string) bool
func lazyInitDefaultTree() (err error) {
if defaultTree != nil {
// already initialized
return nil
}
defer func() {
// newTree might panic. Patch it up.
if rec := recover(); rec != nil {
switch rec := rec.(type) {
case error:
err = rec
default:
err = fmt.Errorf("init default tree: %v", rec)
}
}
}()
defaultTree = newTree()
return nil
}
// Watch sets up a watchpoint on path listening for events given by the events // Watch sets up a watchpoint on path listening for events given by the events
// argument. // argument.
// //
@ -87,9 +63,6 @@ func lazyInitDefaultTree() (err error) {
// e.g. use persistent paths like %userprofile% or watch additionally parent // e.g. use persistent paths like %userprofile% or watch additionally parent
// directory of a recursive watchpoint in order to receive delete events for it. // directory of a recursive watchpoint in order to receive delete events for it.
func Watch(path string, c chan<- EventInfo, events ...Event) error { func Watch(path string, c chan<- EventInfo, events ...Event) error {
if err := lazyInitDefaultTree(); err != nil {
return err
}
return defaultTree.Watch(path, c, nil, events...) return defaultTree.Watch(path, c, nil, events...)
} }
@ -98,10 +71,7 @@ func Watch(path string, c chan<- EventInfo, events ...Event) error {
// doNotWatch. Given a path as argument doNotWatch should return true if the // doNotWatch. Given a path as argument doNotWatch should return true if the
// file or directory should not be watched. // file or directory should not be watched.
func WatchWithFilter(path string, c chan<- EventInfo, func WatchWithFilter(path string, c chan<- EventInfo,
doNotWatch DoNotWatchFn, events ...Event) error { doNotWatch func(string) bool, events ...Event) error {
if err := lazyInitDefaultTree(); err != nil {
return err
}
return defaultTree.Watch(path, c, doNotWatch, events...) return defaultTree.Watch(path, c, doNotWatch, events...)
} }
@ -111,8 +81,5 @@ func WatchWithFilter(path string, c chan<- EventInfo,
// Stop does not close c. When Stop returns, it is guaranteed that c will // Stop does not close c. When Stop returns, it is guaranteed that c will
// receive no more signals. // receive no more signals.
func Stop(c chan<- EventInfo) { func Stop(c chan<- EventInfo) {
if defaultTree == nil {
return
}
defaultTree.Stop(c) defaultTree.Stop(c)
} }

View File

@ -90,6 +90,10 @@ func gostream(_, info uintptr, n C.size_t, paths, flags, ids uintptr) {
if n == 0 { if n == 0 {
return return
} }
fn := streamFuncs.get(info)
if fn == nil {
return
}
ev := make([]FSEvent, 0, int(n)) ev := make([]FSEvent, 0, int(n))
for i := uintptr(0); i < uintptr(n); i++ { for i := uintptr(0); i < uintptr(n); i++ {
switch flags := *(*uint32)(unsafe.Pointer((flags + i*offflag))); { switch flags := *(*uint32)(unsafe.Pointer((flags + i*offflag))); {
@ -104,7 +108,7 @@ func gostream(_, info uintptr, n C.size_t, paths, flags, ids uintptr) {
} }
} }
streamFuncs.get(info)(ev) fn(ev)
} }
// StreamFunc is a callback called when stream receives file events. // StreamFunc is a callback called when stream receives file events.

View File

@ -1,4 +1,4 @@
// Copyright (c) 2014-2015 The Notify Authors. All rights reserved. // Copyright (c) 2014-2018 The Notify Authors. All rights reserved.
// Use of this source code is governed by the MIT license that can be // Use of this source code is governed by the MIT license that can be
// found in the LICENSE file. // found in the LICENSE file.
@ -22,7 +22,7 @@ import (
const readBufferSize = 4096 const readBufferSize = 4096
// Since all operations which go through the Windows completion routine are done // Since all operations which go through the Windows completion routine are done
// asynchronously, filter may set one of the constants belor. They were defined // asynchronously, filter may set one of the constants below. They were defined
// in order to distinguish whether current folder should be re-registered in // in order to distinguish whether current folder should be re-registered in
// ReadDirectoryChangesW function or some control operations need to be executed. // ReadDirectoryChangesW function or some control operations need to be executed.
const ( const (
@ -109,8 +109,13 @@ func (g *grip) register(cph syscall.Handle) (err error) {
// buffer. Directory changes that occur between calls to this function are added // buffer. Directory changes that occur between calls to this function are added
// to the buffer and then, returned with the next call. // to the buffer and then, returned with the next call.
func (g *grip) readDirChanges() error { func (g *grip) readDirChanges() error {
handle := syscall.Handle(atomic.LoadUintptr((*uintptr)(&g.handle)))
if handle == syscall.InvalidHandle {
return nil // Handle was closed.
}
return syscall.ReadDirectoryChanges( return syscall.ReadDirectoryChanges(
g.handle, handle,
&g.buffer[0], &g.buffer[0],
uint32(unsafe.Sizeof(g.buffer)), uint32(unsafe.Sizeof(g.buffer)),
g.recursive, g.recursive,
@ -220,12 +225,27 @@ func (wd *watched) updateGrip(idx int, cph syscall.Handle, reset bool,
// returned from the operating system kernel. // returned from the operating system kernel.
func (wd *watched) closeHandle() (err error) { func (wd *watched) closeHandle() (err error) {
for _, g := range wd.digrip { for _, g := range wd.digrip {
if g != nil && g.handle != syscall.InvalidHandle { if g == nil {
switch suberr := syscall.CloseHandle(g.handle); { continue
case suberr == nil: }
g.handle = syscall.InvalidHandle
case err == nil: for {
err = suberr handle := syscall.Handle(atomic.LoadUintptr((*uintptr)(&g.handle)))
if handle == syscall.InvalidHandle {
break // Already closed.
}
e := syscall.CloseHandle(handle)
if e != nil && err == nil {
err = e
}
// Set invalid handle even when CloseHandle fails. This will leak
// the handle but, since we can't close it anyway, there won't be
// any difference.
if atomic.CompareAndSwapUintptr((*uintptr)(&g.handle),
(uintptr)(handle), (uintptr)(syscall.InvalidHandle)) {
break
} }
} }
} }
@ -272,50 +292,49 @@ func (r *readdcw) RecursiveWatch(path string, event Event) error {
// watch inserts a directory to the group of watched folders. If watched folder // watch inserts a directory to the group of watched folders. If watched folder
// already exists, function tries to rewatch it with new filters(NOT VALID). Moreover, // already exists, function tries to rewatch it with new filters(NOT VALID). Moreover,
// watch starts the main event loop goroutine when called for the first time. // watch starts the main event loop goroutine when called for the first time.
func (r *readdcw) watch(path string, event Event, recursive bool) (err error) { func (r *readdcw) watch(path string, event Event, recursive bool) error {
if event&^(All|fileNotifyChangeAll) != 0 { if event&^(All|fileNotifyChangeAll) != 0 {
return errors.New("notify: unknown event") return errors.New("notify: unknown event")
} }
r.Lock()
wd, ok := r.m[path]
r.Unlock()
if !ok {
if err = r.lazyinit(); err != nil {
return
}
r.Lock() r.Lock()
defer r.Unlock() defer r.Unlock()
if wd, ok = r.m[path]; ok {
dbgprint("watch: exists already") if wd, ok := r.m[path]; ok {
return dbgprint("watch: already exists")
wd.filter &^= stateUnwatch
return nil
} }
if wd, err = newWatched(r.cph, uint32(event), recursive, path); err != nil {
return if err := r.lazyinit(); err != nil {
return err
} }
wd, err := newWatched(r.cph, uint32(event), recursive, path)
if err != nil {
return err
}
r.m[path] = wd r.m[path] = wd
dbgprint("watch: new watch added") dbgprint("watch: new watch added")
} else {
dbgprint("watch: exists already")
}
return nil return nil
} }
// lazyinit creates an I/O completion port and starts the main event processing // lazyinit creates an I/O completion port and starts the main event loop.
// loop. This method uses Double-Checked Locking optimization.
func (r *readdcw) lazyinit() (err error) { func (r *readdcw) lazyinit() (err error) {
invalid := uintptr(syscall.InvalidHandle) invalid := uintptr(syscall.InvalidHandle)
if atomic.LoadUintptr((*uintptr)(&r.cph)) == invalid {
r.Lock()
defer r.Unlock()
if atomic.LoadUintptr((*uintptr)(&r.cph)) == invalid { if atomic.LoadUintptr((*uintptr)(&r.cph)) == invalid {
cph := syscall.InvalidHandle cph := syscall.InvalidHandle
if cph, err = syscall.CreateIoCompletionPort(cph, 0, 0, 0); err != nil { if cph, err = syscall.CreateIoCompletionPort(cph, 0, 0, 0); err != nil {
return return
} }
r.cph, r.start = cph, true r.cph, r.start = cph, true
go r.loop() go r.loop()
} }
}
return return
} }
@ -364,6 +383,7 @@ func (r *readdcw) loopstate(overEx *overlappedEx) {
overEx.parent.parent.recreate(r.cph) overEx.parent.parent.recreate(r.cph)
case stateUnwatch: case stateUnwatch:
dbgprint("loopstate unwatch") dbgprint("loopstate unwatch")
overEx.parent.parent.closeHandle()
delete(r.m, syscall.UTF16ToString(overEx.parent.pathw)) delete(r.m, syscall.UTF16ToString(overEx.parent.pathw))
case stateCPClose: case stateCPClose:
default: default:
@ -495,19 +515,22 @@ func (r *readdcw) RecursiveUnwatch(path string) error {
// TODO : pknap // TODO : pknap
func (r *readdcw) unwatch(path string) (err error) { func (r *readdcw) unwatch(path string) (err error) {
var wd *watched var wd *watched
r.Lock() r.Lock()
defer r.Unlock() defer r.Unlock()
if wd, err = r.nonStateWatchedLocked(path); err != nil { if wd, err = r.nonStateWatchedLocked(path); err != nil {
return return
} }
wd.filter |= stateUnwatch wd.filter |= stateUnwatch
if err = wd.closeHandle(); err != nil { dbgprint("unwatch: set unwatch state")
wd.filter &^= stateUnwatch
return
}
if _, attrErr := syscall.GetFileAttributes(&wd.pathw[0]); attrErr != nil { if _, attrErr := syscall.GetFileAttributes(&wd.pathw[0]); attrErr != nil {
for _, g := range wd.digrip { for _, g := range wd.digrip {
if g != nil { if g == nil {
continue
}
dbgprint("unwatch: posting") dbgprint("unwatch: posting")
if err = syscall.PostQueuedCompletionStatus(r.cph, 0, 0, (*syscall.Overlapped)(unsafe.Pointer(g.ovlapped))); err != nil { if err = syscall.PostQueuedCompletionStatus(r.cph, 0, 0, (*syscall.Overlapped)(unsafe.Pointer(g.ovlapped))); err != nil {
wd.filter &^= stateUnwatch wd.filter &^= stateUnwatch
@ -515,7 +538,7 @@ func (r *readdcw) unwatch(path string) (err error) {
} }
} }
} }
}
return return
} }

View File

@ -1,23 +1,13 @@
// Copyright (c) 2014-2015 The Notify Authors. All rights reserved. // Copyright (c) 2014-2018 The Notify Authors. All rights reserved.
// Use of this source code is governed by the MIT license that can be // Use of this source code is governed by the MIT license that can be
// found in the LICENSE file. // found in the LICENSE file.
// +build !darwin,!linux,!freebsd,!dragonfly,!netbsd,!openbsd,!windows
// +build !kqueue,!solaris
package notify package notify
import "errors" type watcherStub struct{ error }
type stub struct{ error }
// newWatcher stub.
func newWatcher(chan<- EventInfo) watcher {
return stub{errors.New("notify: not implemented")}
}
// Following methods implement notify.watcher interface. // Following methods implement notify.watcher interface.
func (s stub) Watch(string, Event) error { return s } func (s watcherStub) Watch(string, Event) error { return s }
func (s stub) Rewatch(string, Event, Event) error { return s } func (s watcherStub) Rewatch(string, Event, Event) error { return s }
func (s stub) Unwatch(string) (err error) { return s } func (s watcherStub) Unwatch(string) (err error) { return s }
func (s stub) Close() error { return s } func (s watcherStub) Close() error { return s }

View File

@ -106,7 +106,8 @@ func newWatcher(c chan<- EventInfo) watcher {
} }
t.t = newTrigger(t.pthLkp) t.t = newTrigger(t.pthLkp)
if err := t.t.Init(); err != nil { if err := t.t.Init(); err != nil {
panic(err) t.Close()
return watcherStub{fmt.Errorf("failed setting up watcher: %v", err)}
} }
go t.monitor() go t.monitor()
return t return t

2
vendor/manifest vendored
View File

@ -443,7 +443,7 @@
"importpath": "github.com/syncthing/notify", "importpath": "github.com/syncthing/notify",
"repository": "https://github.com/syncthing/notify", "repository": "https://github.com/syncthing/notify",
"vcs": "git", "vcs": "git",
"revision": "b9ceffc925039c77cd9e0d38f248279ccc4399e2", "revision": "cdf89c4039d13726e227d0a472053ea19de021b4",
"branch": "master", "branch": "master",
"notests": true "notests": true
}, },