committed by
kannappanr
parent
c352cadd4b
commit
044a95b6cf
+2
-1
@@ -18,4 +18,5 @@ Filesystem event notification library on steroids. (under active development)
|
||||
- [github.com/rjeczalik/cmd/notify](https://godoc.org/github.com/rjeczalik/cmd/notify)
|
||||
- [github.com/cortesi/devd](https://github.com/cortesi/devd)
|
||||
- [github.com/cortesi/modd](https://github.com/cortesi/modd)
|
||||
|
||||
- [github.com/syncthing/syncthing-inotify](https://github.com/syncthing/syncthing-inotify)
|
||||
- [github.com/OrlovEvgeny/TinyJPG](https://github.com/OrlovEvgeny/TinyJPG)
|
||||
|
||||
+8
-4
@@ -7,16 +7,20 @@ clone_folder: c:\projects\src\github.com\rjeczalik\notify
|
||||
environment:
|
||||
PATH: c:\projects\bin;%PATH%
|
||||
GOPATH: c:\projects
|
||||
NOTIFY_TIMEOUT: 5s
|
||||
NOTIFY_TIMEOUT: 10s
|
||||
GOVERSION: 1.10.3
|
||||
|
||||
install:
|
||||
- rmdir c:\go /s /q
|
||||
- appveyor DownloadFile https://storage.googleapis.com/golang/go%GOVERSION%.windows-amd64.zip
|
||||
- 7z x go%GOVERSION%.windows-amd64.zip -y -oC:\ > NUL
|
||||
|
||||
- cd %APPVEYOR_BUILD_FOLDER%
|
||||
- go version
|
||||
- go get -v -t ./...
|
||||
|
||||
build_script:
|
||||
- go tool vet -all .
|
||||
- go build ./...
|
||||
- go test -v -race ./...
|
||||
- go test -v -timeout 120s -race ./...
|
||||
|
||||
test: off
|
||||
|
||||
|
||||
+46
-4
@@ -2,10 +2,52 @@
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
// +build !debug
|
||||
|
||||
package notify
|
||||
|
||||
func dbgprint(...interface{}) {}
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func dbgprintf(string, ...interface{}) {}
|
||||
var dbgprint func(...interface{})
|
||||
|
||||
var dbgprintf func(string, ...interface{})
|
||||
|
||||
var dbgcallstack func(max int) []string
|
||||
|
||||
func init() {
|
||||
if _, ok := os.LookupEnv("NOTIFY_DEBUG"); ok || debugTag {
|
||||
log.SetOutput(os.Stdout)
|
||||
log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds)
|
||||
dbgprint = func(v ...interface{}) {
|
||||
v = append([]interface{}{"[D] "}, v...)
|
||||
log.Println(v...)
|
||||
}
|
||||
dbgprintf = func(format string, v ...interface{}) {
|
||||
format = "[D] " + format
|
||||
log.Printf(format, v...)
|
||||
}
|
||||
dbgcallstack = func(max int) []string {
|
||||
pc, stack := make([]uintptr, max), make([]string, 0, max)
|
||||
runtime.Callers(2, pc)
|
||||
for _, pc := range pc {
|
||||
if f := runtime.FuncForPC(pc); f != nil {
|
||||
fname := f.Name()
|
||||
idx := strings.LastIndex(fname, string(os.PathSeparator))
|
||||
if idx != -1 {
|
||||
stack = append(stack, fname[idx+1:])
|
||||
} else {
|
||||
stack = append(stack, fname)
|
||||
}
|
||||
}
|
||||
}
|
||||
return stack
|
||||
}
|
||||
return
|
||||
}
|
||||
dbgprint = func(v ...interface{}) {}
|
||||
dbgprintf = func(format string, v ...interface{}) {}
|
||||
dbgcallstack = func(max int) []string { return nil }
|
||||
}
|
||||
|
||||
+2
-36
@@ -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
|
||||
// found in the LICENSE file.
|
||||
|
||||
@@ -6,38 +6,4 @@
|
||||
|
||||
package notify
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func dbgprint(v ...interface{}) {
|
||||
fmt.Printf("[D] ")
|
||||
fmt.Print(v...)
|
||||
fmt.Printf("\n\n")
|
||||
}
|
||||
|
||||
func dbgprintf(format string, v ...interface{}) {
|
||||
fmt.Printf("[D] ")
|
||||
fmt.Printf(format, v...)
|
||||
fmt.Printf("\n\n")
|
||||
}
|
||||
|
||||
func dbgcallstack(max int) []string {
|
||||
pc, stack := make([]uintptr, max), make([]string, 0, max)
|
||||
runtime.Callers(2, pc)
|
||||
for _, pc := range pc {
|
||||
if f := runtime.FuncForPC(pc); f != nil {
|
||||
fname := f.Name()
|
||||
idx := strings.LastIndex(fname, string(os.PathSeparator))
|
||||
if idx != -1 {
|
||||
stack = append(stack, fname[idx+1:])
|
||||
} else {
|
||||
stack = append(stack, fname)
|
||||
}
|
||||
}
|
||||
}
|
||||
return stack
|
||||
}
|
||||
var debugTag = true
|
||||
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
// Copyright (c) 2014-2018 The Notify Authors. All rights reserved.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
// +build !debug
|
||||
|
||||
package notify
|
||||
|
||||
var debugTag = false
|
||||
+5
-2
@@ -12,11 +12,14 @@
|
||||
// source file.
|
||||
//
|
||||
// On top of filesystem watchers notify maintains a watchpoint tree, which provides
|
||||
// strategy for creating and closing filesystem watches and dispatching filesystem
|
||||
// a strategy for creating and closing filesystem watches and dispatching filesystem
|
||||
// events to user channels.
|
||||
//
|
||||
// An event set is just an event list joint using bitwise OR operator
|
||||
// into a single event value.
|
||||
// Both the platform-independent (see Constants) and specific events can be used.
|
||||
// Refer to the event_*.go source files for information about the available
|
||||
// events.
|
||||
//
|
||||
// A filesystem watch or just a watch is platform-specific entity which represents
|
||||
// a single path registered for notifications for specific event set. Setting a watch
|
||||
@@ -35,6 +38,6 @@
|
||||
// A watchpoint is a list of user channel and event set pairs for particular
|
||||
// path (watchpoint tree's node). A single watchpoint can contain multiple
|
||||
// different user channels registered to listen for one or more events. A single
|
||||
// user channel can be registered in one or more watchpoints, recurisve and
|
||||
// user channel can be registered in one or more watchpoints, recursive and
|
||||
// non-recursive ones as well.
|
||||
package notify
|
||||
|
||||
+12
-2
@@ -27,7 +27,11 @@ const (
|
||||
dirmarker
|
||||
)
|
||||
|
||||
// ReadDirectoryChangesW filters.
|
||||
// ReadDirectoryChangesW filters
|
||||
// On Windows the following events can be passed to Watch. A different set of
|
||||
// events (see actions below) are received on the channel passed to Watch.
|
||||
// For more information refer to
|
||||
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa365465(v=vs.85).aspx
|
||||
const (
|
||||
FileNotifyChangeFileName = Event(syscall.FILE_NOTIFY_CHANGE_FILE_NAME)
|
||||
FileNotifyChangeDirName = Event(syscall.FILE_NOTIFY_CHANGE_DIR_NAME)
|
||||
@@ -48,7 +52,13 @@ const (
|
||||
// this flag should be declared in: http://golang.org/src/pkg/syscall/ztypes_windows.go
|
||||
const syscallFileNotifyChangeSecurity = 0x00000100
|
||||
|
||||
// ReadDirectoryChangesW actions.
|
||||
// ReadDirectoryChangesW actions
|
||||
// The following events are returned on the channel passed to Watch, but cannot
|
||||
// be passed to Watch itself (see filters above). You can find a table showing
|
||||
// the relation between actions and filteres at
|
||||
// https://github.com/rjeczalik/notify/issues/10#issuecomment-66179535
|
||||
// The msdn documentation on actions is part of
|
||||
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa364391(v=vs.85).aspx
|
||||
const (
|
||||
FileActionAdded = Event(syscall.FILE_ACTION_ADDED) << 12
|
||||
FileActionRemoved = Event(syscall.FILE_ACTION_REMOVED) << 12
|
||||
|
||||
+5
-2
@@ -6,7 +6,6 @@ package notify
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -71,7 +70,11 @@ Traverse:
|
||||
case errSkip:
|
||||
continue Traverse
|
||||
default:
|
||||
return fmt.Errorf("error while traversing %q: %v", nd.Name, err)
|
||||
return &os.PathError{
|
||||
Op: "error while traversing",
|
||||
Path: nd.Name,
|
||||
Err: err,
|
||||
}
|
||||
}
|
||||
// TODO(rjeczalik): tolerate open failures - add failed names to
|
||||
// AddDirError and notify users which names are not added to the tree.
|
||||
|
||||
+2
-9
@@ -33,14 +33,7 @@ type fen struct {
|
||||
|
||||
// watched is a data structure representing watched file/directory.
|
||||
type watched struct {
|
||||
// p is a path to watched file/directory
|
||||
p string
|
||||
// fi provides information about watched file/dir
|
||||
fi os.FileInfo
|
||||
// eDir represents events watched directly
|
||||
eDir Event
|
||||
// eNonDir represents events watched indirectly
|
||||
eNonDir Event
|
||||
trgWatched
|
||||
}
|
||||
|
||||
// Stop implements trigger.
|
||||
@@ -55,7 +48,7 @@ func (f *fen) Close() (err error) {
|
||||
|
||||
// NewWatched implements trigger.
|
||||
func (*fen) NewWatched(p string, fi os.FileInfo) (*watched, error) {
|
||||
return &watched{p: p, fi: fi}, nil
|
||||
return &watched{trgWatched{p: p, fi: fi}}, nil
|
||||
}
|
||||
|
||||
// Record implements trigger.
|
||||
|
||||
-8
@@ -12,8 +12,6 @@ import (
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
// TODO(rjeczalik): get rid of calls to canonical, it's tree responsibility
|
||||
|
||||
const (
|
||||
failure = uint32(FSEventsMustScanSubDirs | FSEventsUserDropped | FSEventsKernelDropped)
|
||||
filter = uint32(FSEventsCreated | FSEventsRemoved | FSEventsRenamed |
|
||||
@@ -189,9 +187,6 @@ func newWatcher(c chan<- EventInfo) watcher {
|
||||
}
|
||||
|
||||
func (fse *fsevents) watch(path string, event Event, isrec int32) (err error) {
|
||||
if path, err = canonical(path); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, ok := fse.watches[path]; ok {
|
||||
return errAlreadyWatched
|
||||
}
|
||||
@@ -211,9 +206,6 @@ func (fse *fsevents) watch(path string, event Event, isrec int32) (err error) {
|
||||
}
|
||||
|
||||
func (fse *fsevents) unwatch(path string) (err error) {
|
||||
if path, err = canonical(path); err != nil {
|
||||
return
|
||||
}
|
||||
w, ok := fse.watches[path]
|
||||
if !ok {
|
||||
return errNotWatched
|
||||
|
||||
+13
-6
@@ -26,9 +26,9 @@ import "C"
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"runtime"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
@@ -48,7 +48,7 @@ var wg sync.WaitGroup // used to wait until the runloop starts
|
||||
// started and is ready via the wg. It also serves purpose of a dummy source,
|
||||
// thanks to it the runloop does not return as it also has at least one source
|
||||
// registered.
|
||||
var source = C.CFRunLoopSourceCreate(nil, 0, &C.CFRunLoopSourceContext{
|
||||
var source = C.CFRunLoopSourceCreate(C.kCFAllocatorDefault, 0, &C.CFRunLoopSourceContext{
|
||||
perform: (C.CFRunLoopPerformCallBack)(C.gosource),
|
||||
})
|
||||
|
||||
@@ -63,6 +63,10 @@ var (
|
||||
func init() {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
// There is exactly one run loop per thread. Lock this goroutine to its
|
||||
// thread to ensure that it's not rescheduled on a different thread while
|
||||
// setting up the run loop.
|
||||
runtime.LockOSThread()
|
||||
runloop = C.CFRunLoopGetCurrent()
|
||||
C.CFRunLoopAddSource(runloop, source, C.kCFRunLoopDefaultMode)
|
||||
C.CFRunLoopRun()
|
||||
@@ -73,7 +77,6 @@ func init() {
|
||||
|
||||
//export gosource
|
||||
func gosource(unsafe.Pointer) {
|
||||
time.Sleep(time.Second)
|
||||
wg.Done()
|
||||
}
|
||||
|
||||
@@ -87,6 +90,10 @@ func gostream(_, info uintptr, n C.size_t, paths, flags, ids uintptr) {
|
||||
if n == 0 {
|
||||
return
|
||||
}
|
||||
fn := streamFuncs.get(info)
|
||||
if fn == nil {
|
||||
return
|
||||
}
|
||||
ev := make([]FSEvent, 0, int(n))
|
||||
for i := uintptr(0); i < uintptr(n); i++ {
|
||||
switch flags := *(*uint32)(unsafe.Pointer((flags + i*offflag))); {
|
||||
@@ -101,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.
|
||||
@@ -159,8 +166,8 @@ func (s *stream) Start() error {
|
||||
return nil
|
||||
}
|
||||
wg.Wait()
|
||||
p := C.CFStringCreateWithCStringNoCopy(nil, C.CString(s.path), C.kCFStringEncodingUTF8, nil)
|
||||
path := C.CFArrayCreate(nil, (*unsafe.Pointer)(unsafe.Pointer(&p)), 1, nil)
|
||||
p := C.CFStringCreateWithCStringNoCopy(C.kCFAllocatorDefault, C.CString(s.path), C.kCFStringEncodingUTF8, C.kCFAllocatorDefault)
|
||||
path := C.CFArrayCreate(C.kCFAllocatorDefault, (*unsafe.Pointer)(unsafe.Pointer(&p)), 1, nil)
|
||||
ctx := C.FSEventStreamContext{}
|
||||
ref := C.EventStreamCreate(&ctx, C.uintptr_t(s.info), path, C.FSEventStreamEventId(atomic.LoadUint64(&since)), latency, flags)
|
||||
if ref == nilstream {
|
||||
|
||||
+11
-3
@@ -120,7 +120,7 @@ func (i *inotify) lazyinit() error {
|
||||
i.Lock()
|
||||
defer i.Unlock()
|
||||
if atomic.LoadInt32(&i.fd) == invalidDescriptor {
|
||||
fd, err := unix.InotifyInit()
|
||||
fd, err := unix.InotifyInit1(unix.IN_CLOEXEC)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -358,7 +358,7 @@ func (i *inotify) Unwatch(path string) (err error) {
|
||||
return errors.New("notify: path " + path + " is already watched")
|
||||
}
|
||||
fd := atomic.LoadInt32(&i.fd)
|
||||
if _, err = unix.InotifyRmWatch(int(fd), uint32(iwd)); err != nil {
|
||||
if err = removeInotifyWatch(fd, iwd); err != nil {
|
||||
return
|
||||
}
|
||||
i.Lock()
|
||||
@@ -378,7 +378,7 @@ func (i *inotify) Close() (err error) {
|
||||
return nil
|
||||
}
|
||||
for iwd := range i.m {
|
||||
if _, e := unix.InotifyRmWatch(int(i.fd), uint32(iwd)); e != nil && err == nil {
|
||||
if e := removeInotifyWatch(i.fd, iwd); e != nil && err == nil {
|
||||
err = e
|
||||
}
|
||||
delete(i.m, iwd)
|
||||
@@ -395,3 +395,11 @@ func (i *inotify) Close() (err error) {
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// if path was removed, notify already removed the watch and returns EINVAL error
|
||||
func removeInotifyWatch(fd int32, iwd int32) (err error) {
|
||||
if _, err = unix.InotifyRmWatch(int(fd), uint32(iwd)); err != nil && err != unix.EINVAL {
|
||||
return
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
+5
-9
@@ -36,16 +36,9 @@ type kq struct {
|
||||
|
||||
// watched is a data structure representing watched file/directory.
|
||||
type watched struct {
|
||||
// p is a path to watched file/directory.
|
||||
p string
|
||||
trgWatched
|
||||
// fd is a file descriptor for watched file/directory.
|
||||
fd int
|
||||
// fi provides information about watched file/dir.
|
||||
fi os.FileInfo
|
||||
// eDir represents events watched directly.
|
||||
eDir Event
|
||||
// eNonDir represents events watched indirectly.
|
||||
eNonDir Event
|
||||
}
|
||||
|
||||
// Stop implements trigger.
|
||||
@@ -66,7 +59,10 @@ func (*kq) NewWatched(p string, fi os.FileInfo) (*watched, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &watched{fd: fd, p: p, fi: fi}, nil
|
||||
return &watched{
|
||||
trgWatched: trgWatched{p: p, fi: fi},
|
||||
fd: fd,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Record implements trigger.
|
||||
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
// Copyright (c) 2014-2018 The Notify Authors. All rights reserved.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
// +build !darwin,!linux,!freebsd,!dragonfly,!netbsd,!openbsd,!windows
|
||||
// +build !kqueue,!solaris
|
||||
|
||||
package notify
|
||||
|
||||
import "errors"
|
||||
|
||||
// newWatcher stub.
|
||||
func newWatcher(chan<- EventInfo) watcher {
|
||||
return watcherStub{errors.New("notify: not implemented")}
|
||||
}
|
||||
+90
-59
@@ -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
|
||||
// found in the LICENSE file.
|
||||
|
||||
@@ -22,7 +22,7 @@ import (
|
||||
const readBufferSize = 4096
|
||||
|
||||
// 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
|
||||
// ReadDirectoryChangesW function or some control operations need to be executed.
|
||||
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
|
||||
// to the buffer and then, returned with the next call.
|
||||
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(
|
||||
g.handle,
|
||||
handle,
|
||||
&g.buffer[0],
|
||||
uint32(unsafe.Sizeof(g.buffer)),
|
||||
g.recursive,
|
||||
@@ -220,12 +225,27 @@ func (wd *watched) updateGrip(idx int, cph syscall.Handle, reset bool,
|
||||
// returned from the operating system kernel.
|
||||
func (wd *watched) closeHandle() (err error) {
|
||||
for _, g := range wd.digrip {
|
||||
if g != nil && g.handle != syscall.InvalidHandle {
|
||||
switch suberr := syscall.CloseHandle(g.handle); {
|
||||
case suberr == nil:
|
||||
g.handle = syscall.InvalidHandle
|
||||
case err == nil:
|
||||
err = suberr
|
||||
if g == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
for {
|
||||
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,48 +292,49 @@ func (r *readdcw) RecursiveWatch(path string, event Event) error {
|
||||
// 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,
|
||||
// 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 {
|
||||
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()
|
||||
if wd, ok = r.m[path]; ok {
|
||||
r.Unlock()
|
||||
return
|
||||
}
|
||||
if wd, err = newWatched(r.cph, uint32(event), recursive, path); err != nil {
|
||||
r.Unlock()
|
||||
return
|
||||
}
|
||||
r.m[path] = wd
|
||||
r.Unlock()
|
||||
defer r.Unlock()
|
||||
|
||||
if wd, ok := r.m[path]; ok {
|
||||
dbgprint("watch: already exists")
|
||||
wd.filter &^= stateUnwatch
|
||||
return nil
|
||||
}
|
||||
|
||||
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
|
||||
dbgprint("watch: new watch added")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// lazyinit creates an I/O completion port and starts the main event processing
|
||||
// loop. This method uses Double-Checked Locking optimization.
|
||||
// lazyinit creates an I/O completion port and starts the main event loop.
|
||||
func (r *readdcw) lazyinit() (err error) {
|
||||
invalid := uintptr(syscall.InvalidHandle)
|
||||
|
||||
if atomic.LoadUintptr((*uintptr)(&r.cph)) == invalid {
|
||||
r.Lock()
|
||||
defer r.Unlock()
|
||||
if atomic.LoadUintptr((*uintptr)(&r.cph)) == invalid {
|
||||
cph := syscall.InvalidHandle
|
||||
if cph, err = syscall.CreateIoCompletionPort(cph, 0, 0, 0); err != nil {
|
||||
return
|
||||
}
|
||||
r.cph, r.start = cph, true
|
||||
go r.loop()
|
||||
cph := syscall.InvalidHandle
|
||||
if cph, err = syscall.CreateIoCompletionPort(cph, 0, 0, 0); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
r.cph, r.start = cph, true
|
||||
go r.loop()
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -337,33 +358,33 @@ func (r *readdcw) loop() {
|
||||
continue
|
||||
}
|
||||
overEx := (*overlappedEx)(unsafe.Pointer(overlapped))
|
||||
if n == 0 {
|
||||
r.loopstate(overEx)
|
||||
} else {
|
||||
if n != 0 {
|
||||
r.loopevent(n, overEx)
|
||||
if err = overEx.parent.readDirChanges(); err != nil {
|
||||
// TODO: error handling
|
||||
}
|
||||
}
|
||||
r.loopstate(overEx)
|
||||
}
|
||||
}
|
||||
|
||||
// TODO(pknap) : doc
|
||||
func (r *readdcw) loopstate(overEx *overlappedEx) {
|
||||
filter := atomic.LoadUint32(&overEx.parent.parent.filter)
|
||||
r.Lock()
|
||||
defer r.Unlock()
|
||||
filter := overEx.parent.parent.filter
|
||||
if filter&onlyMachineStates == 0 {
|
||||
return
|
||||
}
|
||||
if overEx.parent.parent.count--; overEx.parent.parent.count == 0 {
|
||||
switch filter & onlyMachineStates {
|
||||
case stateRewatch:
|
||||
r.Lock()
|
||||
dbgprint("loopstate rewatch")
|
||||
overEx.parent.parent.recreate(r.cph)
|
||||
r.Unlock()
|
||||
case stateUnwatch:
|
||||
r.Lock()
|
||||
dbgprint("loopstate unwatch")
|
||||
overEx.parent.parent.closeHandle()
|
||||
delete(r.m, syscall.UTF16ToString(overEx.parent.pathw))
|
||||
r.Unlock()
|
||||
case stateCPClose:
|
||||
default:
|
||||
panic(`notify: windows loopstate logic error`)
|
||||
@@ -450,8 +471,8 @@ func (r *readdcw) rewatch(path string, oldevent, newevent uint32, recursive bool
|
||||
}
|
||||
var wd *watched
|
||||
r.Lock()
|
||||
if wd, err = r.nonStateWatched(path); err != nil {
|
||||
r.Unlock()
|
||||
defer r.Unlock()
|
||||
if wd, err = r.nonStateWatchedLocked(path); err != nil {
|
||||
return
|
||||
}
|
||||
if wd.filter&(onlyNotifyChanges|onlyNGlobalEvents) != oldevent {
|
||||
@@ -462,21 +483,19 @@ func (r *readdcw) rewatch(path string, oldevent, newevent uint32, recursive bool
|
||||
if err = wd.closeHandle(); err != nil {
|
||||
wd.filter = oldevent
|
||||
wd.recursive = recursive
|
||||
r.Unlock()
|
||||
return
|
||||
}
|
||||
r.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
// TODO : pknap
|
||||
func (r *readdcw) nonStateWatched(path string) (wd *watched, err error) {
|
||||
func (r *readdcw) nonStateWatchedLocked(path string) (wd *watched, err error) {
|
||||
wd, ok := r.m[path]
|
||||
if !ok || wd == nil {
|
||||
err = errors.New(`notify: ` + path + ` path is unwatched`)
|
||||
return
|
||||
}
|
||||
if filter := atomic.LoadUint32(&wd.filter); filter&onlyMachineStates != 0 {
|
||||
if wd.filter&onlyMachineStates != 0 {
|
||||
err = errors.New(`notify: another re/unwatching operation in progress`)
|
||||
return
|
||||
}
|
||||
@@ -496,18 +515,30 @@ func (r *readdcw) RecursiveUnwatch(path string) error {
|
||||
// TODO : pknap
|
||||
func (r *readdcw) unwatch(path string) (err error) {
|
||||
var wd *watched
|
||||
|
||||
r.Lock()
|
||||
if wd, err = r.nonStateWatched(path); err != nil {
|
||||
r.Unlock()
|
||||
defer r.Unlock()
|
||||
if wd, err = r.nonStateWatchedLocked(path); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
wd.filter |= stateUnwatch
|
||||
if err = wd.closeHandle(); err != nil {
|
||||
wd.filter &^= stateUnwatch
|
||||
r.Unlock()
|
||||
return
|
||||
dbgprint("unwatch: set unwatch state")
|
||||
|
||||
if _, attrErr := syscall.GetFileAttributes(&wd.pathw[0]); attrErr != nil {
|
||||
for _, g := range wd.digrip {
|
||||
if g == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
dbgprint("unwatch: posting")
|
||||
if err = syscall.PostQueuedCompletionStatus(r.cph, 0, 0, (*syscall.Overlapped)(unsafe.Pointer(g.ovlapped))); err != nil {
|
||||
wd.filter &^= stateUnwatch
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
r.Unlock()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
+6
-16
@@ -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
|
||||
// found in the LICENSE file.
|
||||
|
||||
// +build !darwin,!linux,!freebsd,!dragonfly,!netbsd,!openbsd,!windows
|
||||
// +build !kqueue,!solaris
|
||||
|
||||
package notify
|
||||
|
||||
import "errors"
|
||||
|
||||
type stub struct{ error }
|
||||
|
||||
// newWatcher stub.
|
||||
func newWatcher(chan<- EventInfo) watcher {
|
||||
return stub{errors.New("notify: not implemented")}
|
||||
}
|
||||
type watcherStub struct{ error }
|
||||
|
||||
// Following methods implement notify.watcher interface.
|
||||
func (s stub) Watch(string, Event) error { return s }
|
||||
func (s stub) Rewatch(string, Event, Event) error { return s }
|
||||
func (s stub) Unwatch(string) (err error) { return s }
|
||||
func (s stub) Close() error { return s }
|
||||
func (s watcherStub) Watch(string, Event) error { return s }
|
||||
func (s watcherStub) Rewatch(string, Event, Event) error { return s }
|
||||
func (s watcherStub) Unwatch(string) (err error) { return s }
|
||||
func (s watcherStub) Close() error { return s }
|
||||
|
||||
+21
-3
@@ -23,6 +23,7 @@
|
||||
package notify
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -56,6 +57,19 @@ type trigger interface {
|
||||
IsStop(n interface{}, err error) bool
|
||||
}
|
||||
|
||||
// trgWatched is a the base data structure representing watched file/directory.
|
||||
// The platform specific full data structure (watched) must embed this type.
|
||||
type trgWatched struct {
|
||||
// p is a path to watched file/directory.
|
||||
p string
|
||||
// fi provides information about watched file/dir.
|
||||
fi os.FileInfo
|
||||
// eDir represents events watched directly.
|
||||
eDir Event
|
||||
// eNonDir represents events watched indirectly.
|
||||
eNonDir Event
|
||||
}
|
||||
|
||||
// encode Event to native representation. Implementation is to be provided by
|
||||
// platform specific implementation.
|
||||
var encode func(Event, bool) int64
|
||||
@@ -92,7 +106,8 @@ func newWatcher(c chan<- EventInfo) watcher {
|
||||
}
|
||||
t.t = newTrigger(t.pthLkp)
|
||||
if err := t.t.Init(); err != nil {
|
||||
panic(err)
|
||||
t.Close()
|
||||
return watcherStub{fmt.Errorf("failed setting up watcher: %v", err)}
|
||||
}
|
||||
go t.monitor()
|
||||
return t
|
||||
@@ -117,6 +132,9 @@ func (t *trg) Close() (err error) {
|
||||
dbgprintf("trg: closing native watch failed: %q\n", e)
|
||||
err = nonil(err, e)
|
||||
}
|
||||
if remaining := len(t.pthLkp); remaining != 0 {
|
||||
err = nonil(err, fmt.Errorf("Not all watches were removed: len(t.pthLkp) == %v", len(t.pthLkp)))
|
||||
}
|
||||
t.Unlock()
|
||||
return
|
||||
}
|
||||
@@ -175,7 +193,7 @@ func decode(o int64, w Event) (e Event) {
|
||||
func (t *trg) watch(p string, e Event, fi os.FileInfo) error {
|
||||
if err := t.singlewatch(p, e, dir, fi); err != nil {
|
||||
if err != errAlreadyWatched {
|
||||
return nil
|
||||
return err
|
||||
}
|
||||
}
|
||||
if fi.IsDir() {
|
||||
@@ -361,7 +379,7 @@ func (t *trg) singleunwatch(p string, direct mode) error {
|
||||
}
|
||||
if w.eNonDir|w.eDir != 0 {
|
||||
mod := dir
|
||||
if w.eNonDir == 0 {
|
||||
if w.eNonDir != 0 {
|
||||
mod = ndir
|
||||
}
|
||||
if err := t.singlewatch(p, w.eNonDir|w.eDir, mod,
|
||||
|
||||
Vendored
+3
-3
@@ -205,10 +205,10 @@
|
||||
"revisionTime": "2017-08-08T19:02:11Z"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "ZYLNe3yInOcxTtzu7ERLL2hoQxE=",
|
||||
"checksumSHA1": "254KnkAulyXtCDVnQF28vj3b/F8=",
|
||||
"path": "github.com/rjeczalik/notify",
|
||||
"revision": "41a86457b5eea072a7e6f40bbfaf94c8df41d3aa",
|
||||
"revisionTime": "2017-03-01T20:33:46Z"
|
||||
"revision": "0f065fa99b48b842c3fd3e2c8b194c6f2b69f6b8",
|
||||
"revisionTime": "2018-08-27T19:31:19Z"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "d+4iuDj/qQ+2LiWEbQ+AWjxEJOc=",
|
||||
|
||||
Reference in New Issue
Block a user